2007-07-16 14:40:59 +08:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation, version 2 of the
|
|
|
|
* License.
|
|
|
|
*/
|
|
|
|
|
2011-05-24 02:51:41 +08:00
|
|
|
#include <linux/export.h>
|
2007-07-16 14:40:59 +08:00
|
|
|
#include <linux/nsproxy.h>
|
2008-04-29 15:59:25 +08:00
|
|
|
#include <linux/slab.h>
|
2007-07-16 14:40:59 +08:00
|
|
|
#include <linux/user_namespace.h>
|
2013-04-12 08:50:06 +08:00
|
|
|
#include <linux/proc_ns.h>
|
2010-06-13 11:28:03 +08:00
|
|
|
#include <linux/highuid.h>
|
2008-10-16 05:38:45 +08:00
|
|
|
#include <linux/cred.h>
|
2011-11-17 17:59:07 +08:00
|
|
|
#include <linux/securebits.h>
|
2011-11-17 16:11:58 +08:00
|
|
|
#include <linux/keyctl.h>
|
|
|
|
#include <linux/key-type.h>
|
|
|
|
#include <keys/user-type.h>
|
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/ctype.h>
|
2012-08-30 16:24:05 +08:00
|
|
|
#include <linux/projid.h>
|
2013-03-14 02:51:49 +08:00
|
|
|
#include <linux/fs_struct.h>
|
2007-07-16 14:40:59 +08:00
|
|
|
|
2011-01-13 09:00:46 +08:00
|
|
|
static struct kmem_cache *user_ns_cachep __read_mostly;
|
2014-12-10 04:03:14 +08:00
|
|
|
static DEFINE_MUTEX(userns_state_mutex);
|
2011-01-13 09:00:46 +08:00
|
|
|
|
2013-04-15 04:47:02 +08:00
|
|
|
static bool new_idmap_permitted(const struct file *file,
|
|
|
|
struct user_namespace *ns, int cap_setid,
|
2011-11-17 16:11:58 +08:00
|
|
|
struct uid_gid_map *map);
|
2016-07-31 02:53:37 +08:00
|
|
|
static void free_user_ns(struct work_struct *work);
|
2011-11-17 16:11:58 +08:00
|
|
|
|
2012-07-26 21:24:06 +08:00
|
|
|
static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
|
|
|
|
{
|
|
|
|
/* Start with the same capabilities as init but useless for doing
|
|
|
|
* anything as the capabilities are bound to the new user namespace.
|
|
|
|
*/
|
|
|
|
cred->securebits = SECUREBITS_DEFAULT;
|
|
|
|
cred->cap_inheritable = CAP_EMPTY_SET;
|
|
|
|
cred->cap_permitted = CAP_FULL_SET;
|
|
|
|
cred->cap_effective = CAP_FULL_SET;
|
capabilities: ambient capabilities
Credit where credit is due: this idea comes from Christoph Lameter with
a lot of valuable input from Serge Hallyn. This patch is heavily based
on Christoph's patch.
===== The status quo =====
On Linux, there are a number of capabilities defined by the kernel. To
perform various privileged tasks, processes can wield capabilities that
they hold.
Each task has four capability masks: effective (pE), permitted (pP),
inheritable (pI), and a bounding set (X). When the kernel checks for a
capability, it checks pE. The other capability masks serve to modify
what capabilities can be in pE.
Any task can remove capabilities from pE, pP, or pI at any time. If a
task has a capability in pP, it can add that capability to pE and/or pI.
If a task has CAP_SETPCAP, then it can add any capability to pI, and it
can remove capabilities from X.
Tasks are not the only things that can have capabilities; files can also
have capabilities. A file can have no capabilty information at all [1].
If a file has capability information, then it has a permitted mask (fP)
and an inheritable mask (fI) as well as a single effective bit (fE) [2].
File capabilities modify the capabilities of tasks that execve(2) them.
A task that successfully calls execve has its capabilities modified for
the file ultimately being excecuted (i.e. the binary itself if that
binary is ELF or for the interpreter if the binary is a script.) [3] In
the capability evolution rules, for each mask Z, pZ represents the old
value and pZ' represents the new value. The rules are:
pP' = (X & fP) | (pI & fI)
pI' = pI
pE' = (fE ? pP' : 0)
X is unchanged
For setuid binaries, fP, fI, and fE are modified by a moderately
complicated set of rules that emulate POSIX behavior. Similarly, if
euid == 0 or ruid == 0, then fP, fI, and fE are modified differently
(primary, fP and fI usually end up being the full set). For nonroot
users executing binaries with neither setuid nor file caps, fI and fP
are empty and fE is false.
As an extra complication, if you execute a process as nonroot and fE is
set, then the "secure exec" rules are in effect: AT_SECURE gets set,
LD_PRELOAD doesn't work, etc.
This is rather messy. We've learned that making any changes is
dangerous, though: if a new kernel version allows an unprivileged
program to change its security state in a way that persists cross
execution of a setuid program or a program with file caps, this
persistent state is surprisingly likely to allow setuid or file-capped
programs to be exploited for privilege escalation.
===== The problem =====
Capability inheritance is basically useless.
If you aren't root and you execute an ordinary binary, fI is zero, so
your capabilities have no effect whatsoever on pP'. This means that you
can't usefully execute a helper process or a shell command with elevated
capabilities if you aren't root.
On current kernels, you can sort of work around this by setting fI to
the full set for most or all non-setuid executable files. This causes
pP' = pI for nonroot, and inheritance works. No one does this because
it's a PITA and it isn't even supported on most filesystems.
If you try this, you'll discover that every nonroot program ends up with
secure exec rules, breaking many things.
This is a problem that has bitten many people who have tried to use
capabilities for anything useful.
===== The proposed change =====
This patch adds a fifth capability mask called the ambient mask (pA).
pA does what most people expect pI to do.
pA obeys the invariant that no bit can ever be set in pA if it is not
set in both pP and pI. Dropping a bit from pP or pI drops that bit from
pA. This ensures that existing programs that try to drop capabilities
still do so, with a complication. Because capability inheritance is so
broken, setting KEEPCAPS, using setresuid to switch to nonroot uids, and
then calling execve effectively drops capabilities. Therefore,
setresuid from root to nonroot conditionally clears pA unless
SECBIT_NO_SETUID_FIXUP is set. Processes that don't like this can
re-add bits to pA afterwards.
The capability evolution rules are changed:
pA' = (file caps or setuid or setgid ? 0 : pA)
pP' = (X & fP) | (pI & fI) | pA'
pI' = pI
pE' = (fE ? pP' : pA')
X is unchanged
If you are nonroot but you have a capability, you can add it to pA. If
you do so, your children get that capability in pA, pP, and pE. For
example, you can set pA = CAP_NET_BIND_SERVICE, and your children can
automatically bind low-numbered ports. Hallelujah!
Unprivileged users can create user namespaces, map themselves to a
nonzero uid, and create both privileged (relative to their namespace)
and unprivileged process trees. This is currently more or less
impossible. Hallelujah!
You cannot use pA to try to subvert a setuid, setgid, or file-capped
program: if you execute any such program, pA gets cleared and the
resulting evolution rules are unchanged by this patch.
Users with nonzero pA are unlikely to unintentionally leak that
capability. If they run programs that try to drop privileges, dropping
privileges will still work.
It's worth noting that the degree of paranoia in this patch could
possibly be reduced without causing serious problems. Specifically, if
we allowed pA to persist across executing non-pA-aware setuid binaries
and across setresuid, then, naively, the only capabilities that could
leak as a result would be the capabilities in pA, and any attacker
*already* has those capabilities. This would make me nervous, though --
setuid binaries that tried to privilege-separate might fail to do so,
and putting CAP_DAC_READ_SEARCH or CAP_DAC_OVERRIDE into pA could have
unexpected side effects. (Whether these unexpected side effects would
be exploitable is an open question.) I've therefore taken the more
paranoid route. We can revisit this later.
An alternative would be to require PR_SET_NO_NEW_PRIVS before setting
ambient capabilities. I think that this would be annoying and would
make granting otherwise unprivileged users minor ambient capabilities
(CAP_NET_BIND_SERVICE or CAP_NET_RAW for example) much less useful than
it is with this patch.
===== Footnotes =====
[1] Files that are missing the "security.capability" xattr or that have
unrecognized values for that xattr end up with has_cap set to false.
The code that does that appears to be complicated for no good reason.
[2] The libcap capability mask parsers and formatters are dangerously
misleading and the documentation is flat-out wrong. fE is *not* a mask;
it's a single bit. This has probably confused every single person who
has tried to use file capabilities.
[3] Linux very confusingly processes both the script and the interpreter
if applicable, for reasons that elude me. The results from thinking
about a script's file capabilities and/or setuid bits are mostly
discarded.
Preliminary userspace code is here, but it needs updating:
https://git.kernel.org/cgit/linux/kernel/git/luto/util-linux-playground.git/commit/?h=cap_ambient&id=7f5afbd175d2
Here is a test program that can be used to verify the functionality
(from Christoph):
/*
* Test program for the ambient capabilities. This program spawns a shell
* that allows running processes with a defined set of capabilities.
*
* (C) 2015 Christoph Lameter <cl@linux.com>
* Released under: GPL v3 or later.
*
*
* Compile using:
*
* gcc -o ambient_test ambient_test.o -lcap-ng
*
* This program must have the following capabilities to run properly:
* Permissions for CAP_NET_RAW, CAP_NET_ADMIN, CAP_SYS_NICE
*
* A command to equip the binary with the right caps is:
*
* setcap cap_net_raw,cap_net_admin,cap_sys_nice+p ambient_test
*
*
* To get a shell with additional caps that can be inherited by other processes:
*
* ./ambient_test /bin/bash
*
*
* Verifying that it works:
*
* From the bash spawed by ambient_test run
*
* cat /proc/$$/status
*
* and have a look at the capabilities.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <cap-ng.h>
#include <sys/prctl.h>
#include <linux/capability.h>
/*
* Definitions from the kernel header files. These are going to be removed
* when the /usr/include files have these defined.
*/
#define PR_CAP_AMBIENT 47
#define PR_CAP_AMBIENT_IS_SET 1
#define PR_CAP_AMBIENT_RAISE 2
#define PR_CAP_AMBIENT_LOWER 3
#define PR_CAP_AMBIENT_CLEAR_ALL 4
static void set_ambient_cap(int cap)
{
int rc;
capng_get_caps_process();
rc = capng_update(CAPNG_ADD, CAPNG_INHERITABLE, cap);
if (rc) {
printf("Cannot add inheritable cap\n");
exit(2);
}
capng_apply(CAPNG_SELECT_CAPS);
/* Note the two 0s at the end. Kernel checks for these */
if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, cap, 0, 0)) {
perror("Cannot set cap");
exit(1);
}
}
int main(int argc, char **argv)
{
int rc;
set_ambient_cap(CAP_NET_RAW);
set_ambient_cap(CAP_NET_ADMIN);
set_ambient_cap(CAP_SYS_NICE);
printf("Ambient_test forking shell\n");
if (execv(argv[1], argv + 1))
perror("Cannot exec");
return 0;
}
Signed-off-by: Christoph Lameter <cl@linux.com> # Original author
Signed-off-by: Andy Lutomirski <luto@kernel.org>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Kees Cook <keescook@chromium.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Aaron Jones <aaronmdjones@gmail.com>
Cc: Ted Ts'o <tytso@mit.edu>
Cc: Andrew G. Morgan <morgan@kernel.org>
Cc: Mimi Zohar <zohar@linux.vnet.ibm.com>
Cc: Austin S Hemmelgarn <ahferroin7@gmail.com>
Cc: Markku Savela <msa@moth.iki.fi>
Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Cc: James Morris <james.l.morris@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2015-09-05 06:42:45 +08:00
|
|
|
cred->cap_ambient = CAP_EMPTY_SET;
|
2012-07-26 21:24:06 +08:00
|
|
|
cred->cap_bset = CAP_FULL_SET;
|
|
|
|
#ifdef CONFIG_KEYS
|
|
|
|
key_put(cred->request_key_auth);
|
|
|
|
cred->request_key_auth = NULL;
|
|
|
|
#endif
|
|
|
|
/* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
|
|
|
|
cred->user_ns = user_ns;
|
|
|
|
}
|
|
|
|
|
2007-07-16 14:41:01 +08:00
|
|
|
/*
|
2008-10-16 05:38:45 +08:00
|
|
|
* Create a new user namespace, deriving the creator from the user in the
|
|
|
|
* passed credentials, and replacing that user with the new root user for the
|
|
|
|
* new namespace.
|
|
|
|
*
|
|
|
|
* This is called by copy_creds(), which will finish setting the target task's
|
|
|
|
* credentials.
|
2007-07-16 14:41:01 +08:00
|
|
|
*/
|
2008-10-16 05:38:45 +08:00
|
|
|
int create_user_ns(struct cred *new)
|
2007-07-16 14:41:01 +08:00
|
|
|
{
|
2011-11-17 13:52:53 +08:00
|
|
|
struct user_namespace *ns, *parent_ns = new->user_ns;
|
2012-02-08 23:00:08 +08:00
|
|
|
kuid_t owner = new->euid;
|
|
|
|
kgid_t group = new->egid;
|
2011-06-16 01:21:48 +08:00
|
|
|
int ret;
|
2011-11-17 17:32:59 +08:00
|
|
|
|
2013-08-09 00:55:32 +08:00
|
|
|
if (parent_ns->level > 32)
|
|
|
|
return -EUSERS;
|
|
|
|
|
2013-03-15 16:45:51 +08:00
|
|
|
/*
|
|
|
|
* Verify that we can not violate the policy of which files
|
|
|
|
* may be accessed that is specified by the root directory,
|
|
|
|
* by verifing that the root directory is at the root of the
|
|
|
|
* mount namespace which allows all files to be accessed.
|
|
|
|
*/
|
|
|
|
if (current_chrooted())
|
|
|
|
return -EPERM;
|
|
|
|
|
2011-11-17 17:32:59 +08:00
|
|
|
/* The creator needs a mapping in the parent user namespace
|
|
|
|
* or else we won't be able to reasonably tell userspace who
|
|
|
|
* created a user_namespace.
|
|
|
|
*/
|
|
|
|
if (!kuid_has_mapping(parent_ns, owner) ||
|
|
|
|
!kgid_has_mapping(parent_ns, group))
|
|
|
|
return -EPERM;
|
2007-07-16 14:41:01 +08:00
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
|
2007-07-16 14:41:01 +08:00
|
|
|
if (!ns)
|
2008-10-16 05:38:45 +08:00
|
|
|
return -ENOMEM;
|
2007-07-16 14:41:01 +08:00
|
|
|
|
2014-11-01 12:45:45 +08:00
|
|
|
ret = ns_alloc_inum(&ns->ns);
|
2011-06-16 01:21:48 +08:00
|
|
|
if (ret) {
|
|
|
|
kmem_cache_free(user_ns_cachep, ns);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-01 14:32:53 +08:00
|
|
|
ns->ns.ops = &userns_operations;
|
2011-06-16 01:21:48 +08:00
|
|
|
|
2012-12-29 10:58:39 +08:00
|
|
|
atomic_set(&ns->count, 1);
|
2012-07-26 21:24:06 +08:00
|
|
|
/* Leave the new->user_ns reference with the new user namespace. */
|
2011-11-17 13:59:43 +08:00
|
|
|
ns->parent = parent_ns;
|
2013-08-09 00:55:32 +08:00
|
|
|
ns->level = parent_ns->level + 1;
|
2011-11-17 17:32:59 +08:00
|
|
|
ns->owner = owner;
|
|
|
|
ns->group = group;
|
2016-07-31 02:53:37 +08:00
|
|
|
INIT_WORK(&ns->work, free_user_ns);
|
2011-11-17 16:11:58 +08:00
|
|
|
|
2014-12-03 02:27:26 +08:00
|
|
|
/* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
|
|
|
|
mutex_lock(&userns_state_mutex);
|
|
|
|
ns->flags = parent_ns->flags;
|
|
|
|
mutex_unlock(&userns_state_mutex);
|
|
|
|
|
2012-07-26 21:24:06 +08:00
|
|
|
set_cred_user_ns(new, ns);
|
2011-11-17 13:52:53 +08:00
|
|
|
|
2013-09-24 17:35:19 +08:00
|
|
|
#ifdef CONFIG_PERSISTENT_KEYRINGS
|
|
|
|
init_rwsem(&ns->persistent_keyring_register_sem);
|
|
|
|
#endif
|
2008-10-16 05:38:45 +08:00
|
|
|
return 0;
|
2007-07-16 14:40:59 +08:00
|
|
|
}
|
|
|
|
|
2012-07-26 20:15:35 +08:00
|
|
|
int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
|
|
|
|
{
|
|
|
|
struct cred *cred;
|
2013-08-07 01:38:55 +08:00
|
|
|
int err = -ENOMEM;
|
2012-07-26 20:15:35 +08:00
|
|
|
|
|
|
|
if (!(unshare_flags & CLONE_NEWUSER))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
cred = prepare_creds();
|
2013-08-07 01:38:55 +08:00
|
|
|
if (cred) {
|
|
|
|
err = create_user_ns(cred);
|
|
|
|
if (err)
|
|
|
|
put_cred(cred);
|
|
|
|
else
|
|
|
|
*new_cred = cred;
|
|
|
|
}
|
2012-07-26 20:15:35 +08:00
|
|
|
|
2013-08-07 01:38:55 +08:00
|
|
|
return err;
|
2012-07-26 20:15:35 +08:00
|
|
|
}
|
|
|
|
|
2016-07-31 02:53:37 +08:00
|
|
|
static void free_user_ns(struct work_struct *work)
|
2007-07-16 14:40:59 +08:00
|
|
|
{
|
2016-07-31 02:53:37 +08:00
|
|
|
struct user_namespace *parent, *ns =
|
|
|
|
container_of(work, struct user_namespace, work);
|
2011-11-17 17:32:59 +08:00
|
|
|
|
2012-12-29 10:58:39 +08:00
|
|
|
do {
|
|
|
|
parent = ns->parent;
|
2013-09-24 17:35:19 +08:00
|
|
|
#ifdef CONFIG_PERSISTENT_KEYRINGS
|
|
|
|
key_put(ns->persistent_keyring_register);
|
|
|
|
#endif
|
2014-11-01 12:45:45 +08:00
|
|
|
ns_free_inum(&ns->ns);
|
2012-12-29 10:58:39 +08:00
|
|
|
kmem_cache_free(user_ns_cachep, ns);
|
|
|
|
ns = parent;
|
|
|
|
} while (atomic_dec_and_test(&parent->count));
|
2007-07-16 14:40:59 +08:00
|
|
|
}
|
2016-07-31 02:53:37 +08:00
|
|
|
|
|
|
|
void __put_user_ns(struct user_namespace *ns)
|
|
|
|
{
|
|
|
|
schedule_work(&ns->work);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__put_user_ns);
|
2010-06-13 11:28:03 +08:00
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
|
2010-06-13 11:28:03 +08:00
|
|
|
{
|
2011-11-17 16:11:58 +08:00
|
|
|
unsigned idx, extents;
|
|
|
|
u32 first, last, id2;
|
2010-06-13 11:28:03 +08:00
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
id2 = id + count - 1;
|
2010-06-13 11:28:03 +08:00
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
/* Find the matching extent */
|
|
|
|
extents = map->nr_extents;
|
2014-04-15 04:58:55 +08:00
|
|
|
smp_rmb();
|
2011-11-17 16:11:58 +08:00
|
|
|
for (idx = 0; idx < extents; idx++) {
|
|
|
|
first = map->extent[idx].first;
|
|
|
|
last = first + map->extent[idx].count - 1;
|
|
|
|
if (id >= first && id <= last &&
|
|
|
|
(id2 >= first && id2 <= last))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Map the id or note failure */
|
|
|
|
if (idx < extents)
|
|
|
|
id = (id - first) + map->extent[idx].lower_first;
|
|
|
|
else
|
|
|
|
id = (u32) -1;
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 map_id_down(struct uid_gid_map *map, u32 id)
|
|
|
|
{
|
|
|
|
unsigned idx, extents;
|
|
|
|
u32 first, last;
|
|
|
|
|
|
|
|
/* Find the matching extent */
|
|
|
|
extents = map->nr_extents;
|
2014-04-15 04:58:55 +08:00
|
|
|
smp_rmb();
|
2011-11-17 16:11:58 +08:00
|
|
|
for (idx = 0; idx < extents; idx++) {
|
|
|
|
first = map->extent[idx].first;
|
|
|
|
last = first + map->extent[idx].count - 1;
|
|
|
|
if (id >= first && id <= last)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Map the id or note failure */
|
|
|
|
if (idx < extents)
|
|
|
|
id = (id - first) + map->extent[idx].lower_first;
|
|
|
|
else
|
|
|
|
id = (u32) -1;
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 map_id_up(struct uid_gid_map *map, u32 id)
|
|
|
|
{
|
|
|
|
unsigned idx, extents;
|
|
|
|
u32 first, last;
|
|
|
|
|
|
|
|
/* Find the matching extent */
|
|
|
|
extents = map->nr_extents;
|
2014-04-15 04:58:55 +08:00
|
|
|
smp_rmb();
|
2011-11-17 16:11:58 +08:00
|
|
|
for (idx = 0; idx < extents; idx++) {
|
|
|
|
first = map->extent[idx].lower_first;
|
|
|
|
last = first + map->extent[idx].count - 1;
|
|
|
|
if (id >= first && id <= last)
|
|
|
|
break;
|
2010-06-13 11:28:03 +08:00
|
|
|
}
|
2011-11-17 16:11:58 +08:00
|
|
|
/* Map the id or note failure */
|
|
|
|
if (idx < extents)
|
|
|
|
id = (id - first) + map->extent[idx].first;
|
|
|
|
else
|
|
|
|
id = (u32) -1;
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* make_kuid - Map a user-namespace uid pair into a kuid.
|
|
|
|
* @ns: User namespace that the uid is in
|
|
|
|
* @uid: User identifier
|
|
|
|
*
|
|
|
|
* Maps a user-namespace uid pair into a kernel internal kuid,
|
|
|
|
* and returns that kuid.
|
|
|
|
*
|
|
|
|
* When there is no mapping defined for the user-namespace uid
|
|
|
|
* pair INVALID_UID is returned. Callers are expected to test
|
2014-02-17 11:58:12 +08:00
|
|
|
* for and handle INVALID_UID being returned. INVALID_UID
|
2011-11-17 16:11:58 +08:00
|
|
|
* may be tested for using uid_valid().
|
|
|
|
*/
|
|
|
|
kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
|
|
|
|
{
|
|
|
|
/* Map the uid to a global kernel uid */
|
|
|
|
return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(make_kuid);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* from_kuid - Create a uid from a kuid user-namespace pair.
|
|
|
|
* @targ: The user namespace we want a uid in.
|
|
|
|
* @kuid: The kernel internal uid to start with.
|
|
|
|
*
|
|
|
|
* Map @kuid into the user-namespace specified by @targ and
|
|
|
|
* return the resulting uid.
|
|
|
|
*
|
|
|
|
* There is always a mapping into the initial user_namespace.
|
|
|
|
*
|
|
|
|
* If @kuid has no mapping in @targ (uid_t)-1 is returned.
|
|
|
|
*/
|
|
|
|
uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
|
|
|
|
{
|
|
|
|
/* Map the uid from a global kernel uid */
|
|
|
|
return map_id_up(&targ->uid_map, __kuid_val(kuid));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(from_kuid);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* from_kuid_munged - Create a uid from a kuid user-namespace pair.
|
|
|
|
* @targ: The user namespace we want a uid in.
|
|
|
|
* @kuid: The kernel internal uid to start with.
|
|
|
|
*
|
|
|
|
* Map @kuid into the user-namespace specified by @targ and
|
|
|
|
* return the resulting uid.
|
|
|
|
*
|
|
|
|
* There is always a mapping into the initial user_namespace.
|
|
|
|
*
|
|
|
|
* Unlike from_kuid from_kuid_munged never fails and always
|
|
|
|
* returns a valid uid. This makes from_kuid_munged appropriate
|
|
|
|
* for use in syscalls like stat and getuid where failing the
|
|
|
|
* system call and failing to provide a valid uid are not an
|
|
|
|
* options.
|
|
|
|
*
|
|
|
|
* If @kuid has no mapping in @targ overflowuid is returned.
|
|
|
|
*/
|
|
|
|
uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
|
|
|
|
{
|
|
|
|
uid_t uid;
|
|
|
|
uid = from_kuid(targ, kuid);
|
|
|
|
|
|
|
|
if (uid == (uid_t) -1)
|
|
|
|
uid = overflowuid;
|
|
|
|
return uid;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(from_kuid_munged);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* make_kgid - Map a user-namespace gid pair into a kgid.
|
|
|
|
* @ns: User namespace that the gid is in
|
2014-06-07 05:37:21 +08:00
|
|
|
* @gid: group identifier
|
2011-11-17 16:11:58 +08:00
|
|
|
*
|
|
|
|
* Maps a user-namespace gid pair into a kernel internal kgid,
|
|
|
|
* and returns that kgid.
|
|
|
|
*
|
|
|
|
* When there is no mapping defined for the user-namespace gid
|
|
|
|
* pair INVALID_GID is returned. Callers are expected to test
|
|
|
|
* for and handle INVALID_GID being returned. INVALID_GID may be
|
|
|
|
* tested for using gid_valid().
|
|
|
|
*/
|
|
|
|
kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
|
|
|
|
{
|
|
|
|
/* Map the gid to a global kernel gid */
|
|
|
|
return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(make_kgid);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* from_kgid - Create a gid from a kgid user-namespace pair.
|
|
|
|
* @targ: The user namespace we want a gid in.
|
|
|
|
* @kgid: The kernel internal gid to start with.
|
|
|
|
*
|
|
|
|
* Map @kgid into the user-namespace specified by @targ and
|
|
|
|
* return the resulting gid.
|
|
|
|
*
|
|
|
|
* There is always a mapping into the initial user_namespace.
|
|
|
|
*
|
|
|
|
* If @kgid has no mapping in @targ (gid_t)-1 is returned.
|
|
|
|
*/
|
|
|
|
gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
|
|
|
|
{
|
|
|
|
/* Map the gid from a global kernel gid */
|
|
|
|
return map_id_up(&targ->gid_map, __kgid_val(kgid));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(from_kgid);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* from_kgid_munged - Create a gid from a kgid user-namespace pair.
|
|
|
|
* @targ: The user namespace we want a gid in.
|
|
|
|
* @kgid: The kernel internal gid to start with.
|
|
|
|
*
|
|
|
|
* Map @kgid into the user-namespace specified by @targ and
|
|
|
|
* return the resulting gid.
|
|
|
|
*
|
|
|
|
* There is always a mapping into the initial user_namespace.
|
|
|
|
*
|
|
|
|
* Unlike from_kgid from_kgid_munged never fails and always
|
|
|
|
* returns a valid gid. This makes from_kgid_munged appropriate
|
|
|
|
* for use in syscalls like stat and getgid where failing the
|
|
|
|
* system call and failing to provide a valid gid are not options.
|
|
|
|
*
|
|
|
|
* If @kgid has no mapping in @targ overflowgid is returned.
|
|
|
|
*/
|
|
|
|
gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
|
|
|
|
{
|
|
|
|
gid_t gid;
|
|
|
|
gid = from_kgid(targ, kgid);
|
|
|
|
|
|
|
|
if (gid == (gid_t) -1)
|
|
|
|
gid = overflowgid;
|
|
|
|
return gid;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(from_kgid_munged);
|
|
|
|
|
2012-08-30 16:24:05 +08:00
|
|
|
/**
|
|
|
|
* make_kprojid - Map a user-namespace projid pair into a kprojid.
|
|
|
|
* @ns: User namespace that the projid is in
|
|
|
|
* @projid: Project identifier
|
|
|
|
*
|
|
|
|
* Maps a user-namespace uid pair into a kernel internal kuid,
|
|
|
|
* and returns that kuid.
|
|
|
|
*
|
|
|
|
* When there is no mapping defined for the user-namespace projid
|
|
|
|
* pair INVALID_PROJID is returned. Callers are expected to test
|
|
|
|
* for and handle handle INVALID_PROJID being returned. INVALID_PROJID
|
|
|
|
* may be tested for using projid_valid().
|
|
|
|
*/
|
|
|
|
kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
|
|
|
|
{
|
|
|
|
/* Map the uid to a global kernel uid */
|
|
|
|
return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(make_kprojid);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* from_kprojid - Create a projid from a kprojid user-namespace pair.
|
|
|
|
* @targ: The user namespace we want a projid in.
|
|
|
|
* @kprojid: The kernel internal project identifier to start with.
|
|
|
|
*
|
|
|
|
* Map @kprojid into the user-namespace specified by @targ and
|
|
|
|
* return the resulting projid.
|
|
|
|
*
|
|
|
|
* There is always a mapping into the initial user_namespace.
|
|
|
|
*
|
|
|
|
* If @kprojid has no mapping in @targ (projid_t)-1 is returned.
|
|
|
|
*/
|
|
|
|
projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
|
|
|
|
{
|
|
|
|
/* Map the uid from a global kernel uid */
|
|
|
|
return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(from_kprojid);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
|
|
|
|
* @targ: The user namespace we want a projid in.
|
|
|
|
* @kprojid: The kernel internal projid to start with.
|
|
|
|
*
|
|
|
|
* Map @kprojid into the user-namespace specified by @targ and
|
|
|
|
* return the resulting projid.
|
|
|
|
*
|
|
|
|
* There is always a mapping into the initial user_namespace.
|
|
|
|
*
|
|
|
|
* Unlike from_kprojid from_kprojid_munged never fails and always
|
|
|
|
* returns a valid projid. This makes from_kprojid_munged
|
|
|
|
* appropriate for use in syscalls like stat and where
|
|
|
|
* failing the system call and failing to provide a valid projid are
|
|
|
|
* not an options.
|
|
|
|
*
|
|
|
|
* If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
|
|
|
|
*/
|
|
|
|
projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
|
|
|
|
{
|
|
|
|
projid_t projid;
|
|
|
|
projid = from_kprojid(targ, kprojid);
|
|
|
|
|
|
|
|
if (projid == (projid_t) -1)
|
|
|
|
projid = OVERFLOW_PROJID;
|
|
|
|
return projid;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(from_kprojid_munged);
|
|
|
|
|
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
static int uid_m_show(struct seq_file *seq, void *v)
|
|
|
|
{
|
|
|
|
struct user_namespace *ns = seq->private;
|
|
|
|
struct uid_gid_extent *extent = v;
|
|
|
|
struct user_namespace *lower_ns;
|
|
|
|
uid_t lower;
|
2010-06-13 11:28:03 +08:00
|
|
|
|
2012-08-15 12:25:13 +08:00
|
|
|
lower_ns = seq_user_ns(seq);
|
2011-11-17 16:11:58 +08:00
|
|
|
if ((lower_ns == ns) && lower_ns->parent)
|
|
|
|
lower_ns = lower_ns->parent;
|
|
|
|
|
|
|
|
lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
|
|
|
|
|
|
|
|
seq_printf(seq, "%10u %10u %10u\n",
|
|
|
|
extent->first,
|
|
|
|
lower,
|
|
|
|
extent->count);
|
|
|
|
|
|
|
|
return 0;
|
2010-06-13 11:28:03 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
static int gid_m_show(struct seq_file *seq, void *v)
|
2010-06-13 11:28:03 +08:00
|
|
|
{
|
2011-11-17 16:11:58 +08:00
|
|
|
struct user_namespace *ns = seq->private;
|
|
|
|
struct uid_gid_extent *extent = v;
|
|
|
|
struct user_namespace *lower_ns;
|
|
|
|
gid_t lower;
|
2010-06-13 11:28:03 +08:00
|
|
|
|
2012-08-15 12:25:13 +08:00
|
|
|
lower_ns = seq_user_ns(seq);
|
2011-11-17 16:11:58 +08:00
|
|
|
if ((lower_ns == ns) && lower_ns->parent)
|
|
|
|
lower_ns = lower_ns->parent;
|
2010-06-13 11:28:03 +08:00
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
|
|
|
|
|
|
|
|
seq_printf(seq, "%10u %10u %10u\n",
|
|
|
|
extent->first,
|
|
|
|
lower,
|
|
|
|
extent->count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-30 16:24:05 +08:00
|
|
|
static int projid_m_show(struct seq_file *seq, void *v)
|
|
|
|
{
|
|
|
|
struct user_namespace *ns = seq->private;
|
|
|
|
struct uid_gid_extent *extent = v;
|
|
|
|
struct user_namespace *lower_ns;
|
|
|
|
projid_t lower;
|
|
|
|
|
|
|
|
lower_ns = seq_user_ns(seq);
|
|
|
|
if ((lower_ns == ns) && lower_ns->parent)
|
|
|
|
lower_ns = lower_ns->parent;
|
|
|
|
|
|
|
|
lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
|
|
|
|
|
|
|
|
seq_printf(seq, "%10u %10u %10u\n",
|
|
|
|
extent->first,
|
|
|
|
lower,
|
|
|
|
extent->count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-07 05:37:21 +08:00
|
|
|
static void *m_start(struct seq_file *seq, loff_t *ppos,
|
|
|
|
struct uid_gid_map *map)
|
2011-11-17 16:11:58 +08:00
|
|
|
{
|
|
|
|
struct uid_gid_extent *extent = NULL;
|
|
|
|
loff_t pos = *ppos;
|
|
|
|
|
|
|
|
if (pos < map->nr_extents)
|
|
|
|
extent = &map->extent[pos];
|
|
|
|
|
|
|
|
return extent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct user_namespace *ns = seq->private;
|
|
|
|
|
|
|
|
return m_start(seq, ppos, &ns->uid_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct user_namespace *ns = seq->private;
|
|
|
|
|
|
|
|
return m_start(seq, ppos, &ns->gid_map);
|
|
|
|
}
|
|
|
|
|
2012-08-30 16:24:05 +08:00
|
|
|
static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct user_namespace *ns = seq->private;
|
|
|
|
|
|
|
|
return m_start(seq, ppos, &ns->projid_map);
|
|
|
|
}
|
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
|
|
|
|
{
|
|
|
|
(*pos)++;
|
|
|
|
return seq->op->start(seq, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void m_stop(struct seq_file *seq, void *v)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-09 05:21:22 +08:00
|
|
|
const struct seq_operations proc_uid_seq_operations = {
|
2011-11-17 16:11:58 +08:00
|
|
|
.start = uid_m_start,
|
|
|
|
.stop = m_stop,
|
|
|
|
.next = m_next,
|
|
|
|
.show = uid_m_show,
|
|
|
|
};
|
|
|
|
|
2014-08-09 05:21:22 +08:00
|
|
|
const struct seq_operations proc_gid_seq_operations = {
|
2011-11-17 16:11:58 +08:00
|
|
|
.start = gid_m_start,
|
|
|
|
.stop = m_stop,
|
|
|
|
.next = m_next,
|
|
|
|
.show = gid_m_show,
|
|
|
|
};
|
|
|
|
|
2014-08-09 05:21:22 +08:00
|
|
|
const struct seq_operations proc_projid_seq_operations = {
|
2012-08-30 16:24:05 +08:00
|
|
|
.start = projid_m_start,
|
|
|
|
.stop = m_stop,
|
|
|
|
.next = m_next,
|
|
|
|
.show = projid_m_show,
|
|
|
|
};
|
|
|
|
|
2014-06-07 05:37:21 +08:00
|
|
|
static bool mappings_overlap(struct uid_gid_map *new_map,
|
|
|
|
struct uid_gid_extent *extent)
|
2012-12-28 14:27:29 +08:00
|
|
|
{
|
|
|
|
u32 upper_first, lower_first, upper_last, lower_last;
|
|
|
|
unsigned idx;
|
|
|
|
|
|
|
|
upper_first = extent->first;
|
|
|
|
lower_first = extent->lower_first;
|
|
|
|
upper_last = upper_first + extent->count - 1;
|
|
|
|
lower_last = lower_first + extent->count - 1;
|
|
|
|
|
|
|
|
for (idx = 0; idx < new_map->nr_extents; idx++) {
|
|
|
|
u32 prev_upper_first, prev_lower_first;
|
|
|
|
u32 prev_upper_last, prev_lower_last;
|
|
|
|
struct uid_gid_extent *prev;
|
|
|
|
|
|
|
|
prev = &new_map->extent[idx];
|
|
|
|
|
|
|
|
prev_upper_first = prev->first;
|
|
|
|
prev_lower_first = prev->lower_first;
|
|
|
|
prev_upper_last = prev_upper_first + prev->count - 1;
|
|
|
|
prev_lower_last = prev_lower_first + prev->count - 1;
|
|
|
|
|
|
|
|
/* Does the upper range intersect a previous extent? */
|
|
|
|
if ((prev_upper_first <= upper_last) &&
|
|
|
|
(prev_upper_last >= upper_first))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Does the lower range intersect a previous extent? */
|
|
|
|
if ((prev_lower_first <= lower_last) &&
|
|
|
|
(prev_lower_last >= lower_first))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
static ssize_t map_write(struct file *file, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos,
|
|
|
|
int cap_setid,
|
|
|
|
struct uid_gid_map *map,
|
|
|
|
struct uid_gid_map *parent_map)
|
|
|
|
{
|
|
|
|
struct seq_file *seq = file->private_data;
|
|
|
|
struct user_namespace *ns = seq->private;
|
|
|
|
struct uid_gid_map new_map;
|
|
|
|
unsigned idx;
|
2012-12-28 14:27:29 +08:00
|
|
|
struct uid_gid_extent *extent = NULL;
|
2015-12-24 13:13:10 +08:00
|
|
|
char *kbuf = NULL, *pos, *next_line;
|
2011-11-17 16:11:58 +08:00
|
|
|
ssize_t ret = -EINVAL;
|
|
|
|
|
|
|
|
/*
|
2014-12-10 04:03:14 +08:00
|
|
|
* The userns_state_mutex serializes all writes to any given map.
|
2011-11-17 16:11:58 +08:00
|
|
|
*
|
|
|
|
* Any map is only ever written once.
|
|
|
|
*
|
|
|
|
* An id map fits within 1 cache line on most architectures.
|
|
|
|
*
|
|
|
|
* On read nothing needs to be done unless you are on an
|
|
|
|
* architecture with a crazy cache coherency model like alpha.
|
|
|
|
*
|
|
|
|
* There is a one time data dependency between reading the
|
|
|
|
* count of the extents and the values of the extents. The
|
|
|
|
* desired behavior is to see the values of the extents that
|
|
|
|
* were written before the count of the extents.
|
|
|
|
*
|
|
|
|
* To achieve this smp_wmb() is used on guarantee the write
|
2014-04-15 04:58:55 +08:00
|
|
|
* order and smp_rmb() is guaranteed that we don't have crazy
|
|
|
|
* architectures returning stale data.
|
2011-11-17 16:11:58 +08:00
|
|
|
*/
|
2014-12-10 04:03:14 +08:00
|
|
|
mutex_lock(&userns_state_mutex);
|
2011-11-17 16:11:58 +08:00
|
|
|
|
|
|
|
ret = -EPERM;
|
|
|
|
/* Only allow one successful write to the map */
|
|
|
|
if (map->nr_extents != 0)
|
|
|
|
goto out;
|
|
|
|
|
2013-04-15 02:44:04 +08:00
|
|
|
/*
|
|
|
|
* Adjusting namespace settings requires capabilities on the target.
|
2010-06-13 11:28:03 +08:00
|
|
|
*/
|
2013-04-15 02:44:04 +08:00
|
|
|
if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
|
2011-11-17 16:11:58 +08:00
|
|
|
goto out;
|
|
|
|
|
2014-12-06 10:03:28 +08:00
|
|
|
/* Only allow < page size writes at the beginning of the file */
|
2011-11-17 16:11:58 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
if ((*ppos != 0) || (count >= PAGE_SIZE))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Slurp in the user data */
|
2015-12-24 13:13:10 +08:00
|
|
|
kbuf = memdup_user_nul(buf, count);
|
|
|
|
if (IS_ERR(kbuf)) {
|
|
|
|
ret = PTR_ERR(kbuf);
|
|
|
|
kbuf = NULL;
|
2011-11-17 16:11:58 +08:00
|
|
|
goto out;
|
2015-12-24 13:13:10 +08:00
|
|
|
}
|
2011-11-17 16:11:58 +08:00
|
|
|
|
|
|
|
/* Parse the user data */
|
|
|
|
ret = -EINVAL;
|
|
|
|
pos = kbuf;
|
|
|
|
new_map.nr_extents = 0;
|
2014-06-07 05:37:21 +08:00
|
|
|
for (; pos; pos = next_line) {
|
2011-11-17 16:11:58 +08:00
|
|
|
extent = &new_map.extent[new_map.nr_extents];
|
|
|
|
|
|
|
|
/* Find the end of line and ensure I don't look past it */
|
|
|
|
next_line = strchr(pos, '\n');
|
|
|
|
if (next_line) {
|
|
|
|
*next_line = '\0';
|
|
|
|
next_line++;
|
|
|
|
if (*next_line == '\0')
|
|
|
|
next_line = NULL;
|
2010-06-13 11:28:03 +08:00
|
|
|
}
|
2011-11-17 16:11:58 +08:00
|
|
|
|
|
|
|
pos = skip_spaces(pos);
|
|
|
|
extent->first = simple_strtoul(pos, &pos, 10);
|
|
|
|
if (!isspace(*pos))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
pos = skip_spaces(pos);
|
|
|
|
extent->lower_first = simple_strtoul(pos, &pos, 10);
|
|
|
|
if (!isspace(*pos))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
pos = skip_spaces(pos);
|
|
|
|
extent->count = simple_strtoul(pos, &pos, 10);
|
|
|
|
if (*pos && !isspace(*pos))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Verify there is not trailing junk on the line */
|
|
|
|
pos = skip_spaces(pos);
|
|
|
|
if (*pos != '\0')
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Verify we have been given valid starting values */
|
|
|
|
if ((extent->first == (u32) -1) ||
|
2014-06-07 05:37:21 +08:00
|
|
|
(extent->lower_first == (u32) -1))
|
2011-11-17 16:11:58 +08:00
|
|
|
goto out;
|
|
|
|
|
2014-06-07 05:37:21 +08:00
|
|
|
/* Verify count is not zero and does not cause the
|
|
|
|
* extent to wrap
|
|
|
|
*/
|
2011-11-17 16:11:58 +08:00
|
|
|
if ((extent->first + extent->count) <= extent->first)
|
|
|
|
goto out;
|
2014-06-07 05:37:21 +08:00
|
|
|
if ((extent->lower_first + extent->count) <=
|
|
|
|
extent->lower_first)
|
2011-11-17 16:11:58 +08:00
|
|
|
goto out;
|
|
|
|
|
2012-12-28 14:27:29 +08:00
|
|
|
/* Do the ranges in extent overlap any previous extents? */
|
|
|
|
if (mappings_overlap(&new_map, extent))
|
2011-11-17 16:11:58 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
new_map.nr_extents++;
|
|
|
|
|
|
|
|
/* Fail if the file contains too many extents */
|
|
|
|
if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
|
|
|
|
(next_line != NULL))
|
|
|
|
goto out;
|
2010-06-13 11:28:03 +08:00
|
|
|
}
|
2011-11-17 16:11:58 +08:00
|
|
|
/* Be very certaint the new map actually exists */
|
|
|
|
if (new_map.nr_extents == 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = -EPERM;
|
|
|
|
/* Validate the user is allowed to use user id's mapped to. */
|
2013-04-15 04:47:02 +08:00
|
|
|
if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
|
2011-11-17 16:11:58 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Map the lower ids from the parent user namespace to the
|
|
|
|
* kernel global id space.
|
|
|
|
*/
|
|
|
|
for (idx = 0; idx < new_map.nr_extents; idx++) {
|
|
|
|
u32 lower_first;
|
|
|
|
extent = &new_map.extent[idx];
|
|
|
|
|
|
|
|
lower_first = map_id_range_down(parent_map,
|
|
|
|
extent->lower_first,
|
|
|
|
extent->count);
|
|
|
|
|
|
|
|
/* Fail if we can not map the specified extent to
|
|
|
|
* the kernel global id space.
|
|
|
|
*/
|
|
|
|
if (lower_first == (u32) -1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
extent->lower_first = lower_first;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Install the map */
|
|
|
|
memcpy(map->extent, new_map.extent,
|
|
|
|
new_map.nr_extents*sizeof(new_map.extent[0]));
|
|
|
|
smp_wmb();
|
|
|
|
map->nr_extents = new_map.nr_extents;
|
|
|
|
|
|
|
|
*ppos = count;
|
|
|
|
ret = count;
|
|
|
|
out:
|
2014-12-10 04:03:14 +08:00
|
|
|
mutex_unlock(&userns_state_mutex);
|
2015-12-24 13:13:10 +08:00
|
|
|
kfree(kbuf);
|
2011-11-17 16:11:58 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-07 05:37:21 +08:00
|
|
|
ssize_t proc_uid_map_write(struct file *file, const char __user *buf,
|
|
|
|
size_t size, loff_t *ppos)
|
2011-11-17 16:11:58 +08:00
|
|
|
{
|
|
|
|
struct seq_file *seq = file->private_data;
|
|
|
|
struct user_namespace *ns = seq->private;
|
2012-08-15 12:25:13 +08:00
|
|
|
struct user_namespace *seq_ns = seq_user_ns(seq);
|
2011-11-17 16:11:58 +08:00
|
|
|
|
|
|
|
if (!ns->parent)
|
|
|
|
return -EPERM;
|
|
|
|
|
2012-08-15 12:25:13 +08:00
|
|
|
if ((seq_ns != ns) && (seq_ns != ns->parent))
|
|
|
|
return -EPERM;
|
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
return map_write(file, buf, size, ppos, CAP_SETUID,
|
|
|
|
&ns->uid_map, &ns->parent->uid_map);
|
|
|
|
}
|
|
|
|
|
2014-06-07 05:37:21 +08:00
|
|
|
ssize_t proc_gid_map_write(struct file *file, const char __user *buf,
|
|
|
|
size_t size, loff_t *ppos)
|
2011-11-17 16:11:58 +08:00
|
|
|
{
|
|
|
|
struct seq_file *seq = file->private_data;
|
|
|
|
struct user_namespace *ns = seq->private;
|
2012-08-15 12:25:13 +08:00
|
|
|
struct user_namespace *seq_ns = seq_user_ns(seq);
|
2011-11-17 16:11:58 +08:00
|
|
|
|
|
|
|
if (!ns->parent)
|
|
|
|
return -EPERM;
|
|
|
|
|
2012-08-15 12:25:13 +08:00
|
|
|
if ((seq_ns != ns) && (seq_ns != ns->parent))
|
|
|
|
return -EPERM;
|
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
return map_write(file, buf, size, ppos, CAP_SETGID,
|
|
|
|
&ns->gid_map, &ns->parent->gid_map);
|
|
|
|
}
|
|
|
|
|
2014-06-07 05:37:21 +08:00
|
|
|
ssize_t proc_projid_map_write(struct file *file, const char __user *buf,
|
|
|
|
size_t size, loff_t *ppos)
|
2012-08-30 16:24:05 +08:00
|
|
|
{
|
|
|
|
struct seq_file *seq = file->private_data;
|
|
|
|
struct user_namespace *ns = seq->private;
|
|
|
|
struct user_namespace *seq_ns = seq_user_ns(seq);
|
|
|
|
|
|
|
|
if (!ns->parent)
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
if ((seq_ns != ns) && (seq_ns != ns->parent))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
/* Anyone can set any valid project id no capability needed */
|
|
|
|
return map_write(file, buf, size, ppos, -1,
|
|
|
|
&ns->projid_map, &ns->parent->projid_map);
|
|
|
|
}
|
|
|
|
|
2014-06-07 05:37:21 +08:00
|
|
|
static bool new_idmap_permitted(const struct file *file,
|
2013-04-15 04:47:02 +08:00
|
|
|
struct user_namespace *ns, int cap_setid,
|
2011-11-17 16:11:58 +08:00
|
|
|
struct uid_gid_map *new_map)
|
|
|
|
{
|
2014-11-27 13:22:14 +08:00
|
|
|
const struct cred *cred = file->f_cred;
|
2014-12-06 07:51:47 +08:00
|
|
|
/* Don't allow mappings that would allow anything that wouldn't
|
|
|
|
* be allowed without the establishment of unprivileged mappings.
|
|
|
|
*/
|
2014-11-27 13:22:14 +08:00
|
|
|
if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
|
|
|
|
uid_eq(ns->owner, cred->euid)) {
|
2012-07-27 21:21:27 +08:00
|
|
|
u32 id = new_map->extent[0].lower_first;
|
|
|
|
if (cap_setid == CAP_SETUID) {
|
|
|
|
kuid_t uid = make_kuid(ns->parent, id);
|
2014-11-27 13:22:14 +08:00
|
|
|
if (uid_eq(uid, cred->euid))
|
2012-07-27 21:21:27 +08:00
|
|
|
return true;
|
2014-06-07 05:37:21 +08:00
|
|
|
} else if (cap_setid == CAP_SETGID) {
|
2012-07-27 21:21:27 +08:00
|
|
|
kgid_t gid = make_kgid(ns->parent, id);
|
2014-12-06 09:36:04 +08:00
|
|
|
if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
|
|
|
|
gid_eq(gid, cred->egid))
|
2012-07-27 21:21:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-30 16:24:05 +08:00
|
|
|
/* Allow anyone to set a mapping that doesn't require privilege */
|
|
|
|
if (!cap_valid(cap_setid))
|
|
|
|
return true;
|
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
/* Allow the specified ids if we have the appropriate capability
|
|
|
|
* (CAP_SETUID or CAP_SETGID) over the parent user namespace.
|
2013-04-15 04:47:02 +08:00
|
|
|
* And the opener of the id file also had the approprpiate capability.
|
2011-11-17 16:11:58 +08:00
|
|
|
*/
|
2013-04-15 04:47:02 +08:00
|
|
|
if (ns_capable(ns->parent, cap_setid) &&
|
|
|
|
file_ns_capable(file, ns->parent, cap_setid))
|
2011-11-17 16:11:58 +08:00
|
|
|
return true;
|
2010-06-13 11:28:03 +08:00
|
|
|
|
2011-11-17 16:11:58 +08:00
|
|
|
return false;
|
2010-06-13 11:28:03 +08:00
|
|
|
}
|
2011-01-13 09:00:46 +08:00
|
|
|
|
2014-12-03 02:27:26 +08:00
|
|
|
int proc_setgroups_show(struct seq_file *seq, void *v)
|
|
|
|
{
|
|
|
|
struct user_namespace *ns = seq->private;
|
|
|
|
unsigned long userns_flags = ACCESS_ONCE(ns->flags);
|
|
|
|
|
|
|
|
seq_printf(seq, "%s\n",
|
|
|
|
(userns_flags & USERNS_SETGROUPS_ALLOWED) ?
|
|
|
|
"allow" : "deny");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct seq_file *seq = file->private_data;
|
|
|
|
struct user_namespace *ns = seq->private;
|
|
|
|
char kbuf[8], *pos;
|
|
|
|
bool setgroups_allowed;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* Only allow a very narrow range of strings to be written */
|
|
|
|
ret = -EINVAL;
|
|
|
|
if ((*ppos != 0) || (count >= sizeof(kbuf)))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* What was written? */
|
|
|
|
ret = -EFAULT;
|
|
|
|
if (copy_from_user(kbuf, buf, count))
|
|
|
|
goto out;
|
|
|
|
kbuf[count] = '\0';
|
|
|
|
pos = kbuf;
|
|
|
|
|
|
|
|
/* What is being requested? */
|
|
|
|
ret = -EINVAL;
|
|
|
|
if (strncmp(pos, "allow", 5) == 0) {
|
|
|
|
pos += 5;
|
|
|
|
setgroups_allowed = true;
|
|
|
|
}
|
|
|
|
else if (strncmp(pos, "deny", 4) == 0) {
|
|
|
|
pos += 4;
|
|
|
|
setgroups_allowed = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Verify there is not trailing junk on the line */
|
|
|
|
pos = skip_spaces(pos);
|
|
|
|
if (*pos != '\0')
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = -EPERM;
|
|
|
|
mutex_lock(&userns_state_mutex);
|
|
|
|
if (setgroups_allowed) {
|
|
|
|
/* Enabling setgroups after setgroups has been disabled
|
|
|
|
* is not allowed.
|
|
|
|
*/
|
|
|
|
if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
|
|
|
|
goto out_unlock;
|
|
|
|
} else {
|
|
|
|
/* Permanently disabling setgroups after setgroups has
|
|
|
|
* been enabled by writing the gid_map is not allowed.
|
|
|
|
*/
|
|
|
|
if (ns->gid_map.nr_extents != 0)
|
|
|
|
goto out_unlock;
|
|
|
|
ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
|
|
|
|
}
|
|
|
|
mutex_unlock(&userns_state_mutex);
|
|
|
|
|
|
|
|
/* Report a successful write */
|
|
|
|
*ppos = count;
|
|
|
|
ret = count;
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
out_unlock:
|
|
|
|
mutex_unlock(&userns_state_mutex);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-12-06 08:01:11 +08:00
|
|
|
bool userns_may_setgroups(const struct user_namespace *ns)
|
|
|
|
{
|
|
|
|
bool allowed;
|
|
|
|
|
2014-12-10 04:03:14 +08:00
|
|
|
mutex_lock(&userns_state_mutex);
|
2014-12-06 08:01:11 +08:00
|
|
|
/* It is not safe to use setgroups until a gid mapping in
|
|
|
|
* the user namespace has been established.
|
|
|
|
*/
|
|
|
|
allowed = ns->gid_map.nr_extents != 0;
|
2014-12-03 02:27:26 +08:00
|
|
|
/* Is setgroups allowed? */
|
|
|
|
allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
|
2014-12-10 04:03:14 +08:00
|
|
|
mutex_unlock(&userns_state_mutex);
|
2014-12-06 08:01:11 +08:00
|
|
|
|
|
|
|
return allowed;
|
|
|
|
}
|
|
|
|
|
2015-09-24 04:16:04 +08:00
|
|
|
/*
|
|
|
|
* Returns true if @ns is the same namespace as or a descendant of
|
|
|
|
* @target_ns.
|
|
|
|
*/
|
|
|
|
bool current_in_userns(const struct user_namespace *target_ns)
|
|
|
|
{
|
|
|
|
struct user_namespace *ns;
|
|
|
|
for (ns = current_user_ns(); ns; ns = ns->parent) {
|
|
|
|
if (ns == target_ns)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-01 12:25:30 +08:00
|
|
|
static inline struct user_namespace *to_user_ns(struct ns_common *ns)
|
|
|
|
{
|
|
|
|
return container_of(ns, struct user_namespace, ns);
|
|
|
|
}
|
|
|
|
|
2014-11-01 12:37:32 +08:00
|
|
|
static struct ns_common *userns_get(struct task_struct *task)
|
2012-07-26 21:24:06 +08:00
|
|
|
{
|
|
|
|
struct user_namespace *user_ns;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
user_ns = get_user_ns(__task_cred(task)->user_ns);
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
2014-11-01 12:25:30 +08:00
|
|
|
return user_ns ? &user_ns->ns : NULL;
|
2012-07-26 21:24:06 +08:00
|
|
|
}
|
|
|
|
|
2014-11-01 12:37:32 +08:00
|
|
|
static void userns_put(struct ns_common *ns)
|
2012-07-26 21:24:06 +08:00
|
|
|
{
|
2014-11-01 12:25:30 +08:00
|
|
|
put_user_ns(to_user_ns(ns));
|
2012-07-26 21:24:06 +08:00
|
|
|
}
|
|
|
|
|
2014-11-01 12:37:32 +08:00
|
|
|
static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
|
2012-07-26 21:24:06 +08:00
|
|
|
{
|
2014-11-01 12:25:30 +08:00
|
|
|
struct user_namespace *user_ns = to_user_ns(ns);
|
2012-07-26 21:24:06 +08:00
|
|
|
struct cred *cred;
|
|
|
|
|
|
|
|
/* Don't allow gaining capabilities by reentering
|
|
|
|
* the same user namespace.
|
|
|
|
*/
|
|
|
|
if (user_ns == current_user_ns())
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-08-11 07:25:44 +08:00
|
|
|
/* Tasks that share a thread group must share a user namespace */
|
|
|
|
if (!thread_group_empty(current))
|
2012-07-26 21:24:06 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2013-03-14 02:51:49 +08:00
|
|
|
if (current->fs->users != 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-07-26 21:24:06 +08:00
|
|
|
if (!ns_capable(user_ns, CAP_SYS_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
cred = prepare_creds();
|
|
|
|
if (!cred)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
put_user_ns(cred->user_ns);
|
|
|
|
set_cred_user_ns(cred, get_user_ns(user_ns));
|
|
|
|
|
|
|
|
return commit_creds(cred);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct proc_ns_operations userns_operations = {
|
|
|
|
.name = "user",
|
|
|
|
.type = CLONE_NEWUSER,
|
|
|
|
.get = userns_get,
|
|
|
|
.put = userns_put,
|
|
|
|
.install = userns_install,
|
|
|
|
};
|
|
|
|
|
2011-01-13 09:00:46 +08:00
|
|
|
static __init int user_namespaces_init(void)
|
|
|
|
{
|
|
|
|
user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-04 05:48:35 +08:00
|
|
|
subsys_initcall(user_namespaces_init);
|