Skip to contentSkip to conversation

Interaction user namespace, /proc/1 ownership & cap_set

DISCUSSION Daniel P. Berrange 4 participants 16 messages Jul 1, 2013 – Jul 12, 2013 11 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

I'm using a modified version (attached to this mail) of the demo program userns_child_exec.c linked on https://lwn.net/Articles/532593/

  $ gcc -lcap -Wall -o userns_child_exec userns_child_exec.c 

First normal execution appears to work just fine (as root):

  $ ./userns_child_exec -p -m -U -M '0 1000 1' -G '0 1000 1' bash
  Launching child init
  # umount /proc/sys/fs/binfmt_misc
  # umount /proc/sys/fs/binfmt_misc
  # umount /proc/fs/nfsd
  # umount /proc
  # mount -t proc proc /proc/
  # ls -al /proc/1/environ 
  -r--------. 1 root root 0 Jul  1 17:04 /proc/1/environ

My modification adds support for a '-c' arg to call the program to use cap_set() from libcap.so in order to remove the CAP_SYS_MODULE capability.

If I run the program with the '-c' arg present, then the files in the /proc/1/ directory all end up owned by nfsnobody.nfsbody

  $ ./userns_child_exec -c -p -m -U -M '0 1000 1' -G '0 1000 1' bash
  Launching child init
  # umount /proc/sys/fs/binfmt_misc
  # umount /proc/sys/fs/binfmt_misc
  # umount /proc/fs/nfsd
  # umount /proc
  # mount -t proc proc /proc/
  # ls -al /proc/1/environ 
  -r--------. 1 nfsnobody nfsnobody 0 Jul  1 17:01 /proc/1/environ

Why on earth would calling 'cap_set()' to drop a capability cause the user/group ownership of files in /proc/1/ to change ?

Any child processes launched from this point get correct ownership on their /proc/NNN files - only /proc/1/ seems to be affected.

Via strace, we can see the libcap code only calls 3 syscalls:

capget({_LINUX_CAPABILITY_VERSION_3, 0}, NULL) = 0
capget({_LINUX_CAPABILITY_VERSION_3, 0}, {CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_FSETID|CAP_KILL|CAP_SETGID|CAP_SET UID|CAP_SETPCAP|CAP_LINUX_IMMUTABLE|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_ADMIN|CAP_NET_RAW|CAP_IPC_LOCK|CAP_IPC_OWNER|CAP_SYS_MO DULE|CAP_SYS_RAWIO|CAP_SYS_CHROOT|CAP_SYS_PTRACE|CAP_SYS_PACCT|CAP_SYS_ADMIN|CAP_SYS_BOOT|CAP_SYS_NICE|CAP_SYS_RESOURCE|CAP_SYS_TIME|CAP_S YS_TTY_CONFIG|CAP_MKNOD|CAP_LEASE|CAP_AUDIT_WRITE|CAP_AUDIT_CONTROL|CAP_SETFCAP, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER

|CAP_FSETID|CAP_KILL|CAP_SETGID|CAP_SETUID|CAP_SETPCAP|CAP_LINUX_IMMUTABLE|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_ADMIN|CAP_NET_RA
W|CAP_IPC_LOCK|CAP_IPC_OWNER|CAP_SYS_MODULE|CAP_SYS_RAWIO|CAP_SYS_CHROOT|CAP_SYS_PTRACE|CAP_SYS_PACCT|CAP_SYS_ADMIN|CAP_SYS_BOOT|CAP_SYS_N
ICE|CAP_SYS_RESOURCE|CAP_SYS_TIME|CAP_SYS_TTY_CONFIG|CAP_MKNOD|CAP_LEASE|CAP_AUDIT_WRITE|CAP_AUDIT_CONTROL|CAP_SETFCAP, 0}) = 0
capset({_LINUX_CAPABILITY_VERSION_3, 0}, {CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_FSETID|CAP_KILL|CAP_SETGID|CAP_SETUID|CAP_SETPCAP|CAP_LINUX_IMMUTABLE|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_ADMIN|CAP_NET_RAW|CAP_IPC_LOCK|CAP_IPC_OWNER|CAP_SYS_RAWIO|CAP_SYS_CHROOT|CAP_SYS_PTRACE|CAP_SYS_PACCT|CAP_SYS_ADMIN|CAP_SYS_BOOT|CAP_SYS_NICE|CAP_SYS_RESOURCE|CAP_SYS_TIME|CAP_SYS_TTY_CONFIG|CAP_MKNOD|CAP_LEASE|CAP_AUDIT_WRITE|CAP_AUDIT_CONTROL|CAP_SETFCAP, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_FSETID|CAP_KILL|CAP_SETGID|CAP_SETUID|CAP_SETPCAP|CAP_LINUX_IMMUTABLE|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_ADMIN|CAP_NET_RAW|CAP_IPC_LOCK|CAP_IPC_OWNER|CAP_SYS_RAWIO|CAP_SYS_CHROOT|CAP_SYS_PTRACE|CAP_SYS_PACCT|CAP_SYS_ADMIN|CAP_SYS_BOOT|CAP_SYS_NIC
 E|CAP_SYS_RESOURCE|CAP_SYS_TIME|CAP_SYS_TTY_CONFIG|CAP_MKNOD|CAP_LEASE|CAP_AUDIT_WRITE|CAP_AUDIT_CONTROL|CAP_SETFCAP, 0}) = 0

though, for added fun, when running the demo program via strace the problem does not appear :-(

On a slightly related topic, I've noticed that it is not possible to invoke prctl(PR_CAPBSET_DROP) to clear the bounding set for processes inside a container. The kernel code uses capable() instead of ns_capable(). Is this intended, or a missing conversion ?

Indeed, even ignoring namespaces for a minute, I'm curious as to why CAP_SETPCAP is required at all for PR_CAPBSET_DROP ? Is it really a security risk to allow a non-privileged user to remove bits from the bounding set ? For KVM I'd like to be able to use PR_CAPBSET_DROP to prevent a compromised KVM process from using any setuid program to re-gain any kind of capabilities. Similarly I think a container admin may well wish to make use of PR_CAPBSET_DROP to lock down applications there.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

/* userns_child_exec.c

Copyright 2013, Michael Kerrisk
Licensed under GNU General Public License v2 or later

Create a child process that executes a shell command in new namespace(s); allow UID and GID mappings to be specified when creating a user namespace.
*/
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <sys/capability.h>

/* A simple error-handling function: print an error message based
on the value in 'errno' and terminate the calling process */

#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
                        } while (0)

struct child_args {
char **argv; /* Command to be executed by child, with arguments */
int pipe_fd[2]; /* Pipe used to synchronize parent and child */
};

static int verbose;
static int dropcaps;

static void
usage(char *pname)

{
    fprintf(stderr, "Usage: %s [options] cmd [arg...]\n\n", pname);
    fprintf(stderr, "Create a child process that executes a shell command "
            "in a new user namespace,\n"
            "and possibly also other new namespace(s).\n\n");
    fprintf(stderr, "Options can be:\n\n");
#define fpe(str) fprintf(stderr, "    %s", str);
    fpe("-c          Drop caps\n");
    fpe("-i          New IPC namespace\n");
    fpe("-m          New mount namespace\n");
    fpe("-n          New network namespace\n");
    fpe("-p          New PID namespace\n");
    fpe("-u          New UTS namespace\n");
    fpe("-U          New user namespace\n");
    fpe("-M uid_map  Specify UID map for user namespace\n");
    fpe("-G gid_map  Specify GID map for user namespace\n");
    fpe("            If -M or -G is specified, -U is required\n");
    fpe("-v          Display verbose messages\n");
    fpe("\n");
    fpe("Map strings for -M and -G consist of records of the form:\n");
    fpe("\n");
    fpe("    ID-inside-ns   ID-outside-ns   len\n");
    fpe("\n");
    fpe("A map string can contain multiple records, separated by commas;\n");
    fpe("the commas are replaced by newlines before writing to map files.\n");

    exit(EXIT_FAILURE);
}

