]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
userns,pidns: Verify the userns for new pid namespaces
authorEric W. Biederman <ebiederm@xmission.com>
Sat, 29 Apr 2017 19:12:15 +0000 (14:12 -0500)
committerEric W. Biederman <ebiederm@xmission.com>
Thu, 20 Jul 2017 12:43:58 +0000 (07:43 -0500)
It is pointless and confusing to allow a pid namespace hierarchy and
the user namespace hierarchy to get out of sync.  The owner of a child
pid namespace should be the owner of the parent pid namespace or
a descendant of the owner of the parent pid namespace.

Otherwise it is possible to construct scenarios where a process has a
capability over a parent pid namespace but does not have the
capability over a child pid namespace.  Which confusingly makes
permission checks non-transitive.

It requires use of setns into a pid namespace (but not into a user
namespace) to create such a scenario.

Add the function in_userns to help in making this determination.

v2: Optimized in_userns by using level as suggested
    by: Kirill Tkhai <ktkhai@virtuozzo.com>

Ref: 49f4d8b93ccf ("pidns: Capture the user namespace and filter ns_last_pid")
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
include/linux/user_namespace.h
kernel/pid_namespace.c
kernel/user_namespace.c

index 32354b4b4b2ba5ae72034d00c3b1d43fa8c2a15c..4005877bb8b6d5ed633eae03abd60090e5a11d96 100644 (file)
@@ -112,8 +112,9 @@ extern ssize_t proc_projid_map_write(struct file *, const char __user *, size_t,
 extern ssize_t proc_setgroups_write(struct file *, const char __user *, size_t, loff_t *);
 extern int proc_setgroups_show(struct seq_file *m, void *v);
 extern bool userns_may_setgroups(const struct user_namespace *ns);
+extern bool in_userns(const struct user_namespace *ancestor,
+                      const struct user_namespace *child);
 extern bool current_in_userns(const struct user_namespace *target_ns);
-
 struct ns_common *ns_get_owner(struct ns_common *ns);
 #else
 
@@ -144,6 +145,12 @@ static inline bool userns_may_setgroups(const struct user_namespace *ns)
        return true;
 }
 
+static inline bool in_userns(const struct user_namespace *ancestor,
+                            const struct user_namespace *child)
+{
+       return true;
+}
+
 static inline bool current_in_userns(const struct user_namespace *target_ns)
 {
        return true;
index 74a5a7255b4d9cb473cc7708d851c64402cca942..4918314893bc6620ae95660c78588d6d5ac9129c 100644 (file)
@@ -101,6 +101,10 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
        int i;
        int err;
 
+       err = -EINVAL;
+       if (!in_userns(parent_pid_ns->user_ns, user_ns))
+               goto out;
+
        err = -ENOSPC;
        if (level > MAX_PID_NS_LEVEL)
                goto out;
index 2f735cbe05e8aca3b8ee8a3dcc7de493f9757c05..c490f1e4313b998a60686b98fbdf6b0167753e11 100644 (file)
@@ -986,17 +986,21 @@ bool userns_may_setgroups(const struct user_namespace *ns)
 }
 
 /*
- * Returns true if @ns is the same namespace as or a descendant of
- * @target_ns.
+ * Returns true if @child is the same namespace or a descendant of
+ * @ancestor.
  */
+bool in_userns(const struct user_namespace *ancestor,
+              const struct user_namespace *child)
+{
+       const struct user_namespace *ns;
+       for (ns = child; ns->level > ancestor->level; ns = ns->parent)
+               ;
+       return (ns == ancestor);
+}
+
 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;
+       return in_userns(target_ns, current_user_ns());
 }
 
 static inline struct user_namespace *to_user_ns(struct ns_common *ns)