All of lore.kernel.org
 help / color / mirror / Atom feed
* [REVIEW][PATCH] mm: Add a user_ns owner to mm_struct and fix ptrace_may_access
@ 2016-10-17 16:39 ` Eric W. Biederman
  0 siblings, 0 replies; 166+ messages in thread
From: Eric W. Biederman @ 2016-10-17 16:39 UTC (permalink / raw)
  To: Linux Containers
  Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA, Oleg Nesterov,
	Andy Lutomirski, linux-mm-Bw31MaZKKs3YtjvyW6yDsg,
	linux-fsdevel-u79uwXL29TY76Z2rM5mHXA


During exec dumpable is cleared if the file that is being executed is
not readable by the user executing the file.  A bug in
ptrace_may_access allows reading the file if the executable happens to
enter into a subordinate user namespace (aka clone(CLONE_NEWUSER),
unshare(CLONE_NEWUSER), or setns(fd, CLONE_NEWUSER).

This problem is fixed with only necessary userspace breakage by adding
a user namespace owner to mm_struct, captured at the time of exec,
so it is clear in which user namespace CAP_SYS_PTRACE must be present
in to be able to safely give read permission to the executable.

The function ptrace_may_access is modified to verify that the ptracer
has CAP_SYS_ADMIN in task->mm->user_ns instead of task->cred->user_ns.
This ensures that if the task changes it's cred into a subordinate
user namespace it does not become ptraceable.

Cc: stable-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Fixes: 8409cca70561 ("userns: allow ptrace from non-init user namespaces")
Signed-off-by: "Eric W. Biederman" <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
---

It turns out that dumpable needs to be fixed to be user namespace
aware to fix this issue.  When this patch is ready I plan to place it in
my userns tree and send it to Linus, hopefully for -rc2.

 include/linux/mm_types.h |  1 +
 kernel/fork.c            |  9 ++++++---
 kernel/ptrace.c          | 17 ++++++-----------
 mm/init-mm.c             |  2 ++
 4 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 4a8acedf4b7d..08d947fc4c59 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -473,6 +473,7 @@ struct mm_struct {
 	 */
 	struct task_struct __rcu *owner;
 #endif
+	struct user_namespace *user_ns;
 
 	/* store ref to file /proc/<pid>/exe symlink points to */
 	struct file __rcu *exe_file;
diff --git a/kernel/fork.c b/kernel/fork.c
index 623259fc794d..fd85c68c2791 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -742,7 +742,8 @@ static void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
 #endif
 }
 
-static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
+static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
+	struct user_namespace *user_ns)
 {
 	mm->mmap = NULL;
 	mm->mm_rb = RB_ROOT;
@@ -782,6 +783,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
 	if (init_new_context(p, mm))
 		goto fail_nocontext;
 
+	mm->user_ns = get_user_ns(user_ns);
 	return mm;
 
 fail_nocontext:
@@ -827,7 +829,7 @@ struct mm_struct *mm_alloc(void)
 		return NULL;
 
 	memset(mm, 0, sizeof(*mm));
-	return mm_init(mm, current);
+	return mm_init(mm, current, current_user_ns());
 }
 
 /*
@@ -842,6 +844,7 @@ void __mmdrop(struct mm_struct *mm)
 	destroy_context(mm);
 	mmu_notifier_mm_destroy(mm);
 	check_mm(mm);
+	put_user_ns(mm->user_ns);
 	free_mm(mm);
 }
 EXPORT_SYMBOL_GPL(__mmdrop);
@@ -1123,7 +1126,7 @@ static struct mm_struct *dup_mm(struct task_struct *tsk)
 
 	memcpy(mm, oldmm, sizeof(*mm));
 
-	if (!mm_init(mm, tsk))
+	if (!mm_init(mm, tsk, mm->user_ns))
 		goto fail_nomem;
 
 	err = dup_mmap(mm, oldmm);
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 2a99027312a6..f2d1b9afb3f8 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -220,7 +220,7 @@ static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
 static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
 {
 	const struct cred *cred = current_cred(), *tcred;
-	int dumpable = 0;
+	struct mm_struct *mm;
 	kuid_t caller_uid;
 	kgid_t caller_gid;
 
@@ -271,16 +271,11 @@ static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
 	return -EPERM;
 ok:
 	rcu_read_unlock();
-	smp_rmb();
-	if (task->mm)
-		dumpable = get_dumpable(task->mm);
-	rcu_read_lock();
-	if (dumpable != SUID_DUMP_USER &&
-	    !ptrace_has_cap(__task_cred(task)->user_ns, mode)) {
-		rcu_read_unlock();
-		return -EPERM;
-	}
-	rcu_read_unlock();
+	mm = task->mm;
+	if (!mm ||
+	    ((get_dumpable(mm) != SUID_DUMP_USER) &&
+	     !ptrace_has_cap(mm->user_ns, mode)))
+	    return -EPERM;
 
 	return security_ptrace_access_check(task, mode);
 }
diff --git a/mm/init-mm.c b/mm/init-mm.c
index a56a851908d2..975e49f00f34 100644
--- a/mm/init-mm.c
+++ b/mm/init-mm.c
@@ -6,6 +6,7 @@
 #include <linux/cpumask.h>
 
 #include <linux/atomic.h>
+#include <linux/user_namespace.h>
 #include <asm/pgtable.h>
 #include <asm/mmu.h>
 
@@ -21,5 +22,6 @@ struct mm_struct init_mm = {
 	.mmap_sem	= __RWSEM_INITIALIZER(init_mm.mmap_sem),
 	.page_table_lock =  __SPIN_LOCK_UNLOCKED(init_mm.page_table_lock),
 	.mmlist		= LIST_HEAD_INIT(init_mm.mmlist),
+	.user_ns	= &init_user_ns,
 	INIT_MM_CONTEXT(init_mm)
 };
-- 
2.8.3

^ permalink raw reply related	[flat|nested] 166+ messages in thread

end of thread, other threads:[~2016-11-19 18:47 UTC | newest]

Thread overview: 166+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-17 16:39 [REVIEW][PATCH] mm: Add a user_ns owner to mm_struct and fix ptrace_may_access Eric W. Biederman
2016-10-17 16:39 ` Eric W. Biederman
2016-10-17 16:39 ` Eric W. Biederman
2016-10-17 17:25 ` Jann Horn
     [not found]   ` <20161017172547.GJ14666-J1fxOzX/cBvk1uMJSBkQmQ@public.gmane.org>
2016-10-17 17:33     ` Eric W. Biederman
2016-10-17 17:33       ` Eric W. Biederman
2016-10-17 17:33       ` Eric W. Biederman
2016-10-17 17:33       ` Eric W. Biederman
     [not found] ` <87twcbq696.fsf-JOvCrm2gF+uungPnsOpG7nhyD016LWXt@public.gmane.org>