/* Update the mapping file 'map_file', with the value provided in
'mapping', a string that defines a UID or GID mapping. A UID or GID mapping consists of one or more newline-delimited records of the form:

ID_inside-ns ID-outside-ns length

Requiring the user to supply a string that contains newlines is of course inconvenient for command-line use. Thus, we permit the use of commas to delimit records in this string, and replace them with newlines before writing the string to the file. */

static void
update_map(char *mapping, char *map_file)

{
    int fd, j;
    size_t map_len;     /* Length of 'mapping' */

    /* Replace commas in mapping string with newlines */

    map_len = strlen(mapping);
    for (j = 0; j < map_len; j++)
        if (mapping[j] == ',')
            mapping[j] = '\n';

    fd = open(map_file, O_RDWR);
    if (fd == -1) {
        fprintf(stderr, "open %s: %s\n", map_file, strerror(errno));
        exit(EXIT_FAILURE);
    }

    if (write(fd, mapping, map_len) != map_len) {
        fprintf(stderr, "write %s: %s\n", map_file, strerror(errno));
        exit(EXIT_FAILURE);
    }

    close(fd);
}

static int /* Start function for cloned child */
childFunc(void *arg)

{
    struct child_args *args = (struct child_args *) arg;
    char ch;
 
    /* Wait until the parent has updated the UID and GID mappings. See
       the comment in main(). We wait for end of file on a pipe that will
       be closed by the parent process once it has updated the mappings. */

    close(args->pipe_fd[1]);    /* Close our descriptor for the write end
                                   of the pipe so that we see EOF when
                                   parent closes its descriptor */
    if (read(args->pipe_fd[0], &ch, 1) != 0) {
        fprintf(stderr, "Failure in child: read from pipe returned != 0\n");
        exit(EXIT_FAILURE);
    }

    /* Execute a shell command */
    if (setreuid(0, 0) < 0)
      errExit("setreuid");
    if (setregid(0, 0) < 0)
      errExit("setregid");

    if (dropcaps) {
      cap_t caps;
      cap_value_t val[] = { CAP_SYS_MODULE };
      caps = cap_get_proc();

      cap_set_flag(caps,
		   CAP_EFFECTIVE,
		   1, val, CAP_CLEAR);
      cap_set_flag(caps,
		   CAP_PERMITTED,
		   1, val, CAP_CLEAR);
      cap_set_flag(caps,
		   CAP_INHERITABLE,
		   1, val, CAP_CLEAR);

      cap_set_proc(caps);
    }

    fprintf(stderr, "Launching child init\n");
    execvp(args->argv[0], args->argv);
    errExit("execvp");
}

#define STACK_SIZE (1024 * 1024)

static char child_stack[STACK_SIZE]; /* Space for child's stack */

int
main(int argc, char *argv[])

{
    int flags, opt;
    pid_t child_pid;
    struct child_args args;
    char *uid_map, *gid_map;
    char map_path[PATH_MAX];

    /* Parse command-line options. The initial '+' character in
       the final getopt() argument prevents GNU-style permutation
       of command-line options. That's useful, since sometimes
       the 'command' to be executed by this program itself
       has command-line options. We don't want getopt() to treat
       those as options to this program. */

    flags = 0;
    verbose = 0;
    gid_map = NULL;
    uid_map = NULL;
    while ((opt = getopt(argc, argv, "+imnpucUM:G:v")) != -1) {
        switch (opt) {
        case 'i': flags |= CLONE_NEWIPC;        break;
        case 'm': flags |= CLONE_NEWNS;         break;
        case 'n': flags |= CLONE_NEWNET;        break;
        case 'p': flags |= CLONE_NEWPID;        break;
        case 'u': flags |= CLONE_NEWUTS;        break;
	case 'c': dropcaps = 1;                 break;
        case 'v': verbose = 1;                  break;
        case 'M': uid_map = optarg;             break;
        case 'G': gid_map = optarg;             break;
        case 'U': flags |= CLONE_NEWUSER;       break;
        default:  usage(argv[0]);
        }
    }

    /* -M or -G without -U is nonsensical */

    if ((uid_map != NULL || gid_map != NULL) &&
            !(flags & CLONE_NEWUSER))
        usage(argv[0]);

    args.argv = &argv[optind];

    /* We use a pipe to synchronize the parent and child, in order to
       ensure that the parent sets the UID and GID maps before the child
       calls execve(). This ensures that the child maintains its
       capabilities during the execve() in the common case where we
       want to map the child's effective user ID to 0 in the new user
       namespace. Without this synchronization, the child would lose
       its capabilities if it performed an execve() with nonzero
       user IDs (see the capabilities(7) man page for details of the
       transformation of a process's capabilities during execve()). */

    if (pipe(args.pipe_fd) == -1)
        errExit("pipe");

    /* Create the child in new namespace(s) */

    child_pid = clone(childFunc, child_stack + STACK_SIZE,
                      flags | SIGCHLD, &args);
    if (child_pid == -1)
        errExit("clone");

    /* Parent falls through to here */

    if (verbose)
        printf("%s: PID of child created by clone() is %ld\n",
                argv[0], (long) child_pid);

    /* Update the UID and GID maps in the child */

    if (uid_map != NULL) {
        snprintf(map_path, PATH_MAX, "/proc/%ld/uid_map",
                (long) child_pid);
        update_map(uid_map, map_path);
    }
    if (gid_map != NULL) {
        snprintf(map_path, PATH_MAX, "/proc/%ld/gid_map",
                (long) child_pid);
        update_map(gid_map, map_path);
    }

    /* Close the write end of the pipe, to signal to the child that we
       have updated the UID and GID maps */

    close(args.pipe_fd[1]);

    if (waitpid(child_pid, NULL, 0) == -1)      /* Wait for child */
        errExit("waitpid");

    if (verbose)
        printf("%s: terminating\n", argv[0]);

    exit(EXIT_SUCCESS);
}

_______________________________________________
Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
https://lists.linuxfoundation.org/mailman/listinfo/containers

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 50 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

I'm using a modified version (attached to this mail) of the demo program userns_child_exec.c linked on https://lwn.net/Articles/532593/

  $ gcc -lcap -Wall -o userns_child_exec userns_child_exec.c 

First normal execution appears to work just fine (as root):

  $ ./userns_child_exec -p -m -U -M '0 1000 1' -G '0 1000 1' bash
  Launching child init
  # umount /proc/sys/fs/binfmt_misc
  # umount /proc/sys/fs/binfmt_misc
  # umount /proc/fs/nfsd
  # umount /proc
  # mount -t proc proc /proc/
  # ls -al /proc/1/environ 
  -r--------. 1 root root 0 Jul  1 17:04 /proc/1/environ

My modification adds support for a '-c' arg to call the program to use cap_set() from libcap.so in order to remove the CAP_SYS_MODULE capability.

