Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm...
authorLinus Torvalds <torvalds@linux-foundation.org>
Thu, 24 May 2012 00:42:39 +0000 (17:42 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 24 May 2012 00:42:39 +0000 (17:42 -0700)
Pull user namespace enhancements from Eric Biederman:
 "This is a course correction for the user namespace, so that we can
  reach an inexpensive, maintainable, and reasonably complete
  implementation.

  Highlights:
   - Config guards make it impossible to enable the user namespace and
     code that has not been converted to be user namespace safe.

   - Use of the new kuid_t type ensures the if you somehow get past the
     config guards the kernel will encounter type errors if you enable
     user namespaces and attempt to compile in code whose permission
     checks have not been updated to be user namespace safe.

   - All uids from child user namespaces are mapped into the initial
     user namespace before they are processed.  Removing the need to add
     an additional check to see if the user namespace of the compared
     uids remains the same.

   - With the user namespaces compiled out the performance is as good or
     better than it is today.

   - For most operations absolutely nothing changes performance or
     operationally with the user namespace enabled.

   - The worst case performance I could come up with was timing 1
     billion cache cold stat operations with the user namespace code
     enabled.  This went from 156s to 164s on my laptop (or 156ns to
     164ns per stat operation).

   - (uid_t)-1 and (gid_t)-1 are reserved as an internal error value.
     Most uid/gid setting system calls treat these value specially
     anyway so attempting to use -1 as a uid would likely cause
     entertaining failures in userspace.

   - If setuid is called with a uid that can not be mapped setuid fails.
     I have looked at sendmail, login, ssh and every other program I
     could think of that would call setuid and they all check for and
     handle the case where setuid fails.

   - If stat or a similar system call is called from a context in which
     we can not map a uid we lie and return overflowuid.  The LFS
     experience suggests not lying and returning an error code might be
     better, but the historical precedent with uids is different and I
     can not think of anything that would break by lying about a uid we
     can't map.

   - Capabilities are localized to the current user namespace making it
     safe to give the initial user in a user namespace all capabilities.

  My git tree covers all of the modifications needed to convert the core
  kernel and enough changes to make a system bootable to runlevel 1."

Fix up trivial conflicts due to nearby independent changes in fs/stat.c

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace: (46 commits)
  userns:  Silence silly gcc warning.
  cred: use correct cred accessor with regards to rcu read lock
  userns: Convert the move_pages, and migrate_pages permission checks to use uid_eq
  userns: Convert cgroup permission checks to use uid_eq
  userns: Convert tmpfs to use kuid and kgid where appropriate
  userns: Convert sysfs to use kgid/kuid where appropriate
  userns: Convert sysctl permission checks to use kuid and kgids.
  userns: Convert proc to use kuid/kgid where appropriate
  userns: Convert ext4 to user kuid/kgid where appropriate
  userns: Convert ext3 to use kuid/kgid where appropriate
  userns: Convert ext2 to use kuid/kgid where appropriate.
  userns: Convert devpts to use kuid/kgid where appropriate
  userns: Convert binary formats to use kuid/kgid where appropriate
  userns: Add negative depends on entries to avoid building code that is userns unsafe
  userns: signal remove unnecessary map_cred_ns
  userns: Teach inode_capable to understand inodes whose uids map to other namespaces.
  userns: Fail exec for suid and sgid binaries with ids outside our user namespace.
  userns: Convert stat to return values mapped from kuids and kgids
  userns: Convert user specfied uids and gids in chown into kuids and kgid
  userns: Use uid_eq gid_eq helpers when comparing kuids and kgids in the vfs
  ...

30 files changed:
1  2 
arch/x86/ia32/sys_ia32.c
fs/binfmt_elf.c
fs/binfmt_elf_fdpic.c
fs/exec.c
fs/ext4/ext4.h
fs/ext4/super.c
fs/locks.c
fs/namei.c
fs/open.c
fs/proc/base.c
fs/proc/root.c
fs/stat.c
include/linux/capability.h
include/linux/fs.h
include/linux/sched.h
init/Kconfig
kernel/cgroup.c
kernel/cred.c
kernel/sched/core.c
kernel/signal.c
kernel/sys.c
kernel/timer.c
mm/mempolicy.c
mm/migrate.c
net/core/sock.c
net/ipv4/ping.c
security/commoncap.c
security/keys/key.c
security/keys/permission.c
security/keys/process_keys.c

Simple merge
diff --cc fs/binfmt_elf.c
Simple merge
Simple merge
diff --cc fs/exec.c
index 1e8efdc80412794d0e566db69783f2a310408aa6,e001bdfac5306391a74ded9ea0a5ac4efd856892..52c9e2ff6e6bd8b6f763e56ceafda431731cea9b
+++ b/fs/exec.c
@@@ -1295,12 -1288,14 +1295,15 @@@ int prepare_binprm(struct linux_binprm 
        bprm->cred->euid = current_euid();
        bprm->cred->egid = current_egid();
  
 -      if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
 +      if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) &&
 +          !current->no_new_privs) {
                /* Set-uid? */
                if (mode & S_ISUID) {
+                       if (!kuid_has_mapping(bprm->cred->user_ns, inode->i_uid))
+                               return -EPERM;
                        bprm->per_clear |= PER_CLEAR_ON_SETID;
                        bprm->cred->euid = inode->i_uid;
                }
  
                /* Set-gid? */
diff --cc fs/ext4/ext4.h
Simple merge
diff --cc fs/ext4/super.c
index e1fb1d5de58eab4150792974f9784751f4638557,9d8eba0de27d02f69dc45e71227ce2ea782f24b1..436b4223df66a889dc4e8f685cc89195afec8650
@@@ -1448,18 -1448,10 +1448,20 @@@ static int handle_mount_opt(struct supe
  {
        struct ext4_sb_info *sbi = EXT4_SB(sb);
        const struct mount_opts *m;
+       kuid_t uid;
+       kgid_t gid;
        int arg = 0;
  
 +#ifdef CONFIG_QUOTA
 +      if (token == Opt_usrjquota)
 +              return set_qf_name(sb, USRQUOTA, &args[0]);
 +      else if (token == Opt_grpjquota)
 +              return set_qf_name(sb, GRPQUOTA, &args[0]);
 +      else if (token == Opt_offusrjquota)
 +              return clear_qf_name(sb, USRQUOTA);
 +      else if (token == Opt_offgrpjquota)
 +              return clear_qf_name(sb, GRPQUOTA);
 +#endif
        if (args->from && match_int(args, &arg))
                return -1;
        switch (token) {
diff --cc fs/locks.c
Simple merge
diff --cc fs/namei.c
Simple merge
diff --cc fs/open.c
Simple merge
diff --cc fs/proc/base.c
index 57b8159f26f328e4fdd6308ef1b656a587cf7a28,c47904994b78248fc1984914eab3b3c382952139..d2d3108a611c8cf96b6d1aa275270a3929556ccf
@@@ -1815,17 -1811,10 +1816,17 @@@ static int tid_fd_revalidate(struct den
                                        inode->i_gid = cred->egid;
                                        rcu_read_unlock();
                                } else {
-                                       inode->i_uid = 0;
-                                       inode->i_gid = 0;
+                                       inode->i_uid = GLOBAL_ROOT_UID;
+                                       inode->i_gid = GLOBAL_ROOT_GID;
                                }
 -                              inode->i_mode &= ~(S_ISUID | S_ISGID);
 +
 +                              i_mode = S_IFLNK;
 +                              if (f_mode & FMODE_READ)
 +                                      i_mode |= S_IRUSR | S_IXUSR;
 +                              if (f_mode & FMODE_WRITE)
 +                                      i_mode |= S_IWUSR | S_IXUSR;
 +                              inode->i_mode = i_mode;
 +
                                security_task_to_inode(task, inode);
                                put_task_struct(task);
                                return 1;
diff --cc fs/proc/root.c
Simple merge
diff --cc fs/stat.c
index 0cef3366a919db83f7e57b4b44be81d755ded16e,31acca5f5a0cd55152acae75f381f9337d8a9afa..b6ff11825fc8a9c37f8d45ccf01e1fbdc1115868
+++ b/fs/stat.c
@@@ -224,9 -215,17 +224,9 @@@ static int cp_new_stat(struct kstat *st
        tmp.st_nlink = stat->nlink;
        if (tmp.st_nlink != stat->nlink)
                return -EOVERFLOW;
-       SET_UID(tmp.st_uid, stat->uid);
-       SET_GID(tmp.st_gid, stat->gid);
+       SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
+       SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
 -#if BITS_PER_LONG == 32
 -      tmp.st_rdev = old_encode_dev(stat->rdev);
 -#else
 -      tmp.st_rdev = new_encode_dev(stat->rdev);
 -#endif
 -#if BITS_PER_LONG == 32
 -      if (stat->size > MAX_NON_LFS)
 -              return -EOVERFLOW;
 -#endif        
 +      tmp.st_rdev = encode_dev(stat->rdev);
        tmp.st_size = stat->size;
        tmp.st_atime = stat->atime.tv_sec;
        tmp.st_mtime = stat->mtime.tv_sec;
Simple merge
Simple merge
Simple merge
diff --cc init/Kconfig
Simple merge
diff --cc kernel/cgroup.c
Simple merge
diff --cc kernel/cred.c
Simple merge
Simple merge
diff --cc kernel/signal.c
Simple merge
diff --cc kernel/sys.c
Simple merge
diff --cc kernel/timer.c
Simple merge
diff --cc mm/mempolicy.c
Simple merge
diff --cc mm/migrate.c
Simple merge
diff --cc net/core/sock.c
Simple merge
diff --cc net/ipv4/ping.c
Simple merge
index f80d116093915acb906b7bd80a9424d5d1f98f0b,ff9b113bb07cdb9a32328aa6f4cd9c3f4a2d792e..e771cb1b2d7947f0c85651b38cc7c9c1d3da11d7
@@@ -506,18 -508,11 +509,18 @@@ int cap_bprm_set_creds(struct linux_bin
        }
  skip:
  
 +      /* if we have fs caps, clear dangerous personality flags */
 +      if (!cap_issubset(new->cap_permitted, old->cap_permitted))
 +              bprm->per_clear |= PER_CLEAR_ON_SETID;
 +
 +
        /* Don't let someone trace a set[ug]id/setpcap binary with the revised
 -       * credentials unless they have the appropriate permit
 +       * credentials unless they have the appropriate permit.
 +       *
 +       * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
         */
-       if ((new->euid != old->uid ||
-            new->egid != old->gid ||
+       if ((!uid_eq(new->euid, old->uid) ||
+            !gid_eq(new->egid, old->gid) ||
             !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
            bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
                /* downgrade; they get no more than they had, and maybe less */
Simple merge
Simple merge
Simple merge