2016-10-17 17:25   ` Jann Horn
2016-10-18 13:50   ` Michal Hocko
2016-10-18 13:50 ` Michal Hocko
2016-10-18 13:50   ` Michal Hocko
     [not found]   ` <20161018135031.GB13117-2MMpYkNvuYDjFM9bn6wA6Q@public.gmane.org>
2016-10-18 13:57     ` Jann Horn
2016-10-18 13:57       ` Jann Horn
2016-10-18 14:56     ` Eric W. Biederman
2016-10-18 14:56       ` Eric W. Biederman
2016-10-18 14:56       ` Eric W. Biederman
2016-10-18 14:56       ` Eric W. Biederman
2016-10-18 15:05       ` Jann Horn
     [not found]         ` <20161018150507.GP14666-J1fxOzX/cBvk1uMJSBkQmQ@public.gmane.org>
2016-10-18 15:35           ` Eric W. Biederman
2016-10-18 15:35             ` Eric W. Biederman
2016-10-18 15:35             ` Eric W. Biederman
2016-10-18 15:35             ` Eric W. Biederman
     [not found]             ` <87twc9656s.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-10-18 19:12               ` Jann Horn
2016-10-18 19:12                 ` Jann Horn
2016-10-18 19:12                 ` Jann Horn
     [not found]                 ` <20161018191206.GA1210-GiL72Q0nGm9Crx9znvW9yA@public.gmane.org>
