Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp4867620imm; Tue, 9 Oct 2018 06:30:21 -0700 (PDT) X-Google-Smtp-Source: ACcGV61TO2PIi4k3pLMgrsvOVcQpRWL3sAcsNl972aSD1GWdRvFu2VvP6P8CT+YoyrAaz3EKRsM1 X-Received: by 2002:a65:48cc:: with SMTP id o12-v6mr24490787pgs.22.1539091821508; Tue, 09 Oct 2018 06:30:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539091821; cv=none; d=google.com; s=arc-20160816; b=EtAa1zPjKcg6S4blrIBk29Dt9Ak++UhgotQSbFXzbPZwlus/h+8nZLXu1xuU/jqrQ0 FnpUKlRfL0UcSedMeNFM0qWUKscY5SnGVyAJUxi5cLgsgixdL2AiyNw77+cngsRG8zYZ ewTSh41WbEVAWsqnmRIeKxUzPnfxT+RPvac8w7iZuLMZMMDQZCcoIWkKSUM1t/csGeiq LJoo14Arf9b8v5XinRs/cq+Nm4tvFxah1ZQMgRilFdnRrMV+DxdNSgTOGVcA9C7upgni xVptQq6mseRks5vhmCVRlHZFATnBgE3KSPAWci3Zepqgj0EyDLzT7ZQaWh6kq3luOnOd C5Hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=T1/+hAFKdTDWVbPDaFjudEv3SOe7Pb5KuQiniu3D8J4=; b=c3ZZrhk6+pB7sDEQg8mcHqHpCiFboxLJu8qBpnkIQuJIVqTgI7kDF1nmB4qtrh7XUZ A27Fdn8+WFyqdoFbzFYhZVoJLEcQqq4w2OYb9w9qehi20QD8gbwaB+h+LUCSqLjkGAJb hjlm4dVQ2YWkBxUoz8gKjYjCRS7yJzloy/ztttWrCtItRcPrLRuJ+BOCl5mfNMP2EJlV /GBbLoPyv1mvruRN0Wo+s2GkahYxwSxNs0OEoohX3lBkHGmE2tJ1f8a/BbX4xeZeXtPO zfFUtOjFl0eokbu3EaVut1xG4NShW1YSp9hJWIeWgCxMDWNFaYMD4lS+hrCUuO15Jj1I 9ytg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brauner.io header.s=google header.b=C5vieWP5; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y40-v6si21525795pla.331.2018.10.09.06.30.06; Tue, 09 Oct 2018 06:30:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@brauner.io header.s=google header.b=C5vieWP5; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727776AbeJIUp7 (ORCPT + 99 others); Tue, 9 Oct 2018 16:45:59 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:52257 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726649AbeJIUp7 (ORCPT ); Tue, 9 Oct 2018 16:45:59 -0400 Received: by mail-wm1-f65.google.com with SMTP id 189-v6so1893096wmw.2 for ; Tue, 09 Oct 2018 06:29:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brauner.io; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=T1/+hAFKdTDWVbPDaFjudEv3SOe7Pb5KuQiniu3D8J4=; b=C5vieWP5Spesh1yIt817pM+ilsQe2I6wl/OUgj//eDVk4pB29fBt6vHTmK6mK0U2i9 AHnTsTj723fIIG6Vt4qfkJjKBpmQvMKza9lVehTgYMuH9aHA9JBxE3WN8XQH5OhbMlAS D3y4PvwgrUCPC6v4mJSHWIRLFAloeTmWdRMPPBwtMXcXwsfGVcF1/eQV/5bei18DdRvi 2EIED/lNep+DkFlfx54TJYCMZydPu+P8DvFuZDf5Fmd2NrHgDa7knEL8zOaiaROmQEFn deMoxrVsQZVhCUs8YPdd5gS4yW6h13j6mIok+6XuXIyglPKHKV7yUytuDdm3xbwbpULu eKtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=T1/+hAFKdTDWVbPDaFjudEv3SOe7Pb5KuQiniu3D8J4=; b=YDoFQb1+S/EuNyynWMi/wewW+oxzY9Nelz+qmECyYod1n/h+9L9kfL3KzgXCDmUECg XvLCXRjkb+X8cO7iKrW3cGxQ1W2GXqT41uOadNLdEdHHL1JXecI6ESy5m+hltfjKqsGG rKG3M1zpweRF1nPjhBRKlULb1BB+zq6kFsTy4AkQYycDeaLNhYBb/sF/K1DFW5rASd+3 sEKbypNM3DXAq3Mn4ICh1AtgM4W1S7/xa9Adl/S7+dkUwjsdnh7+oCVM31hqVh5hbb7M XcP4aBkwSiG+b97G//h4zH+qKPYsjgBJIFmmbhvKMNoaWjHe8QfKJfNW0e8Md6Y/8z4c In9w== X-Gm-Message-State: ABuFfogsdm4vZfCd7pnBhO6dyIHvAabhORZ0yNttw8Oh4g3b1ubBaTIz EayqU8c6m3iHzfDxIBK+PmCeYQ== X-Received: by 2002:a1c:e141:: with SMTP id y62-v6mr2093121wmg.138.1539091739058; Tue, 09 Oct 2018 06:28:59 -0700 (PDT) Received: from brauner.io (u-084-c138.eap.uni-tuebingen.de. [134.2.84.138]) by smtp.gmail.com with ESMTPSA id o3-v6sm14128055wrw.93.2018.10.09.06.28.57 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 09 Oct 2018 06:28:58 -0700 (PDT) Date: Tue, 9 Oct 2018 15:28:52 +0200 From: Christian Brauner To: Jann Horn Cc: Tycho Andersen , Kees Cook , Linux API , containers@lists.linux-foundation.org, suda.akihiro@lab.ntt.co.jp, Oleg Nesterov , kernel list , "Eric W. Biederman" , linux-fsdevel@vger.kernel.org, Christian Brauner , Andy Lutomirski , linux-security-module Subject: Re: [PATCH v7 3/6] seccomp: add a way to get a listener fd from ptrace Message-ID: <20181009132850.fp6yne2vgmfpi27k@brauner.io> References: <20180927151119.9989-1-tycho@tycho.ws> <20180927151119.9989-4-tycho@tycho.ws> <20181008151629.hkgzzsluevwtuclw@brauner.io> <20181008162147.ubfxxsv2425l2zsp@brauner.io> <20181008181815.pwnqxngj22mhm2vj@brauner.io> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 09, 2018 at 02:39:53PM +0200, Jann Horn wrote: > On Mon, Oct 8, 2018 at 8:18 PM Christian Brauner wrote: > > On Mon, Oct 08, 2018 at 06:42:00PM +0200, Jann Horn wrote: > > > On Mon, Oct 8, 2018 at 6:21 PM Christian Brauner wrote: > > > > On Mon, Oct 08, 2018 at 05:33:22PM +0200, Jann Horn wrote: > > > > > On Mon, Oct 8, 2018 at 5:16 PM Christian Brauner wrote: > > > > > > On Thu, Sep 27, 2018 at 09:11:16AM -0600, Tycho Andersen wrote: > > > > > > > diff --git a/kernel/seccomp.c b/kernel/seccomp.c > > > > > > > index 44a31ac8373a..17685803a2af 100644 > > > > > > > --- a/kernel/seccomp.c > > > > > > > +++ b/kernel/seccomp.c > > > > > > > @@ -1777,4 +1777,35 @@ static struct file *init_listener(struct task_struct *task, > > > > > > > > > > > > > > return ret; > > > > > > > } > > > > > > > + > > > > > > > +long seccomp_new_listener(struct task_struct *task, > > > > > > > + unsigned long filter_off) > > > > > > > +{ > > > > > > > + struct seccomp_filter *filter; > > > > > > > + struct file *listener; > > > > > > > + int fd; > > > > > > > + > > > > > > > + if (!capable(CAP_SYS_ADMIN)) > > > > > > > + return -EACCES; > > > > > > > > > > > > I know this might have been discussed a while back but why exactly do we > > > > > > require CAP_SYS_ADMIN in init_userns and not in the target userns? What > > > > > > if I want to do a setns()fd, CLONE_NEWUSER) to the target process and > > > > > > use ptrace from in there? > > > > > > > > > > See https://lore.kernel.org/lkml/CAG48ez3R+ZJ1vwGkDfGzKX2mz6f=jjJWsO5pCvnH68P+RKO8Ow@mail.gmail.com/ > > > > > . Basically, the problem is that this doesn't just give you capability > > > > > over the target task, but also over every other task that has the same > > > > > filter installed; you need some sort of "is the caller capable over > > > > > the filter and anyone who uses it" check. > > > > > > > > Thanks. > > > > But then this new ptrace feature as it stands is imho currently broken. > > > > If you can install a seccomp filter with SECCOMP_RET_USER_NOTIF if you > > > > are ns_cpabable(CAP_SYS_ADMIN) and also get an fd via seccomp() itself > > > > if you are ns_cpabable(CAP_SYS_ADMIN) > > Actually, you don't need CAP_SYS_ADMIN for seccomp() at all as long as > you enable the NNP flag, I think? Yes, if you turn on NNP you don't even need sys_admin. > > > > > then either the new ptrace() api > > > > extension should be fixed to allow for this too or the seccomp() way of > > > > retrieving the pid - which I really think we want - needs to be fixed to > > > > require capable(CAP_SYS_ADMIN) too. > > > > The solution where both require ns_capable(CAP_SYS_ADMIN) is - imho - > > > > the preferred way to solve this. > > > > Everything else will just be confusing. > > > > > > First you say "broken", then you say "confusing". Which one do you mean? > > > > Both. It's broken in so far as it places a seemingly unnecessary > > restriction that could be fixed. You outlined one possible fix yourself > > in the link you provided. > > If by "possible fix" you mean "check whether the seccomp filter is > only attached to a single task": That wouldn't fundamentally change > the situation, it would only add an additional special case. > > > And it's confusing in so far as there is a way > > via seccomp() to get the fd without said requirement. > > I don't find it confusing at all. seccomp() and ptrace() are very Fine, then that's a matter of opinion. I find it counterintuitive that you can get an fd without privileges via one interface but not via another. > different situations: When you use seccomp(), infrastructure is Sure. Note, that this is _one_ of the reasons why I want to make sure we keep the native seccomp() only based way of getting an fd without forcing userspace to switching to a differnet kernel api. > already in place for ensuring that your filter is only applied to > processes over which you are capable, and propagation is limited by > inheritance from your task down. When you use ptrace(), you need a > pretty different sort of access check that checks whether you're > privileged over ancestors, siblings and so on of the target task. So, don't get me wrong I'm not arguing against the ptrace() interface in general. If this is something that people find useful, fine. But, I would like to have a simple single-syscall pure-seccomp() based way of getting an fd, i.e. what we have in patch 1 of this series. > > But thinking about it more, I think that CAP_SYS_ADMIN over the saved > current->mm->user_ns of the task that installed the filter (stored as > a "struct user_namespace *" in the filter) should be acceptable. Hm... Why not CAP_SYS_PTRACE? One more thing. Citing from [1] > I think there's a security problem here. Imagine the following scenario: > > 1. task A (uid==0) sets up a seccomp filter that uses SECCOMP_RET_USER_NOTIF > 2. task A forks off a child B > 3. task B uses setuid(1) to drop its privileges > 4. task B becomes dumpable again, either via prctl(PR_SET_DUMPABLE, 1) > or via execve() > 5. task C (the attacker, uid==1) attaches to task B via ptrace > 6. task C uses PTRACE_SECCOMP_NEW_LISTENER on task B Sorry, to be late to the party but would this really pass __ptrace_may_access() in ptrace_attach()? It doesn't seem obvious to me that it would... Doesn't look like it would get past: tcred = __task_cred(task); if (uid_eq(caller_uid, tcred->euid) && uid_eq(caller_uid, tcred->suid) && uid_eq(caller_uid, tcred->uid) && gid_eq(caller_gid, tcred->egid) && gid_eq(caller_gid, tcred->sgid) && gid_eq(caller_gid, tcred->gid)) goto ok; if (ptrace_has_cap(tcred->user_ns, mode)) goto ok; rcu_read_unlock(); return -EPERM; ok: rcu_read_unlock(); mm = task->mm; if (mm && ((get_dumpable(mm) != SUID_DUMP_USER) && !ptrace_has_cap(mm->user_ns, mode))) return -EPERM; > 7. because the seccomp filter is shared by task A and task B, task C > is now able to influence syscall results for syscalls performed by > task A [1]: https://lore.kernel.org/lkml/CAG48ez3R+ZJ1vwGkDfGzKX2mz6f=jjJWsO5pCvnH68P+RKO8Ow@mail.gmail.com/