From nobody Fri May 8 11:36:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3A67C433FE for ; Wed, 4 May 2022 19:54:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357943AbiEDT6O (ORCPT ); Wed, 4 May 2022 15:58:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377708AbiEDT6G (ORCPT ); Wed, 4 May 2022 15:58:06 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A8654EA08 for ; Wed, 4 May 2022 12:54:26 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id fv2so2160035pjb.4 for ; Wed, 04 May 2022 12:54:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5CetlpCftC8fa22AwQ7f9noDAbp04u3fxXYOBJDWXdo=; b=i7PO7MJFWaOICLk7YlYbL76Mf0xaAQ93IS19rBh5kZDqEelU24XSWeAGy/kM3aRVFv 4pu6lwIhJ6xGsn9VmwyfLpA97TjUL5z/vu6itVt2PsL5RcBV9E3IssZWPxdHqY7Z0JCM iQsJUrBX3PvK+cr11Oe8toqnVFCeeZY6TVGqo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5CetlpCftC8fa22AwQ7f9noDAbp04u3fxXYOBJDWXdo=; b=A8ft8o3jZgCI7orEa4hAmgwnLooJM6Yx9hk/HpKxK2YgXR0yIrcamo1zQm6xRltdQz /NK2a8qkcakDMTBw6Y0RVT+nYW+FMi8F10f5t2TFDUsVsdocndGPvEK+QthBFABo2+Al BUiz98VnWY3iAHzFHE/mbazsWA4yA6R42nMsUaBwCKh3d5kgEmwqQUsyVjPaMUFUn3qA daJa522Ck2nv+tJtimyk+NE4Cr5KGU62pj40PAuaCc714n5sP2blRqgQK70rZs5MJbUx Rwd5h/EoWA7yK3rnkN9mTNUs6l8Utc763J7/Z8fGYgBK9bIR03nNn0LbzdyK2RbOOd+H /bxg== X-Gm-Message-State: AOAM531g+okBYHZoOo0+HvWNcmPKpeq4pvAuCeJ8HcaxlFbtL+wv85fC guTt+DuDwT2nf1dotRquQ7bI0w== X-Google-Smtp-Source: ABdhPJzmCKD4KIjLDv8vNDJK4ppeaWC2YAVRNAFyqVpurA+XTRKpw2HAvCD1vyC3w23XbEThwD8IPQ== X-Received: by 2002:a17:902:ecc8:b0:15e:9e46:cb7e with SMTP id a8-20020a170902ecc800b0015e9e46cb7emr18203039plh.111.1651694065547; Wed, 04 May 2022 12:54:25 -0700 (PDT) Received: from localhost ([2620:15c:202:201:35b6:c77b:be04:3bd5]) by smtp.gmail.com with UTF8SMTPSA id n5-20020aa79045000000b0050dc7628143sm8586582pfo.29.2022.05.04.12.54.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 May 2022 12:54:25 -0700 (PDT) From: Matthias Kaehlcke To: Alasdair Kergon , Mike Snitzer , Kees Cook , James Morris , "Serge E . Hallyn" Cc: dm-devel@redhat.com, linux-kernel@vger.kernel.org, linux-raid@vger.kernel.org, Song Liu , Douglas Anderson , linux-security-module@vger.kernel.org, Matthias Kaehlcke Subject: [PATCH v3 1/3] dm: Add verity helpers for LoadPin Date: Wed, 4 May 2022 12:54:17 -0700 Message-Id: <20220504125404.v3.1.I3e928575a23481121e73286874c4c2bdb403355d@changeid> X-Mailer: git-send-email 2.36.0.464.gb9c8b46e94-goog In-Reply-To: <20220504195419.1143099-1-mka@chromium.org> References: <20220504195419.1143099-1-mka@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" LoadPin limits loading of kernel modules, firmware and certain other files to a 'pinned' file system (typically a read-only rootfs). To provide more flexibility LoadPin is being extended to also allow loading these files from trusted dm-verity devices. For that purpose LoadPin can be provided with a list of verity root digests that it should consider as trusted. Add a bunch of helpers to allow LoadPin to check whether a DM device is a trusted verity device. The new functions broadly fall in two categories: those that need access to verity internals (like the root digest), and the 'glue' between LoadPin and verity. The new file dm-verity-loadpin.c contains the glue functions. Signed-off-by: Matthias Kaehlcke --- Changes in v3: - none Changes in v2: - none drivers/md/Makefile | 6 +++ drivers/md/dm-verity-loadpin.c | 80 +++++++++++++++++++++++++++++++ drivers/md/dm-verity-target.c | 33 +++++++++++++ drivers/md/dm-verity.h | 4 ++ include/linux/dm-verity-loadpin.h | 27 +++++++++++ 5 files changed, 150 insertions(+) create mode 100644 drivers/md/dm-verity-loadpin.c create mode 100644 include/linux/dm-verity-loadpin.h diff --git a/drivers/md/Makefile b/drivers/md/Makefile index 0454b0885b01..e12cd004d375 100644 --- a/drivers/md/Makefile +++ b/drivers/md/Makefile @@ -100,6 +100,12 @@ ifeq ($(CONFIG_IMA),y) dm-mod-objs +=3D dm-ima.o endif =20 +ifeq ($(CONFIG_DM_VERITY),y) +ifeq ($(CONFIG_SECURITY_LOADPIN),y) +dm-mod-objs +=3D dm-verity-loadpin.o +endif +endif + ifeq ($(CONFIG_DM_VERITY_FEC),y) dm-verity-objs +=3D dm-verity-fec.o endif diff --git a/drivers/md/dm-verity-loadpin.c b/drivers/md/dm-verity-loadpin.c new file mode 100644 index 000000000000..972ca93a2231 --- /dev/null +++ b/drivers/md/dm-verity-loadpin.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include + +#include "dm.h" +#include "dm-verity.h" + +static struct list_head *trusted_root_digests; + +/* + * Sets the root digests of verity devices which LoadPin considers as trus= ted. + * + * This function must only be called once. + */ +void dm_verity_loadpin_set_trusted_root_digests(struct list_head *digests) +{ + if (!trusted_root_digests) + trusted_root_digests =3D digests; + else + pr_warn("verity root digests trusted by LoadPin are already set!!!\n"); +} + +static bool is_trusted_verity_target(struct dm_target *ti) +{ + u8 *root_digest; + unsigned int digest_size; + struct trusted_root_digest *trd; + bool trusted =3D false; + + if (!dm_is_verity_target(ti)) + return false; + + if (dm_verity_get_root_digest(ti, &root_digest, &digest_size)) + return false; + + list_for_each_entry(trd, trusted_root_digests, node) { + if ((trd->len =3D=3D digest_size) && + !memcmp(trd->data, root_digest, digest_size)) { + trusted =3D true; + break; + } + } + + kfree(root_digest); + + return trusted; +} + +/* + * Determines whether a mapped device is a verity device that is trusted + * by LoadPin. + */ +bool dm_verity_loadpin_is_md_trusted(struct mapped_device *md) +{ + int srcu_idx; + struct dm_table *table; + unsigned int num_targets; + bool trusted =3D false; + int i; + + if (!trusted_root_digests || list_empty(trusted_root_digests)) + return false; + + table =3D dm_get_live_table(md, &srcu_idx); + num_targets =3D dm_table_get_num_targets(table); + for (i =3D 0; i < num_targets; i++) { + struct dm_target *ti =3D dm_table_get_target(table, i); + + if (is_trusted_verity_target(ti)) { + trusted =3D true; + break; + } + } + + dm_put_live_table(md, srcu_idx); + + return trusted; +} diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c index 80133aae0db3..6f07b849fcb2 100644 --- a/drivers/md/dm-verity-target.c +++ b/drivers/md/dm-verity-target.c @@ -19,6 +19,7 @@ #include #include #include +#include =20 #define DM_MSG_PREFIX "verity" =20 @@ -1310,6 +1311,38 @@ static int verity_ctr(struct dm_target *ti, unsigned= argc, char **argv) return r; } =20 +/* + * Check whether a DM target is a verity target. + */ +bool dm_is_verity_target(struct dm_target *ti) +{ + return ti->type->module =3D=3D THIS_MODULE; +} +EXPORT_SYMBOL_GPL(dm_is_verity_target); + +/* + * Get the root digest of a verity target. + * + * Returns a copy of the root digest, the caller is responsible for + * freeing the memory of the digest. + */ +int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest, unsi= gned int *digest_size) +{ + struct dm_verity *v =3D ti->private; + + if (!dm_is_verity_target(ti)) + return -EINVAL; + + *root_digest =3D kmemdup(v->root_digest, v->digest_size, GFP_KERNEL); + if (*root_digest =3D=3D NULL) + return -ENOMEM; + + *digest_size =3D v->digest_size; + + return 0; +} +EXPORT_SYMBOL_GPL(dm_verity_get_root_digest); + static struct target_type verity_target =3D { .name =3D "verity", .version =3D {1, 8, 0}, diff --git a/drivers/md/dm-verity.h b/drivers/md/dm-verity.h index 4e769d13473a..c832cc3e3d24 100644 --- a/drivers/md/dm-verity.h +++ b/drivers/md/dm-verity.h @@ -129,4 +129,8 @@ extern int verity_hash(struct dm_verity *v, struct ahas= h_request *req, extern int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io = *io, sector_t block, u8 *digest, bool *is_zero); =20 +extern bool dm_is_verity_target(struct dm_target *ti); +extern int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_diges= t, + unsigned int *digest_size); + #endif /* DM_VERITY_H */ diff --git a/include/linux/dm-verity-loadpin.h b/include/linux/dm-verity-lo= adpin.h new file mode 100644 index 000000000000..12a86911d05a --- /dev/null +++ b/include/linux/dm-verity-loadpin.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __LINUX_DM_VERITY_LOADPIN_H +#define __LINUX_DM_VERITY_LOADPIN_H + +#include + +struct mapped_device; + +struct trusted_root_digest { + u8 *data; + unsigned int len; + struct list_head node; +}; + +#if IS_ENABLED(CONFIG_SECURITY_LOADPIN) && IS_BUILTIN(CONFIG_DM_VERITY) +void dm_verity_loadpin_set_trusted_root_digests(struct list_head *digests); +bool dm_verity_loadpin_is_md_trusted(struct mapped_device *md); +#else +static inline void dm_verity_loadpin_set_trusted_root_digests(struct list_= head *digests) {} +static inline bool dm_verity_loadpin_is_md_trusted(struct mapped_device *m= d) +{ + return false; +} +#endif + +#endif /* __LINUX_DM_LOADPIN_H */ --=20 2.36.0.464.gb9c8b46e94-goog From nobody Fri May 8 11:36:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7E4DC433F5 for ; Wed, 4 May 2022 19:54:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377758AbiEDT6R (ORCPT ); Wed, 4 May 2022 15:58:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377749AbiEDT6H (ORCPT ); Wed, 4 May 2022 15:58:07 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52F2F4EA2D for ; Wed, 4 May 2022 12:54:28 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id iq2-20020a17090afb4200b001d93cf33ae9so6070931pjb.5 for ; Wed, 04 May 2022 12:54:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Gp9xlLGWh8jE9LSf7azZ10DOrZcyf+qHkQB0hbwlc6c=; b=Re8w0E71m1NuEGuoKe1+lP1K0mogJQPMQNwOvjgX8GgvdMmbzNvUUBh+uQTk019amF TvDUGdy3yBqpR3CrPi7vZz2JuiApQ08NyIFnvgWplvbqwg4OLntXWKscqGy1zf8jg9Tl X7yfbHlvVMNgs3QO+3sDsG6C8Gm9CJ+q9RwYM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Gp9xlLGWh8jE9LSf7azZ10DOrZcyf+qHkQB0hbwlc6c=; b=iDvouFAO1v/Luyk98N8mC9HxYZYmXUNCYfuG6y3uNUWiz3hC9Vx0x7S0ImgDq8BVhR ZMShfwFueL2soIQnFoTcMqUkam+ucL6EXqCYAEVMSNd4Z2Zen783MXJTWS+EXx/v2ztm QPUOj9V7r03DiFNmdVTFqGaWLvmvi6VePY+6NWSDOvZ/ppwZ5Z7mmZDturzzm3rPMyO2 RgfGXWwMweYgvE6q6x4yTp3RHgmVMko9UHIJ+H1yqmJp6Cqpm8q4TA5nv0ga7s74AOSv +MxGnyYZSxshZ46tzXxqDjpcdU/1MwrOkGCaDfEJInP0VXmHXu5VJfFyGfw2cM9zxjvm UipA== X-Gm-Message-State: AOAM531r9GqOpx06O8nOqI7AHZLjVKAyKheSdfLdm5cYBgBM2cyl3V7Y hr8OuK8Y4pRI9pExskgf/doolQ== X-Google-Smtp-Source: ABdhPJyBJFV2oTf0rlK//p4KZ3djT6dJ0kNaH/ylPKKm050V3L+WL306CMkubdaar/oyJwhAqtSE/w== X-Received: by 2002:a17:903:20f:b0:158:d86a:f473 with SMTP id r15-20020a170903020f00b00158d86af473mr23385549plh.92.1651694067352; Wed, 04 May 2022 12:54:27 -0700 (PDT) Received: from localhost ([2620:15c:202:201:35b6:c77b:be04:3bd5]) by smtp.gmail.com with UTF8SMTPSA id s7-20020a62e707000000b0050dc76281cfsm8598643pfh.169.2022.05.04.12.54.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 May 2022 12:54:26 -0700 (PDT) From: Matthias Kaehlcke To: Alasdair Kergon , Mike Snitzer , Kees Cook , James Morris , "Serge E . Hallyn" Cc: dm-devel@redhat.com, linux-kernel@vger.kernel.org, linux-raid@vger.kernel.org, Song Liu , Douglas Anderson , linux-security-module@vger.kernel.org, Matthias Kaehlcke Subject: [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices Date: Wed, 4 May 2022 12:54:18 -0700 Message-Id: <20220504125404.v3.2.I01c67af41d2f6525c6d023101671d7339a9bc8b5@changeid> X-Mailer: git-send-email 2.36.0.464.gb9c8b46e94-goog In-Reply-To: <20220504195419.1143099-1-mka@chromium.org> References: <20220504195419.1143099-1-mka@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Extend LoadPin to allow loading of kernel files from trusted dm-verity [1] devices. This change adds the concept of trusted verity devices to LoadPin. LoadPin maintains a list of root digests of verity devices it considers trusted. Userspace can populate this list through an ioctl on the new LoadPin securityfs entry 'dm-verity'. The ioctl receives a file descriptor of a file with verity digests as parameter. Verity reads the digests from this file after confirming that the file is located on the pinned root. The list of trusted digests can only be set up once, which is typically done at boot time. When a kernel file is read LoadPin first checks (as usual) whether the file is located on the pinned root, if so the file can be loaded. Otherwise, if the verity extension is enabled, LoadPin determines whether the file is located on a verity backed device and whether the root digest of that device is in the list of trusted digests. The file can be loaded if the verity device has a trusted root digest. Background: As of now LoadPin restricts loading of kernel files to a single pinned filesystem, typically the rootfs. This works for many systems, however it can result in a bloated rootfs (and OTA updates) on platforms where multiple boards with different hardware configurations use the same rootfs image. Especially when 'optional' files are large it may be preferable to download/install them only when they are actually needed by a given board. Chrome OS uses Downloadable Content (DLC) [2] to deploy certain 'packages' at runtime. As an example a DLC package could contain firmware for a peripheral that is not present on all boards. DLCs use dm-verity to verify the integrity of the DLC content. [1] https://www.kernel.org/doc/html/latest/admin-guide/device-mapper/verity= .html [2] https://chromium.googlesource.com/chromiumos/platform2/+/HEAD/dlcservic= e/docs/developer.md Signed-off-by: Matthias Kaehlcke Reported-by: kernel test robot --- Changes in v3: - added securityfs for LoadPin (currently only populated when CONFIG_SECURITY_LOADPIN_VERITY=3Dy) - added uapi include for LoadPin - changed the interface for setting up the list of trusted digests from sysctl to ioctl on securityfs entry - added stub for loadpin_is_fs_trusted() to be used CONFIG_SECURITY_LOADPIN_VERITY is not select - depend on CONFIG_SECURITYFS instead of CONFIG_SYSTCL - updated Kconfig help - minor changes in read_trusted_verity_root_digests() - updated commit message Changes in v2: - userspace now passes the path of the file with the verity digests via systcl, instead of the digests themselves - renamed sysctl file to 'trusted_verity_root_digests_path' - have CONFIG_SECURITY_LOADPIN_VERITY depend on CONFIG_SYSCTL - updated Kconfig doc - updated commit message include/uapi/linux/loadpin.h | 19 ++++ security/loadpin/Kconfig | 16 +++ security/loadpin/loadpin.c | 184 ++++++++++++++++++++++++++++++++++- 3 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/loadpin.h diff --git a/include/uapi/linux/loadpin.h b/include/uapi/linux/loadpin.h new file mode 100644 index 000000000000..d303a582209b --- /dev/null +++ b/include/uapi/linux/loadpin.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * Copyright (c) 2022, Google LLC + */ + +#ifndef _UAPI_LINUX_LOOP_LOADPIN_H +#define _UAPI_LINUX_LOOP_LOADPIN_H + +#define LOADPIN_IOC_MAGIC 'L' + +/** + * LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS - Set up the root digests of ver= ity devices + * that loadpin should trust. + * + * Takes a file descriptor from which to read the root digests of trusted = verity devices. + */ +#define LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS _IOW(LOADPIN_IOC_MAGIC, 0x0= 0, unsigned int) + +#endif /* _UAPI_LINUX_LOOP_LOADPIN_H */ diff --git a/security/loadpin/Kconfig b/security/loadpin/Kconfig index 91be65dec2ab..e319ca8e3f3d 100644 --- a/security/loadpin/Kconfig +++ b/security/loadpin/Kconfig @@ -18,3 +18,19 @@ config SECURITY_LOADPIN_ENFORCE If selected, LoadPin will enforce pinning at boot. If not selected, it can be enabled at boot with the kernel parameter "loadpin.enforce=3D1". + +config SECURITY_LOADPIN_VERITY + bool "Allow reading files from certain other filesystems that use dm-veri= ty" + depends on DM_VERITY=3Dy && SECURITYFS + help + If selected LoadPin can allow reading files from filesystems + that use dm-verity. LoadPin maintains a list of verity root + digests it considers trusted. A verity backed filesystem is + considered trusted if its root digest is found in the list + of trusted digests. + + The list of trusted verity can be populated through an ioctl + on the LoadPin securityfs entry 'dm-verity'. The ioctl + expects a file descriptor of a file with verity digests as + parameter. The file must be located on the pinned root and + contain a comma separated list of digests. diff --git a/security/loadpin/loadpin.c b/security/loadpin/loadpin.c index b12f7d986b1e..c29ce562a366 100644 --- a/security/loadpin/loadpin.c +++ b/security/loadpin/loadpin.c @@ -18,6 +18,9 @@ #include #include /* current */ #include +#include +#include +#include =20 static void report_load(const char *origin, struct file *file, char *opera= tion) { @@ -43,6 +46,9 @@ static char *exclude_read_files[READING_MAX_ID]; static int ignore_read_file_id[READING_MAX_ID] __ro_after_init; static struct super_block *pinned_root; static DEFINE_SPINLOCK(pinned_root_spinlock); +#ifdef CONFIG_SECURITY_LOADPIN_VERITY +static LIST_HEAD(trusted_verity_root_digests); +#endif =20 #ifdef CONFIG_SYSCTL =20 @@ -118,6 +124,24 @@ static void loadpin_sb_free_security(struct super_bloc= k *mnt_sb) } } =20 +#ifdef CONFIG_SECURITY_LOADPIN_VERITY +static bool loadpin_is_fs_trusted(struct super_block *sb) +{ + struct mapped_device *md =3D dm_get_md(sb->s_bdev->bd_dev); + bool trusted; + + if (!md) + return false; + + trusted =3D dm_verity_loadpin_is_md_trusted(md); + dm_put(md); + + return trusted; +} +#else +static inline bool loadpin_is_fs_trusted(struct super_block *sb) { return = false; }; +#endif + static int loadpin_read_file(struct file *file, enum kernel_read_file_id i= d, bool contents) { @@ -174,7 +198,8 @@ static int loadpin_read_file(struct file *file, enum ke= rnel_read_file_id id, spin_unlock(&pinned_root_spinlock); } =20 - if (IS_ERR_OR_NULL(pinned_root) || load_root !=3D pinned_root) { + if (IS_ERR_OR_NULL(pinned_root) || + ((load_root !=3D pinned_root) && !loadpin_is_fs_trusted(load_root))) { if (unlikely(!enforce)) { report_load(origin, file, "pinning-ignored"); return 0; @@ -240,6 +265,7 @@ static int __init loadpin_init(void) enforce ? "" : "not "); parse_exclude(); security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin"); + return 0; } =20 @@ -248,6 +274,162 @@ DEFINE_LSM(loadpin) =3D { .init =3D loadpin_init, }; =20 +#ifdef CONFIG_SECURITY_LOADPIN_VERITY + +enum loadpin_securityfs_interface_index { + LOADPIN_DM_VERITY, +}; + +static int read_trusted_verity_root_digests(unsigned int fd) +{ + struct fd f; + void *data; + int rc; + char *p, *d; + + /* The list of trusted root digests can only be set up once */ + if (!list_empty(&trusted_verity_root_digests)) + return -EPERM; + + f =3D fdget(fd); + if (!f.file) + return -EINVAL; + + data =3D kzalloc(SZ_4K, GFP_KERNEL); + if (!data) { + rc =3D -ENOMEM; + goto err; + } + + rc =3D kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY= ); + if (rc < 0) + goto err; + + ((char *)data)[rc] =3D '\0'; + + p =3D strim(data); + while ((d =3D strsep(&p, ",")) !=3D NULL) { + int len =3D strlen(d); + struct trusted_root_digest *trd; + + if (len % 2) { + rc =3D -EPROTO; + goto err; + } + + len /=3D 2; + + trd =3D kzalloc(sizeof(*trd), GFP_KERNEL); + if (!trd) { + rc =3D -ENOMEM; + goto err; + } + + trd->data =3D kzalloc(len, GFP_KERNEL); + if (!trd->data) { + kfree(trd); + rc =3D -ENOMEM; + goto err; + } + + if (hex2bin(trd->data, d, len)) { + kfree(trd); + kfree(trd->data); + rc =3D -EPROTO; + goto err; + } + + trd->len =3D len; + + list_add_tail(&trd->node, &trusted_verity_root_digests); + } + + kfree(data); + fdput(f); + + if (!list_empty(&trusted_verity_root_digests)) + dm_verity_loadpin_set_trusted_root_digests(&trusted_verity_root_digests); + + return 0; + +err: + kfree(data); + + { + struct trusted_root_digest *trd, *tmp; + + list_for_each_entry_safe(trd, tmp, &trusted_verity_root_digests, node) { + kfree(trd->data); + list_del(&trd->node); + kfree(trd); + } + } + + fdput(f); + + return rc; +} + +/******************************** securityfs *****************************= ***/ + +static long dm_verity_ioctl(struct file *filp, unsigned int cmd, unsigned = long arg) +{ + void __user *uarg =3D (void __user *)arg; + unsigned int fd; + int rc; + + switch (cmd) { + case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS: + rc =3D copy_from_user(&fd, uarg, sizeof(fd)); + if (rc) + return rc; + + return read_trusted_verity_root_digests(fd); + + default: + return -EINVAL; + } +} + +static const struct file_operations loadpin_dm_verity_ops =3D { + .unlocked_ioctl =3D dm_verity_ioctl, + .compat_ioctl =3D compat_ptr_ioctl, +}; + +/** + * init_loadpin_securityfs - create the securityfs directory for LoadPin + * + * We can not put this method normally under the loadpin_init() code path = since + * the security subsystem gets initialized before the vfs caches. + * + * Returns 0 if the securityfs directory creation was successful. + */ +static int __init init_loadpin_securityfs(void) +{ + struct dentry *loadpin_dir, *dentry; + + loadpin_dir =3D securityfs_create_dir("loadpin", NULL); + if (IS_ERR(loadpin_dir)) { + pr_err("LoadPin: could not create securityfs dir: %d\n", + PTR_ERR(loadpin_dir)); + return PTR_ERR(loadpin_dir); + } + + dentry =3D securityfs_create_file("dm-verity", 0600, loadpin_dir, + (void *)LOADPIN_DM_VERITY, &loadpin_dm_verity_ops); + if (IS_ERR(dentry)) { + pr_err("LoadPin: could not create securityfs entry 'dm-verity': %d\n", + PTR_ERR(dentry)); + return PTR_ERR(dentry); + } + + return 0; +} + +fs_initcall(init_loadpin_securityfs); + +#endif /* CONFIG_SECURITY_LOADPIN_VERITY */ + /* Should not be mutable after boot, so not listed in sysfs (perm =3D=3D 0= ). */ module_param(enforce, int, 0); MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning"); --=20 2.36.0.464.gb9c8b46e94-goog From nobody Fri May 8 11:36:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1EE3C433F5 for ; Wed, 4 May 2022 19:54:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377768AbiEDT6U (ORCPT ); Wed, 4 May 2022 15:58:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377752AbiEDT6H (ORCPT ); Wed, 4 May 2022 15:58:07 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78D2F4ECCE for ; Wed, 4 May 2022 12:54:29 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id d17so2420674plg.0 for ; Wed, 04 May 2022 12:54:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qM82pLe5LIpNsG3poDlPVKU5bEYefBi/IlcSrVO4JAI=; b=neUBEwGA4kq8JLwN5POTUHELuHm+l5M7u79I7XDLezrivLCU1aE6AFNc57wWU+0ODY 9N0fRDhZ0iUfFp3kqga3HicjJzX8kYd9uLjlyBcfk0hPy4NyqkuuifzyrRzn8XzhgtH4 CCEL3s0utu1QINUSJ+cZwchHFdenNYLU5VxWI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qM82pLe5LIpNsG3poDlPVKU5bEYefBi/IlcSrVO4JAI=; b=DXvYwFvrZC2TdaSXgVQvBNqooX7g9MjwTXPcMLslO3RIRRl04nBNBULM+R8Kjzd3NM TtCzW61+XY53SfIcNiazZOvPCLFgzasjA2Ad81MapsB4AfNUxfm9PZfnEQi3BRle53CC Txq/3Y8ayMhmHKOpWVc5W1DOlfset/hw+7nw3FOFpUuc1U/U4e8M/5Tsfa0DiGsF8+F9 fdmwY5uvqH0D1omLAAWQ6iBM7JfU2Us57MLlIv2Adz8+IAbsvK13N5Y6SA/BJMGYee3w +0EG4u76ixh63PRwWJjR+fTSb57MGQlCSv03WWIVwJ0ZQTazkhHXiGx//jZWBSpBcYvx 6Ucw== X-Gm-Message-State: AOAM533wZ1/nSN4WtTliXSTOF6XHwTGwiHPuDXaJNIqOTy4cZbZiQL31 8ekbUFFIKW7bMp9DV9onOtBcqw== X-Google-Smtp-Source: ABdhPJyQN0M+9153jv/w/MlWINhzXu1GoODr13+92d+vbgDekp0dhUhMFOATx1GkA2fDTz3kWcPUcw== X-Received: by 2002:a17:903:1249:b0:15e:8b15:b7d2 with SMTP id u9-20020a170903124900b0015e8b15b7d2mr24555926plh.150.1651694068934; Wed, 04 May 2022 12:54:28 -0700 (PDT) Received: from localhost ([2620:15c:202:201:35b6:c77b:be04:3bd5]) by smtp.gmail.com with UTF8SMTPSA id t3-20020a17090aae0300b001d5e1b124a0sm3727332pjq.7.2022.05.04.12.54.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 May 2022 12:54:28 -0700 (PDT) From: Matthias Kaehlcke To: Alasdair Kergon , Mike Snitzer , Kees Cook , James Morris , "Serge E . Hallyn" Cc: dm-devel@redhat.com, linux-kernel@vger.kernel.org, linux-raid@vger.kernel.org, Song Liu , Douglas Anderson , linux-security-module@vger.kernel.org, Matthias Kaehlcke Subject: [PATCH v3 3/3] dm: verity-loadpin: Use CONFIG_SECURITY_LOADPIN_VERITY for conditional compilation Date: Wed, 4 May 2022 12:54:19 -0700 Message-Id: <20220504125404.v3.3.I5aca2dcc3b06de4bf53696cd21329dce8272b8aa@changeid> X-Mailer: git-send-email 2.36.0.464.gb9c8b46e94-goog In-Reply-To: <20220504195419.1143099-1-mka@chromium.org> References: <20220504195419.1143099-1-mka@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The verity glue for LoadPin is only needed when CONFIG_SECURITY_LOADPIN_VER= ITY is set, use this option for conditional compilation instead of the combo of CONFIG_DM_VERITY and CONFIG_SECURITY_LOADPIN. Signed-off-by: Matthias Kaehlcke --- Changes in v3: - none Changes in v2: - none drivers/md/Makefile | 7 +------ include/linux/dm-verity-loadpin.h | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/drivers/md/Makefile b/drivers/md/Makefile index e12cd004d375..a96441752ec7 100644 --- a/drivers/md/Makefile +++ b/drivers/md/Makefile @@ -83,6 +83,7 @@ obj-$(CONFIG_DM_LOG_WRITES) +=3D dm-log-writes.o obj-$(CONFIG_DM_INTEGRITY) +=3D dm-integrity.o obj-$(CONFIG_DM_ZONED) +=3D dm-zoned.o obj-$(CONFIG_DM_WRITECACHE) +=3D dm-writecache.o +obj-$(CONFIG_SECURITY_LOADPIN_VERITY) +=3D dm-verity-loadpin.o =20 ifeq ($(CONFIG_DM_INIT),y) dm-mod-objs +=3D dm-init.o @@ -100,12 +101,6 @@ ifeq ($(CONFIG_IMA),y) dm-mod-objs +=3D dm-ima.o endif =20 -ifeq ($(CONFIG_DM_VERITY),y) -ifeq ($(CONFIG_SECURITY_LOADPIN),y) -dm-mod-objs +=3D dm-verity-loadpin.o -endif -endif - ifeq ($(CONFIG_DM_VERITY_FEC),y) dm-verity-objs +=3D dm-verity-fec.o endif diff --git a/include/linux/dm-verity-loadpin.h b/include/linux/dm-verity-lo= adpin.h index 12a86911d05a..be63ac76f98d 100644 --- a/include/linux/dm-verity-loadpin.h +++ b/include/linux/dm-verity-loadpin.h @@ -13,7 +13,7 @@ struct trusted_root_digest { struct list_head node; }; =20 -#if IS_ENABLED(CONFIG_SECURITY_LOADPIN) && IS_BUILTIN(CONFIG_DM_VERITY) +#if IS_ENABLED(CONFIG_SECURITY_LOADPIN_VERITY) void dm_verity_loadpin_set_trusted_root_digests(struct list_head *digests); bool dm_verity_loadpin_is_md_trusted(struct mapped_device *md); #else --=20 2.36.0.464.gb9c8b46e94-goog