04 Apr, 2014

1 commit

  • Pull security subsystem updates from James Morris:
    "Apart from reordering the SELinux mmap code to ensure DAC is called
    before MAC, these are minor maintenance updates"

    * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: (23 commits)
    selinux: correctly label /proc inodes in use before the policy is loaded
    selinux: put the mmap() DAC controls before the MAC controls
    selinux: fix the output of ./scripts/get_maintainer.pl for SELinux
    evm: enable key retention service automatically
    ima: skip memory allocation for empty files
    evm: EVM does not use MD5
    ima: return d_name.name if d_path fails
    integrity: fix checkpatch errors
    ima: fix erroneous removal of security.ima xattr
    security: integrity: Use a more current logging style
    MAINTAINERS: email updates and other misc. changes
    ima: reduce memory usage when a template containing the n field is used
    ima: restore the original behavior for sending data with ima template
    Integrity: Pass commname via get_task_comm()
    fs: move i_readcount
    ima: use static const char array definitions
    security: have cap_dentry_init_security return error
    ima: new helper: file_inode(file)
    kernel: Mark function as static in kernel/seccomp.c
    capability: Use current logging styles
    ...

    Linus Torvalds
     

24 Feb, 2014

1 commit


14 Jan, 2014

1 commit

  • - Always report the current process as capset now always only works on
    the current process. This prevents reporting 0 or a random pid in
    a random pid namespace.

    - Don't bother to pass the pid as is available.

    Signed-off-by: "Eric W. Biederman"
    (cherry picked from commit bcc85f0af31af123e32858069eb2ad8f39f90e67)
    (cherry picked from commit f911cac4556a7a23e0b3ea850233d13b32328692)

    Signed-off-by: Richard Guy Briggs
    [eparis: fix build error when audit disabled]
    Signed-off-by: Eric Paris

    Eric W. Biederman
     

10 Sep, 2013

1 commit

  • Pull xfs updates from Ben Myers:
    "For 3.12-rc1 there are a number of bugfixes in addition to work to
    ease usage of shared code between libxfs and the kernel, the rest of
    the work to enable project and group quotas to be used simultaneously,
    performance optimisations in the log and the CIL, directory entry file
    type support, fixes for log space reservations, some spelling/grammar
    cleanups, and the addition of user namespace support.

    - introduce readahead to log recovery
    - add directory entry file type support
    - fix a number of spelling errors in comments
    - introduce new Q_XGETQSTATV quotactl for project quotas
    - add USER_NS support
    - log space reservation rework
    - CIL optimisations
    - kernel/userspace libxfs rework"

    * tag 'xfs-for-linus-v3.12-rc1' of git://oss.sgi.com/xfs/xfs: (112 commits)
    xfs: XFS_MOUNT_QUOTA_ALL needed by userspace
    xfs: dtype changed xfs_dir2_sfe_put_ino to xfs_dir3_sfe_put_ino
    Fix wrong flag ASSERT in xfs_attr_shortform_getvalue
    xfs: finish removing IOP_* macros.
    xfs: inode log reservations are too small
    xfs: check correct status variable for xfs_inobt_get_rec() call
    xfs: inode buffers may not be valid during recovery readahead
    xfs: check LSN ordering for v5 superblocks during recovery
    xfs: btree block LSN escaping to disk uninitialised
    XFS: Assertion failed: first < BBTOB(bp->b_length), file: fs/xfs/xfs_trans_buf.c, line: 568
    xfs: fix bad dquot buffer size in log recovery readahead
    xfs: don't account buffer cancellation during log recovery readahead
    xfs: check for underflow in xfs_iformat_fork()
    xfs: xfs_dir3_sfe_put_ino can be static
    xfs: introduce object readahead to log recovery
    xfs: Simplify xfs_ail_min() with list_first_entry_or_null()
    xfs: Register hotcpu notifier after initialization
    xfs: add xfs sb v4 support for dirent filetype field
    xfs: Add write support for dirent filetype field
    xfs: Add read-only support for dirent filetype field
    ...

    Linus Torvalds
     

31 Aug, 2013

1 commit


16 Aug, 2013

1 commit

  • Use inode_capable() to check if SUID|SGID bits should be cleared to match
    similar check in inode_change_ok().

    The check for CAP_LINUX_IMMUTABLE was not modified since all other file
    systems also check against init_user_ns rather than current_user_ns.

    Only allow changing of projid from init_user_ns.

    Reviewed-by: Dave Chinner
    Reviewed-by: Gao feng
    Signed-off-by: Dwight Engen
    Signed-off-by: Ben Myers

    Dwight Engen
     

15 Apr, 2013

1 commit


16 May, 2012

1 commit


08 Apr, 2012