If I run the program with the '-c' arg present, then the files in the /proc/1/ directory all end up owned by nfsnobody.nfsbody

  $ ./userns_child_exec -c -p -m -U -M '0 1000 1' -G '0 1000 1' bash
  Launching child init
  # umount /proc/sys/fs/binfmt_misc
  # umount /proc/sys/fs/binfmt_misc
  # umount /proc/fs/nfsd
  # umount /proc
  # mount -t proc proc /proc/
  # ls -al /proc/1/environ 
  -r--------. 1 nfsnobody nfsnobody 0 Jul  1 17:01 /proc/1/environ

Why on earth would calling 'cap_set()' to drop a capability cause the user/group ownership of files in /proc/1/ to change ?

Any child processes launched from this point get correct ownership on their /proc/NNN files - only /proc/1/ seems to be affected.

Via strace, we can see the libcap code only calls 3 syscalls:

capget({_LINUX_CAPABILITY_VERSION_3, 0}, NULL) = 0
capget({_LINUX_CAPABILITY_VERSION_3, 0}, {CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_FSETID|CAP_KILL|CAP_SETGID|CAP_SET UID|CAP_SETPCAP|CAP_LINUX_IMMUTABLE|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_ADMIN|CAP_NET_RAW|CAP_IPC_LOCK|CAP_IPC_OWNER|CAP_SYS_MO DULE|CAP_SYS_RAWIO|CAP_SYS_CHROOT|CAP_SYS_PTRACE|CAP_SYS_PACCT|CAP_SYS_ADMIN|CAP_SYS_BOOT|CAP_SYS_NICE|CAP_SYS_RESOURCE|CAP_SYS_TIME|CAP_S YS_TTY_CONFIG|CAP_MKNOD|CAP_LEASE|CAP_AUDIT_WRITE|CAP_AUDIT_CONTROL|CAP_SETFCAP, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER

Quoted 4 lines
|CAP_FSETID|CAP_KILL|CAP_SETGID|CAP_SETUID|CAP_SETPCAP|CAP_LINUX_IMMUTABLE|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_ADMIN|CAP_NET_RA
W|CAP_IPC_LOCK|CAP_IPC_OWNER|CAP_SYS_MODULE|CAP_SYS_RAWIO|CAP_SYS_CHROOT|CAP_SYS_PTRACE|CAP_SYS_PACCT|CAP_SYS_ADMIN|CAP_SYS_BOOT|CAP_SYS_N
ICE|CAP_SYS_RESOURCE|CAP_SYS_TIME|CAP_SYS_TTY_CONFIG|CAP_MKNOD|CAP_LEASE|CAP_AUDIT_WRITE|CAP_AUDIT_CONTROL|CAP_SETFCAP, 0}) = 0
capset({_LINUX_CAPABILITY_VERSION_3, 0}, {CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_FSETID|CAP_KILL|CAP_SETGID|CAP_SETUID|CAP_SETPCAP|CAP_LINUX_IMMUTABLE|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_ADMIN|CAP_NET_RAW|CAP_IPC_LOCK|CAP_IPC_OWNER|CAP_SYS_RAWIO|CAP_SYS_CHROOT|CAP_SYS_PTRACE|CAP_SYS_PACCT|CAP_SYS_ADMIN|CAP_SYS_BOOT|CAP_SYS_NICE|CAP_SYS_RESOURCE|CAP_SYS_TIME|CAP_SYS_TTY_CONFIG|CAP_MKNOD|CAP_LEASE|CAP_AUDIT_WRITE|CAP_AUDIT_CONTROL|CAP_SETFCAP, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_FSETID|CAP_KILL|CAP_SETGID|CAP_SETUID|CAP_SETPCAP|CAP_LINUX_IMMUTABLE|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_ADMIN|CAP_NET_RAW|CAP_IPC_LOCK|CAP_IPC_OWNER|CAP_SYS_RAWIO|CAP_SYS_CHROOT|CAP_SYS_PTRACE|CAP_SYS_PACCT|CAP_SYS_ADMIN|CAP_SYS_BOOT|CAP_SYS_N

ICE|CAP_SYS_RESOURCE|CAP_SYS_TIME|CAP_SYS_TTY_CONFIG|CAP_MKNOD|CAP_LEASE|CAP_AUDIT_WRITE|CAP_AUDIT_CONTROL|CAP_SETFCAP, 0}) = 0

Quoted 11 lines