2016-10-18 21:07                   ` Eric W. Biederman
2016-10-18 21:07                 ` Eric W. Biederman
2016-10-18 21:07                   ` Eric W. Biederman
2016-10-18 21:07                   ` Eric W. Biederman
     [not found]                   ` <87r37dnz74.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-10-18 21:15                     ` [REVIEW][PATCH] exec: Don't exec files the userns root can not read Eric W. Biederman
2016-10-18 21:15                   ` Eric W. Biederman
2016-10-18 21:15                     ` Eric W. Biederman
2016-10-18 21:15                     ` Eric W. Biederman
2016-10-19  6:13                     ` Amir Goldstein
2016-10-19  6:13                       ` Amir Goldstein
2016-10-19 13:33                       ` Eric W. Biederman
2016-10-19 13:33                         ` Eric W. Biederman
2016-10-19 13:33                         ` Eric W. Biederman
     [not found]                         ` <87mvi0mpix.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-10-19 17:04                           ` Eric W. Biederman
2016-10-19 17:04                             ` Eric W. Biederman
2016-10-19 17:04                             ` Eric W. Biederman
2016-10-19 17:04                             ` Eric W. Biederman
     [not found]                       ` <CAOQ4uxjyZF346vq-Oi=HwB=jj6ePycHBnEfvVPet9KqPxL9mgg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-10-19 13:33                         ` Eric W. Biederman
     [not found]                     ` <87k2d5nytz.fsf_-_-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-10-19  6:13                       ` Amir Goldstein
2016-10-19 15:30                       ` Andy Lutomirski
2016-10-19 15:30                     ` Andy Lutomirski
2016-10-19 15:30                       ` Andy Lutomirski
     [not found]                       ` <CALCETrU4SZYUEPrv4JkpUpA+0sZ=EirZRftRDp+a5hce5E7HgA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-10-19 16:52                         ` Eric W. Biederman
2016-10-19 16:52                       ` Eric W. Biederman
2016-10-19 16:52                         ` Eric W. Biederman
2016-10-19 16:52                         ` Eric W. Biederman
     [not found]                         ` <87y41kjn6l.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-10-19 17:29                           ` Jann Horn
2016-10-19 18:36                           ` Andy Lutomirski
2016-10-19 17:29                         ` Jann Horn
2016-10-19 17:29                           ` Jann Horn
     [not found]                           ` <20161019172917.GE1210-GiL72Q0nGm9Crx9znvW9yA@public.gmane.org>
2016-10-19 17:32                             ` Andy Lutomirski
2016-10-19 17:32                           ` Andy Lutomirski
2016-10-19 17:32                             ` Andy Lutomirski
2016-10-19 17:55                             ` Eric W. Biederman
2016-10-19 17:55                               ` Eric W. Biederman
2016-10-19 17:55                               ` Eric W. Biederman
     [not found]                               ` <87pomwi5p2.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-10-19 18:38                                 ` Andy Lutomirski
2016-10-19 18:38                                   ` Andy Lutomirski
2016-10-19 18:38                                   ` Andy Lutomirski
2016-10-19 21:26                                   ` Eric W. Biederman
2016-10-19 21:26                                     ` Eric W. Biederman
2016-10-19 21:26                                     ` Eric W. Biederman
2016-10-19 23:17                                     ` Andy Lutomirski
2016-10-19 23:17                                       ` Andy Lutomirski
2016-11-17 17:02                                       ` [REVIEW][PATCH 0/3] Fixing ptrace vs exec vs userns interactions Eric W. Biederman
2016-11-17 17:02                                         ` Eric W. Biederman
2016-11-17 17:02                                         ` Eric W. Biederman
2016-11-17 17:05                                         ` [REVIEW][PATCH 1/3] ptrace: Capture the ptracer's creds not PT_PTRACE_CAP Eric W. Biederman
2016-11-17 17:05                                           ` Eric W. Biederman
2016-11-17 17:05                                           ` Eric W. Biederman
2016-11-17 23:14                                           ` Kees Cook
2016-11-17 23:14                                             ` Kees Cook
2016-11-18 18:56                                             ` Eric W. Biederman
2016-11-18 18:56                                               ` Eric W. Biederman
2016-11-18 18:56                                               ` Eric W. Biederman
     [not found]                                             ` <CAGXu5jKbVkCGVSoxNQ=pTCBX1Boe3rPR1P56P-kR9AHWYHBs2w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-11-18 18:56                                               ` Eric W. Biederman
     [not found]                                           ` <87oa1eavfx.fsf_-_-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-11-17 23:14                                             ` Kees Cook
2016-11-17 23:27                                             ` Andy Lutomirski
2016-11-17 23:27                                           ` Andy Lutomirski
2016-11-17 23:27                                             ` Andy Lutomirski
     [not found]                                             ` <CALCETrUSnPfzpabQMNuyOu09j9QDzRDeoQVF_U51=ow3bP5pkw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-11-17 23:44                                               ` Eric W. Biederman