1 commit

  • This represents a change in strategy of how to handle user namespaces.
    Instead of tagging everything explicitly with a user namespace and bulking
    up all of the comparisons of uids and gids in the kernel, all uids and gids
    in use will have a mapping to a flat kuid and kgid spaces respectively. This
    allows much more of the existing logic to be preserved and in general
    allows for faster code.

    In this new and improved world we allow someone to utiliize capabilities
    over an inode if the inodes owner mapps into the capabilities holders user
    namespace and the user has capabilities in their user namespace. Which
    is simple and efficient.

    Moving the fs uid comparisons to be comparisons in a flat kuid space
    follows in later patches, something that is only significant if you
    are using user namespaces.

    Acked-by: Serge Hallyn
    Signed-off-by: Eric W. Biederman

    Eric W. Biederman
     

18 Jan, 2012

1 commit

  • This reverts commit d2a7009f0bb03fa22ad08dd25472efa0568126b9.

    J. R. Okajima explains:

    "After this commit, I am afraid access(2) on NFS may not work
    correctly. The scenario based upon my guess.
    - access(2) overrides the credentials.
    - calls inode_permission() -- ... -- generic_permission() --
    ns_capable().
    - while the old ns_capable() calls security_capable(current_cred()),
    the new ns_capable() calls has_ns_capability(current) --
    security_capable(__task_cred(t)).

    current_cred() returns current->cred which is effective (overridden)
    credentials, but __task_cred(current) returns current->real_cred (the
    NFSD's credential). And the overridden credentials by access(2) lost."

    Requested-by: J. R. Okajima
    Acked-by: Eric Paris
    Signed-off-by: Linus Torvalds

    Linus Torvalds
     

15 Jan, 2012

1 commit

  • * 'for-linus' of git://selinuxproject.org/~jmorris/linux-security:
    capabilities: remove __cap_full_set definition
    security: remove the security_netlink_recv hook as it is equivalent to capable()
    ptrace: do not audit capability check when outputing /proc/pid/stat
    capabilities: remove task_ns_* functions
    capabitlies: ns_capable can use the cap helpers rather than lsm call
    capabilities: style only - move capable below ns_capable
    capabilites: introduce new has_ns_capabilities_noaudit
    capabilities: call has_ns_capability from has_capability
    capabilities: remove all _real_ interfaces
    capabilities: introduce security_capable_noaudit
    capabilities: reverse arguments to security_capable
    capabilities: remove the task from capable LSM hook entirely
    selinux: sparse fix: fix several warnings in the security server cod
    selinux: sparse fix: fix warnings in netlink code
    selinux: sparse fix: eliminate warnings for selinuxfs
    selinux: sparse fix: declare selinux_disable() in security.h
    selinux: sparse fix: move selinux_complete_init
    selinux: sparse fix: make selinux_secmark_refcount static
    SELinux: Fix RCU deref check warning in sel_netport_insert()

    Manually fix up a semantic mis-merge wrt security_netlink_recv():

    - the interface was removed in commit fd7784615248 ("security: remove
    the security_netlink_recv hook as it is equivalent to capable()")

    - a new user of it appeared in commit a38f7907b926 ("crypto: Add
    userspace configuration API")

    causing no automatic merge conflict, but Eric Paris pointed out the
    issue.

    Linus Torvalds
     

06 Jan, 2012

7 commits

  • task_ in the front of a function, in the security subsystem anyway, means
    to me at least, that we are operating with that task as the subject of the
    security decision. In this case what it means is that we are using current as
    the subject but we use the task to get the right namespace. Who in the world
    would ever realize that's what task_ns_capability means just by the name? This
    patch eliminates the task_ns functions entirely and uses the has_ns_capability
    function instead. This means we explicitly open code the ns in question in
    the caller. I think it makes the caller a LOT more clear what is going on.

    Signed-off-by: Eric Paris
    Acked-by: Serge E. Hallyn

    Eric Paris
     
  • Just to reduce the number of places to change if we every change the LSM
    hook, use the capability helpers internally when possible.

    Signed-off-by: Eric Paris
    Acked-by: Serge E. Hallyn

    Eric Paris
     
  • Although the current code is fine for consistency this moves the capable
    code below the function it calls in the c file. It doesn't actually change
    code.

    Signed-off-by: Eric Paris
    Acked-by: Serge E. Hallyn

    Eric Paris
     
  • For consistency in interfaces, introduce a new interface called
    has_ns_capabilities_noaudit. It checks if the given task has the given
    capability in the given namespace. Use this new function by
    has_capabilities_noaudit.

    Signed-off-by: Eric Paris
    Acked-by: Serge E. Hallyn

    Eric Paris
     
  • Declare the more specific has_ns_capability first in the code and then call it
    from has_capability. The declaration reversal isn't stricty necessary since
    they are both declared in header files, but it just makes sense to put more
    specific functions first in the code.

    Signed-off-by: Eric Paris
    Acked-by: Serge E. Hallyn

    Eric Paris
     
  • The name security_real_capable and security_real_capable_noaudit just don't
    make much sense to me. Convert them to use security_capable and
    security_capable_noaudit.

    Signed-off-by: Eric Paris
    Acked-by: Serge E. Hallyn

    Eric Paris
     
  • security_capable takes ns, cred, cap. But the LSM capable() hook takes
    cred, ns, cap. The capability helper functions also take cred, ns, cap.
    Rather than flip argument order just to flip it back, leave them alone.
    Heck, this should be a little faster since argument will be in the right
    place!

    Signed-off-by: Eric Paris

    Eric Paris
     

31 Oct, 2011

1 commit

  • The changed files were only including linux/module.h for the
    EXPORT_SYMBOL infrastructure, and nothing else. Revector them
    onto the isolated export header for faster compile times.

    Nothing to see here but a whole lot of instances of:

    -#include
    +#include

    This commit is only changing the kernel dir; next targets
    will probably be mm, fs, the arch dirs, etc.

    Signed-off-by: Paul Gortmaker

    Paul Gortmaker
     

19 May, 2011

1 commit


14 May, 2011

1 commit

  • If !CONFIG_USERNS, have current_user_ns() defined to (&init_user_ns).

    Get rid of _current_user_ns. This requires nsown_capable() to be
    defined in capability.c rather than as static inline in capability.h,
    so do that.

    Request_key needs init_user_ns defined at current_user_ns if
    !CONFIG_USERNS, so forward-declare that in cred.h if !CONFIG_USERNS
    at current_user_ns() define.

    Compile-tested with and without CONFIG_USERNS.

    Signed-off-by: Serge E. Hallyn
    [ This makes a huge performance difference for acl_permission_check(),
    up to 30%. And that is one of the hottest kernel functions for loads
    that are pathname-lookup heavy. ]
    Signed-off-by: Linus Torvalds

    Serge E. Hallyn
     

04 Apr, 2011

2 commits


24 Mar, 2011

2 commits

  • So we can let type safety keep things sane, and as a bonus we can remove
    the declaration of init_user_ns in capability.h.

    Signed-off-by: Serge E. Hallyn
    Cc: "Eric W. Biederman"
    Cc: Daniel Lezcano
    Cc: David Howells
    Cc: James Morris
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Serge E. Hallyn
     
  • - Introduce ns_capable to test for a capability in a non-default
    user namespace.
    - Teach cap_capable to handle capabilities in a non-default
    user namespace.

    The motivation is to get to the unprivileged creation of new
    namespaces. It looks like this gets us 90% of the way there, with
    only potential uid confusion issues left.

    I still need to handle getting all caps after creation but otherwise I
    think I have a good starter patch that achieves all of your goals.

    Changelog:
    11/05/2010: [serge] add apparmor
    12/14/2010: [serge] fix capabilities to created user namespaces
    Without this, if user serge creates a user_ns, he won't have
    capabilities to the user_ns he created. THis is because we
    were first checking whether his effective caps had the caps
    he needed and returning -EPERM if not, and THEN checking whether
    he was the creator. Reverse those checks.
    12/16/2010: [serge] security_real_capable needs ns argument in !security case
    01/11/2011: [serge] add task_ns_capable helper
    01/11/2011: [serge] add nsown_capable() helper per Bastian Blank suggestion
    02/16/2011: [serge] fix a logic bug: the root user is always creator of
    init_user_ns, but should not always have capabilities to
    it! Fix the check in cap_capable().
    02/21/2011: Add the required user_ns parameter to security_capable,
    fixing a compile failure.
    02/23/2011: Convert some macros to functions as per akpm comments. Some
    couldn't be converted because we can't easily forward-declare
    them (they are inline if !SECURITY, extern if SECURITY). Add
    a current_user_ns function so we can use it in capability.h
    without #including cred.h. Move all forward declarations
    together to the top of the #ifdef __KERNEL__ section, and use
    kernel-doc format.
    02/23/2011: Per dhowells, clean up comment in cap_capable().
    02/23/2011: Per akpm, remove unreachable 'return -EPERM' in cap_capable.

    (Original written and signed off by Eric; latest, modified version
    acked by him)

    [akpm@linux-foundation.org: fix build]
    [akpm@linux-foundation.org: export current_user_ns() for ecryptfs]
    [serge.hallyn@canonical.com: remove unneeded extra argument in selinux's task_has_capability]
    Signed-off-by: Eric W. Biederman
    Signed-off-by: Serge E. Hallyn
    Acked-by: "Eric W. Biederman"
    Acked-by: Daniel Lezcano
    Acked-by: David Howells
    Cc: James Morris
    Signed-off-by: Serge E. Hallyn
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Serge E. Hallyn
     

11 Feb, 2011

1 commit


03 Apr, 2010

1 commit


10 Dec, 2009

1 commit

  • cap_get_target_pid() protects the task lookup with tasklist_lock.
    security_capget() is called under tasklist_lock as well but
    tasklist_lock does not protect anything there. The capabilities are
    protected by RCU already.

    So tasklist_lock only protects the lookup and prevents the task going
    away, which can be done with rcu_read_lock() as well.

    Signed-off-by: Thomas Gleixner
    Signed-off-by: James Morris

    Thomas Gleixner
     

24 Nov, 2009

2 commits

  • As far as I know, all distros currently ship kernels with default
    CONFIG_SECURITY_FILE_CAPABILITIES=y. Since having the option on
    leaves a 'no_file_caps' option to boot without file capabilities,
    the main reason to keep the option is that turning it off saves
    you (on my s390x partition) 5k. In particular, vmlinux sizes
    came to:

    without patch fscaps=n: 53598392
    without patch fscaps=y: 53603406
    with this patch applied: 53603342

    with the security-next tree.

    Against this we must weigh the fact that there is no simple way for
    userspace to figure out whether file capabilities are supported,
    while things like per-process securebits, capability bounding
    sets, and adding bits to pI if CAP_SETPCAP is in pE are not supported
    with SECURITY_FILE_CAPABILITIES=n, leaving a bit of a problem for
    applications wanting to know whether they can use them and/or why
    something failed.

    It also adds another subtly different set of semantics which we must
    maintain at the risk of severe security regressions.

    So this patch removes the SECURITY_FILE_CAPABILITIES compile
    option. It drops the kernel size by about 50k over the stock
    SECURITY_FILE_CAPABILITIES=y kernel, by removing the
    cap_limit_ptraced_target() function.

    Changelog:
    Nov 20: remove cap_limit_ptraced_target() as it's logic
    was ifndef'ed.

    Signed-off-by: Serge E. Hallyn
    Acked-by: Andrew G. Morgan"
    Signed-off-by: James Morris

    Serge E. Hallyn
     
  • When libcap, or other libraries attempt to confirm/determine the supported
    capability version magic, they generally supply a NULL dataptr to capget().

    In this case, while returning the supported/preferred magic (via a
    modified header content), the return code of this system call may be 0,
    -EINVAL, or -EFAULT.

    No libcap code depends on the previous -EINVAL etc. return code, and
    all of the above three return codes can accompany a valid (successful)
    attempt to determine the requested magic value.

    This patch cleans up the system call to return 0, if the call is
    successfully being used to determine the supported/preferred capability
    magic value.

    Signed-off-by: Andrew G. Morgan
    Acked-by: Steve Grubb
    Acked-by: Serge Hallyn
    Signed-off-by: James Morris

    Andrew G. Morgan
     

14 Oct, 2009

1 commit

  • The capabilities syscall has a copy_from_user() call where gcc currently
    cannot prove to itself that the copy is always within bounds.

    This patch adds a very explicity bound check to prove to gcc that this
    copy_from_user cannot overflow its destination buffer.

    Signed-off-by: Arjan van de Ven
    Acked-by: James Morris
    Signed-off-by: Andrew Morton
    Signed-off-by: James Morris

    Arjan van de Ven
     

14 Jan, 2009

1 commit


07 Jan, 2009

3 commits

  • James Morris
     
  • Fix a regression in cap_capable() due to:

    commit 3b11a1decef07c19443d24ae926982bc8ec9f4c0
    Author: David Howells
    Date: Fri Nov 14 10:39:26 2008 +1100

    CRED: Differentiate objective and effective subjective credentials on a task

    The problem is that the above patch allows a process to have two sets of
    credentials, and for the most part uses the subjective credentials when
    accessing current's creds.

    There is, however, one exception: cap_capable(), and thus capable(), uses the
    real/objective credentials of the target task, whether or not it is the current
    task.

    Ordinarily this doesn't matter, since usually the two cred pointers in current
    point to the same set of creds. However, sys_faccessat() makes use of this
    facility to override the credentials of the calling process to make its test,
    without affecting the creds as seen from other processes.

    One of the things sys_faccessat() does is to make an adjustment to the
    effective capabilities mask, which cap_capable(), as it stands, then ignores.

    The affected capability check is in generic_permission():

    if (!(mask & MAY_EXEC) || execute_ok(inode))
    if (capable(CAP_DAC_OVERRIDE))
    return 0;

    This change passes the set of credentials to be tested down into the commoncap
    and SELinux code. The security functions called by capable() and
    has_capability() select the appropriate set of credentials from the process
    being checked.

    This can be tested by compiling the following program from the XFS testsuite:

    /*
    * t_access_root.c - trivial test program to show permission bug.
    *
    * Written by Michael Kerrisk - copyright ownership not pursued.
    * Sourced from: http://linux.derkeiler.com/Mailing-Lists/Kernel/2003-10/6030.html
    */
    #include
    #include
    #include
    #include
    #include
    #include

    #define UID 500
    #define GID 100
    #define PERM 0
    #define TESTPATH "/tmp/t_access"

    static void
    errExit(char *msg)
    {
    perror(msg);
    exit(EXIT_FAILURE);
    } /* errExit */

    static void
    accessTest(char *file, int mask, char *mstr)
    {
    printf("access(%s, %s) returns %d\n", file, mstr, access(file, mask));
    } /* accessTest */

    int
    main(int argc, char *argv[])
    {
    int fd, perm, uid, gid;
    char *testpath;
    char cmd[PATH_MAX + 20];

    testpath = (argc > 1) ? argv[1] : TESTPATH;
    perm = (argc > 2) ? strtoul(argv[2], NULL, 8) : PERM;
    uid = (argc > 3) ? atoi(argv[3]) : UID;
    gid = (argc > 4) ? atoi(argv[4]) : GID;

    unlink(testpath);

    fd = open(testpath, O_RDWR | O_CREAT, 0);
    if (fd == -1) errExit("open");

    if (fchown(fd, uid, gid) == -1) errExit("fchown");
    if (fchmod(fd, perm) == -1) errExit("fchmod");
    close(fd);

    snprintf(cmd, sizeof(cmd), "ls -l %s", testpath);
    system(cmd);

    if (seteuid(uid) == -1) errExit("seteuid");

    accessTest(testpath, 0, "0");
    accessTest(testpath, R_OK, "R_OK");
    accessTest(testpath, W_OK, "W_OK");
    accessTest(testpath, X_OK, "X_OK");
    accessTest(testpath, R_OK | W_OK, "R_OK | W_OK");
    accessTest(testpath, R_OK | X_OK, "R_OK | X_OK");
    accessTest(testpath, W_OK | X_OK, "W_OK | X_OK");
    accessTest(testpath, R_OK | W_OK | X_OK, "R_OK | W_OK | X_OK");

    exit(EXIT_SUCCESS);
    } /* main */

    This can be run against an Ext3 filesystem as well as against an XFS
    filesystem. If successful, it will show:

    [root@andromeda src]# ./t_access_root /tmp/xxx 0 4043 4043
    ---------- 1 dhowells dhowells 0 2008-12-31 03:00 /tmp/xxx
    access(/tmp/xxx, 0) returns 0
    access(/tmp/xxx, R_OK) returns 0
    access(/tmp/xxx, W_OK) returns 0
    access(/tmp/xxx, X_OK) returns -1
    access(/tmp/xxx, R_OK | W_OK) returns 0
    access(/tmp/xxx, R_OK | X_OK) returns -1
    access(/tmp/xxx, W_OK | X_OK) returns -1
    access(/tmp/xxx, R_OK | W_OK | X_OK) returns -1

    If unsuccessful, it will show:

    [root@andromeda src]# ./t_access_root /tmp/xxx 0 4043 4043
    ---------- 1 dhowells dhowells 0 2008-12-31 02:56 /tmp/xxx
    access(/tmp/xxx, 0) returns 0
    access(/tmp/xxx, R_OK) returns -1
    access(/tmp/xxx, W_OK) returns -1
    access(/tmp/xxx, X_OK) returns -1
    access(/tmp/xxx, R_OK | W_OK) returns -1
    access(/tmp/xxx, R_OK | X_OK) returns -1
    access(/tmp/xxx, W_OK | X_OK) returns -1
    access(/tmp/xxx, R_OK | W_OK | X_OK) returns -1

    I've also tested the fix with the SELinux and syscalls LTP testsuites.

    Signed-off-by: David Howells
    Tested-by: J. Bruce Fields
    Acked-by: Serge Hallyn
    Signed-off-by: James Morris

    David Howells
     
  • This reverts commit 14eaddc967b16017d4a1a24d2be6c28ecbe06ed8.

    David has a better version to come.

    James Morris
     

05 Jan, 2009

2 commits

  • Fix a regression in cap_capable() due to:

    commit 5ff7711e635b32f0a1e558227d030c7e45b4a465
    Author: David Howells
    Date: Wed Dec 31 02:52:28 2008 +0000

    CRED: Differentiate objective and effective subjective credentials on a task

    The problem is that the above patch allows a process to have two sets of
    credentials, and for the most part uses the subjective credentials when
    accessing current's creds.

    There is, however, one exception: cap_capable(), and thus capable(), uses the
    real/objective credentials of the target task, whether or not it is the current
    task.

    Ordinarily this doesn't matter, since usually the two cred pointers in current
    point to the same set of creds. However, sys_faccessat() makes use of this
    facility to override the credentials of the calling process to make its test,
    without affecting the creds as seen from other processes.

    One of the things sys_faccessat() does is to make an adjustment to the
    effective capabilities mask, which cap_capable(), as it stands, then ignores.

    The affected capability check is in generic_permission():

    if (!(mask & MAY_EXEC) || execute_ok(inode))
    if (capable(CAP_DAC_OVERRIDE))
    return 0;

    This change splits capable() from has_capability() down into the commoncap and
    SELinux code. The capable() security op now only deals with the current
    process, and uses the current process's subjective creds. A new security op -
    task_capable() - is introduced that can check any task's objective creds.

    strictly the capable() security op is superfluous with the presence of the
    task_capable() op, however it should be faster to call the capable() op since
    two fewer arguments need be passed down through the various layers.

    This can be tested by compiling the following program from the XFS testsuite:

    /*
    * t_access_root.c - trivial test program to show permission bug.
    *
    * Written by Michael Kerrisk - copyright ownership not pursued.
    * Sourced from: http://linux.derkeiler.com/Mailing-Lists/Kernel/2003-10/6030.html
    */
    #include
    #include
    #include
    #include
    #include
    #include

    #define UID 500
    #define GID 100
    #define PERM 0
    #define TESTPATH "/tmp/t_access"

    static void
    errExit(char *msg)
    {
    perror(msg);
    exit(EXIT_FAILURE);
    } /* errExit */

    static void
    accessTest(char *file, int mask, char *mstr)
    {
    printf("access(%s, %s) returns %d\n", file, mstr, access(file, mask));
    } /* accessTest */

    int
    main(int argc, char *argv[])
    {
    int fd, perm, uid, gid;
    char *testpath;
    char cmd[PATH_MAX + 20];

    testpath = (argc > 1) ? argv[1] : TESTPATH;
    perm = (argc > 2) ? strtoul(argv[2], NULL, 8) : PERM;
    uid = (argc > 3) ? atoi(argv[3]) : UID;
    gid = (argc > 4) ? atoi(argv[4]) : GID;

    unlink(testpath);

    fd = open(testpath, O_RDWR | O_CREAT, 0);
    if (fd == -1) errExit("open");

    if (fchown(fd, uid, gid) == -1) errExit("fchown");
    if (fchmod(fd, perm) == -1) errExit("fchmod");
    close(fd);

    snprintf(cmd, sizeof(cmd), "ls -l %s", testpath);
    system(cmd);

    if (seteuid(uid) == -1) errExit("seteuid");

    accessTest(testpath, 0, "0");
    accessTest(testpath, R_OK, "R_OK");
    accessTest(testpath, W_OK, "W_OK");
    accessTest(testpath, X_OK, "X_OK");
    accessTest(testpath, R_OK | W_OK, "R_OK | W_OK");
    accessTest(testpath, R_OK | X_OK, "R_OK | X_OK");
    accessTest(testpath, W_OK | X_OK, "W_OK | X_OK");
    accessTest(testpath, R_OK | W_OK | X_OK, "R_OK | W_OK | X_OK");

    exit(EXIT_SUCCESS);
    } /* main */

    This can be run against an Ext3 filesystem as well as against an XFS
    filesystem. If successful, it will show:

    [root@andromeda src]# ./t_access_root /tmp/xxx 0 4043 4043
    ---------- 1 dhowells dhowells 0 2008-12-31 03:00 /tmp/xxx
    access(/tmp/xxx, 0) returns 0
    access(/tmp/xxx, R_OK) returns 0
    access(/tmp/xxx, W_OK) returns 0
    access(/tmp/xxx, X_OK) returns -1
    access(/tmp/xxx, R_OK | W_OK) returns 0
    access(/tmp/xxx, R_OK | X_OK) returns -1
    access(/tmp/xxx, W_OK | X_OK) returns -1
    access(/tmp/xxx, R_OK | W_OK | X_OK) returns -1

    If unsuccessful, it will show:

    [root@andromeda src]# ./t_access_root /tmp/xxx 0 4043 4043
    ---------- 1 dhowells dhowells 0 2008-12-31 02:56 /tmp/xxx
    access(/tmp/xxx, 0) returns 0
    access(/tmp/xxx, R_OK) returns -1
    access(/tmp/xxx, W_OK) returns -1
    access(/tmp/xxx, X_OK) returns -1
    access(/tmp/xxx, R_OK | W_OK) returns -1
    access(/tmp/xxx, R_OK | X_OK) returns -1
    access(/tmp/xxx, W_OK | X_OK) returns -1
    access(/tmp/xxx, R_OK | W_OK | X_OK) returns -1

    I've also tested the fix with the SELinux and syscalls LTP testsuites.

    Signed-off-by: David Howells
    Signed-off-by: James Morris

    David Howells
     
  • * no allocations
    * return void
    * don't duplicate checked for dummy context

    Signed-off-by: Al Viro

    Al Viro
     

14 Nov, 2008

3 commits

  • Inaugurate copy-on-write credentials management. This uses RCU to manage the
    credentials pointer in the task_struct with respect to accesses by other tasks.
    A process may only modify its own credentials, and so does not need locking to
    access or modify its own credentials.

    A mutex (cred_replace_mutex) is added to the task_struct to control the effect
    of PTRACE_ATTACHED on credential calculations, particularly with respect to
    execve().

    With this patch, the contents of an active credentials struct may not be
    changed directly; rather a new set of credentials must be prepared, modified
    and committed using something like the following sequence of events:

    struct cred *new = prepare_creds();
    int ret = blah(new);
    if (ret < 0) {
    abort_creds(new);
    return ret;
    }
    return commit_creds(new);

    There are some exceptions to this rule: the keyrings pointed to by the active
    credentials may be instantiated - keyrings violate the COW rule as managing
    COW keyrings is tricky, given that it is possible for a task to directly alter
    the keys in a keyring in use by another task.

    To help enforce this, various pointers to sets of credentials, such as those in
    the task_struct, are declared const. The purpose of this is compile-time
    discouragement of altering credentials through those pointers. Once a set of
    credentials has been made public through one of these pointers, it may not be
    modified, except under special circumstances:

    (1) Its reference count may incremented and decremented.

    (2) The keyrings to which it points may be modified, but not replaced.

    The only safe way to modify anything else is to create a replacement and commit
    using the functions described in Documentation/credentials.txt (which will be
    added by a later patch).

    This patch and the preceding patches have been tested with the LTP SELinux
    testsuite.

    This patch makes several logical sets of alteration:

    (1) execve().

    This now prepares and commits credentials in various places in the
    security code rather than altering the current creds directly.

    (2) Temporary credential overrides.

    do_coredump() and sys_faccessat() now prepare their own credentials and
    temporarily override the ones currently on the acting thread, whilst
    preventing interference from other threads by holding cred_replace_mutex
    on the thread being dumped.

    This will be replaced in a future patch by something that hands down the
    credentials directly to the functions being called, rather than altering
    the task's objective credentials.

    (3) LSM interface.

    A number of functions have been changed, added or removed:

    (*) security_capset_check(), ->capset_check()
    (*) security_capset_set(), ->capset_set()

    Removed in favour of security_capset().

    (*) security_capset(), ->capset()

    New. This is passed a pointer to the new creds, a pointer to the old
    creds and the proposed capability sets. It should fill in the new
    creds or return an error. All pointers, barring the pointer to the
    new creds, are now const.

    (*) security_bprm_apply_creds(), ->bprm_apply_creds()

    Changed; now returns a value, which will cause the process to be
    killed if it's an error.

    (*) security_task_alloc(), ->task_alloc_security()

    Removed in favour of security_prepare_creds().

    (*) security_cred_free(), ->cred_free()

    New. Free security data attached to cred->security.

    (*) security_prepare_creds(), ->cred_prepare()

    New. Duplicate any security data attached to cred->security.

    (*) security_commit_creds(), ->cred_commit()

    New. Apply any security effects for the upcoming installation of new
    security by commit_creds().

    (*) security_task_post_setuid(), ->task_post_setuid()

    Removed in favour of security_task_fix_setuid().

    (*) security_task_fix_setuid(), ->task_fix_setuid()

    Fix up the proposed new credentials for setuid(). This is used by
    cap_set_fix_setuid() to implicitly adjust capabilities in line with
    setuid() changes. Changes are made to the new credentials, rather
    than the task itself as in security_task_post_setuid().

    (*) security_task_reparent_to_init(), ->task_reparent_to_init()

    Removed. Instead the task being reparented to init is referred
    directly to init's credentials.

    NOTE! This results in the loss of some state: SELinux's osid no
    longer records the sid of the thread that forked it.

    (*) security_key_alloc(), ->key_alloc()
    (*) security_key_permission(), ->key_permission()

    Changed. These now take cred pointers rather than task pointers to
    refer to the security context.

    (4) sys_capset().

    This has been simplified and uses less locking. The LSM functions it
    calls have been merged.

    (5) reparent_to_kthreadd().

    This gives the current thread the same credentials as init by simply using
    commit_thread() to point that way.

    (6) __sigqueue_alloc() and switch_uid()

    __sigqueue_alloc() can't stop the target task from changing its creds
    beneath it, so this function gets a reference to the currently applicable
    user_struct which it then passes into the sigqueue struct it returns if
    successful.

    switch_uid() is now called from commit_creds(), and possibly should be
    folded into that. commit_creds() should take care of protecting
    __sigqueue_alloc().

    (7) [sg]et[ug]id() and co and [sg]et_current_groups.

    The set functions now all use prepare_creds(), commit_creds() and
    abort_creds() to build and check a new set of credentials before applying
    it.

    security_task_set[ug]id() is called inside the prepared section. This
    guarantees that nothing else will affect the creds until we've finished.

    The calling of set_dumpable() has been moved into commit_creds().

    Much of the functionality of set_user() has been moved into
    commit_creds().

    The get functions all simply access the data directly.

    (8) security_task_prctl() and cap_task_prctl().

    security_task_prctl() has been modified to return -ENOSYS if it doesn't
    want to handle a function, or otherwise return the return value directly
    rather than through an argument.

    Additionally, cap_task_prctl() now prepares a new set of credentials, even
    if it doesn't end up using it.

    (9) Keyrings.

    A number of changes have been made to the keyrings code:

    (a) switch_uid_keyring(), copy_keys(), exit_keys() and suid_keys() have
    all been dropped and built in to the credentials functions directly.
    They may want separating out again later.

    (b) key_alloc() and search_process_keyrings() now take a cred pointer
    rather than a task pointer to specify the security context.

    (c) copy_creds() gives a new thread within the same thread group a new
    thread keyring if its parent had one, otherwise it discards the thread
    keyring.

    (d) The authorisation key now points directly to the credentials to extend
    the search into rather pointing to the task that carries them.

    (e) Installing thread, process or session keyrings causes a new set of
    credentials to be created, even though it's not strictly necessary for
    process or session keyrings (they're shared).

    (10) Usermode helper.

    The usermode helper code now carries a cred struct pointer in its
    subprocess_info struct instead of a new session keyring pointer. This set
    of credentials is derived from init_cred and installed on the new process
    after it has been cloned.

    call_usermodehelper_setup() allocates the new credentials and
    call_usermodehelper_freeinfo() discards them if they haven't been used. A
    special cred function (prepare_usermodeinfo_creds()) is provided
    specifically for call_usermodehelper_setup() to call.

    call_usermodehelper_setkeys() adjusts the credentials to sport the
    supplied keyring as the new session keyring.

    (11) SELinux.

    SELinux has a number of changes, in addition to those to support the LSM
    interface changes mentioned above:

    (a) selinux_setprocattr() no longer does its check for whether the
    current ptracer can access processes with the new SID inside the lock
    that covers getting the ptracer's SID. Whilst this lock ensures that
    the check is done with the ptracer pinned, the result is only valid
    until the lock is released, so there's no point doing it inside the
    lock.

    (12) is_single_threaded().

    This function has been extracted from selinux_setprocattr() and put into
    a file of its own in the lib/ directory as join_session_keyring() now
    wants to use it too.

    The code in SELinux just checked to see whether a task shared mm_structs
    with other tasks (CLONE_VM), but that isn't good enough. We really want
    to know if they're part of the same thread group (CLONE_THREAD).

    (13) nfsd.

    The NFS server daemon now has to use the COW credentials to set the
    credentials it is going to use. It really needs to pass the credentials
    down to the functions it calls, but it can't do that until other patches
    in this series have been applied.

    Signed-off-by: David Howells
    Acked-by: James Morris
    Signed-off-by: James Morris

    David Howells
     
  • Separate the task security context from task_struct. At this point, the
    security data is temporarily embedded in the task_struct with two pointers
    pointing to it.

    Note that the Alpha arch is altered as it refers to (E)UID and (E)GID in
    entry.S via asm-offsets.

    With comment fixes Signed-off-by: Marc Dionne

    Signed-off-by: David Howells
    Acked-by: James Morris
    Acked-by: Serge Hallyn
    Signed-off-by: James Morris

    David Howells
     
  • Take away the ability for sys_capset() to affect processes other than current.

    This means that current will not need to lock its own credentials when reading
    them against interference by other processes.

    This has effectively been the case for a while anyway, since:

    (1) Without LSM enabled, sys_capset() is disallowed.

    (2) With file-based capabilities, sys_capset() is neutered.

    Signed-off-by: David Howells
    Acked-by: Serge Hallyn
    Acked-by: Andrew G. Morgan
    Acked-by: James Morris
    Signed-off-by: James Morris

    David Howells