Received: by 2002:a05:7412:a9a3:b0:f9:327e:43ab with SMTP id o35csp60348rdh; Mon, 18 Dec 2023 04:31:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IEE/gNcVfgGLwHt+m+p1FQDzJT9RTC/nvH6Yc4dXrXtu5NXa39H7iljbg+YtWUp4DpdS9Us X-Received: by 2002:a05:6a20:3d8b:b0:191:bc5f:e813 with SMTP id s11-20020a056a203d8b00b00191bc5fe813mr11487955pzi.85.1702902662191; Mon, 18 Dec 2023 04:31:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702902662; cv=none; d=google.com; s=arc-20160816; b=qGWzFHUD15Wt/+jiF6zVo+307Pw2NYYlDwz8YerFhHhdADhp62rm15ghO9XVjoR59g AFE43NRa9ibvihnyRsfU1RXLsO0t6cH+jNjfyBAyXuapZEbWWBwPBWDr8TK0Gl3SZi+e 1EBDeuF6sF6ztHipIge5oCLAYZU8IznRppxLSwMETDFGcL7v+7DKa42cDae90WN4+W9C 0DHpXRbr6A8AxzrAqlS6N/cEgcCHFVLq0pn9kWxYxe1NcCcbHHJkXDLG5kYyr0WOdo6p KIZQfVP4AqzG/TxSMWmCJ+8CKk8U7OPSS8i8fZZrLGLXjKOTeT9l+y0YhgMili+wY2tb oVow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=yVXRbjtdFraOWL1XW85nafJ3yF2y+U2euc21R1UvdB4=; fh=SrefI3hdaz+ntbx0u5bo3KNwV8JR+LMJu/eiBT6d3CU=; b=IYkdPO2YHEk2Ka+HWmLAvrHm6HoUENOkXR5FmGCXPhj7Jn8/O7QdC1xRaXmS+Ewnpu Xngyh7nrcPzVayzuvL7IwUTjCyYLbevrgDkJiYFMmrQvx1snbWhj6tjw9Y+tUq+y+ddp WawyANV2qD9lgQI2+q23c5bOBVAjQ6xVPCRze9qGy2qxzeKL+dCnUlwmN+jFam6nDcB+ DCCWTC3xc+GGzCPd0t/6+ymWm6uhddw1deRGpFi+1ejW8ImacagqZN3MQ33B5wx7kGzD RMIj7Y6KHTj3pQauzvO9fqpJaiZ14IdcAbefEaQ6BW7FSHQjNDWWtnI2RcEY5hk68AZ5 auwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=USyQ0cig; spf=pass (google.com: domain of linux-kernel+bounces-3568-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3568-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id ca11-20020a056a00418b00b006d8d123ba0fsi367444pfb.306.2023.12.18.04.31.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 04:31:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3568-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=USyQ0cig; spf=pass (google.com: domain of linux-kernel+bounces-3568-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3568-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 00614B21D5F for ; Mon, 18 Dec 2023 12:30:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 966734B5CA; Mon, 18 Dec 2023 12:30:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="USyQ0cig" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD9CB4B5DE; Mon, 18 Dec 2023 12:30:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 933AFC433C7; Mon, 18 Dec 2023 12:30:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702902643; bh=AA/8yh8b80ZQsaF4+TUE7HQt/5ZSOagkn1HY7nMlrt8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=USyQ0cigRyoU+7qQTCkP8/cqIxRRjB8ZXVPg70kxRRZImeGrhq0VS6W/7VJeP18bV +9y+z0XXYn0BLZ3MwXql72rE4PylbwEoo+U397TxkRZbkQDdh5hjGrVdrxJvY1u6SC ufaBvvHtHI537nNdedn2VcGMlKQ9ApfN5cV+wieiJRSCxq7kP9QEKOHeX+FLLpl1Ez 5hF2saq1XZC9DwArwRF8BxTk/WaAfo3p8cLaqiv+1juAnojmMVVL2AEic8YFjRsEXM xxNdvPNPRTPSA0N45n8M0prwDIG0CYHAueVlNPDDrIhZxmKoutDKuluniYSLCcqN2y WfWbSmNv2E/1Q== Date: Mon, 18 Dec 2023 13:30:34 +0100 From: Christian Brauner To: Alexei Starovoitov Cc: Michael =?utf-8?B?V2Vpw58=?= , Alexander Mikhalitsyn , Alexei Starovoitov , Paul Moore , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Quentin Monnet , Alexander Viro , Miklos Szeredi , Amir Goldstein , "Serge E. Hallyn" , bpf , LKML , Linux-Fsdevel , LSM List , gyroidos@aisec.fraunhofer.de Subject: Re: [RFC PATCH v3 3/3] devguard: added device guard for mknod in non-initial userns Message-ID: <20231218-chipsatz-abfangen-d62626dfb9e2@brauner> References: <20231213143813.6818-1-michael.weiss@aisec.fraunhofer.de> <20231213143813.6818-4-michael.weiss@aisec.fraunhofer.de> <20231215-golfanlage-beirren-f304f9dafaca@brauner> <61b39199-022d-4fd8-a7bf-158ee37b3c08@aisec.fraunhofer.de> <20231215-kubikmeter-aufsagen-62bf8d4e3d75@brauner> <20231216-vorrecht-anrief-b096fa50b3f7@brauner> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Sat, Dec 16, 2023 at 09:41:10AM -0800, Alexei Starovoitov wrote: > On Sat, Dec 16, 2023 at 2:38 AM Christian Brauner wrote: > > > > On Fri, Dec 15, 2023 at 10:08:08AM -0800, Alexei Starovoitov wrote: > > > On Fri, Dec 15, 2023 at 6:15 AM Christian Brauner wrote: > > > > > > > > On Fri, Dec 15, 2023 at 02:26:53PM +0100, Michael Weiß wrote: > > > > > On 15.12.23 13:31, Christian Brauner wrote: > > > > > > On Wed, Dec 13, 2023 at 03:38:13PM +0100, Michael Weiß wrote: > > > > > >> devguard is a simple LSM to allow CAP_MKNOD in non-initial user > > > > > >> namespace in cooperation of an attached cgroup device program. We > > > > > >> just need to implement the security_inode_mknod() hook for this. > > > > > >> In the hook, we check if the current task is guarded by a device > > > > > >> cgroup using the lately introduced cgroup_bpf_current_enabled() > > > > > >> helper. If so, we strip out SB_I_NODEV from the super block. > > > > > >> > > > > > >> Access decisions to those device nodes are then guarded by existing > > > > > >> device cgroups mechanism. > > > > > >> > > > > > >> Signed-off-by: Michael Weiß > > > > > >> --- > > > > > > > > > > > > I think you misunderstood me... My point was that I believe you don't > > > > > > need an additional LSM at all and no additional LSM hook. But I might be > > > > > > wrong. Only a POC would show. > > > > > > > > > > Yeah sorry, I got your point now. > > > > > > > > I think I might have had a misconception about how this works. > > > > A bpf LSM program can't easily alter a kernel object such as struct > > > > super_block I've been told. > > > > > > Right. bpf cannot change arbitrary kernel objects, > > > but we can add a kfunc that will change a specific bit in a specific > > > data structure. > > > Adding a new lsm hook that does: > > > rc = call_int_hook(sb_device_access, 0, sb); > > > switch (rc) { > > > case 0: do X > > > case 1: do Y > > > > > > is the same thing, but uglier, since return code will be used > > > to do this action. > > > The 'do X' can be one kfunc > > > and 'do Y' can be another. > > > If later we find out that 'do X' is not a good idea we can remove > > > that kfunc. > > > > The reason I moved the SB_I_MANAGED_DEVICES here is that I want a single > > central place where that is done for any possible LSM that wants to > > implement device management. So we don't have to go chasing where that > > bit is set for each LSM. I also don't want to have LSMs raise bits in > > sb->s_iflags directly as that's VFS property. > > a kfunc that sets a bit in sb->s_iflags will be the same central place. For the BPF LSM. I'm talking the same place for al LSMs. > It will be somewhere in the fs/ directory and vfs maintainers can do what they > wish with it, including removal. > For traditional LSM one would need to do an accurate code review to make > sure that they don't mess with sb->s_iflags while for bpf_lsm it > will be done automatically. That kfunc will be that only one central place. I'm not generally opposed to kfuncs ofc but here it just seems a bit pointless. What we want is to keep SB_I_{NODEV,MANAGED_DEVICES} confined to alloc_super(). The only central place it's raised where we control all locking and logic. So it doesn't even have to appear in any security_*() hooks. diff --git a/security/security.c b/security/security.c index 088a79c35c26..bf440d15615d 100644 --- a/security/security.c +++ b/security/security.c @@ -1221,6 +1221,33 @@ int security_sb_alloc(struct super_block *sb) return rc; } +/* + * security_sb_device_access() - Let LSMs handle device access + * @sb: filesystem superblock + * + * Let an LSM take over device access management for this superblock. + * + * Return: Returns 1 if LSMs handle device access, 0 if none does and -ERRNO on + * failure. + */ +int security_sb_device_access(struct super_block *sb) +{ + int thisrc; + int rc = LSM_RET_DEFAULT(sb_device_access); + struct security_hook_list *hp; + + hlist_for_each_entry(hp, &security_hook_heads.sb_device_access, list) { + thisrc = hp->hook.sb_device_access(sb); + if (thisrc < 0) + return thisrc; + /* At least one LSM claimed device access management. */ + if (thisrc == 1) + rc = 1; + } + + return rc; +} + /** * security_sb_delete() - Release super_block LSM associated objects * @sb: filesystem superblock diff --git a/fs/super.c b/fs/super.c index 076392396e72..2295c0f76e56 100644 --- a/fs/super.c +++ b/fs/super.c @@ -325,7 +325,7 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags, { struct super_block *s = kzalloc(sizeof(struct super_block), GFP_USER); static const struct super_operations default_op; - int i; + int err, i; if (!s) return NULL; @@ -362,8 +362,16 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags, } s->s_bdi = &noop_backing_dev_info; s->s_flags = flags; - if (s->s_user_ns != &init_user_ns) + + err = security_sb_device_access(s); + if (err < 0) + goto fail; + + if (err) + s->s_iflags |= SB_I_MANAGED_DEVICES; + else if (s->s_user_ns != &init_user_ns) s->s_iflags |= SB_I_NODEV; + INIT_HLIST_NODE(&s->s_instances); INIT_HLIST_BL_HEAD(&s->s_roots); mutex_init(&s->s_sync_lock);