1 /* 2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC. 3 * Copyright (C) 2007 The Regents of the University of California. 4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). 5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>. 6 * UCRL-CODE-235197 7 * 8 * This file is part of the SPL, Solaris Porting Layer. 9 * 10 * The SPL is free software; you can redistribute it and/or modify it 11 * under the terms of the GNU General Public License as published by the 12 * Free Software Foundation; either version 2 of the License, or (at your 13 * option) any later version. 14 * 15 * The SPL is distributed in the hope that it will be useful, but WITHOUT 16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 18 * for more details. 19 * 20 * You should have received a copy of the GNU General Public License along 21 * with the SPL. If not, see <http://www.gnu.org/licenses/>. 22 */ 23 24 #ifndef _SPL_CRED_H 25 #define _SPL_CRED_H 26 27 #include <linux/module.h> 28 #include <linux/cred.h> 29 #include <linux/sched.h> 30 #include <sys/types.h> 31 #include <sys/vfs.h> 32 33 typedef struct cred cred_t; 34 35 extern struct task_struct init_task; 36 37 #define kcred ((cred_t *)(init_task.cred)) 38 #define CRED() ((cred_t *)current_cred()) 39 40 /* Linux 4.9 API change, GROUP_AT was removed */ 41 #ifndef GROUP_AT 42 #define GROUP_AT(gi, i) ((gi)->gid[i]) 43 #endif 44 45 #define KUID_TO_SUID(x) (__kuid_val(x)) 46 #define KGID_TO_SGID(x) (__kgid_val(x)) 47 #define SUID_TO_KUID(x) (KUIDT_INIT(x)) 48 #define SGID_TO_KGID(x) (KGIDT_INIT(x)) 49 #define KGIDP_TO_SGIDP(x) (&(x)->val) 50 51 /* Check if the user ns is the initial one */ 52 static inline boolean_t 53 zfs_is_init_userns(struct user_namespace *user_ns) 54 { 55 #if defined(CONFIG_USER_NS) 56 return (user_ns == kcred->user_ns); 57 #else 58 return (B_FALSE); 59 #endif 60 } 61 62 static inline struct user_namespace *zfs_i_user_ns(struct inode *inode) 63 { 64 #ifdef HAVE_SUPER_USER_NS 65 return (inode->i_sb->s_user_ns); 66 #else 67 return (kcred->user_ns); 68 #endif 69 } 70 71 static inline boolean_t zfs_no_idmapping(struct user_namespace *mnt_userns, 72 struct user_namespace *fs_userns) 73 { 74 return (zfs_is_init_userns(mnt_userns) || mnt_userns == fs_userns); 75 } 76 77 static inline uid_t zfs_uid_to_vfsuid(struct user_namespace *mnt_userns, 78 struct user_namespace *fs_userns, uid_t uid) 79 { 80 if (zfs_no_idmapping(mnt_userns, fs_userns)) 81 return (uid); 82 if (!zfs_is_init_userns(fs_userns)) 83 uid = from_kuid(fs_userns, KUIDT_INIT(uid)); 84 if (uid == (uid_t)-1) 85 return (uid); 86 return (__kuid_val(make_kuid(mnt_userns, uid))); 87 } 88 89 static inline gid_t zfs_gid_to_vfsgid(struct user_namespace *mnt_userns, 90 struct user_namespace *fs_userns, gid_t gid) 91 { 92 if (zfs_no_idmapping(mnt_userns, fs_userns)) 93 return (gid); 94 if (!zfs_is_init_userns(fs_userns)) 95 gid = from_kgid(fs_userns, KGIDT_INIT(gid)); 96 if (gid == (gid_t)-1) 97 return (gid); 98 return (__kgid_val(make_kgid(mnt_userns, gid))); 99 } 100 101 static inline uid_t zfs_vfsuid_to_uid(struct user_namespace *mnt_userns, 102 struct user_namespace *fs_userns, uid_t uid) 103 { 104 if (zfs_no_idmapping(mnt_userns, fs_userns)) 105 return (uid); 106 uid = from_kuid(mnt_userns, KUIDT_INIT(uid)); 107 if (uid == (uid_t)-1) 108 return (uid); 109 if (zfs_is_init_userns(fs_userns)) 110 return (uid); 111 return (__kuid_val(make_kuid(fs_userns, uid))); 112 } 113 114 static inline gid_t zfs_vfsgid_to_gid(struct user_namespace *mnt_userns, 115 struct user_namespace *fs_userns, gid_t gid) 116 { 117 if (zfs_no_idmapping(mnt_userns, fs_userns)) 118 return (gid); 119 gid = from_kgid(mnt_userns, KGIDT_INIT(gid)); 120 if (gid == (gid_t)-1) 121 return (gid); 122 if (zfs_is_init_userns(fs_userns)) 123 return (gid); 124 return (__kgid_val(make_kgid(fs_userns, gid))); 125 } 126 127 extern void crhold(cred_t *cr); 128 extern void crfree(cred_t *cr); 129 extern uid_t crgetuid(const cred_t *cr); 130 extern uid_t crgetruid(const cred_t *cr); 131 extern gid_t crgetgid(const cred_t *cr); 132 extern int crgetngroups(const cred_t *cr); 133 extern gid_t *crgetgroups(const cred_t *cr); 134 extern int groupmember(gid_t gid, const cred_t *cr); 135 #endif /* _SPL_CRED_H */ 136