though, for added fun, when running the demo program via strace the problem does not appear :-(

On a slightly related topic, I've noticed that it is not possible to invoke prctl(PR_CAPBSET_DROP) to clear the bounding set for processes inside a container. The kernel code uses capable() instead of ns_capable(). Is this intended, or a missing conversion ?

Quoted 8 lines

Indeed, even ignoring namespaces for a minute, I'm curious as to why CAP_SETPCAP is required at all for PR_CAPBSET_DROP ? Is it really a security risk to allow a non-privileged user to remove bits from the bounding set ? For KVM I'd like to be able to use PR_CAPBSET_DROP to prevent a compromised KVM process from using any setuid program to re-gain any kind of capabilities. Similarly I think a container admin may well wish to make use of PR_CAPBSET_DROP to lock down applications there.

Opps, I should have mentioned that I'm using 3.9.4 kernel. Basically the Fedora 3.9.4-303 build, but with CONFIG_XFS_FS=n and CONFIG_USER_NS=y set in the Kconfig.

Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Am 01.07.2013 18:19, schrieb Daniel P. Berrange:

Quoted 46 lines · Daniel P. Berrange
Quoted 45 lines

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

I'm using a modified version (attached to this mail) of the demo program userns_child_exec.c linked on https://lwn.net/Articles/532593/

  $ gcc -lcap -Wall -o userns_child_exec userns_child_exec.c 

First normal execution appears to work just fine (as root):

  $ ./userns_child_exec -p -m -U -M '0 1000 1' -G '0 1000 1' bash
  Launching child init
  # umount /proc/sys/fs/binfmt_misc
  # umount /proc/sys/fs/binfmt_misc
  # umount /proc/fs/nfsd
  # umount /proc
  # mount -t proc proc /proc/
  # ls -al /proc/1/environ 
  -r--------. 1 root root 0 Jul  1 17:04 /proc/1/environ

My modification adds support for a '-c' arg to call the program to use cap_set() from libcap.so in order to remove the CAP_SYS_MODULE capability.

If I run the program with the '-c' arg present, then the files in the /proc/1/ directory all end up owned by nfsnobody.nfsbody

  $ ./userns_child_exec -c -p -m -U -M '0 1000 1' -G '0 1000 1' bash
  Launching child init
  # umount /proc/sys/fs/binfmt_misc
  # umount /proc/sys/fs/binfmt_misc
  # umount /proc/fs/nfsd
  # umount /proc
  # mount -t proc proc /proc/
  # ls -al /proc/1/environ 
  -r--------. 1 nfsnobody nfsnobody 0 Jul  1 17:01 /proc/1/environ

Why on earth would calling 'cap_set()' to drop a capability cause the user/group ownership of files in /proc/1/ to change ?

Any child processes launched from this point get correct ownership on their /proc/NNN files - only /proc/1/ seems to be affected.

Via strace, we can see the libcap code only calls 3 syscalls:

Quoted 9 lines
Quoted 9 lines

capget({_LINUX_CAPABILITY_VERSION_3, 0}, NULL) = 0
capget({_LINUX_CAPABILITY_VERSION_3, 0}, {CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_FSETID|CAP_KILL|CAP_SETGID|CAP_SET UID|CAP_SETPCAP|CAP_LINUX_IMMUTABLE|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_ADMIN|CAP_NET_RAW|CAP_IPC_LOCK|CAP_IPC_OWNER|CAP_SYS_MO DULE|CAP_SYS_RAWIO|CAP_SYS_CHROOT|CAP_SYS_PTRACE|CAP_SYS_PACCT|CAP_SYS_ADMIN|CAP_SYS_BOOT|CAP_SYS_NICE|CAP_SYS_RESOURCE|CAP_SYS_TIME|CAP_S YS_TTY_CONFIG|CAP_MKNOD|CAP_LEASE|CAP_AUDIT_WRITE|CAP_AUDIT_CONTROL|CAP_SETFCAP, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER

|CAP_FSETID|CAP_KILL|CAP_SETGID|CAP_SETUID|CAP_SETPCAP|CAP_LINUX_IMMUTABLE|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_ADMIN|CAP_NET_RA
W|CAP_IPC_LOCK|CAP_IPC_OWNER|CAP_SYS_MODULE|CAP_SYS_RAWIO|CAP_SYS_CHROOT|CAP_SYS_PTRACE|CAP_SYS_PACCT|CAP_SYS_ADMIN|CAP_SYS_BOOT|CAP_SYS_N
ICE|CAP_SYS_RESOURCE|CAP_SYS_TIME|CAP_SYS_TTY_CONFIG|CAP_MKNOD|CAP_LEASE|CAP_AUDIT_WRITE|CAP_AUDIT_CONTROL|CAP_SETFCAP, 0}) = 0
capset({_LINUX_CAPABILITY_VERSION_3, 0}, {CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_FSETID|CAP_KILL|CAP_SETGID|CAP_SETUID|CAP_SETPCAP|CAP_LINUX_IMMUTABLE|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_ADMIN|CAP_NET_RAW|CAP_IPC_LOCK|CAP_IPC_OWNER|CAP_SYS_RAWIO|CAP_SYS_CHROOT|CAP_SYS_PTRACE|CAP_SYS_PACCT|CAP_SYS_ADMIN|CAP_SYS_BOOT|CAP_SYS_NICE|CAP_SYS_RESOURCE|CAP_SYS_TIME|CAP_SYS_TTY_CONFIG|CAP_MKNOD|CAP_LEASE|CAP_AUDIT_WRITE|CAP_AUDIT_CONTROL|CAP_SETFCAP, CAP_CHOWN|CAP_DAC_OVERRIDE|CAP_DAC_READ_SEARCH|CAP_FOWNER|CAP_FSETID|CAP_KILL|CAP_SETGID|CAP_SETUID|CAP_SETPCAP|CAP_LINUX_IMMUTABLE|CAP_NET_BIND_SERVICE|CAP_NET_BROADCAST|CAP_NET_ADMIN|CAP_NET_RAW|CAP_IPC_LOCK|CAP_IPC_OWNER|CAP_SYS_RAWIO|CAP_SYS_CHROOT|CAP_SYS_PTRACE|CAP_SYS_PACCT|CAP_SYS_ADMIN|CAP_SYS_BOOT|CAP_SYS_

NICE|CAP_SYS_RESOURCE|CAP_SYS_TIME|CAP_SYS_TTY_CONFIG|CAP_MKNOD|CAP_LEASE|CAP_AUDIT_WRITE|CAP_AUDIT_CONTROL|CAP_SETFCAP, 0}) = 0

Quoted 21 lines
Quoted 11 lines

though, for added fun, when running the demo program via strace the problem does not appear :-(

On a slightly related topic, I've noticed that it is not possible to invoke prctl(PR_CAPBSET_DROP) to clear the bounding set for processes inside a container. The kernel code uses capable() instead of ns_capable(). Is this intended, or a missing conversion ?

Quoted 8 lines

Indeed, even ignoring namespaces for a minute, I'm curious as to why CAP_SETPCAP is required at all for PR_CAPBSET_DROP ? Is it really a security risk to allow a non-privileged user to remove bits from the bounding set ? For KVM I'd like to be able to use PR_CAPBSET_DROP to prevent a compromised KVM process from using any setuid program to re-gain any kind of capabilities. Similarly I think a container admin may well wish to make use of PR_CAPBSET_DROP to lock down applications there.

Quoted 3 lines

Opps, I should have mentioned that I'm using 3.9.4 kernel. Basically the Fedora 3.9.4-303 build, but with CONFIG_XFS_FS=n and CONFIG_USER_NS=y set in the Kconfig.

FWIW, I can reproduce the issue on v3.10 vanilla.

Thanks,
//richard

#5 lore raw
In reply to a message not in the archive
Quoted 3 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

This problem is occured after we call setuid/gid.

for example, a task whose pid is 1234 calls
setregid(10,10);
setreuid(10,10);

The uid/gid of the /proc/1234 is 10:0
ll /proc/1234 -d
dr-xr-xr-x 8 uucp wheel 0 Jul 2 10:57 /proc/1234

the uid/gid of the files under /proc/1234 are two kinds... ll /proc/1234
dr-xr-xr-x 2 uucp wheel 0 Jul 2 10:58 attr -rw-r--r-- 1 root root 0 Jul 2 10:58 autogroup
...
dr-xr-xr-x 5 uucp wheel 0 Jul 2 10:58 net dr-x--x--x 2 root root 0 Jul 2 10:58 ns
...
dr-xr-xr-x 3 uucp wheel 0 Jul 2 10:58 task

I checked the pre_revalidate and found the owner of the files under /proc/<pid> will be set to the GLOBAL_ROOT_UID if the task executed setuid/setgid(task_dumpable is false). Is this what we expected? why? For user namespace,the owner of /proc/1/* is incorrect and after task call setuid/gid in user namespace, the owner of /proc/<pid-of-this-task>/* is incorrect too.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw
Quoted 26 lines · Gao feng
Quoted 3 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

This problem is occured after we call setuid/gid.

for example, a task whose pid is 1234 calls
setregid(10,10);
setreuid(10,10);

The uid/gid of the /proc/1234 is 10:0
ll /proc/1234 -d
dr-xr-xr-x 8 uucp wheel 0 Jul 2 10:57 /proc/1234

the uid/gid of the files under /proc/1234 are two kinds... ll /proc/1234
dr-xr-xr-x 2 uucp wheel 0 Jul 2 10:58 attr -rw-r--r-- 1 root root 0 Jul 2 10:58 autogroup
...
dr-xr-xr-x 5 uucp wheel 0 Jul 2 10:58 net dr-x--x--x 2 root root 0 Jul 2 10:58 ns
...
dr-xr-xr-x 3 uucp wheel 0 Jul 2 10:58 task

Quoted 3 lines

I checked the pre_revalidate and found the owner of the files under /proc/<pid> will be set to the GLOBAL_ROOT_UID if the task executed setuid/setgid(task_dumpable is false). Is this what we expected? why?

Expected yes. Perfect perhaps not.

That piece of code has not been examined to see if it is safe to use make_kuid(task_user_ns(task), 0), instead of GLOBAL_ROOT_UID.

Quoted 3 lines

For user namespace,the owner of /proc/1/* is incorrect and
after task call setuid/gid in user namespace, the owner of /proc/<pid-of-this-task>/* is incorrect too.

From the current semantics of dumpable GLOBAL_ROOT_UID is correct.

Please double check but I believe /proc/self should continue to work, despite this.

The practical question is there anything that can go wrong if we allow the root of the user namespace of the process to read it. Especially since several permission changes can happen a process may stop being dumpable before we enter the user namespace. So it is not immediately clear that relaxing the dumpable rules is safe.

Eric

A message that is not in the archive (sent off-list or not yet received): 87wqp9uz9a.fsf-aS9lmoZGLiVWk0Htik3J/[email protected]

#9 lore raw

Am 02.07.2013 10:44, schrieb Eric W. Biederman:

Quoted 37 lines
Quoted 26 lines · Gao feng
Quoted 3 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

This problem is occured after we call setuid/gid.

for example, a task whose pid is 1234 calls
setregid(10,10);
setreuid(10,10);

The uid/gid of the /proc/1234 is 10:0
ll /proc/1234 -d
dr-xr-xr-x 8 uucp wheel 0 Jul 2 10:57 /proc/1234

the uid/gid of the files under /proc/1234 are two kinds... ll /proc/1234
dr-xr-xr-x 2 uucp wheel 0 Jul 2 10:58 attr -rw-r--r-- 1 root root 0 Jul 2 10:58 autogroup
...
dr-xr-xr-x 5 uucp wheel 0 Jul 2 10:58 net dr-x--x--x 2 root root 0 Jul 2 10:58 ns
...
dr-xr-xr-x 3 uucp wheel 0 Jul 2 10:58 task

Quoted 3 lines

I checked the pre_revalidate and found the owner of the files under /proc/<pid> will be set to the GLOBAL_ROOT_UID if the task executed setuid/setgid(task_dumpable is false). Is this what we expected? why?

Expected yes. Perfect perhaps not.

That piece of code has not been examined to see if it is safe to use make_kuid(task_user_ns(task), 0), instead of GLOBAL_ROOT_UID.

Quoted 8 lines
Quoted 3 lines

For user namespace,the owner of /proc/1/* is incorrect and
after task call setuid/gid in user namespace, the owner of /proc/<pid-of-this-task>/* is incorrect too.

From the current semantics of dumpable GLOBAL_ROOT_UID is correct.

Please double check but I believe /proc/self should continue to work, despite this.

/proc/self is not an option. systemd (in particular some of it's tools with pid != 1) read from /proc/1/environ to find out what environment variables it got to detect LXC and other visualization environments.
With userns enabled this check fails and systemd goes nuts because it thinks that it lives on top of a "real" Linux.

Thanks,
//richard

A message that is not in the archive (sent off-list or not yet received): 51D295C5.1080003-/[email protected]

#11 lore raw
Quoted 13 lines · Richard Weinberger

Am 02.07.2013 10:44, schrieb Eric W. Biederman:

Quoted 12 lines
Quoted 10 lines · Gao feng
Quoted 3 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

This problem is occured after we call setuid/gid.

for example, a task whose pid is 1234 calls
setregid(10,10);
setreuid(10,10);

If seems to get reset to the right values (0:0) when we execve() the init binary though. This doesn't happen if we have invoked the capset() syscall in between the setregid & the execve() calls.

Quoted 29 lines
Quoted 29 lines
Quoted 16 lines

The uid/gid of the /proc/1234 is 10:0
ll /proc/1234 -d
dr-xr-xr-x 8 uucp wheel 0 Jul 2 10:57 /proc/1234

the uid/gid of the files under /proc/1234 are two kinds... ll /proc/1234
dr-xr-xr-x 2 uucp wheel 0 Jul 2 10:58 attr -rw-r--r-- 1 root root 0 Jul 2 10:58 autogroup
...
dr-xr-xr-x 5 uucp wheel 0 Jul 2 10:58 net dr-x--x--x 2 root root 0 Jul 2 10:58 ns
...
dr-xr-xr-x 3 uucp wheel 0 Jul 2 10:58 task

Quoted 3 lines

I checked the pre_revalidate and found the owner of the files under /proc/<pid> will be set to the GLOBAL_ROOT_UID if the task executed setuid/setgid(task_dumpable is false). Is this what we expected? why?

Expected yes. Perfect perhaps not.

That piece of code has not been examined to see if it is safe to use make_kuid(task_user_ns(task), 0), instead of GLOBAL_ROOT_UID.

Quoted 3 lines

For user namespace,the owner of /proc/1/* is incorrect and
after task call setuid/gid in user namespace, the owner of /proc/<pid-of-this-task>/* is incorrect too.

Quoted 8 lines
Quoted 4 lines

From the current semantics of dumpable GLOBAL_ROOT_UID is correct.

Please double check but I believe /proc/self should continue to work, despite this.

/proc/self is not an option. systemd (in particular some of it's tools with pid != 1) read from /proc/1/environ to find out what environment variables it got to detect LXC and other visualization environments.
With userns enabled this check fails and systemd goes nuts because it thinks that it lives on top of a "real" Linux.

I don't even see how /proc/self would solve this, since it is just a symlink pointing to /proc/1 in this scenario, so the ownership of files at /proc/1/XXXX would still be wrong.

This isn't really a systemd specific problem either, I think any app would expect to be able to read its own files under /proc/$PID/

Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

A message that is not in the archive (sent off-list or not yet received): [email protected]

#13 lore raw

Am 02.07.2013 11:25, schrieb Daniel P. Berrange:

Quoted 53 lines · Richard Weinberger
Quoted 13 lines

Am 02.07.2013 10:44, schrieb Eric W. Biederman:

Quoted 12 lines
Quoted 10 lines · Gao feng
Quoted 3 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

This problem is occured after we call setuid/gid.

for example, a task whose pid is 1234 calls
setregid(10,10);
setreuid(10,10);

If seems to get reset to the right values (0:0) when we execve() the init binary though. This doesn't happen if we have invoked the capset() syscall in between the setregid & the execve() calls.

Quoted 34 lines
Quoted 33 lines
Quoted 16 lines

The uid/gid of the /proc/1234 is 10:0
ll /proc/1234 -d
dr-xr-xr-x 8 uucp wheel 0 Jul 2 10:57 /proc/1234

the uid/gid of the files under /proc/1234 are two kinds... ll /proc/1234
dr-xr-xr-x 2 uucp wheel 0 Jul 2 10:58 attr -rw-r--r-- 1 root root 0 Jul 2 10:58 autogroup
...
dr-xr-xr-x 5 uucp wheel 0 Jul 2 10:58 net dr-x--x--x 2 root root 0 Jul 2 10:58 ns
...
dr-xr-xr-x 3 uucp wheel 0 Jul 2 10:58 task

Quoted 3 lines

I checked the pre_revalidate and found the owner of the files under /proc/<pid> will be set to the GLOBAL_ROOT_UID if the task executed setuid/setgid(task_dumpable is false). Is this what we expected? why?

Expected yes. Perfect perhaps not.

That piece of code has not been examined to see if it is safe to use make_kuid(task_user_ns(task), 0), instead of GLOBAL_ROOT_UID.

Quoted 3 lines

For user namespace,the owner of /proc/1/* is incorrect and
after task call setuid/gid in user namespace, the owner of /proc/<pid-of-this-task>/* is incorrect too.

From the current semantics of dumpable GLOBAL_ROOT_UID is correct.

Please double check but I believe /proc/self should continue to work, despite this.

Quoted 7 lines
Quoted 3 lines

/proc/self is not an option. systemd (in particular some of it's tools with pid != 1) read from /proc/1/environ to find out what environment variables it got to detect LXC and other visualization environments.
With userns enabled this check fails and systemd goes nuts because it thinks that it lives on top of a "real" Linux.

I don't even see how /proc/self would solve this, since it is just a symlink pointing to /proc/1 in this scenario, so the ownership of files at /proc/1/XXXX would still be wrong.

Yep.

Quoted 3 lines

This isn't really a systemd specific problem either, I think any app would expect to be able to read its own files under /proc/$PID/

True. I was not blaming systemd.
In my case systemd is just the program which suffers from the issue.

Thanks,
//richard

#14 lore raw
In reply to a message not in the archive
Quoted 11 lines · Daniel P. Berrange
Quoted 10 lines · Richard Weinberger

Am 02.07.2013 10:44, schrieb Eric W. Biederman:

Quoted 9 lines
Quoted 7 lines · Gao feng
Quoted 3 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

This problem is occured after we call setuid/gid.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

for example, a task whose pid is 1234 calls
setregid(10,10);
setreuid(10,10);

If seems to get reset to the right values (0:0) when we execve() the init binary though. This doesn't happen if we have invoked the capset() syscall in between the setregid & the execve() calls.

Yes, execve() should reset the dumpable state.

I took a quick look and I don't see a way around set_dumpable calls in setup_new_exec. Why the process remains undumpable after exec is worth investigating. That logic should not be user namespace specific however.

Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 14 lines

The uid/gid of the /proc/1234 is 10:0
ll /proc/1234 -d
dr-xr-xr-x 8 uucp wheel 0 Jul 2 10:57 /proc/1234

the uid/gid of the files under /proc/1234 are two kinds... ll /proc/1234
dr-xr-xr-x 2 uucp wheel 0 Jul 2 10:58 attr -rw-r--r-- 1 root root 0 Jul 2 10:58 autogroup
...
dr-xr-xr-x 5 uucp wheel 0 Jul 2 10:58 net dr-x--x--x 2 root root 0 Jul 2 10:58 ns
...
dr-xr-xr-x 3 uucp wheel 0 Jul 2 10:58 task

Quoted 3 lines

I checked the pre_revalidate and found the owner of the files under /proc/<pid> will be set to the GLOBAL_ROOT_UID if the task executed setuid/setgid(task_dumpable is false). Is this what we expected? why?

Expected yes. Perfect perhaps not.

That piece of code has not been examined to see if it is safe to use make_kuid(task_user_ns(task), 0), instead of GLOBAL_ROOT_UID.

Quoted 3 lines

For user namespace,the owner of /proc/1/* is incorrect and
after task call setuid/gid in user namespace, the owner of /proc/<pid-of-this-task>/* is incorrect too.

From the current semantics of dumpable GLOBAL_ROOT_UID is correct.

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

Please double check but I believe /proc/self should continue to work, despite this.

/proc/self is not an option. systemd (in particular some of it's tools with pid != 1) read from /proc/1/environ to find out what environment variables it got to detect LXC and other visualization environments. With userns enabled this check fails and systemd goes nuts because it thinks that it lives on top of a "real" Linux.

How odd. Last I was paying attention it was the selinux policy that you could only access your own proc files, because of the way ptrace was limited.

As for systemd doing the wrong thing, it sounds like Richard has found a fertile source of imperfections.

Quoted 7 lines

I don't even see how /proc/self would solve this, since it is just a symlink pointing to /proc/1 in this scenario, so the ownership of files at /proc/1/XXXX would still be wrong.

This isn't really a systemd specific problem either, I think any app would expect to be able to read its own files under /proc/$PID/

I meant there is a special case in the permission check for accessing your own files as you must do when going through /proc/self. It is worth verifying that special case for accessing your own files continues to work even when you are in a user namespace.

Eric

A message that is not in the archive (sent off-list or not yet received): 87ehbhthbl.fsf-aS9lmoZGLiVWk0Htik3J/[email protected]

#16 lore raw
Quoted 21 lines · Eric W. Biederman
Quoted 11 lines · Daniel P. Berrange
Quoted 10 lines · Richard Weinberger

Am 02.07.2013 10:44, schrieb Eric W. Biederman:

Quoted 9 lines
Quoted 7 lines · Gao feng
Quoted 3 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

This problem is occured after we call setuid/gid.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

for example, a task whose pid is 1234 calls
setregid(10,10);
setreuid(10,10);

If seems to get reset to the right values (0:0) when we execve() the init binary though. This doesn't happen if we have invoked the capset() syscall in between the setregid & the execve() calls.

Quoted 7 lines

Yes, execve() should reset the dumpable state.

I took a quick look and I don't see a way around set_dumpable calls in setup_new_exec. Why the process remains undumpable after exec is worth investigating. That logic should not be user namespace specific however.

I think it's the install_exec_creds, it calls commit_creds to set process undumpable

        /* dumpability changes */
        if (!uid_eq(old->euid, new->euid) ||
            !gid_eq(old->egid, new->egid) ||
            !uid_eq(old->fsuid, new->fsuid) ||
            !gid_eq(old->fsgid, new->fsgid) ||
            !cred_cap_issubset(old, new)) {
                if (task->mm)
                        set_dumpable(task->mm, suid_dumpable);
                task->pdeath_signal = 0;
                smp_wmb();
        }

A message that is not in the archive (sent off-list or not yet received): [email protected]

#18 lore raw
Quoted 32 lines · Gao feng
Quoted 21 lines · Eric W. Biederman
Quoted 11 lines · Daniel P. Berrange
Quoted 10 lines · Richard Weinberger

Am 02.07.2013 10:44, schrieb Eric W. Biederman:

Quoted 9 lines
Quoted 7 lines · Gao feng
Quoted 3 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

This problem is occured after we call setuid/gid.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

for example, a task whose pid is 1234 calls
setregid(10,10);
setreuid(10,10);

If seems to get reset to the right values (0:0) when we execve() the init binary though. This doesn't happen if we have invoked the capset() syscall in between the setregid & the execve() calls.

Quoted 7 lines

Yes, execve() should reset the dumpable state.

I took a quick look and I don't see a way around set_dumpable calls in setup_new_exec. Why the process remains undumpable after exec is worth investigating. That logic should not be user namespace specific however.

I think it's the install_exec_creds, it calls commit_creds to set process undumpable

Quoted 11 lines
        /* dumpability changes */
        if (!uid_eq(old->euid, new->euid) ||
            !gid_eq(old->egid, new->egid) ||
            !uid_eq(old->fsuid, new->fsuid) ||
            !gid_eq(old->fsgid, new->fsgid) ||
            !cred_cap_issubset(old, new)) {
                if (task->mm)
                        set_dumpable(task->mm, suid_dumpable);
                task->pdeath_signal = 0;
                smp_wmb();
        }

That looks like it could do it. Especially if exec is increasing your capabilities.

Eric

A message that is not in the archive (sent off-list or not yet received): 8761wsudgk.fsf-aS9lmoZGLiVWk0Htik3J/[email protected]

#20 lore raw
Quoted 46 lines · Eric W. Biederman
Quoted 32 lines · Gao feng
Quoted 21 lines · Eric W. Biederman
Quoted 11 lines · Daniel P. Berrange
Quoted 10 lines · Richard Weinberger

Am 02.07.2013 10:44, schrieb Eric W. Biederman:

Quoted 9 lines
Quoted 7 lines · Gao feng
Quoted 3 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

This problem is occured after we call setuid/gid.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

for example, a task whose pid is 1234 calls
setregid(10,10);
setreuid(10,10);

If seems to get reset to the right values (0:0) when we execve() the init binary though. This doesn't happen if we have invoked the capset() syscall in between the setregid & the execve() calls.

Quoted 7 lines

Yes, execve() should reset the dumpable state.

I took a quick look and I don't see a way around set_dumpable calls in setup_new_exec. Why the process remains undumpable after exec is worth investigating. That logic should not be user namespace specific however.

I think it's the install_exec_creds, it calls commit_creds to set process undumpable

Quoted 11 lines
        /* dumpability changes */
        if (!uid_eq(old->euid, new->euid) ||
            !gid_eq(old->egid, new->egid) ||
            !uid_eq(old->fsuid, new->fsuid) ||
            !gid_eq(old->fsgid, new->fsgid) ||
            !cred_cap_issubset(old, new)) {
                if (task->mm)
                        set_dumpable(task->mm, suid_dumpable);
                task->pdeath_signal = 0;
                smp_wmb();
        }
Quoted 2 lines

That looks like it could do it. Especially if exec is increasing your capabilities.

Ah, yes, that would explain it. My demo is removing the SYS_MODULE capability, and then exec'ing the shell binary. Since we are uid==0, and prctl(PR_CAPBSET_DROP) is not available inside the user namespace, the rules for capabilities vs execve() call will cause the shell binary to regain SYS_MODULE capability bit.

So the problem I'm seeing in libvirt is all a result of the fact that we can't use PR_CAPBSET_DROP inside the user namespace. Given that there's no point trying to drop any capabilities inside the user namespace.

The only slight problem here is that we want to drop CAP_MKNOD so that systemd can detect that it shouldn't attempt to run any units which would rely on mknod.

Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

A message that is not in the archive (sent off-list or not yet received): [email protected]

#22 lore raw
Quoted 56 lines · Daniel P. Berrange
Quoted 46 lines · Eric W. Biederman
Quoted 32 lines · Gao feng
Quoted 21 lines · Eric W. Biederman
Quoted 11 lines · Daniel P. Berrange
Quoted 10 lines · Richard Weinberger

Am 02.07.2013 10:44, schrieb Eric W. Biederman:

Quoted 9 lines
Quoted 7 lines · Gao feng
Quoted 3 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

This problem is occured after we call setuid/gid.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

for example, a task whose pid is 1234 calls
setregid(10,10);
setreuid(10,10);

If seems to get reset to the right values (0:0) when we execve() the init binary though. This doesn't happen if we have invoked the capset() syscall in between the setregid & the execve() calls.

Quoted 7 lines

Yes, execve() should reset the dumpable state.

I took a quick look and I don't see a way around set_dumpable calls in setup_new_exec. Why the process remains undumpable after exec is worth investigating. That logic should not be user namespace specific however.

I think it's the install_exec_creds, it calls commit_creds to set process undumpable

Quoted 11 lines
        /* dumpability changes */
        if (!uid_eq(old->euid, new->euid) ||
            !gid_eq(old->egid, new->egid) ||
            !uid_eq(old->fsuid, new->fsuid) ||
            !gid_eq(old->fsgid, new->fsgid) ||
            !cred_cap_issubset(old, new)) {
                if (task->mm)
                        set_dumpable(task->mm, suid_dumpable);
                task->pdeath_signal = 0;
                smp_wmb();
        }
Quoted 2 lines

That looks like it could do it. Especially if exec is increasing your capabilities.

Ah, yes, that would explain it. My demo is removing the SYS_MODULE capability, and then exec'ing the shell binary. Since we are uid==0, and prctl(PR_CAPBSET_DROP) is not available inside the user namespace, the rules for capabilities vs execve() call will cause the shell binary to regain SYS_MODULE capability bit.

Quoted 8 lines

So the problem I'm seeing in libvirt is all a result of the fact that we can't use PR_CAPBSET_DROP inside the user namespace. Given that there's no point trying to drop any capabilities inside the user namespace.

The only slight problem here is that we want to drop CAP_MKNOD so that systemd can detect that it shouldn't attempt to run any units which would rely on mknod.

I just looked at that and I don't see a justification for the restriciton.

Could you try the patch below and see if it fixes things for you?

Eric

From: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected]>
Date: Tue, 2 Jul 2013 10:04:54 -0700
Subject: [PATCH] userns: Allow PR_CAPBSET_DROP in a user namespace.

As the capabilites and capability bounding set are per user namespace properties it is safe to allow changing them with just CAP_SETPCAP permission in the user namespace.

---
 security/commoncap.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
security/commoncap.c +1 -1
index 4d787e6..fd9b08f 100644
@@ -843,7 +843,7 @@ int cap_task_setnice(struct task_struct *p, int nice)
  */
 static long cap_prctl_drop(struct cred *new, unsigned long cap)
 {
-	if (!capable(CAP_SETPCAP))
+	if (!ns_capable(current_user_ns(), CAP_SETPCAP))
 		return -EPERM;
 	if (!cap_valid(cap))
 		return -EINVAL;
Signature · 1 line
1.7.5.4

A message that is not in the archive (sent off-list or not yet received): 87k3l8sx6l.fsf-aS9lmoZGLiVWk0Htik3J/[email protected]

#24 lore raw

Am 02.07.2013 19:12, schrieb Eric W. Biederman:

Quoted 63 lines
Quoted 61 lines · Daniel P. Berrange
Quoted 46 lines · Eric W. Biederman
Quoted 32 lines · Gao feng
Quoted 21 lines · Eric W. Biederman
Quoted 11 lines · Daniel P. Berrange
Quoted 10 lines · Richard Weinberger

Am 02.07.2013 10:44, schrieb Eric W. Biederman:

Quoted 9 lines
Quoted 7 lines · Gao feng
Quoted 3 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

This problem is occured after we call setuid/gid.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

for example, a task whose pid is 1234 calls
setregid(10,10);
setreuid(10,10);

If seems to get reset to the right values (0:0) when we execve() the init binary though. This doesn't happen if we have invoked the capset() syscall in between the setregid & the execve() calls.

Quoted 7 lines

Yes, execve() should reset the dumpable state.

I took a quick look and I don't see a way around set_dumpable calls in setup_new_exec. Why the process remains undumpable after exec is worth investigating. That logic should not be user namespace specific however.

I think it's the install_exec_creds, it calls commit_creds to set process undumpable

Quoted 11 lines
        /* dumpability changes */
        if (!uid_eq(old->euid, new->euid) ||
            !gid_eq(old->egid, new->egid) ||
            !uid_eq(old->fsuid, new->fsuid) ||
            !gid_eq(old->fsgid, new->fsgid) ||
            !cred_cap_issubset(old, new)) {
                if (task->mm)
                        set_dumpable(task->mm, suid_dumpable);
                task->pdeath_signal = 0;
                smp_wmb();
        }
Quoted 2 lines

That looks like it could do it. Especially if exec is increasing your capabilities.

Ah, yes, that would explain it. My demo is removing the SYS_MODULE capability, and then exec'ing the shell binary. Since we are uid==0, and prctl(PR_CAPBSET_DROP) is not available inside the user namespace, the rules for capabilities vs execve() call will cause the shell binary to regain SYS_MODULE capability bit.

So the problem I'm seeing in libvirt is all a result of the fact that we can't use PR_CAPBSET_DROP inside the user namespace. Given that there's no point trying to drop any capabilities inside the user namespace.

Quoted 8 lines
Quoted 3 lines

The only slight problem here is that we want to drop CAP_MKNOD so that systemd can detect that it shouldn't attempt to run any units which would rely on mknod.

I just looked at that and I don't see a justification for the restriciton.

Could you try the patch below and see if it fixes things for you?

With the patch applied my test program is able to drop it's caps (using libcap-ng) and does not regain them upon execve. Also reading from /proc/1/environ works. :)

Quoted 7 lines

Eric

From: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected]>
Date: Tue, 2 Jul 2013 10:04:54 -0700
Subject: [PATCH] userns: Allow PR_CAPBSET_DROP in a user namespace.

Quoted 5 lines

As the capabilites and capability bounding set are per user namespace properties it is safe to allow changing them with just CAP_SETPCAP permission in the user namespace.

Quoted 10 lines
---
 security/commoncap.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
security/commoncap.c +0 -0
index 4d787e6..fd9b08f 100644

security/commoncap.c int cap_task_setnice(struct task_struct *p, int nice) · line 849

Quoted 8 lines
security/commoncap.c +1 -1
@@ -843,7 +843,7 @@ int cap_task_setnice(struct task_struct *p, int nice)
  */
 static long cap_prctl_drop(struct cred *new, unsigned long cap)
 {
-	if (!capable(CAP_SETPCAP))
+	if (!ns_capable(current_user_ns(), CAP_SETPCAP))
 		return -EPERM;
 	if (!cap_valid(cap))
 		return -EINVAL;

Thanks,
//richard

#25 lore raw
In reply to a message not in the archive

Am 02.07.2013 19:12, schrieb Eric W. Biederman:

Quoted 94 lines
Quoted 56 lines · Daniel P. Berrange
Quoted 46 lines · Eric W. Biederman
Quoted 32 lines · Gao feng
Quoted 21 lines · Eric W. Biederman
Quoted 11 lines · Daniel P. Berrange
Quoted 10 lines · Richard Weinberger

Am 02.07.2013 10:44, schrieb Eric W. Biederman:

Quoted 9 lines
Quoted 7 lines · Gao feng
Quoted 3 lines · Daniel P. Berrange

I'm struggling debugging a strange problem with interaction between user namespaces, cap_set and ownership of files in /proc/1/

This problem is occured after we call setuid/gid.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

for example, a task whose pid is 1234 calls
setregid(10,10);
setreuid(10,10);

If seems to get reset to the right values (0:0) when we execve() the init binary though. This doesn't happen if we have invoked the capset() syscall in between the setregid & the execve() calls.

Quoted 7 lines

Yes, execve() should reset the dumpable state.

I took a quick look and I don't see a way around set_dumpable calls in setup_new_exec. Why the process remains undumpable after exec is worth investigating. That logic should not be user namespace specific however.

I think it's the install_exec_creds, it calls commit_creds to set process undumpable

Quoted 11 lines
        /* dumpability changes */
        if (!uid_eq(old->euid, new->euid) ||
            !gid_eq(old->egid, new->egid) ||
            !uid_eq(old->fsuid, new->fsuid) ||
            !gid_eq(old->fsgid, new->fsgid) ||
            !cred_cap_issubset(old, new)) {
                if (task->mm)
                        set_dumpable(task->mm, suid_dumpable);
                task->pdeath_signal = 0;
                smp_wmb();
        }
Quoted 2 lines

That looks like it could do it. Especially if exec is increasing your capabilities.

Ah, yes, that would explain it. My demo is removing the SYS_MODULE capability, and then exec'ing the shell binary. Since we are uid==0, and prctl(PR_CAPBSET_DROP) is not available inside the user namespace, the rules for capabilities vs execve() call will cause the shell binary to regain SYS_MODULE capability bit.

Quoted 8 lines

So the problem I'm seeing in libvirt is all a result of the fact that we can't use PR_CAPBSET_DROP inside the user namespace. Given that there's no point trying to drop any capabilities inside the user namespace.

The only slight problem here is that we want to drop CAP_MKNOD so that systemd can detect that it shouldn't attempt to run any units which would rely on mknod.

I just looked at that and I don't see a justification for the restriciton.

Could you try the patch below and see if it fixes things for you?

Eric

From: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected]>
Date: Tue, 2 Jul 2013 10:04:54 -0700
Subject: [PATCH] userns: Allow PR_CAPBSET_DROP in a user namespace.

As the capabilites and capability bounding set are per user namespace properties it is safe to allow changing them with just CAP_SETPCAP permission in the user namespace.

---
 security/commoncap.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
security/commoncap.c +0 -0
index 4d787e6..fd9b08f 100644

security/commoncap.c int cap_task_setnice(struct task_struct *p, int nice) · line 849

Quoted 8 lines
security/commoncap.c +1 -1
@@ -843,7 +843,7 @@ int cap_task_setnice(struct task_struct *p, int nice)
  */
 static long cap_prctl_drop(struct cred *new, unsigned long cap)
 {
-	if (!capable(CAP_SETPCAP))
+	if (!ns_capable(current_user_ns(), CAP_SETPCAP))
 		return -EPERM;
 	if (!cap_valid(cap))
 		return -EINVAL;

Is this fix already on it's way to mainline?

Thanks,
//richard

author Tested-by
#26 lore raw
In reply to a message not in the archive
Quoted 43 lines · Eric W. Biederman
Quoted 6 lines · Daniel P. Berrange

Ah, yes, that would explain it. My demo is removing the SYS_MODULE capability, and then exec'ing the shell binary. Since we are uid==0, and prctl(PR_CAPBSET_DROP) is not available inside the user namespace, the rules for capabilities vs execve() call will cause the shell binary to regain SYS_MODULE capability bit.

Quoted 8 lines

So the problem I'm seeing in libvirt is all a result of the fact that we can't use PR_CAPBSET_DROP inside the user namespace. Given that there's no point trying to drop any capabilities inside the user namespace.

The only slight problem here is that we want to drop CAP_MKNOD so that systemd can detect that it shouldn't attempt to run any units which would rely on mknod.

I just looked at that and I don't see a justification for the restriciton.

Could you try the patch below and see if it fixes things for you?

Eric

From: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected]>
Date: Tue, 2 Jul 2013 10:04:54 -0700
Subject: [PATCH] userns: Allow PR_CAPBSET_DROP in a user namespace.

As the capabilites and capability bounding set are per user namespace properties it is safe to allow changing them with just CAP_SETPCAP permission in the user namespace.

---
 security/commoncap.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
security/commoncap.c +0 -0
index 4d787e6..fd9b08f 100644

security/commoncap.c int cap_task_setnice(struct task_struct *p, int nice) · line 849

Quoted 8 lines
security/commoncap.c +1 -1
@@ -843,7 +843,7 @@ int cap_task_setnice(struct task_struct *p, int nice)
  */
 static long cap_prctl_drop(struct cred *new, unsigned long cap)
 {
-	if (!capable(CAP_SETPCAP))
+	if (!ns_capable(current_user_ns(), CAP_SETPCAP))
 		return -EPERM;
 	if (!cap_valid(cap))
 		return -EINVAL;

Yes, that works in my testing with libvirt. Feel free to add

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|