xref: /linux/kernel/capability.c (revision a7f25dc23ff6d238ed70e8a3a8a3792cde3bcc68)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/kernel/capability.c
4  *
5  * Copyright (C) 1997  Andrew Main <zefram@fysh.org>
6  *
7  * Integrated into 2.1.97+,  Andrew G. Morgan <morgan@kernel.org>
8  * 30 May 2002:	Cleanup, Robert M. Love <rml@tech9.net>
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/audit.h>
14 #include <linux/capability.h>
15 #include <linux/mm.h>
16 #include <linux/export.h>
17 #include <linux/security.h>
18 #include <linux/syscalls.h>
19 #include <linux/pid_namespace.h>
20 #include <linux/user_namespace.h>
21 #include <linux/uaccess.h>
22 
23 int file_caps_enabled = 1;
24 
file_caps_disable(char * str)25 static int __init file_caps_disable(char *str)
26 {
27 	file_caps_enabled = 0;
28 	return 1;
29 }
30 __setup("no_file_caps", file_caps_disable);
31 
32 #ifdef CONFIG_MULTIUSER
33 /*
34  * More recent versions of libcap are available from:
35  *
36  *   http://www.kernel.org/pub/linux/libs/security/linux-privs/
37  */
38 
warn_legacy_capability_use(void)39 static void warn_legacy_capability_use(void)
40 {
41 	pr_info_once("warning: `%s' uses 32-bit capabilities (legacy support in use)\n",
42 		     current->comm);
43 }
44 
45 /*
46  * Version 2 capabilities worked fine, but the linux/capability.h file
47  * that accompanied their introduction encouraged their use without
48  * the necessary user-space source code changes. As such, we have
49  * created a version 3 with equivalent functionality to version 2, but
50  * with a header change to protect legacy source code from using
51  * version 2 when it wanted to use version 1. If your system has code
52  * that trips the following warning, it is using version 2 specific
53  * capabilities and may be doing so insecurely.
54  *
55  * The remedy is to either upgrade your version of libcap (to 2.10+,
56  * if the application is linked against it), or recompile your
57  * application with modern kernel headers and this warning will go
58  * away.
59  */
60 
warn_deprecated_v2(void)61 static void warn_deprecated_v2(void)
62 {
63 	pr_info_once("warning: `%s' uses deprecated v2 capabilities in a way that may be insecure\n",
64 		     current->comm);
65 }
66 
67 /*
68  * Version check. Return the number of u32s in each capability flag
69  * array, or a negative value on error.
70  */
cap_validate_magic(cap_user_header_t header,unsigned * tocopy)71 static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
72 {
73 	__u32 version;
74 
75 	if (get_user(version, &header->version))
76 		return -EFAULT;
77 
78 	switch (version) {
79 	case _LINUX_CAPABILITY_VERSION_1:
80 		warn_legacy_capability_use();
81 		*tocopy = _LINUX_CAPABILITY_U32S_1;
82 		break;
83 	case _LINUX_CAPABILITY_VERSION_2:
84 		warn_deprecated_v2();
85 		fallthrough;	/* v3 is otherwise equivalent to v2 */
86 	case _LINUX_CAPABILITY_VERSION_3:
87 		*tocopy = _LINUX_CAPABILITY_U32S_3;
88 		break;
89 	default:
90 		if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
91 			return -EFAULT;
92 		return -EINVAL;
93 	}
94 
95 	return 0;
96 }
97 
98 /*
99  * The only thing that can change the capabilities of the current
100  * process is the current process. As such, we can't be in this code
101  * at the same time as we are in the process of setting capabilities
102  * in this process. The net result is that we can limit our use of
103  * locks to when we are reading the caps of another process.
104  */
cap_get_target_pid(pid_t pid,kernel_cap_t * pEp,kernel_cap_t * pIp,kernel_cap_t * pPp)105 static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
106 				     kernel_cap_t *pIp, kernel_cap_t *pPp)
107 {
108 	int ret;
109 
110 	if (pid && (pid != task_pid_vnr(current))) {
111 		const struct task_struct *target;
112 
113 		rcu_read_lock();
114 
115 		target = find_task_by_vpid(pid);
116 		if (!target)
117 			ret = -ESRCH;
118 		else
119 			ret = security_capget(target, pEp, pIp, pPp);
120 
121 		rcu_read_unlock();
122 	} else
123 		ret = security_capget(current, pEp, pIp, pPp);
124 
125 	return ret;
126 }
127 
128 /**
129  * sys_capget - get the capabilities of a given process.
130  * @header: pointer to struct that contains capability version and
131  *	target pid data
132  * @dataptr: pointer to struct that contains the effective, permitted,
133  *	and inheritable capabilities that are returned
134  *
135  * Returns 0 on success and < 0 on error.
136  */
SYSCALL_DEFINE2(capget,cap_user_header_t,header,cap_user_data_t,dataptr)137 SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
138 {
139 	int ret = 0;
140 	pid_t pid;
141 	unsigned tocopy;
142 	kernel_cap_t pE, pI, pP;
143 	struct __user_cap_data_struct kdata[2];
144 
145 	ret = cap_validate_magic(header, &tocopy);
146 	if ((dataptr == NULL) || (ret != 0))
147 		return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
148 
149 	if (get_user(pid, &header->pid))
150 		return -EFAULT;
151 
152 	if (pid < 0)
153 		return -EINVAL;
154 
155 	ret = cap_get_target_pid(pid, &pE, &pI, &pP);
156 	if (ret)
157 		return ret;
158 
159 	/*
160 	 * Annoying legacy format with 64-bit capabilities exposed
161 	 * as two sets of 32-bit fields, so we need to split the
162 	 * capability values up.
163 	 */
164 	kdata[0].effective   = pE.val; kdata[1].effective   = pE.val >> 32;
165 	kdata[0].permitted   = pP.val; kdata[1].permitted   = pP.val >> 32;
166 	kdata[0].inheritable = pI.val; kdata[1].inheritable = pI.val >> 32;
167 
168 	/*
169 	 * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
170 	 * we silently drop the upper capabilities here. This
171 	 * has the effect of making older libcap
172 	 * implementations implicitly drop upper capability
173 	 * bits when they perform a: capget/modify/capset
174 	 * sequence.
175 	 *
176 	 * This behavior is considered fail-safe
177 	 * behavior. Upgrading the application to a newer
178 	 * version of libcap will enable access to the newer
179 	 * capabilities.
180 	 *
181 	 * An alternative would be to return an error here
182 	 * (-ERANGE), but that causes legacy applications to
183 	 * unexpectedly fail; the capget/modify/capset aborts
184 	 * before modification is attempted and the application
185 	 * fails.
186 	 */
187 	if (copy_to_user(dataptr, kdata, tocopy * sizeof(kdata[0])))
188 		return -EFAULT;
189 
190 	return 0;
191 }
192 
mk_kernel_cap(u32 low,u32 high)193 static kernel_cap_t mk_kernel_cap(u32 low, u32 high)
194 {
195 	return (kernel_cap_t) { (low | ((u64)high << 32)) & CAP_VALID_MASK };
196 }
197 
198 /**
199  * sys_capset - set capabilities for a process or (*) a group of processes
200  * @header: pointer to struct that contains capability version and
201  *	target pid data
202  * @data: pointer to struct that contains the effective, permitted,
203  *	and inheritable capabilities
204  *
205  * Set capabilities for the current process only.  The ability to any other
206  * process(es) has been deprecated and removed.
207  *
208  * The restrictions on setting capabilities are specified as:
209  *
210  * I: any raised capabilities must be a subset of the old permitted
211  * P: any raised capabilities must be a subset of the old permitted
212  * E: must be set to a subset of new permitted
213  *
214  * Returns 0 on success and < 0 on error.
215  */
SYSCALL_DEFINE2(capset,cap_user_header_t,header,const cap_user_data_t,data)216 SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
217 {
218 	struct __user_cap_data_struct kdata[2] = { { 0, }, };
219 	unsigned tocopy, copybytes;
220 	kernel_cap_t inheritable, permitted, effective;
221 	struct cred *new;
222 	int ret;
223 	pid_t pid;
224 
225 	ret = cap_validate_magic(header, &tocopy);
226 	if (ret != 0)
227 		return ret;
228 
229 	if (get_user(pid, &header->pid))
230 		return -EFAULT;
231 
232 	/* may only affect current now */
233 	if (pid != 0 && pid != task_pid_vnr(current))
234 		return -EPERM;
235 
236 	copybytes = tocopy * sizeof(struct __user_cap_data_struct);
237 	if (copybytes > sizeof(kdata))
238 		return -EFAULT;
239 
240 	if (copy_from_user(&kdata, data, copybytes))
241 		return -EFAULT;
242 
243 	effective   = mk_kernel_cap(kdata[0].effective,   kdata[1].effective);
244 	permitted   = mk_kernel_cap(kdata[0].permitted,   kdata[1].permitted);
245 	inheritable = mk_kernel_cap(kdata[0].inheritable, kdata[1].inheritable);
246 
247 	new = prepare_creds();
248 	if (!new)
249 		return -ENOMEM;
250 
251 	ret = security_capset(new, current_cred(),
252 			      &effective, &inheritable, &permitted);
253 	if (ret < 0)
254 		goto error;
255 
256 	audit_log_capset(new, current_cred());
257 
258 	return commit_creds(new);
259 
260 error:
261 	abort_creds(new);
262 	return ret;
263 }
264 
265 /**
266  * has_ns_capability - Does a task have a capability in a specific user ns
267  * @t: The task in question
268  * @ns: target user namespace
269  * @cap: The capability to be tested for
270  *
271  * Return true if the specified task has the given superior capability
272  * currently in effect to the specified user namespace, false if not.
273  *
274  * Note that this does not set PF_SUPERPRIV on the task.
275  */
has_ns_capability(struct task_struct * t,struct user_namespace * ns,int cap)276 bool has_ns_capability(struct task_struct *t,
277 		       struct user_namespace *ns, int cap)
278 {
279 	int ret;
280 
281 	rcu_read_lock();
282 	ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NONE);
283 	rcu_read_unlock();
284 
285 	return (ret == 0);
286 }
287 
288 /**
289  * has_ns_capability_noaudit - Does a task have a capability (unaudited)
290  * in a specific user ns.
291  * @t: The task in question
292  * @ns: target user namespace
293  * @cap: The capability to be tested for
294  *
295  * Return true if the specified task has the given superior capability
296  * currently in effect to the specified user namespace, false if not.
297  * Do not write an audit message for the check.
298  *
299  * Note that this does not set PF_SUPERPRIV on the task.
300  */
has_ns_capability_noaudit(struct task_struct * t,struct user_namespace * ns,int cap)301 bool has_ns_capability_noaudit(struct task_struct *t,
302 			       struct user_namespace *ns, int cap)
303 {
304 	int ret;
305 
306 	rcu_read_lock();
307 	ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NOAUDIT);
308 	rcu_read_unlock();
309 
310 	return (ret == 0);
311 }
312 
313 /**
314  * has_capability_noaudit - Does a task have a capability (unaudited) in the
315  * initial user ns
316  * @t: The task in question
317  * @cap: The capability to be tested for
318  *
319  * Return true if the specified task has the given superior capability
320  * currently in effect to init_user_ns, false if not.  Don't write an
321  * audit message for the check.
322  *
323  * Note that this does not set PF_SUPERPRIV on the task.
324  */
has_capability_noaudit(struct task_struct * t,int cap)325 bool has_capability_noaudit(struct task_struct *t, int cap)
326 {
327 	return has_ns_capability_noaudit(t, &init_user_ns, cap);
328 }
329 
ns_capable_common(struct user_namespace * ns,int cap,unsigned int opts)330 static bool ns_capable_common(struct user_namespace *ns,
331 			      int cap,
332 			      unsigned int opts)
333 {
334 	int capable;
335 
336 	if (unlikely(!cap_valid(cap))) {
337 		pr_crit("capable() called with invalid cap=%u\n", cap);
338 		BUG();
339 	}
340 
341 	capable = security_capable(current_cred(), ns, cap, opts);
342 	if (capable == 0) {
343 		current->flags |= PF_SUPERPRIV;
344 		return true;
345 	}
346 	return false;
347 }
348 
349 /**
350  * ns_capable - Determine if the current task has a superior capability in effect
351  * @ns:  The usernamespace we want the capability in
352  * @cap: The capability to be tested for
353  *
354  * Return true if the current task has the given superior capability currently
355  * available for use, false if not.
356  *
357  * This sets PF_SUPERPRIV on the task if the capability is available on the
358  * assumption that it's about to be used.
359  */
ns_capable(struct user_namespace * ns,int cap)360 bool ns_capable(struct user_namespace *ns, int cap)
361 {
362 	return ns_capable_common(ns, cap, CAP_OPT_NONE);
363 }
364 EXPORT_SYMBOL(ns_capable);
365 
366 /**
367  * ns_capable_noaudit - Determine if the current task has a superior capability
368  * (unaudited) in effect
369  * @ns:  The usernamespace we want the capability in
370  * @cap: The capability to be tested for
371  *
372  * Return true if the current task has the given superior capability currently
373  * available for use, false if not.
374  *
375  * This sets PF_SUPERPRIV on the task if the capability is available on the
376  * assumption that it's about to be used.
377  */
ns_capable_noaudit(struct user_namespace * ns,int cap)378 bool ns_capable_noaudit(struct user_namespace *ns, int cap)
379 {
380 	return ns_capable_common(ns, cap, CAP_OPT_NOAUDIT);
381 }
382 EXPORT_SYMBOL(ns_capable_noaudit);
383 
384 /**
385  * ns_capable_setid - Determine if the current task has a superior capability
386  * in effect, while signalling that this check is being done from within a
387  * setid or setgroups syscall.
388  * @ns:  The usernamespace we want the capability in
389  * @cap: The capability to be tested for
390  *
391  * Return true if the current task has the given superior capability currently
392  * available for use, false if not.
393  *
394  * This sets PF_SUPERPRIV on the task if the capability is available on the
395  * assumption that it's about to be used.
396  */
ns_capable_setid(struct user_namespace * ns,int cap)397 bool ns_capable_setid(struct user_namespace *ns, int cap)
398 {
399 	return ns_capable_common(ns, cap, CAP_OPT_INSETID);
400 }
401 EXPORT_SYMBOL(ns_capable_setid);
402 
403 /**
404  * capable - Determine if the current task has a superior capability in effect
405  * @cap: The capability to be tested for
406  *
407  * Return true if the current task has the given superior capability currently
408  * available for use, false if not.
409  *
410  * This sets PF_SUPERPRIV on the task if the capability is available on the
411  * assumption that it's about to be used.
412  */
capable(int cap)413 bool capable(int cap)
414 {
415 	return ns_capable(&init_user_ns, cap);
416 }
417 EXPORT_SYMBOL(capable);
418 
419 /**
420  * capable_noaudit - Determine if the current task has a superior
421  * capability in effect by checking the process's effective
422  * capabilities (unaudited).
423  * @cap: The capability to be tested for
424  *
425  * This is the same as capable(), except it uses CAP_OPT_NOAUDIT as to prevent
426  * issuing spurious audit messages.
427  *
428  * This sets PF_SUPERPRIV on the task if the capability is available on the
429  * assumption that it's about to be used.
430  */
capable_noaudit(int cap)431 bool capable_noaudit(int cap)
432 {
433 	return ns_capable_noaudit(&init_user_ns, cap);
434 }
435 EXPORT_SYMBOL(capable_noaudit);
436 #endif /* CONFIG_MULTIUSER */
437 
438 /**
439  * file_ns_capable - Determine if the file's opener had a capability in effect
440  * @file:  The file we want to check
441  * @ns:  The usernamespace we want the capability in
442  * @cap: The capability to be tested for
443  *
444  * Return true if task that opened the file had a capability in effect
445  * when the file was opened.
446  *
447  * This does not set PF_SUPERPRIV because the caller may not
448  * actually be privileged.
449  */
file_ns_capable(const struct file * file,struct user_namespace * ns,int cap)450 bool file_ns_capable(const struct file *file, struct user_namespace *ns,
451 		     int cap)
452 {
453 
454 	if (WARN_ON_ONCE(!cap_valid(cap)))
455 		return false;
456 
457 	if (security_capable(file->f_cred, ns, cap, CAP_OPT_NONE) == 0)
458 		return true;
459 
460 	return false;
461 }
462 EXPORT_SYMBOL(file_ns_capable);
463 
464 /**
465  * privileged_wrt_inode_uidgid - Do capabilities in the namespace work over the inode?
466  * @ns: The user namespace in question
467  * @idmap: idmap of the mount @inode was found from
468  * @inode: The inode in question
469  *
470  * Return true if the inode uid and gid are within the namespace.
471  */
privileged_wrt_inode_uidgid(struct user_namespace * ns,struct mnt_idmap * idmap,const struct inode * inode)472 bool privileged_wrt_inode_uidgid(struct user_namespace *ns,
473 				 struct mnt_idmap *idmap,
474 				 const struct inode *inode)
475 {
476 	return vfsuid_has_mapping(ns, i_uid_into_vfsuid(idmap, inode)) &&
477 	       vfsgid_has_mapping(ns, i_gid_into_vfsgid(idmap, inode));
478 }
479 
480 /**
481  * capable_wrt_inode_uidgid - Check nsown_capable and uid and gid mapped
482  * @idmap: idmap of the mount @inode was found from
483  * @inode: The inode in question
484  * @cap: The capability in question
485  *
486  * Return true if the current task has the given capability targeted at
487  * its own user namespace and that the given inode's uid and gid are
488  * mapped into the current user namespace.
489  */
capable_wrt_inode_uidgid(struct mnt_idmap * idmap,const struct inode * inode,int cap)490 bool capable_wrt_inode_uidgid(struct mnt_idmap *idmap,
491 			      const struct inode *inode, int cap)
492 {
493 	struct user_namespace *ns = current_user_ns();
494 
495 	return ns_capable(ns, cap) &&
496 	       privileged_wrt_inode_uidgid(ns, idmap, inode);
497 }
498 EXPORT_SYMBOL(capable_wrt_inode_uidgid);
499 
500 /**
501  * ptracer_capable - Determine if the ptracer holds CAP_SYS_PTRACE in the namespace
502  * @tsk: The task that may be ptraced
503  * @ns: The user namespace to search for CAP_SYS_PTRACE in
504  *
505  * Return true if the task that is ptracing the current task had CAP_SYS_PTRACE
506  * in the specified user namespace.
507  */
ptracer_capable(struct task_struct * tsk,struct user_namespace * ns)508 bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns)
509 {
510 	int ret = 0;  /* An absent tracer adds no restrictions */
511 	const struct cred *cred;
512 
513 	rcu_read_lock();
514 	cred = rcu_dereference(tsk->ptracer_cred);
515 	if (cred)
516 		ret = security_capable(cred, ns, CAP_SYS_PTRACE,
517 				       CAP_OPT_NOAUDIT);
518 	rcu_read_unlock();
519 	return (ret == 0);
520 }
521