Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * linux/kernel/capability.c
0004  *
0005  * Copyright (C) 1997  Andrew Main <zefram@fysh.org>
0006  *
0007  * Integrated into 2.1.97+,  Andrew G. Morgan <morgan@kernel.org>
0008  * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
0009  */
0010 
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012 
0013 #include <linux/audit.h>
0014 #include <linux/capability.h>
0015 #include <linux/mm.h>
0016 #include <linux/export.h>
0017 #include <linux/security.h>
0018 #include <linux/syscalls.h>
0019 #include <linux/pid_namespace.h>
0020 #include <linux/user_namespace.h>
0021 #include <linux/uaccess.h>
0022 
0023 /*
0024  * Leveraged for setting/resetting capabilities
0025  */
0026 
0027 const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
0028 EXPORT_SYMBOL(__cap_empty_set);
0029 
0030 int file_caps_enabled = 1;
0031 
0032 static int __init file_caps_disable(char *str)
0033 {
0034     file_caps_enabled = 0;
0035     return 1;
0036 }
0037 __setup("no_file_caps", file_caps_disable);
0038 
0039 #ifdef CONFIG_MULTIUSER
0040 /*
0041  * More recent versions of libcap are available from:
0042  *
0043  *   http://www.kernel.org/pub/linux/libs/security/linux-privs/
0044  */
0045 
0046 static void warn_legacy_capability_use(void)
0047 {
0048     char name[sizeof(current->comm)];
0049 
0050     pr_info_once("warning: `%s' uses 32-bit capabilities (legacy support in use)\n",
0051              get_task_comm(name, current));
0052 }
0053 
0054 /*
0055  * Version 2 capabilities worked fine, but the linux/capability.h file
0056  * that accompanied their introduction encouraged their use without
0057  * the necessary user-space source code changes. As such, we have
0058  * created a version 3 with equivalent functionality to version 2, but
0059  * with a header change to protect legacy source code from using
0060  * version 2 when it wanted to use version 1. If your system has code
0061  * that trips the following warning, it is using version 2 specific
0062  * capabilities and may be doing so insecurely.
0063  *
0064  * The remedy is to either upgrade your version of libcap (to 2.10+,
0065  * if the application is linked against it), or recompile your
0066  * application with modern kernel headers and this warning will go
0067  * away.
0068  */
0069 
0070 static void warn_deprecated_v2(void)
0071 {
0072     char name[sizeof(current->comm)];
0073 
0074     pr_info_once("warning: `%s' uses deprecated v2 capabilities in a way that may be insecure\n",
0075              get_task_comm(name, current));
0076 }
0077 
0078 /*
0079  * Version check. Return the number of u32s in each capability flag
0080  * array, or a negative value on error.
0081  */
0082 static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
0083 {
0084     __u32 version;
0085 
0086     if (get_user(version, &header->version))
0087         return -EFAULT;
0088 
0089     switch (version) {
0090     case _LINUX_CAPABILITY_VERSION_1:
0091         warn_legacy_capability_use();
0092         *tocopy = _LINUX_CAPABILITY_U32S_1;
0093         break;
0094     case _LINUX_CAPABILITY_VERSION_2:
0095         warn_deprecated_v2();
0096         fallthrough;    /* v3 is otherwise equivalent to v2 */
0097     case _LINUX_CAPABILITY_VERSION_3:
0098         *tocopy = _LINUX_CAPABILITY_U32S_3;
0099         break;
0100     default:
0101         if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
0102             return -EFAULT;
0103         return -EINVAL;
0104     }
0105 
0106     return 0;
0107 }
0108 
0109 /*
0110  * The only thing that can change the capabilities of the current
0111  * process is the current process. As such, we can't be in this code
0112  * at the same time as we are in the process of setting capabilities
0113  * in this process. The net result is that we can limit our use of
0114  * locks to when we are reading the caps of another process.
0115  */
0116 static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
0117                      kernel_cap_t *pIp, kernel_cap_t *pPp)
0118 {
0119     int ret;
0120 
0121     if (pid && (pid != task_pid_vnr(current))) {
0122         struct task_struct *target;
0123 
0124         rcu_read_lock();
0125 
0126         target = find_task_by_vpid(pid);
0127         if (!target)
0128             ret = -ESRCH;
0129         else
0130             ret = security_capget(target, pEp, pIp, pPp);
0131 
0132         rcu_read_unlock();
0133     } else
0134         ret = security_capget(current, pEp, pIp, pPp);
0135 
0136     return ret;
0137 }
0138 
0139 /**
0140  * sys_capget - get the capabilities of a given process.
0141  * @header: pointer to struct that contains capability version and
0142  *  target pid data
0143  * @dataptr: pointer to struct that contains the effective, permitted,
0144  *  and inheritable capabilities that are returned
0145  *
0146  * Returns 0 on success and < 0 on error.
0147  */
0148 SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
0149 {
0150     int ret = 0;
0151     pid_t pid;
0152     unsigned tocopy;
0153     kernel_cap_t pE, pI, pP;
0154 
0155     ret = cap_validate_magic(header, &tocopy);
0156     if ((dataptr == NULL) || (ret != 0))
0157         return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
0158 
0159     if (get_user(pid, &header->pid))
0160         return -EFAULT;
0161 
0162     if (pid < 0)
0163         return -EINVAL;
0164 
0165     ret = cap_get_target_pid(pid, &pE, &pI, &pP);
0166     if (!ret) {
0167         struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
0168         unsigned i;
0169 
0170         for (i = 0; i < tocopy; i++) {
0171             kdata[i].effective = pE.cap[i];
0172             kdata[i].permitted = pP.cap[i];
0173             kdata[i].inheritable = pI.cap[i];
0174         }
0175 
0176         /*
0177          * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
0178          * we silently drop the upper capabilities here. This
0179          * has the effect of making older libcap
0180          * implementations implicitly drop upper capability
0181          * bits when they perform a: capget/modify/capset
0182          * sequence.
0183          *
0184          * This behavior is considered fail-safe
0185          * behavior. Upgrading the application to a newer
0186          * version of libcap will enable access to the newer
0187          * capabilities.
0188          *
0189          * An alternative would be to return an error here
0190          * (-ERANGE), but that causes legacy applications to
0191          * unexpectedly fail; the capget/modify/capset aborts
0192          * before modification is attempted and the application
0193          * fails.
0194          */
0195         if (copy_to_user(dataptr, kdata, tocopy
0196                  * sizeof(struct __user_cap_data_struct))) {
0197             return -EFAULT;
0198         }
0199     }
0200 
0201     return ret;
0202 }
0203 
0204 /**
0205  * sys_capset - set capabilities for a process or (*) a group of processes
0206  * @header: pointer to struct that contains capability version and
0207  *  target pid data
0208  * @data: pointer to struct that contains the effective, permitted,
0209  *  and inheritable capabilities
0210  *
0211  * Set capabilities for the current process only.  The ability to any other
0212  * process(es) has been deprecated and removed.
0213  *
0214  * The restrictions on setting capabilities are specified as:
0215  *
0216  * I: any raised capabilities must be a subset of the old permitted
0217  * P: any raised capabilities must be a subset of the old permitted
0218  * E: must be set to a subset of new permitted
0219  *
0220  * Returns 0 on success and < 0 on error.
0221  */
0222 SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
0223 {
0224     struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
0225     unsigned i, tocopy, copybytes;
0226     kernel_cap_t inheritable, permitted, effective;
0227     struct cred *new;
0228     int ret;
0229     pid_t pid;
0230 
0231     ret = cap_validate_magic(header, &tocopy);
0232     if (ret != 0)
0233         return ret;
0234 
0235     if (get_user(pid, &header->pid))
0236         return -EFAULT;
0237 
0238     /* may only affect current now */
0239     if (pid != 0 && pid != task_pid_vnr(current))
0240         return -EPERM;
0241 
0242     copybytes = tocopy * sizeof(struct __user_cap_data_struct);
0243     if (copybytes > sizeof(kdata))
0244         return -EFAULT;
0245 
0246     if (copy_from_user(&kdata, data, copybytes))
0247         return -EFAULT;
0248 
0249     for (i = 0; i < tocopy; i++) {
0250         effective.cap[i] = kdata[i].effective;
0251         permitted.cap[i] = kdata[i].permitted;
0252         inheritable.cap[i] = kdata[i].inheritable;
0253     }
0254     while (i < _KERNEL_CAPABILITY_U32S) {
0255         effective.cap[i] = 0;
0256         permitted.cap[i] = 0;
0257         inheritable.cap[i] = 0;
0258         i++;
0259     }
0260 
0261     effective.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
0262     permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
0263     inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
0264 
0265     new = prepare_creds();
0266     if (!new)
0267         return -ENOMEM;
0268 
0269     ret = security_capset(new, current_cred(),
0270                   &effective, &inheritable, &permitted);
0271     if (ret < 0)
0272         goto error;
0273 
0274     audit_log_capset(new, current_cred());
0275 
0276     return commit_creds(new);
0277 
0278 error:
0279     abort_creds(new);
0280     return ret;
0281 }
0282 
0283 /**
0284  * has_ns_capability - Does a task have a capability in a specific user ns
0285  * @t: The task in question
0286  * @ns: target user namespace
0287  * @cap: The capability to be tested for
0288  *
0289  * Return true if the specified task has the given superior capability
0290  * currently in effect to the specified user namespace, false if not.
0291  *
0292  * Note that this does not set PF_SUPERPRIV on the task.
0293  */
0294 bool has_ns_capability(struct task_struct *t,
0295                struct user_namespace *ns, int cap)
0296 {
0297     int ret;
0298 
0299     rcu_read_lock();
0300     ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NONE);
0301     rcu_read_unlock();
0302 
0303     return (ret == 0);
0304 }
0305 
0306 /**
0307  * has_capability - Does a task have a capability in init_user_ns
0308  * @t: The task in question
0309  * @cap: The capability to be tested for
0310  *
0311  * Return true if the specified task has the given superior capability
0312  * currently in effect to the initial user namespace, false if not.
0313  *
0314  * Note that this does not set PF_SUPERPRIV on the task.
0315  */
0316 bool has_capability(struct task_struct *t, int cap)
0317 {
0318     return has_ns_capability(t, &init_user_ns, cap);
0319 }
0320 EXPORT_SYMBOL(has_capability);
0321 
0322 /**
0323  * has_ns_capability_noaudit - Does a task have a capability (unaudited)
0324  * in a specific user ns.
0325  * @t: The task in question
0326  * @ns: target user namespace
0327  * @cap: The capability to be tested for
0328  *
0329  * Return true if the specified task has the given superior capability
0330  * currently in effect to the specified user namespace, false if not.
0331  * Do not write an audit message for the check.
0332  *
0333  * Note that this does not set PF_SUPERPRIV on the task.
0334  */
0335 bool has_ns_capability_noaudit(struct task_struct *t,
0336                    struct user_namespace *ns, int cap)
0337 {
0338     int ret;
0339 
0340     rcu_read_lock();
0341     ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NOAUDIT);
0342     rcu_read_unlock();
0343 
0344     return (ret == 0);
0345 }
0346 
0347 /**
0348  * has_capability_noaudit - Does a task have a capability (unaudited) in the
0349  * initial user ns
0350  * @t: The task in question
0351  * @cap: The capability to be tested for
0352  *
0353  * Return true if the specified task has the given superior capability
0354  * currently in effect to init_user_ns, false if not.  Don't write an
0355  * audit message for the check.
0356  *
0357  * Note that this does not set PF_SUPERPRIV on the task.
0358  */
0359 bool has_capability_noaudit(struct task_struct *t, int cap)
0360 {
0361     return has_ns_capability_noaudit(t, &init_user_ns, cap);
0362 }
0363 EXPORT_SYMBOL(has_capability_noaudit);
0364 
0365 static bool ns_capable_common(struct user_namespace *ns,
0366                   int cap,
0367                   unsigned int opts)
0368 {
0369     int capable;
0370 
0371     if (unlikely(!cap_valid(cap))) {
0372         pr_crit("capable() called with invalid cap=%u\n", cap);
0373         BUG();
0374     }
0375 
0376     capable = security_capable(current_cred(), ns, cap, opts);
0377     if (capable == 0) {
0378         current->flags |= PF_SUPERPRIV;
0379         return true;
0380     }
0381     return false;
0382 }
0383 
0384 /**
0385  * ns_capable - Determine if the current task has a superior capability in effect
0386  * @ns:  The usernamespace we want the capability in
0387  * @cap: The capability to be tested for
0388  *
0389  * Return true if the current task has the given superior capability currently
0390  * available for use, false if not.
0391  *
0392  * This sets PF_SUPERPRIV on the task if the capability is available on the
0393  * assumption that it's about to be used.
0394  */
0395 bool ns_capable(struct user_namespace *ns, int cap)
0396 {
0397     return ns_capable_common(ns, cap, CAP_OPT_NONE);
0398 }
0399 EXPORT_SYMBOL(ns_capable);
0400 
0401 /**
0402  * ns_capable_noaudit - Determine if the current task has a superior capability
0403  * (unaudited) in effect
0404  * @ns:  The usernamespace we want the capability in
0405  * @cap: The capability to be tested for
0406  *
0407  * Return true if the current task has the given superior capability currently
0408  * available for use, false if not.
0409  *
0410  * This sets PF_SUPERPRIV on the task if the capability is available on the
0411  * assumption that it's about to be used.
0412  */
0413 bool ns_capable_noaudit(struct user_namespace *ns, int cap)
0414 {
0415     return ns_capable_common(ns, cap, CAP_OPT_NOAUDIT);
0416 }
0417 EXPORT_SYMBOL(ns_capable_noaudit);
0418 
0419 /**
0420  * ns_capable_setid - Determine if the current task has a superior capability
0421  * in effect, while signalling that this check is being done from within a
0422  * setid or setgroups syscall.
0423  * @ns:  The usernamespace we want the capability in
0424  * @cap: The capability to be tested for
0425  *
0426  * Return true if the current task has the given superior capability currently
0427  * available for use, false if not.
0428  *
0429  * This sets PF_SUPERPRIV on the task if the capability is available on the
0430  * assumption that it's about to be used.
0431  */
0432 bool ns_capable_setid(struct user_namespace *ns, int cap)
0433 {
0434     return ns_capable_common(ns, cap, CAP_OPT_INSETID);
0435 }
0436 EXPORT_SYMBOL(ns_capable_setid);
0437 
0438 /**
0439  * capable - Determine if the current task has a superior capability in effect
0440  * @cap: The capability to be tested for
0441  *
0442  * Return true if the current task has the given superior capability currently
0443  * available for use, false if not.
0444  *
0445  * This sets PF_SUPERPRIV on the task if the capability is available on the
0446  * assumption that it's about to be used.
0447  */
0448 bool capable(int cap)
0449 {
0450     return ns_capable(&init_user_ns, cap);
0451 }
0452 EXPORT_SYMBOL(capable);
0453 #endif /* CONFIG_MULTIUSER */
0454 
0455 /**
0456  * file_ns_capable - Determine if the file's opener had a capability in effect
0457  * @file:  The file we want to check
0458  * @ns:  The usernamespace we want the capability in
0459  * @cap: The capability to be tested for
0460  *
0461  * Return true if task that opened the file had a capability in effect
0462  * when the file was opened.
0463  *
0464  * This does not set PF_SUPERPRIV because the caller may not
0465  * actually be privileged.
0466  */
0467 bool file_ns_capable(const struct file *file, struct user_namespace *ns,
0468              int cap)
0469 {
0470 
0471     if (WARN_ON_ONCE(!cap_valid(cap)))
0472         return false;
0473 
0474     if (security_capable(file->f_cred, ns, cap, CAP_OPT_NONE) == 0)
0475         return true;
0476 
0477     return false;
0478 }
0479 EXPORT_SYMBOL(file_ns_capable);
0480 
0481 /**
0482  * privileged_wrt_inode_uidgid - Do capabilities in the namespace work over the inode?
0483  * @ns: The user namespace in question
0484  * @inode: The inode in question
0485  *
0486  * Return true if the inode uid and gid are within the namespace.
0487  */
0488 bool privileged_wrt_inode_uidgid(struct user_namespace *ns,
0489                  struct user_namespace *mnt_userns,
0490                  const struct inode *inode)
0491 {
0492     return kuid_has_mapping(ns, i_uid_into_mnt(mnt_userns, inode)) &&
0493            kgid_has_mapping(ns, i_gid_into_mnt(mnt_userns, inode));
0494 }
0495 
0496 /**
0497  * capable_wrt_inode_uidgid - Check nsown_capable and uid and gid mapped
0498  * @inode: The inode in question
0499  * @cap: The capability in question
0500  *
0501  * Return true if the current task has the given capability targeted at
0502  * its own user namespace and that the given inode's uid and gid are
0503  * mapped into the current user namespace.
0504  */
0505 bool capable_wrt_inode_uidgid(struct user_namespace *mnt_userns,
0506                   const struct inode *inode, int cap)
0507 {
0508     struct user_namespace *ns = current_user_ns();
0509 
0510     return ns_capable(ns, cap) &&
0511            privileged_wrt_inode_uidgid(ns, mnt_userns, inode);
0512 }
0513 EXPORT_SYMBOL(capable_wrt_inode_uidgid);
0514 
0515 /**
0516  * ptracer_capable - Determine if the ptracer holds CAP_SYS_PTRACE in the namespace
0517  * @tsk: The task that may be ptraced
0518  * @ns: The user namespace to search for CAP_SYS_PTRACE in
0519  *
0520  * Return true if the task that is ptracing the current task had CAP_SYS_PTRACE
0521  * in the specified user namespace.
0522  */
0523 bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns)
0524 {
0525     int ret = 0;  /* An absent tracer adds no restrictions */
0526     const struct cred *cred;
0527 
0528     rcu_read_lock();
0529     cred = rcu_dereference(tsk->ptracer_cred);
0530     if (cred)
0531         ret = security_capable(cred, ns, CAP_SYS_PTRACE,
0532                        CAP_OPT_NOAUDIT);
0533     rcu_read_unlock();
0534     return (ret == 0);
0535 }