2016-11-17 23:44                                             ` Eric W. Biederman
2016-11-17 23:44                                               ` Eric W. Biederman
2016-11-17 23:44                                               ` Eric W. Biederman
2016-11-17 17:08                                         ` [REVIEW][PATCH 2/3] exec: Don't allow ptracing an exec of an unreadable file Eric W. Biederman
2016-11-17 17:08                                           ` Eric W. Biederman
2016-11-17 17:08                                           ` Eric W. Biederman
     [not found]                                           ` <87inrmavax.fsf_-_-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-11-17 20:47                                             ` Willy Tarreau
2016-11-17 23:29                                             ` Andy Lutomirski
2016-11-17 20:47                                           ` Willy Tarreau
2016-11-17 20:47                                             ` Willy Tarreau
     [not found]                                             ` <20161117204707.GB10421-K+wRfnb2/UA@public.gmane.org>
2016-11-17 21:07                                               ` Kees Cook
2016-11-17 21:07                                             ` Kees Cook
2016-11-17 21:07                                               ` Kees Cook
2016-11-17 21:32                                               ` Willy Tarreau
2016-11-17 21:32                                                 ` Willy Tarreau
2016-11-17 21:51                                                 ` Eric W. Biederman
2016-11-17 21:51                                                   ` Eric W. Biederman
2016-11-17 21:51                                                   ` Eric W. Biederman
2016-11-17 22:50                                                   ` [REVIEW][PATCH 2/3] ptrace: Don't allow accessing an undumpable mm Eric W. Biederman
2016-11-17 22:50                                                     ` Eric W. Biederman
2016-11-17 22:50                                                     ` Eric W. Biederman
     [not found]                                                     ` <87shqpzpok.fsf_-_-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-11-17 23:17                                                       ` Kees Cook
2016-11-17 23:17                                                     ` Kees Cook
2016-11-17 23:17                                                       ` Kees Cook
     [not found]                                                   ` <874m3522sy.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-11-17 22:50                                                     ` Eric W. Biederman
     [not found]                                                 ` <20161117213258.GA10839-K+wRfnb2/UA@public.gmane.org>
2016-11-17 21:51                                                   ` [REVIEW][PATCH 2/3] exec: Don't allow ptracing an exec of an unreadable file Eric W. Biederman
     [not found]                                               ` <CAGXu5jJc6TmzdVp+4OMDAt5Kd68hHbNBXaRPD8X0+m558hx3qw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-11-17 21:32                                                 ` Willy Tarreau
2016-11-17 23:28                                                 ` Andy Lutomirski
2016-11-17 23:28                                                   ` Andy Lutomirski
2016-11-17 23:28                                                   ` Andy Lutomirski
2016-11-17 23:29                                           ` Andy Lutomirski
2016-11-17 23:29                                             ` Andy Lutomirski
     [not found]                                             ` <CALCETrUvKpRCXRE+K512E_q9-o8Gzgb+3XsAzSo+ZFdgqeX-eQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-11-17 23:55                                               ` Eric W. Biederman
2016-11-17 23:55                                             ` Eric W. Biederman
2016-11-17 23:55                                               ` Eric W. Biederman
2016-11-17 23:55                                               ` Eric W. Biederman
2016-11-18  0:10                                               ` Andy Lutomirski
2016-11-18  0:10                                                 ` Andy Lutomirski
2016-11-18  0:35                                                 ` Eric W. Biederman
2016-11-18  0:35                                                   ` Eric W. Biederman
2016-11-18  0:35                                                   ` Eric W. Biederman
     [not found]                                                 ` <CALCETrX=61Sk9qim+Psjn83gohuizEsrpUC9gF-vwQTtR4GuJw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-11-18  0:35                                                   ` Eric W. Biederman
     [not found]                                               ` <87mvgxwtjv.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-11-18  0:10                                                 ` Andy Lutomirski
