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 extern zidmap_t *zfs_get_init_idmap(void);
52 
53 /* Check if the user ns is the initial one */
54 static inline boolean_t
55 zfs_is_init_userns(struct user_namespace *user_ns)
56 {
57 #if defined(CONFIG_USER_NS)
58 	return (user_ns == kcred->user_ns);
59 #else
60 	return (B_FALSE);
61 #endif
62 }
63 
64 static inline struct user_namespace *zfs_i_user_ns(struct inode *inode)
65 {
66 #ifdef HAVE_SUPER_USER_NS
67 	return (inode->i_sb->s_user_ns);
68 #else
69 	return (kcred->user_ns);
70 #endif
71 }
72 
73 static inline boolean_t zfs_no_idmapping(struct user_namespace *mnt_userns,
74     struct user_namespace *fs_userns)
75 {
76 	return (zfs_is_init_userns(mnt_userns) || mnt_userns == fs_userns);
77 }
78 
79 static inline uid_t zfs_uid_to_vfsuid(zidmap_t *mnt_userns,
80     struct user_namespace *fs_userns, uid_t uid)
81 {
82 	struct user_namespace *owner = idmap_owner(mnt_userns);
83 	if (zfs_no_idmapping(owner, fs_userns))
84 		return (uid);
85 	if (!zfs_is_init_userns(fs_userns))
86 		uid = from_kuid(fs_userns, KUIDT_INIT(uid));
87 	if (uid == (uid_t)-1)
88 		return (uid);
89 	return (__kuid_val(make_kuid(owner, uid)));
90 }
91 
92 static inline gid_t zfs_gid_to_vfsgid(zidmap_t *mnt_userns,
93     struct user_namespace *fs_userns, gid_t gid)
94 {
95 	struct user_namespace *owner = idmap_owner(mnt_userns);
96 	if (zfs_no_idmapping(owner, fs_userns))
97 		return (gid);
98 	if (!zfs_is_init_userns(fs_userns))
99 		gid = from_kgid(fs_userns, KGIDT_INIT(gid));
100 	if (gid == (gid_t)-1)
101 		return (gid);
102 	return (__kgid_val(make_kgid(owner, gid)));
103 }
104 
105 static inline uid_t zfs_vfsuid_to_uid(zidmap_t *mnt_userns,
106     struct user_namespace *fs_userns, uid_t uid)
107 {
108 	struct user_namespace *owner = idmap_owner(mnt_userns);
109 	if (zfs_no_idmapping(owner, fs_userns))
110 		return (uid);
111 	uid = from_kuid(owner, KUIDT_INIT(uid));
112 	if (uid == (uid_t)-1)
113 		return (uid);
114 	if (zfs_is_init_userns(fs_userns))
115 		return (uid);
116 	return (__kuid_val(make_kuid(fs_userns, uid)));
117 }
118 
119 static inline gid_t zfs_vfsgid_to_gid(zidmap_t *mnt_userns,
120     struct user_namespace *fs_userns, gid_t gid)
121 {
122 	struct user_namespace *owner = idmap_owner(mnt_userns);
123 	if (zfs_no_idmapping(owner, fs_userns))
124 		return (gid);
125 	gid = from_kgid(owner, KGIDT_INIT(gid));
126 	if (gid == (gid_t)-1)
127 		return (gid);
128 	if (zfs_is_init_userns(fs_userns))
129 		return (gid);
130 	return (__kgid_val(make_kgid(fs_userns, gid)));
131 }
132 
133 extern void crhold(cred_t *cr);
134 extern void crfree(cred_t *cr);
135 extern uid_t crgetuid(const cred_t *cr);
136 extern uid_t crgetruid(const cred_t *cr);
137 extern gid_t crgetgid(const cred_t *cr);
138 extern int crgetngroups(const cred_t *cr);
139 extern gid_t *crgetgroups(const cred_t *cr);
140 extern int groupmember(gid_t gid, const cred_t *cr);
141 #endif  /* _SPL_CRED_H */
142