linux/security/commoncap.c
<<
>>
Prefs
   1/* Common capabilities, needed by capability.o and root_plug.o
   2 *
   3 *      This program is free software; you can redistribute it and/or modify
   4 *      it under the terms of the GNU General Public License as published by
   5 *      the Free Software Foundation; either version 2 of the License, or
   6 *      (at your option) any later version.
   7 *
   8 */
   9
  10#include <linux/capability.h>
  11#include <linux/audit.h>
  12#include <linux/module.h>
  13#include <linux/init.h>
  14#include <linux/kernel.h>
  15#include <linux/security.h>
  16#include <linux/file.h>
  17#include <linux/mm.h>
  18#include <linux/mman.h>
  19#include <linux/pagemap.h>
  20#include <linux/swap.h>
  21#include <linux/skbuff.h>
  22#include <linux/netlink.h>
  23#include <linux/ptrace.h>
  24#include <linux/xattr.h>
  25#include <linux/hugetlb.h>
  26#include <linux/mount.h>
  27#include <linux/sched.h>
  28#include <linux/prctl.h>
  29#include <linux/securebits.h>
  30
  31int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
  32{
  33        NETLINK_CB(skb).eff_cap = current_cap();
  34        return 0;
  35}
  36
  37int cap_netlink_recv(struct sk_buff *skb, int cap)
  38{
  39        if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
  40                return -EPERM;
  41        return 0;
  42}
  43EXPORT_SYMBOL(cap_netlink_recv);
  44
  45/**
  46 * cap_capable - Determine whether a task has a particular effective capability
  47 * @tsk: The task to query
  48 * @cred: The credentials to use
  49 * @cap: The capability to check for
  50 * @audit: Whether to write an audit message or not
  51 *
  52 * Determine whether the nominated task has the specified capability amongst
  53 * its effective set, returning 0 if it does, -ve if it does not.
  54 *
  55 * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
  56 * and has_capability() functions.  That is, it has the reverse semantics:
  57 * cap_has_capability() returns 0 when a task has a capability, but the
  58 * kernel's capable() and has_capability() returns 1 for this case.
  59 */
  60int cap_capable(struct task_struct *tsk, const struct cred *cred, int cap,
  61                int audit)
  62{
  63        return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
  64}
  65
  66/**
  67 * cap_settime - Determine whether the current process may set the system clock
  68 * @ts: The time to set
  69 * @tz: The timezone to set
  70 *
  71 * Determine whether the current process may set the system clock and timezone
  72 * information, returning 0 if permission granted, -ve if denied.
  73 */
  74int cap_settime(struct timespec *ts, struct timezone *tz)
  75{
  76        if (!capable(CAP_SYS_TIME))
  77                return -EPERM;
  78        return 0;
  79}
  80
  81/**
  82 * cap_ptrace_may_access - Determine whether the current process may access
  83 *                         another
  84 * @child: The process to be accessed
  85 * @mode: The mode of attachment.
  86 *
  87 * Determine whether a process may access another, returning 0 if permission
  88 * granted, -ve if denied.
  89 */
  90int cap_ptrace_may_access(struct task_struct *child, unsigned int mode)
  91{
  92        int ret = 0;
  93
  94        rcu_read_lock();
  95        if (!cap_issubset(__task_cred(child)->cap_permitted,
  96                          current_cred()->cap_permitted) &&
  97            !capable(CAP_SYS_PTRACE))
  98                ret = -EPERM;
  99        rcu_read_unlock();
 100        return ret;
 101}
 102
 103/**
 104 * cap_ptrace_traceme - Determine whether another process may trace the current
 105 * @parent: The task proposed to be the tracer
 106 *
 107 * Determine whether the nominated task is permitted to trace the current
 108 * process, returning 0 if permission is granted, -ve if denied.
 109 */
 110int cap_ptrace_traceme(struct task_struct *parent)
 111{
 112        int ret = 0;
 113
 114        rcu_read_lock();
 115        if (!cap_issubset(current_cred()->cap_permitted,
 116                          __task_cred(parent)->cap_permitted) &&
 117            !has_capability(parent, CAP_SYS_PTRACE))
 118                ret = -EPERM;
 119        rcu_read_unlock();
 120        return ret;
 121}
 122
 123/**
 124 * cap_capget - Retrieve a task's capability sets
 125 * @target: The task from which to retrieve the capability sets
 126 * @effective: The place to record the effective set
 127 * @inheritable: The place to record the inheritable set
 128 * @permitted: The place to record the permitted set
 129 *
 130 * This function retrieves the capabilities of the nominated task and returns
 131 * them to the caller.
 132 */
 133int cap_capget(struct task_struct *target, kernel_cap_t *effective,
 134               kernel_cap_t *inheritable, kernel_cap_t *permitted)
 135{
 136        const struct cred *cred;
 137
 138        /* Derived from kernel/capability.c:sys_capget. */
 139        rcu_read_lock();
 140        cred = __task_cred(target);
 141        *effective   = cred->cap_effective;
 142        *inheritable = cred->cap_inheritable;
 143        *permitted   = cred->cap_permitted;
 144        rcu_read_unlock();
 145        return 0;
 146}
 147
 148/*
 149 * Determine whether the inheritable capabilities are limited to the old
 150 * permitted set.  Returns 1 if they are limited, 0 if they are not.
 151 */
 152static inline int cap_inh_is_capped(void)
 153{
 154#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
 155
 156        /* they are so limited unless the current task has the CAP_SETPCAP
 157         * capability
 158         */
 159        if (cap_capable(current, current_cred(), CAP_SETPCAP,
 160                        SECURITY_CAP_AUDIT) == 0)
 161                return 0;
 162#endif
 163        return 1;
 164}
 165
 166/**
 167 * cap_capset - Validate and apply proposed changes to current's capabilities
 168 * @new: The proposed new credentials; alterations should be made here
 169 * @old: The current task's current credentials
 170 * @effective: A pointer to the proposed new effective capabilities set
 171 * @inheritable: A pointer to the proposed new inheritable capabilities set
 172 * @permitted: A pointer to the proposed new permitted capabilities set
 173 *
 174 * This function validates and applies a proposed mass change to the current
 175 * process's capability sets.  The changes are made to the proposed new
 176 * credentials, and assuming no error, will be committed by the caller of LSM.
 177 */
 178int cap_capset(struct cred *new,
 179               const struct cred *old,
 180               const kernel_cap_t *effective,
 181               const kernel_cap_t *inheritable,
 182               const kernel_cap_t *permitted)
 183{
 184        if (cap_inh_is_capped() &&
 185            !cap_issubset(*inheritable,
 186                          cap_combine(old->cap_inheritable,
 187                                      old->cap_permitted)))
 188                /* incapable of using this inheritable set */
 189                return -EPERM;
 190
 191        if (!cap_issubset(*inheritable,
 192                          cap_combine(old->cap_inheritable,
 193                                      old->cap_bset)))
 194                /* no new pI capabilities outside bounding set */
 195                return -EPERM;
 196
 197        /* verify restrictions on target's new Permitted set */
 198        if (!cap_issubset(*permitted, old->cap_permitted))
 199                return -EPERM;
 200
 201        /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
 202        if (!cap_issubset(*effective, *permitted))
 203                return -EPERM;
 204
 205        new->cap_effective   = *effective;
 206        new->cap_inheritable = *inheritable;
 207        new->cap_permitted   = *permitted;
 208        return 0;
 209}
 210
 211/*
 212 * Clear proposed capability sets for execve().
 213 */
 214static inline void bprm_clear_caps(struct linux_binprm *bprm)
 215{
 216        cap_clear(bprm->cred->cap_permitted);
 217        bprm->cap_effective = false;
 218}
 219
 220#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
 221
 222/**
 223 * cap_inode_need_killpriv - Determine if inode change affects privileges
 224 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
 225 *
 226 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
 227 * affects the security markings on that inode, and if it is, should
 228 * inode_killpriv() be invoked or the change rejected?
 229 *
 230 * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
 231 * -ve to deny the change.
 232 */
 233int cap_inode_need_killpriv(struct dentry *dentry)
 234{
 235        struct inode *inode = dentry->d_inode;
 236        int error;
 237
 238        if (!inode->i_op->getxattr)
 239               return 0;
 240
 241        error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
 242        if (error <= 0)
 243                return 0;
 244        return 1;
 245}
 246
 247/**
 248 * cap_inode_killpriv - Erase the security markings on an inode
 249 * @dentry: The inode/dentry to alter
 250 *
 251 * Erase the privilege-enhancing security markings on an inode.
 252 *
 253 * Returns 0 if successful, -ve on error.
 254 */
 255int cap_inode_killpriv(struct dentry *dentry)
 256{
 257        struct inode *inode = dentry->d_inode;
 258
 259        if (!inode->i_op->removexattr)
 260               return 0;
 261
 262        return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
 263}
 264
 265/*
 266 * Calculate the new process capability sets from the capability sets attached
 267 * to a file.
 268 */
 269static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
 270                                          struct linux_binprm *bprm,
 271                                          bool *effective)
 272{
 273        struct cred *new = bprm->cred;
 274        unsigned i;
 275        int ret = 0;
 276
 277        if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
 278                *effective = true;
 279
 280        CAP_FOR_EACH_U32(i) {
 281                __u32 permitted = caps->permitted.cap[i];
 282                __u32 inheritable = caps->inheritable.cap[i];
 283
 284                /*
 285                 * pP' = (X & fP) | (pI & fI)
 286                 */
 287                new->cap_permitted.cap[i] =
 288                        (new->cap_bset.cap[i] & permitted) |
 289                        (new->cap_inheritable.cap[i] & inheritable);
 290
 291                if (permitted & ~new->cap_permitted.cap[i])
 292                        /* insufficient to execute correctly */
 293                        ret = -EPERM;
 294        }
 295
 296        /*
 297         * For legacy apps, with no internal support for recognizing they
 298         * do not have enough capabilities, we return an error if they are
 299         * missing some "forced" (aka file-permitted) capabilities.
 300         */
 301        return *effective ? ret : 0;
 302}
 303
 304/*
 305 * Extract the on-exec-apply capability sets for an executable file.
 306 */
 307int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
 308{
 309        struct inode *inode = dentry->d_inode;
 310        __u32 magic_etc;
 311        unsigned tocopy, i;
 312        int size;
 313        struct vfs_cap_data caps;
 314
 315        memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
 316
 317        if (!inode || !inode->i_op->getxattr)
 318                return -ENODATA;
 319
 320        size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
 321                                   XATTR_CAPS_SZ);
 322        if (size == -ENODATA || size == -EOPNOTSUPP)
 323                /* no data, that's ok */
 324                return -ENODATA;
 325        if (size < 0)
 326                return size;
 327
 328        if (size < sizeof(magic_etc))
 329                return -EINVAL;
 330
 331        cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
 332
 333        switch (magic_etc & VFS_CAP_REVISION_MASK) {
 334        case VFS_CAP_REVISION_1:
 335                if (size != XATTR_CAPS_SZ_1)
 336                        return -EINVAL;
 337                tocopy = VFS_CAP_U32_1;
 338                break;
 339        case VFS_CAP_REVISION_2:
 340                if (size != XATTR_CAPS_SZ_2)
 341                        return -EINVAL;
 342                tocopy = VFS_CAP_U32_2;
 343                break;
 344        default:
 345                return -EINVAL;
 346        }
 347
 348        CAP_FOR_EACH_U32(i) {
 349                if (i >= tocopy)
 350                        break;
 351                cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
 352                cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
 353        }
 354
 355        return 0;
 356}
 357
 358/*
 359 * Attempt to get the on-exec apply capability sets for an executable file from
 360 * its xattrs and, if present, apply them to the proposed credentials being
 361 * constructed by execve().
 362 */
 363static int get_file_caps(struct linux_binprm *bprm, bool *effective)
 364{
 365        struct dentry *dentry;
 366        int rc = 0;
 367        struct cpu_vfs_cap_data vcaps;
 368
 369        bprm_clear_caps(bprm);
 370
 371        if (!file_caps_enabled)
 372                return 0;
 373
 374        if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
 375                return 0;
 376
 377        dentry = dget(bprm->file->f_dentry);
 378
 379        rc = get_vfs_caps_from_disk(dentry, &vcaps);
 380        if (rc < 0) {
 381                if (rc == -EINVAL)
 382                        printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
 383                                __func__, rc, bprm->filename);
 384                else if (rc == -ENODATA)
 385                        rc = 0;
 386                goto out;
 387        }
 388
 389        rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);
 390        if (rc == -EINVAL)
 391                printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
 392                       __func__, rc, bprm->filename);
 393
 394out:
 395        dput(dentry);
 396        if (rc)
 397                bprm_clear_caps(bprm);
 398
 399        return rc;
 400}
 401
 402#else
 403int cap_inode_need_killpriv(struct dentry *dentry)
 404{
 405        return 0;
 406}
 407
 408int cap_inode_killpriv(struct dentry *dentry)
 409{
 410        return 0;
 411}
 412
 413int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
 414{
 415        memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
 416        return -ENODATA;
 417}
 418
 419static inline int get_file_caps(struct linux_binprm *bprm, bool *effective)
 420{
 421        bprm_clear_caps(bprm);
 422        return 0;
 423}
 424#endif
 425
 426/*
 427 * Determine whether a exec'ing process's new permitted capabilities should be
 428 * limited to just what it already has.
 429 *
 430 * This prevents processes that are being ptraced from gaining access to
 431 * CAP_SETPCAP, unless the process they're tracing already has it, and the
 432 * binary they're executing has filecaps that elevate it.
 433 *
 434 *  Returns 1 if they should be limited, 0 if they are not.
 435 */
 436static inline int cap_limit_ptraced_target(void)
 437{
 438#ifndef CONFIG_SECURITY_FILE_CAPABILITIES
 439        if (capable(CAP_SETPCAP))
 440                return 0;
 441#endif
 442        return 1;
 443}
 444
 445/**
 446 * cap_bprm_set_creds - Set up the proposed credentials for execve().
 447 * @bprm: The execution parameters, including the proposed creds
 448 *
 449 * Set up the proposed credentials for a new execution context being
 450 * constructed by execve().  The proposed creds in @bprm->cred is altered,
 451 * which won't take effect immediately.  Returns 0 if successful, -ve on error.
 452 */
 453int cap_bprm_set_creds(struct linux_binprm *bprm)
 454{
 455        const struct cred *old = current_cred();
 456        struct cred *new = bprm->cred;
 457        bool effective;
 458        int ret;
 459
 460        effective = false;
 461        ret = get_file_caps(bprm, &effective);
 462        if (ret < 0)
 463                return ret;
 464
 465        if (!issecure(SECURE_NOROOT)) {
 466                /*
 467                 * To support inheritance of root-permissions and suid-root
 468                 * executables under compatibility mode, we override the
 469                 * capability sets for the file.
 470                 *
 471                 * If only the real uid is 0, we do not set the effective bit.
 472                 */
 473                if (new->euid == 0 || new->uid == 0) {
 474                        /* pP' = (cap_bset & ~0) | (pI & ~0) */
 475                        new->cap_permitted = cap_combine(old->cap_bset,
 476                                                         old->cap_inheritable);
 477                }
 478                if (new->euid == 0)
 479                        effective = true;
 480        }
 481
 482        /* Don't let someone trace a set[ug]id/setpcap binary with the revised
 483         * credentials unless they have the appropriate permit
 484         */
 485        if ((new->euid != old->uid ||
 486             new->egid != old->gid ||
 487             !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
 488            bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
 489                /* downgrade; they get no more than they had, and maybe less */
 490                if (!capable(CAP_SETUID)) {
 491                        new->euid = new->uid;
 492                        new->egid = new->gid;
 493                }
 494                if (cap_limit_ptraced_target())
 495                        new->cap_permitted = cap_intersect(new->cap_permitted,
 496                                                           old->cap_permitted);
 497        }
 498
 499        new->suid = new->fsuid = new->euid;
 500        new->sgid = new->fsgid = new->egid;
 501
 502        /* For init, we want to retain the capabilities set in the initial
 503         * task.  Thus we skip the usual capability rules
 504         */
 505        if (!is_global_init(current)) {
 506                if (effective)
 507                        new->cap_effective = new->cap_permitted;
 508                else
 509                        cap_clear(new->cap_effective);
 510        }
 511        bprm->cap_effective = effective;
 512
 513        /*
 514         * Audit candidate if current->cap_effective is set
 515         *
 516         * We do not bother to audit if 3 things are true:
 517         *   1) cap_effective has all caps
 518         *   2) we are root
 519         *   3) root is supposed to have all caps (SECURE_NOROOT)
 520         * Since this is just a normal root execing a process.
 521         *
 522         * Number 1 above might fail if you don't have a full bset, but I think
 523         * that is interesting information to audit.
 524         */
 525        if (!cap_isclear(new->cap_effective)) {
 526                if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
 527                    new->euid != 0 || new->uid != 0 ||
 528                    issecure(SECURE_NOROOT)) {
 529                        ret = audit_log_bprm_fcaps(bprm, new, old);
 530                        if (ret < 0)
 531                                return ret;
 532                }
 533        }
 534
 535        new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
 536        return 0;
 537}
 538
 539/**
 540 * cap_bprm_secureexec - Determine whether a secure execution is required
 541 * @bprm: The execution parameters
 542 *
 543 * Determine whether a secure execution is required, return 1 if it is, and 0
 544 * if it is not.
 545 *
 546 * The credentials have been committed by this point, and so are no longer
 547 * available through @bprm->cred.
 548 */
 549int cap_bprm_secureexec(struct linux_binprm *bprm)
 550{
 551        const struct cred *cred = current_cred();
 552
 553        if (cred->uid != 0) {
 554                if (bprm->cap_effective)
 555                        return 1;
 556                if (!cap_isclear(cred->cap_permitted))
 557                        return 1;
 558        }
 559
 560        return (cred->euid != cred->uid ||
 561                cred->egid != cred->gid);
 562}
 563
 564/**
 565 * cap_inode_setxattr - Determine whether an xattr may be altered
 566 * @dentry: The inode/dentry being altered
 567 * @name: The name of the xattr to be changed
 568 * @value: The value that the xattr will be changed to
 569 * @size: The size of value
 570 * @flags: The replacement flag
 571 *
 572 * Determine whether an xattr may be altered or set on an inode, returning 0 if
 573 * permission is granted, -ve if denied.
 574 *
 575 * This is used to make sure security xattrs don't get updated or set by those
 576 * who aren't privileged to do so.
 577 */
 578int cap_inode_setxattr(struct dentry *dentry, const char *name,
 579                       const void *value, size_t size, int flags)
 580{
 581        if (!strcmp(name, XATTR_NAME_CAPS)) {
 582                if (!capable(CAP_SETFCAP))
 583                        return -EPERM;
 584                return 0;
 585        }
 586
 587        if (!strncmp(name, XATTR_SECURITY_PREFIX,
 588                     sizeof(XATTR_SECURITY_PREFIX) - 1)  &&
 589            !capable(CAP_SYS_ADMIN))
 590                return -EPERM;
 591        return 0;
 592}
 593
 594/**
 595 * cap_inode_removexattr - Determine whether an xattr may be removed
 596 * @dentry: The inode/dentry being altered
 597 * @name: The name of the xattr to be changed
 598 *
 599 * Determine whether an xattr may be removed from an inode, returning 0 if
 600 * permission is granted, -ve if denied.
 601 *
 602 * This is used to make sure security xattrs don't get removed by those who
 603 * aren't privileged to remove them.
 604 */
 605int cap_inode_removexattr(struct dentry *dentry, const char *name)
 606{
 607        if (!strcmp(name, XATTR_NAME_CAPS)) {
 608                if (!capable(CAP_SETFCAP))
 609                        return -EPERM;
 610                return 0;
 611        }
 612
 613        if (!strncmp(name, XATTR_SECURITY_PREFIX,
 614                     sizeof(XATTR_SECURITY_PREFIX) - 1)  &&
 615            !capable(CAP_SYS_ADMIN))
 616                return -EPERM;
 617        return 0;
 618}
 619
 620/*
 621 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
 622 * a process after a call to setuid, setreuid, or setresuid.
 623 *
 624 *  1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
 625 *  {r,e,s}uid != 0, the permitted and effective capabilities are
 626 *  cleared.
 627 *
 628 *  2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
 629 *  capabilities of the process are cleared.
 630 *
 631 *  3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
 632 *  capabilities are set to the permitted capabilities.
 633 *
 634 *  fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
 635 *  never happen.
 636 *
 637 *  -astor
 638 *
 639 * cevans - New behaviour, Oct '99
 640 * A process may, via prctl(), elect to keep its capabilities when it
 641 * calls setuid() and switches away from uid==0. Both permitted and
 642 * effective sets will be retained.
 643 * Without this change, it was impossible for a daemon to drop only some
 644 * of its privilege. The call to setuid(!=0) would drop all privileges!
 645 * Keeping uid 0 is not an option because uid 0 owns too many vital
 646 * files..
 647 * Thanks to Olaf Kirch and Peter Benie for spotting this.
 648 */
 649static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
 650{
 651        if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
 652            (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
 653            !issecure(SECURE_KEEP_CAPS)) {
 654                cap_clear(new->cap_permitted);
 655                cap_clear(new->cap_effective);
 656        }
 657        if (old->euid == 0 && new->euid != 0)
 658                cap_clear(new->cap_effective);
 659        if (old->euid != 0 && new->euid == 0)
 660                new->cap_effective = new->cap_permitted;
 661}
 662
 663/**
 664 * cap_task_fix_setuid - Fix up the results of setuid() call
 665 * @new: The proposed credentials
 666 * @old: The current task's current credentials
 667 * @flags: Indications of what has changed
 668 *
 669 * Fix up the results of setuid() call before the credential changes are
 670 * actually applied, returning 0 to grant the changes, -ve to deny them.
 671 */
 672int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
 673{
 674        switch (flags) {
 675        case LSM_SETID_RE:
 676        case LSM_SETID_ID:
 677        case LSM_SETID_RES:
 678                /* juggle the capabilities to follow [RES]UID changes unless
 679                 * otherwise suppressed */
 680                if (!issecure(SECURE_NO_SETUID_FIXUP))
 681                        cap_emulate_setxuid(new, old);
 682                break;
 683
 684        case LSM_SETID_FS:
 685                /* juggle the capabilties to follow FSUID changes, unless
 686                 * otherwise suppressed
 687                 *
 688                 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
 689                 *          if not, we might be a bit too harsh here.
 690                 */
 691                if (!issecure(SECURE_NO_SETUID_FIXUP)) {
 692                        if (old->fsuid == 0 && new->fsuid != 0)
 693                                new->cap_effective =
 694                                        cap_drop_fs_set(new->cap_effective);
 695
 696                        if (old->fsuid != 0 && new->fsuid == 0)
 697                                new->cap_effective =
 698                                        cap_raise_fs_set(new->cap_effective,
 699                                                         new->cap_permitted);
 700                }
 701                break;
 702
 703        default:
 704                return -EINVAL;
 705        }
 706
 707        return 0;
 708}
 709
 710#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
 711/*
 712 * Rationale: code calling task_setscheduler, task_setioprio, and
 713 * task_setnice, assumes that
 714 *   . if capable(cap_sys_nice), then those actions should be allowed
 715 *   . if not capable(cap_sys_nice), but acting on your own processes,
 716 *      then those actions should be allowed
 717 * This is insufficient now since you can call code without suid, but
 718 * yet with increased caps.
 719 * So we check for increased caps on the target process.
 720 */
 721static int cap_safe_nice(struct task_struct *p)
 722{
 723        int is_subset;
 724
 725        rcu_read_lock();
 726        is_subset = cap_issubset(__task_cred(p)->cap_permitted,
 727                                 current_cred()->cap_permitted);
 728        rcu_read_unlock();
 729
 730        if (!is_subset && !capable(CAP_SYS_NICE))
 731                return -EPERM;
 732        return 0;
 733}
 734
 735/**
 736 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
 737 * @p: The task to affect
 738 * @policy: The policy to effect
 739 * @lp: The parameters to the scheduling policy
 740 *
 741 * Detemine if the requested scheduler policy change is permitted for the
 742 * specified task, returning 0 if permission is granted, -ve if denied.
 743 */
 744int cap_task_setscheduler(struct task_struct *p, int policy,
 745                           struct sched_param *lp)
 746{
 747        return cap_safe_nice(p);
 748}
 749
 750/**
 751 * cap_task_ioprio - Detemine if I/O priority change is permitted
 752 * @p: The task to affect
 753 * @ioprio: The I/O priority to set
 754 *
 755 * Detemine if the requested I/O priority change is permitted for the specified
 756 * task, returning 0 if permission is granted, -ve if denied.
 757 */
 758int cap_task_setioprio(struct task_struct *p, int ioprio)
 759{
 760        return cap_safe_nice(p);
 761}
 762
 763/**
 764 * cap_task_ioprio - Detemine if task priority change is permitted
 765 * @p: The task to affect
 766 * @nice: The nice value to set
 767 *
 768 * Detemine if the requested task priority change is permitted for the
 769 * specified task, returning 0 if permission is granted, -ve if denied.
 770 */
 771int cap_task_setnice(struct task_struct *p, int nice)
 772{
 773        return cap_safe_nice(p);
 774}
 775
 776/*
 777 * Implement PR_CAPBSET_DROP.  Attempt to remove the specified capability from
 778 * the current task's bounding set.  Returns 0 on success, -ve on error.
 779 */
 780static long cap_prctl_drop(struct cred *new, unsigned long cap)
 781{
 782        if (!capable(CAP_SETPCAP))
 783                return -EPERM;
 784        if (!cap_valid(cap))
 785                return -EINVAL;
 786
 787        cap_lower(new->cap_bset, cap);
 788        return 0;
 789}
 790
 791#else
 792int cap_task_setscheduler (struct task_struct *p, int policy,
 793                           struct sched_param *lp)
 794{
 795        return 0;
 796}
 797int cap_task_setioprio (struct task_struct *p, int ioprio)
 798{
 799        return 0;
 800}
 801int cap_task_setnice (struct task_struct *p, int nice)
 802{
 803        return 0;
 804}
 805#endif
 806
 807/**
 808 * cap_task_prctl - Implement process control functions for this security module
 809 * @option: The process control function requested
 810 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
 811 *
 812 * Allow process control functions (sys_prctl()) to alter capabilities; may
 813 * also deny access to other functions not otherwise implemented here.
 814 *
 815 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
 816 * here, other -ve on error.  If -ENOSYS is returned, sys_prctl() and other LSM
 817 * modules will consider performing the function.
 818 */
 819int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
 820                   unsigned long arg4, unsigned long arg5)
 821{
 822        struct cred *new;
 823        long error = 0;
 824
 825        new = prepare_creds();
 826        if (!new)
 827                return -ENOMEM;
 828
 829        switch (option) {
 830        case PR_CAPBSET_READ:
 831                error = -EINVAL;
 832                if (!cap_valid(arg2))
 833                        goto error;
 834                error = !!cap_raised(new->cap_bset, arg2);
 835                goto no_change;
 836
 837#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
 838        case PR_CAPBSET_DROP:
 839                error = cap_prctl_drop(new, arg2);
 840                if (error < 0)
 841                        goto error;
 842                goto changed;
 843
 844        /*
 845         * The next four prctl's remain to assist with transitioning a
 846         * system from legacy UID=0 based privilege (when filesystem
 847         * capabilities are not in use) to a system using filesystem
 848         * capabilities only - as the POSIX.1e draft intended.
 849         *
 850         * Note:
 851         *
 852         *  PR_SET_SECUREBITS =
 853         *      issecure_mask(SECURE_KEEP_CAPS_LOCKED)
 854         *    | issecure_mask(SECURE_NOROOT)
 855         *    | issecure_mask(SECURE_NOROOT_LOCKED)
 856         *    | issecure_mask(SECURE_NO_SETUID_FIXUP)
 857         *    | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
 858         *
 859         * will ensure that the current process and all of its
 860         * children will be locked into a pure
 861         * capability-based-privilege environment.
 862         */
 863        case PR_SET_SECUREBITS:
 864                error = -EPERM;
 865                if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
 866                     & (new->securebits ^ arg2))                        /*[1]*/
 867                    || ((new->securebits & SECURE_ALL_LOCKS & ~arg2))   /*[2]*/
 868                    || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS))   /*[3]*/
 869                    || (cap_capable(current, current_cred(), CAP_SETPCAP,
 870                                    SECURITY_CAP_AUDIT) != 0)           /*[4]*/
 871                        /*
 872                         * [1] no changing of bits that are locked
 873                         * [2] no unlocking of locks
 874                         * [3] no setting of unsupported bits
 875                         * [4] doing anything requires privilege (go read about
 876                         *     the "sendmail capabilities bug")
 877                         */
 878                    )
 879                        /* cannot change a locked bit */
 880                        goto error;
 881                new->securebits = arg2;
 882                goto changed;
 883
 884        case PR_GET_SECUREBITS:
 885                error = new->securebits;
 886                goto no_change;
 887
 888#endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
 889
 890        case PR_GET_KEEPCAPS:
 891                if (issecure(SECURE_KEEP_CAPS))
 892                        error = 1;
 893                goto no_change;
 894
 895        case PR_SET_KEEPCAPS:
 896                error = -EINVAL;
 897                if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
 898                        goto error;
 899                error = -EPERM;
 900                if (issecure(SECURE_KEEP_CAPS_LOCKED))
 901                        goto error;
 902                if (arg2)
 903                        new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
 904                else
 905                        new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
 906                goto changed;
 907
 908        default:
 909                /* No functionality available - continue with default */
 910                error = -ENOSYS;
 911                goto error;
 912        }
 913
 914        /* Functionality provided */
 915changed:
 916        return commit_creds(new);
 917
 918no_change:
 919        error = 0;
 920error:
 921        abort_creds(new);
 922        return error;
 923}
 924
 925/**
 926 * cap_syslog - Determine whether syslog function is permitted
 927 * @type: Function requested
 928 *
 929 * Determine whether the current process is permitted to use a particular
 930 * syslog function, returning 0 if permission is granted, -ve if not.
 931 */
 932int cap_syslog(int type)
 933{
 934        if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
 935                return -EPERM;
 936        return 0;
 937}
 938
 939/**
 940 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
 941 * @mm: The VM space in which the new mapping is to be made
 942 * @pages: The size of the mapping
 943 *
 944 * Determine whether the allocation of a new virtual mapping by the current
 945 * task is permitted, returning 0 if permission is granted, -ve if not.
 946 */
 947int cap_vm_enough_memory(struct mm_struct *mm, long pages)
 948{
 949        int cap_sys_admin = 0;
 950
 951        if (cap_capable(current, current_cred(), CAP_SYS_ADMIN,
 952                        SECURITY_CAP_NOAUDIT) == 0)
 953                cap_sys_admin = 1;
 954        return __vm_enough_memory(mm, pages, cap_sys_admin);
 955}
 956
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.