Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2741436pxb; Mon, 19 Apr 2021 12:42:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw8IB4YkVBIWbnvYO495uKCwp6s6gGvgNSKaeFtu1fC9QfK6HcUkEwEt1iFpSrFiuk3+XDB X-Received: by 2002:a05:6402:30a3:: with SMTP id df3mr13731054edb.230.1618861371143; Mon, 19 Apr 2021 12:42:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618861371; cv=none; d=google.com; s=arc-20160816; b=NhdhTKZeHz9FlC8kiRCkDrLV2QkhbSKHXUSwIy/Z0PxqDaHkzPhCuwNJFDk+1lzhT6 PjhoGB9k2aIwD4GPQ5PVjQL+0GnaIj4Wyc4rM+IxNrnd1+IFEq9ObQ7+DUlky2FQYQsf Dgf006Bl0xPiSWwnKom0CEIlDMuD8snpITzp8QOZ7/0vSGqPu0GTelNELZVPoqR2j9JP ywG/8jlVhZwM8OHxnH3jQwnw6mf0OB9zumYUojDsOVjyRHvWt8kCDxRYEgM/sJ4dvB4B EeslaSgMz8tiJ55iutjbMF3wjLzwHpHmzxKixrY7rUpxJ/gk5khYQYl+kvHPxS3zUFGj kF2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=iOI5LrX2XvJYa0Pw4nXUnP+WOmrtpcHcZRisQeHjbr4=; b=Q421PB3oeN8r4aTzzFcDxSARcKL4U5v3F9ylVx/X3cOmMSDCKdmNu05kv8y+rQb7bf +D4kG4tm4ioVrhutWrhcqblnmdcFricpLssePhPhlcawUB9iznGaS8jQObj0VHIux4ky DCU1/usvs2Un6TI3StNHF/dmIokP62HE2cG81iJmUc6N1obU3IVdIb+ja9SRomiX88fK FXx3pQcl19UcagvepsAFBd+MGvvWZq7IYojuchk4+PesCFrdxu/RGFHv2l52avCAdqO+ qSc+/qBGdM9Hk8krs0zInnnIvIAq97sRWLIoK9hCfFrnVhgxi5OvYqQJ9U6DJ2834bcw XPmw== 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 cb26si6680413edb.240.2021.04.19.12.42.26; Mon, 19 Apr 2021 12:42:51 -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 S232305AbhDSQJt (ORCPT + 99 others); Mon, 19 Apr 2021 12:09:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:40502 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229842AbhDSQJs (ORCPT ); Mon, 19 Apr 2021 12:09:48 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2F2B961363; Mon, 19 Apr 2021 16:09:14 +0000 (UTC) Date: Mon, 19 Apr 2021 18:09:11 +0200 From: Christian Brauner To: "Serge E. Hallyn" Cc: lkml , Linus Torvalds , Kees Cook , "Andrew G. Morgan" , "Eric W. Biederman" , Greg Kroah-Hartman , security@kernel.org, Tycho Andersen , Andy Lutomirski Subject: Re: [PATCH] capabilities: require CAP_SETFCAP to map uid 0 (v3.3) Message-ID: <20210419160911.5pguvpj7kfuj6rnr@wittgenstein> References: <20210416045851.GA13811@mail.hallyn.com> <20210416150501.zam55gschpn2w56i@wittgenstein> <20210416213453.GA29094@mail.hallyn.com> <20210417021945.GA687@mail.hallyn.com> <20210417200434.GA17430@mail.hallyn.com> <20210419122514.GA20598@mail.hallyn.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210419122514.GA20598@mail.hallyn.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 19, 2021 at 07:25:14AM -0500, Serge Hallyn wrote: > cap_setfcap is required to create file capabilities. > > Since 8db6c34f1dbc ("Introduce v3 namespaced file capabilities"), a > process running as uid 0 but without cap_setfcap is able to work around > this as follows: unshare a new user namespace which maps parent uid 0 > into the child namespace. While this task will not have new > capabilities against the parent namespace, there is a loophole due to > the way namespaced file capabilities are represented as xattrs. File > capabilities valid in userns 1 are distinguished from file capabilities > valid in userns 2 by the kuid which underlies uid 0. Therefore the > restricted root process can unshare a new self-mapping namespace, add a > namespaced file capability onto a file, then use that file capability in > the parent namespace. > > To prevent that, do not allow mapping parent uid 0 if the process which > opened the uid_map file does not have CAP_SETFCAP, which is the capability > for setting file capabilities. > > As a further wrinkle: a task can unshare its user namespace, then > open its uid_map file itself, and map (only) its own uid. In this > case we do not have the credential from before unshare, which was > potentially more restricted. So, when creating a user namespace, we > record whether the creator had CAP_SETFCAP. Then we can use that > during map_write(). > > With this patch: > > 1. Unprivileged user can still unshare -Ur > > ubuntu@caps:~$ unshare -Ur > root@caps:~# logout > > 2. Root user can still unshare -Ur > > ubuntu@caps:~$ sudo bash > root@caps:/home/ubuntu# unshare -Ur > root@caps:/home/ubuntu# logout > > 3. Root user without CAP_SETFCAP cannot unshare -Ur: > > root@caps:/home/ubuntu# /sbin/capsh --drop=cap_setfcap -- > root@caps:/home/ubuntu# /sbin/setcap cap_setfcap=p /sbin/setcap > unable to set CAP_SETFCAP effective capability: Operation not permitted > root@caps:/home/ubuntu# unshare -Ur > unshare: write failed /proc/self/uid_map: Operation not permitted > > Note: an alternative solution would be to allow uid 0 mappings by > processes without CAP_SETFCAP, but to prevent such a namespace from > writing any file capabilities. This approach can be seen here: > https://git.kernel.org/pub/scm/linux/kernel/git/sergeh/linux.git/log/?h=2021-04-15/setfcap-nsfscaps-v4 > Ah, can you link to the previous fix and its revert, please? I think that was mentioned in the formerly private thread as well but we forgot: commit 95ebabde382c371572297915b104e55403674e73 Author: Eric W. Biederman Date: Thu Dec 17 09:42:00 2020 -0600 capabilities: Don't allow writing ambiguous v3 file capabilities commit 3b0c2d3eaa83da259d7726192cf55a137769012f Author: Eric W. Biederman Date: Fri Mar 12 15:07:09 2021 -0600 Revert 95ebabde382c ("capabilities: Don't allow writing ambiguous v3 file capabilities") > Signed-off-by: Serge Hallyn > Reviewed-by: Andrew G. Morgan > Tested-by: Christian Brauner > Reviewed-by: Christian Brauner > Cc: "Eric W. Biederman"