2016-11-17 17:10                                         ` [REVIEW][PATCH 3/3] exec: Ensure mm->user_ns contains the execed files Eric W. Biederman
2016-11-17 17:10                                           ` Eric W. Biederman
2016-11-17 17:10                                           ` Eric W. Biederman
2016-11-19  7:17                                         ` [REVIEW][PATCH 0/3] Fixing ptrace vs exec vs userns interactions Willy Tarreau
2016-11-19  7:17                                           ` Willy Tarreau
2016-11-19  9:28                                           ` Willy Tarreau
2016-11-19  9:28                                             ` Willy Tarreau
2016-11-19  9:33                                             ` Willy Tarreau
2016-11-19  9:33                                               ` Willy Tarreau
     [not found]                                             ` <20161119092804.GA13553-K+wRfnb2/UA@public.gmane.org>
2016-11-19  9:33                                               ` Willy Tarreau
2016-11-19 18:44                                               ` Eric W. Biederman
2016-11-19 18:44                                             ` Eric W. Biederman
2016-11-19 18:44                                               ` Eric W. Biederman
2016-11-19 18:44                                               ` Eric W. Biederman
     [not found]                                           ` <20161119071700.GA13347-K+wRfnb2/UA@public.gmane.org>
2016-11-19  9:28                                             ` Willy Tarreau
2016-11-19 18:35                                             ` Eric W. Biederman
2016-11-19 18:35                                           ` Eric W. Biederman
2016-11-19 18:35                                             ` Eric W. Biederman
2016-11-19 18:35                                             ` Eric W. Biederman
2016-11-19 18:37                                             ` Eric W. Biederman
2016-11-19 18:37                                               ` Eric W. Biederman
2016-11-19 18:37                                               ` Eric W. Biederman
     [not found]                                             ` <87d1hrjp23.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-11-19 18:37                                               ` Eric W. Biederman
     [not found]                                         ` <87twb6avk8.fsf_-_-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-11-17 17:05                                           ` [REVIEW][PATCH 1/3] ptrace: Capture the ptracer's creds not PT_PTRACE_CAP Eric W. Biederman
2016-11-17 17:08                                           ` [REVIEW][PATCH 2/3] exec: Don't allow ptracing an exec of an unreadable file Eric W. Biederman
2016-11-17 17:10                                           ` [REVIEW][PATCH 3/3] exec: Ensure mm->user_ns contains the execed files Eric W. Biederman
2016-11-19  7:17                                           ` [REVIEW][PATCH 0/3] Fixing ptrace vs exec vs userns interactions Willy Tarreau
     [not found]                                       ` <CALCETrXA2EnE8X3HzetLG6zS8YSVjJQJrsSumTfvEcGq=r5vsw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-11-17 17:02                                         ` Eric W. Biederman
     [not found]                                     ` <87pomwghda.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-10-19 23:17                                       ` [REVIEW][PATCH] exec: Don't exec files the userns root can not read Andy Lutomirski
     [not found]                                   ` <CALCETrUz2oU6OYwQ9K4M-SUg6FeDsd6Q1gf1w-cJRGg2PdmK8g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-10-19 21:26                                     ` Eric W. Biederman
     [not found]                             ` <CALCETrWSY1SRse5oqSwZ=goQ+ZALd2XcTP3SZ8ry49C8rNd98Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-10-19 17:55                               ` Eric W. Biederman
2016-10-19 18:36                         ` Andy Lutomirski
2016-10-18 18:06       ` [REVIEW][PATCH] mm: Add a user_ns owner to mm_struct and fix ptrace_may_access Michal Hocko
2016-10-18 18:06         ` Michal Hocko
     [not found]       ` <8737jt903u.fsf-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
2016-10-18 15:05         ` Jann Horn
2016-10-18 18:06         ` Michal Hocko

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.