1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * This is <linux/capability.h>
4  *
5  * Andrew G. Morgan <morgan@kernel.org>
6  * Alexander Kjeldaas <astor@guardian.no>
7  * with help from Aleph1, Roland Buresund and Andrew Main.
8  *
9  * See here for the libcap library ("POSIX draft" compliance):
10  *
11  * ftp://www.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/
12  */
13 #ifndef _LINUX_CAPABILITY_H
14 #define _LINUX_CAPABILITY_H
15 
16 #include <uapi/linux/capability.h>
17 
18 
19 #define _KERNEL_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3
20 #define _KERNEL_CAPABILITY_U32S    _LINUX_CAPABILITY_U32S_3
21 
22 extern int file_caps_enabled;
23 
24 typedef struct kernel_cap_struct {
25 	__u32 cap[_KERNEL_CAPABILITY_U32S];
26 } kernel_cap_t;
27 
28 /* exact same as vfs_cap_data but in cpu endian and always filled completely */
29 struct cpu_vfs_cap_data {
30 	__u32 magic_etc;
31 	kernel_cap_t permitted;
32 	kernel_cap_t inheritable;
33 };
34 
35 #define _USER_CAP_HEADER_SIZE  (sizeof(struct __user_cap_header_struct))
36 #define _KERNEL_CAP_T_SIZE     (sizeof(kernel_cap_t))
37 
38 
39 struct file;
40 struct inode;
41 struct dentry;
42 struct task_struct;
43 struct user_namespace;
44 
45 extern const kernel_cap_t __cap_empty_set;
46 extern const kernel_cap_t __cap_init_eff_set;
47 
48 /*
49  * Internal kernel functions only
50  */
51 
52 #define CAP_FOR_EACH_U32(__capi)  \
53 	for (__capi = 0; __capi < _KERNEL_CAPABILITY_U32S; ++__capi)
54 
55 /*
56  * CAP_FS_MASK and CAP_NFSD_MASKS:
57  *
58  * The fs mask is all the privileges that fsuid==0 historically meant.
59  * At one time in the past, that included CAP_MKNOD and CAP_LINUX_IMMUTABLE.
60  *
61  * It has never meant setting security.* and trusted.* xattrs.
62  *
63  * We could also define fsmask as follows:
64  *   1. CAP_FS_MASK is the privilege to bypass all fs-related DAC permissions
65  *   2. The security.* and trusted.* xattrs are fs-related MAC permissions
66  */
67 
68 # define CAP_FS_MASK_B0     (CAP_TO_MASK(CAP_CHOWN)		\
69 			    | CAP_TO_MASK(CAP_MKNOD)		\
70 			    | CAP_TO_MASK(CAP_DAC_OVERRIDE)	\
71 			    | CAP_TO_MASK(CAP_DAC_READ_SEARCH)	\
72 			    | CAP_TO_MASK(CAP_FOWNER)		\
73 			    | CAP_TO_MASK(CAP_FSETID))
74 
75 # define CAP_FS_MASK_B1     (CAP_TO_MASK(CAP_MAC_OVERRIDE))
76 
77 #if _KERNEL_CAPABILITY_U32S != 2
78 # error Fix up hand-coded capability macro initializers
79 #else /* HAND-CODED capability initializers */
80 
81 #define CAP_LAST_U32			((_KERNEL_CAPABILITY_U32S) - 1)
82 #define CAP_LAST_U32_VALID_MASK		(CAP_TO_MASK(CAP_LAST_CAP + 1) -1)
83 
84 # define CAP_EMPTY_SET    ((kernel_cap_t){{ 0, 0 }})
85 # define CAP_FULL_SET     ((kernel_cap_t){{ ~0, CAP_LAST_U32_VALID_MASK }})
86 # define CAP_FS_SET       ((kernel_cap_t){{ CAP_FS_MASK_B0 \
87 				    | CAP_TO_MASK(CAP_LINUX_IMMUTABLE), \
88 				    CAP_FS_MASK_B1 } })
89 # define CAP_NFSD_SET     ((kernel_cap_t){{ CAP_FS_MASK_B0 \
90 				    | CAP_TO_MASK(CAP_SYS_RESOURCE), \
91 				    CAP_FS_MASK_B1 } })
92 
93 #endif /* _KERNEL_CAPABILITY_U32S != 2 */
94 
95 # define cap_clear(c)         do { (c) = __cap_empty_set; } while (0)
96 
97 #define cap_raise(c, flag)  ((c).cap[CAP_TO_INDEX(flag)] |= CAP_TO_MASK(flag))
98 #define cap_lower(c, flag)  ((c).cap[CAP_TO_INDEX(flag)] &= ~CAP_TO_MASK(flag))
99 #define cap_raised(c, flag) ((c).cap[CAP_TO_INDEX(flag)] & CAP_TO_MASK(flag))
100 
101 #define CAP_BOP_ALL(c, a, b, OP)                                    \
102 do {                                                                \
103 	unsigned __capi;                                            \
104 	CAP_FOR_EACH_U32(__capi) {                                  \
105 		c.cap[__capi] = a.cap[__capi] OP b.cap[__capi];     \
106 	}                                                           \
107 } while (0)
108 
109 #define CAP_UOP_ALL(c, a, OP)                                       \
110 do {                                                                \
111 	unsigned __capi;                                            \
112 	CAP_FOR_EACH_U32(__capi) {                                  \
113 		c.cap[__capi] = OP a.cap[__capi];                   \
114 	}                                                           \
115 } while (0)
116 
cap_combine(const kernel_cap_t a,const kernel_cap_t b)117 static inline kernel_cap_t cap_combine(const kernel_cap_t a,
118 				       const kernel_cap_t b)
119 {
120 	kernel_cap_t dest;
121 	CAP_BOP_ALL(dest, a, b, |);
122 	return dest;
123 }
124 
cap_intersect(const kernel_cap_t a,const kernel_cap_t b)125 static inline kernel_cap_t cap_intersect(const kernel_cap_t a,
126 					 const kernel_cap_t b)
127 {
128 	kernel_cap_t dest;
129 	CAP_BOP_ALL(dest, a, b, &);
130 	return dest;
131 }
132 
cap_drop(const kernel_cap_t a,const kernel_cap_t drop)133 static inline kernel_cap_t cap_drop(const kernel_cap_t a,
134 				    const kernel_cap_t drop)
135 {
136 	kernel_cap_t dest;
137 	CAP_BOP_ALL(dest, a, drop, &~);
138 	return dest;
139 }
140 
cap_invert(const kernel_cap_t c)141 static inline kernel_cap_t cap_invert(const kernel_cap_t c)
142 {
143 	kernel_cap_t dest;
144 	CAP_UOP_ALL(dest, c, ~);
145 	return dest;
146 }
147 
cap_isclear(const kernel_cap_t a)148 static inline bool cap_isclear(const kernel_cap_t a)
149 {
150 	unsigned __capi;
151 	CAP_FOR_EACH_U32(__capi) {
152 		if (a.cap[__capi] != 0)
153 			return false;
154 	}
155 	return true;
156 }
157 
158 /*
159  * Check if "a" is a subset of "set".
160  * return true if ALL of the capabilities in "a" are also in "set"
161  *	cap_issubset(0101, 1111) will return true
162  * return false if ANY of the capabilities in "a" are not in "set"
163  *	cap_issubset(1111, 0101) will return false
164  */
cap_issubset(const kernel_cap_t a,const kernel_cap_t set)165 static inline bool cap_issubset(const kernel_cap_t a, const kernel_cap_t set)
166 {
167 	kernel_cap_t dest;
168 	dest = cap_drop(a, set);
169 	return cap_isclear(dest);
170 }
171 
172 /* Used to decide between falling back on the old suser() or fsuser(). */
173 
cap_drop_fs_set(const kernel_cap_t a)174 static inline kernel_cap_t cap_drop_fs_set(const kernel_cap_t a)
175 {
176 	const kernel_cap_t __cap_fs_set = CAP_FS_SET;
177 	return cap_drop(a, __cap_fs_set);
178 }
179 
cap_raise_fs_set(const kernel_cap_t a,const kernel_cap_t permitted)180 static inline kernel_cap_t cap_raise_fs_set(const kernel_cap_t a,
181 					    const kernel_cap_t permitted)
182 {
183 	const kernel_cap_t __cap_fs_set = CAP_FS_SET;
184 	return cap_combine(a,
185 			   cap_intersect(permitted, __cap_fs_set));
186 }
187 
cap_drop_nfsd_set(const kernel_cap_t a)188 static inline kernel_cap_t cap_drop_nfsd_set(const kernel_cap_t a)
189 {
190 	const kernel_cap_t __cap_fs_set = CAP_NFSD_SET;
191 	return cap_drop(a, __cap_fs_set);
192 }
193 
cap_raise_nfsd_set(const kernel_cap_t a,const kernel_cap_t permitted)194 static inline kernel_cap_t cap_raise_nfsd_set(const kernel_cap_t a,
195 					      const kernel_cap_t permitted)
196 {
197 	const kernel_cap_t __cap_nfsd_set = CAP_NFSD_SET;
198 	return cap_combine(a,
199 			   cap_intersect(permitted, __cap_nfsd_set));
200 }
201 
202 #ifdef CONFIG_MULTIUSER
203 extern bool has_capability(struct task_struct *t, int cap);
204 extern bool has_ns_capability(struct task_struct *t,
205 			      struct user_namespace *ns, int cap);
206 extern bool has_capability_noaudit(struct task_struct *t, int cap);
207 extern bool has_ns_capability_noaudit(struct task_struct *t,
208 				      struct user_namespace *ns, int cap);
209 extern bool capable(int cap);
210 extern bool ns_capable(struct user_namespace *ns, int cap);
211 extern bool ns_capable_noaudit(struct user_namespace *ns, int cap);
212 #else
has_capability(struct task_struct * t,int cap)213 static inline bool has_capability(struct task_struct *t, int cap)
214 {
215 	return true;
216 }
has_ns_capability(struct task_struct * t,struct user_namespace * ns,int cap)217 static inline bool has_ns_capability(struct task_struct *t,
218 			      struct user_namespace *ns, int cap)
219 {
220 	return true;
221 }
has_capability_noaudit(struct task_struct * t,int cap)222 static inline bool has_capability_noaudit(struct task_struct *t, int cap)
223 {
224 	return true;
225 }
has_ns_capability_noaudit(struct task_struct * t,struct user_namespace * ns,int cap)226 static inline bool has_ns_capability_noaudit(struct task_struct *t,
227 				      struct user_namespace *ns, int cap)
228 {
229 	return true;
230 }
capable(int cap)231 static inline bool capable(int cap)
232 {
233 	return true;
234 }
ns_capable(struct user_namespace * ns,int cap)235 static inline bool ns_capable(struct user_namespace *ns, int cap)
236 {
237 	return true;
238 }
ns_capable_noaudit(struct user_namespace * ns,int cap)239 static inline bool ns_capable_noaudit(struct user_namespace *ns, int cap)
240 {
241 	return true;
242 }
243 #endif /* CONFIG_MULTIUSER */
244 extern bool privileged_wrt_inode_uidgid(struct user_namespace *ns, const struct inode *inode);
245 extern bool capable_wrt_inode_uidgid(const struct inode *inode, int cap);
246 extern bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap);
247 extern bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns);
248 
249 /* audit system wants to get cap info from files as well */
250 extern int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps);
251 
252 extern int cap_convert_nscap(struct dentry *dentry, void **ivalue, size_t size);
253 
254 #endif /* !_LINUX_CAPABILITY_H */
255