Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3260486ybz; Mon, 27 Apr 2020 12:52:39 -0700 (PDT) X-Google-Smtp-Source: APiQypLLNPz3jBj1zmHxfKGiS4JOj09pHllgYDI/QB4L6TZT65I+utelj5bS2F5X+QYe3CoyxkLo X-Received: by 2002:a17:906:9482:: with SMTP id t2mr20820009ejx.241.1588017159574; Mon, 27 Apr 2020 12:52:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588017159; cv=none; d=google.com; s=arc-20160816; b=V30swbKFI6/0cplHbA+V6tAobvZ0p1cfXOzyBAXXtqM4sVGK8gzfB4OkUfpbsFpKwn dWMrNlOM3OH33mOgaa9boxN0yDd1vLSZHY2lOmc3bTV5JGVAdlo0MW+qMyvRSjrG+KYX rUYSgVTY2eE3ano1xB0VO8b8srd0oEoFNuN6e5H0mZmKneT/rGsKOUkjcYFqbPfRP8+p ajidvcEopvFrBpVbP63bIK9H1bgmW0GJuTnwYoi2Z9crDgLyIB7Gf3+PJjutbqhti0OC 2tiMHQkShXbIgdUjd7QodWTRTr0X9esFKea/EZP04vsfvRqUi0/0bZzDBOe8oTaSIDeN rHlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=BasAf39XH3+JVS7lQSPv4viiSqs2xF8M0m5dhukstgc=; b=hqSZtbnRv2OhgkiglOga5dPctViZGss8aZKAwJDPBH/OqB6OUYuHfEKciZHznVG7Ct OdHlQyA0kMTImRpc33n47I/BXB8Dy3VnO9TMmq8NV4+qqdcg2k5FlHkhUNMQaHMwlv9G YvmumUjcy3+vx9qKa081TxoPgf+4liVblkn4vZYFs1JSZe50uUkWl8hYPEcIMrQ3Quqd Tvi41VaYfVnrrVk+Qyufynd6E6sg6r4AHAW5mLlfIPlWRyljpdTCBAPgr9Gm/kQMYh5e rFi+UXPHLHtnMQ2AAUTo/jMhdH66HGWDLqI5stSxbKZtoDixXNLLmYR3S4wvSxVh9bTm qeTA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v10si309344edl.365.2020.04.27.12.52.15; Mon, 27 Apr 2020 12:52:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726548AbgD0TsU (ORCPT + 99 others); Mon, 27 Apr 2020 15:48:20 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:43192 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725919AbgD0TsU (ORCPT ); Mon, 27 Apr 2020 15:48:20 -0400 Received: from ip5f5af183.dynamic.kabel-deutschland.de ([95.90.241.131] helo=wittgenstein) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1jT9jk-0003p6-AT; Mon, 27 Apr 2020 19:48:16 +0000 Date: Mon, 27 Apr 2020 21:48:15 +0200 From: Christian Brauner To: Jann Horn Cc: kernel list , Alexander Viro , =?utf-8?B?U3TDqXBoYW5l?= Graber , Linux Containers , "Eric W . Biederman" , Serge Hallyn , Aleksa Sarai , linux-security-module , Kernel Hardening , Linux API Subject: Re: [PATCH] nsproxy: attach to namespaces via pidfds Message-ID: <20200427194815.nc22k7mj543swk7z@wittgenstein> References: <20200427143646.619227-1-christian.brauner@ubuntu.com> <20200427181507.ry3hw7ufiifwhi5k@wittgenstein> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 27, 2020 at 09:41:20PM +0200, Jann Horn wrote: > On Mon, Apr 27, 2020 at 8:15 PM Christian Brauner > wrote: > > On Mon, Apr 27, 2020 at 07:28:56PM +0200, Jann Horn wrote: > > > On Mon, Apr 27, 2020 at 4:47 PM Christian Brauner > > > wrote: > [...] > > > > That means > > > > setns(nsfd, CLONE_NEWNET) equals setns(pidfd, CLONE_NEWNET). However, > > > > when a pidfd is passed, multiple namespace flags can be specified in the > > > > second setns() argument and setns() will attach the caller to all the > > > > specified namespaces all at once or to none of them. If 0 is specified > > > > together with a pidfd then setns() will interpret it the same way 0 is > > > > interpreted together with a nsfd argument, i.e. attach to any/all > > > > namespaces. > > > [...] > > > > Apart from significiantly reducing the number of syscalls from double > > > > digit to single digit which is a decent reason post-spectre/meltdown > > > > this also allows to switch to a set of namespaces atomically, i.e. > > > > either attaching to all the specified namespaces succeeds or we fail. > > > > > > Apart from the issues I've pointed out below, I think it's worth > > > calling out explicitly that with the current design, the switch will > > > not, in fact, be fully atomic - the process will temporarily be in > > > intermediate stages where the switches to some namespaces have > > > completed while the switches to other namespaces are still pending; > > > and while there will be less of these intermediate stages than before, > > > it also means that they will be less explicit to userspace. > > > > Right, that can be fixed by switching to the unshare model of getting a > > new set of credentials and committing it after the nsproxy has been > > installed? Then there shouldn't be an intermediate state anymore or > > rather an intermediate stage where we can still fail somehow. > > It still wouldn't be atomic (in the sense of parallelism, not in the > sense of intermediate error handling) though; for example, if task B > does setns(, 0) and task C concurrently does > setns(, 0), then task C may end up with the new mount > namespace of task B but the old user namespace, or something like > that. If C is more privileged than B, that may cause C to have more > privileges through its configuration of namespaces than B does (e.g. > by running in the &init_user_ns but with a mount namespace owned by an > unprivileged user), which C may not expect. Same thing for racing > between unshare() and setns(). > > [...] > > > > + put_user_ns(user_ns); > > > > + } > > > > +#else > > > > + if (flags & CLONE_NEWUSER) > > > > + ret = -EINVAL; > > > > +#endif > > > > + > > > > + if (!ret && wants_ns(flags, CLONE_NEWNS)) > > > > + ret = __ns_install(nsproxy, mnt_ns_to_common(nsp->mnt_ns)); > > > > > > And this one might be even worse, because the mount namespace change > > > itself is only stored in the nsproxy at this point, but the cwd and > > > root paths have already been overwritten on the task's fs_struct. > > > > > > To actually make sys_set_ns() atomic, I think you'd need some > > > moderately complicated prep work, splitting the ->install handlers up > > > into prep work and a commit phase that can't fail. > > > > Wouldn't it be sufficient to move to an unshare like model, i.e. > > creating a new set of creds, and passing the new user_ns to > > create_new_namespaces() as well as having a temporary new_fs struct? > > That should get rid of all intermediate stages. > > Ah, good point, I didn't realize that that already exists for unshare(). Let me try and switch the patch to that. Christian