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