From nobody Sun May 10 18:33:28 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 AB398C4332F for ; Tue, 26 Apr 2022 21:31:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355462AbiDZVec (ORCPT ); Tue, 26 Apr 2022 17:34:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355456AbiDZVe1 (ORCPT ); Tue, 26 Apr 2022 17:34:27 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECEC8255B3 for ; Tue, 26 Apr 2022 14:31:17 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id h1so19094920pfv.12 for ; Tue, 26 Apr 2022 14:31:17 -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=oklCy92EPimOxfFRDN0VLcpfYtlk8bEuVQrptcICGjQ=; b=Oijq75H4ZnSjjmdQykwCej7MQp89ReCNNPoI8tw26yzcDylGtn+C6N1eJ/ItXrszGx DDegeANpnswmwzhGZKt2ACu86LqsfO5LlcrUSFkmnhAuOHt4jYBAIq6g9UHTNYjycCxb e5t2R01QUhV3FcltAtNtQuCbaDaRMDsXGqUX0= 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=oklCy92EPimOxfFRDN0VLcpfYtlk8bEuVQrptcICGjQ=; b=Jnh03YqSVhMWcgqdtgTLxPNMB3Yx6X7nqNt0QFS9fBx38sx4XsjFSHko9O0LtLPDNj WHFGPt0jS3yzzq7SqNNa7sr/k3kIRDfmdVvBxtdIvsGUxKt1m06aUeo7torlUX56RjkW Qcjf68Sb496EbYKqnXeZRxxv1fInbttJ7BqDB2mC6gUp3qMQ87fvJ6GylpTa4lLHjMVZ HyT1Yn61XbIYKxDBH6nMCFV3G4xmNZGGTqA2l3zJGcl57JLPqghnrMm0TtCfx+5H3OVH X/Voycj2Z2aAMH0WdecZ0F7oAy7vwvm+lReId3pnm/LmFapnnSR3EHLIoelIDRw/k821 wrug== X-Gm-Message-State: AOAM530LY1GNqecsPucBnjYy/jv3bCTjohYIdtbaqo9qeW3YUFoqZLfn rSAmEDonW0VVlTS5n4+K6AlrHQ== X-Google-Smtp-Source: ABdhPJzUwQwV4e6Xy13BncRXONpdBAH4A4s7nHNnuR0F+ID7/PII+ISnk9adQiRXf94sAFkfyX9loQ== X-Received: by 2002:a63:2d46:0:b0:3c1:424a:2a90 with SMTP id t67-20020a632d46000000b003c1424a2a90mr582225pgt.35.1651008677493; Tue, 26 Apr 2022 14:31:17 -0700 (PDT) Received: from localhost ([2620:15c:202:201:eb63:4211:eb5d:f6b]) by smtp.gmail.com with UTF8SMTPSA id d141-20020a621d93000000b00505aa1026f1sm15919562pfd.51.2022.04.26.14.31.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 26 Apr 2022 14:31:17 -0700 (PDT) From: Matthias Kaehlcke To: Alasdair Kergon , Mike Snitzer , Kees Cook , James Morris , "Serge E . Hallyn" Cc: linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, dm-devel@redhat.com, linux-raid@vger.kernel.org, Douglas Anderson , Song Liu , Matthias Kaehlcke Subject: [PATCH v2 1/3] dm: Add verity helpers for LoadPin Date: Tue, 26 Apr 2022 14:31:08 -0700 Message-Id: <20220426143059.v2.1.I3e928575a23481121e73286874c4c2bdb403355d@changeid> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog In-Reply-To: <20220426213110.3572568-1-mka@chromium.org> References: <20220426213110.3572568-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 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..0638e61dba51 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 digests, 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.rc2.479.g8af0fa9b8e-goog From nobody Sun May 10 18:33:28 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 0FEC2C433F5 for ; Tue, 26 Apr 2022 21:31:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355485AbiDZVee (ORCPT ); Tue, 26 Apr 2022 17:34:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355461AbiDZVe3 (ORCPT ); Tue, 26 Apr 2022 17:34:29 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3602A25587 for ; Tue, 26 Apr 2022 14:31:20 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id z21so9441207pgj.1 for ; Tue, 26 Apr 2022 14:31:20 -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=yNE83scqZXxcJEDYMgDTyuvR1Ggrx94+lQMxMZ/IowU=; b=ZKtJ8XGA/SqxhMFiJZUOvHxaI+WraHLnICxfudfiaWADp0gXMDJhi76vjNZeRj9tZn HPO35gNJx446gcW6soKp1SYDvLPLJ9HbIPY4+h4LtDlO8B9TI5aL1/cO7fWEK17Ilgcd rCf8BKnGHybajm2daHl8zy+7/3CznCm0IdHtg= 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=yNE83scqZXxcJEDYMgDTyuvR1Ggrx94+lQMxMZ/IowU=; b=PH8D8CH7ddnv0Yfc7zEnYpIFnS1ESGvzjfgiSDorlp+Ub4REXyZuWDSN8GVFYatyM3 yfm2ioDxBPc5Jl7M6G/TnL+AkHOzpd00pvV8xHrADJKp8UYuUyDe3PzRkSgfeTyP33sX R1Ej3reTMy0bH1aKSZ7NWqbhowL5FIIWrlw0739ZalOnmSH+mIktxYMiqeIgYJAPI01O Yak1tsEm9FbxfAxvikI4JHNfWHA/aw53gWoXFWUOxIs+DRCXJTegXGUciYaQI/tdmSSp xTzHK57fwIGrXN22+JT9iwprfj00aeykH1k+OTfSABdmIQhcxLpMwYOIRlnI3JszPzIl nJUQ== X-Gm-Message-State: AOAM530KrMlXZ9h/4O6V0SvoOweL2IiGvX6IkheGOfWfu5Iut+aLoUYG mKL/mYQwXhawkZD1Sl7DSYVMhQ== X-Google-Smtp-Source: ABdhPJyMQWm4GBBd8xOq6MlO1DMwKqAq9qQ61DtlsZ0/uc1Wn8+/SKp/8D2U8jV/KOG4uEk/ZjmK/w== X-Received: by 2002:a05:6a00:174a:b0:50d:44ca:4b with SMTP id j10-20020a056a00174a00b0050d44ca004bmr10662538pfc.0.1651008679684; Tue, 26 Apr 2022 14:31:19 -0700 (PDT) Received: from localhost ([2620:15c:202:201:eb63:4211:eb5d:f6b]) by smtp.gmail.com with UTF8SMTPSA id pb15-20020a17090b3c0f00b001cd4989ff4esm3587166pjb.21.2022.04.26.14.31.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 26 Apr 2022 14:31:19 -0700 (PDT) From: Matthias Kaehlcke To: Alasdair Kergon , Mike Snitzer , Kees Cook , James Morris , "Serge E . Hallyn" Cc: linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, dm-devel@redhat.com, linux-raid@vger.kernel.org, Douglas Anderson , Song Liu , Matthias Kaehlcke Subject: [PATCH v2 2/3] LoadPin: Enable loading from trusted dm-verity devices Date: Tue, 26 Apr 2022 14:31:09 -0700 Message-Id: <20220426143059.v2.2.I01c67af41d2f6525c6d023101671d7339a9bc8b5@changeid> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog In-Reply-To: <20220426213110.3572568-1-mka@chromium.org> References: <20220426213110.3572568-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. Userspace can use the new systl file 'loadpin/trusted_verity_root_digests_path' to provide LoadPin with the path of a file with a list of root digests from dm-verity devices that LoadPin should consider as trusted. This file must be located on the pinned root. 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. The path of the file with the trusted root digests can only be written once, which is typically done at boot time. 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 --- I'm still doubting what would be the best way to configure the list of trusted digests. The approach in v2 of writing a path through sysctl is flexible, but it also feels a bit odd. I did some experiments with passing a file descriptor through sysctl, but it's also odd and has its own issues. Passing the list through a kernel parameter seems hacky. A Kconfig string would work, but can be have issues when the same config is used for different platforms, where some may have trusted digests and others not. 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 security/loadpin/Kconfig | 16 +++ security/loadpin/loadpin.c | 200 ++++++++++++++++++++++++++++++++++++- 2 files changed, 215 insertions(+), 1 deletion(-) diff --git a/security/loadpin/Kconfig b/security/loadpin/Kconfig index 91be65dec2ab..20681dc4b8a5 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 && SYSCTL + 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. + + Userspace can populate the list of trusted digests by writing + the path of a file with the digests to the syctl file + 'trusted_verity_root_digests_path'. 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..db10b7ff6efd 100644 --- a/security/loadpin/loadpin.c +++ b/security/loadpin/loadpin.c @@ -18,6 +18,8 @@ #include #include /* current */ #include +#include +#include =20 static void report_load(const char *origin, struct file *file, char *opera= tion) { @@ -43,6 +45,10 @@ 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); +static const char *verity_digests_path; +#endif =20 #ifdef CONFIG_SYSCTL =20 @@ -65,6 +71,176 @@ static struct ctl_table loadpin_sysctl_table[] =3D { { } }; =20 +#ifdef CONFIG_SECURITY_LOADPIN_VERITY + +static int loadpin_read_file(struct file *file, enum kernel_read_file_id i= d, + bool contents); + +static int read_trusted_verity_root_digests(struct file *file) +{ + void *data; + char *p, *d; + int err, rc; + + data =3D kzalloc(SZ_4K, GFP_KERNEL); + if (!data) + return -ENOMEM; + + rc =3D kernel_read_file(file, 0, &data, SZ_4K - 1, NULL, READING_POLICY); + if (rc < 0) + return rc; + + ((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) { + err =3D -EPROTO; + goto free_mem; + } + + len /=3D 2; + + trd =3D kzalloc(sizeof(*trd), GFP_KERNEL); + if (!trd) { + err =3D -ENOMEM; + goto free_mem; + } + + trd->data =3D kzalloc(len, GFP_KERNEL); + if (!trd->data) { + kfree(trd); + err =3D -ENOMEM; + goto free_mem; + } + + if (hex2bin(trd->data, d, len)) { + kfree(trd); + err =3D -EPROTO; + goto free_mem; + } + + list_add_tail(&trd->node, &trusted_verity_root_digests); + + trd->len =3D len; + } + + kfree(data); + + return 0; + +free_mem: + 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); + } + } + + return err; +} + +static int proc_verity_digests(struct ctl_table *table, int write, + void *buffer, size_t *lenp, loff_t *ppos) +{ + struct ctl_table tbl =3D *table; + + if (write) { + int rc; + char *digests_path; + struct file *file; + + if (*ppos) + return -EINVAL; + + if (verity_digests_path !=3D NULL) + return -EPERM; + + digests_path =3D kzalloc(tbl.maxlen, GFP_KERNEL); + if (!digests_path) + return -ENOMEM; + + tbl.data =3D digests_path; + + rc =3D proc_dostring(&tbl, write, buffer, lenp, ppos); + if (rc) { + kfree(digests_path); + return rc; + } + + /* only absolute paths are allowed */ + if (digests_path[0] !=3D '/') { + kfree(digests_path); + return -EINVAL; + } + + file =3D filp_open(digests_path, O_RDONLY, 0); + if (IS_ERR(file)) { + int err =3D PTR_ERR(file); + + if (err =3D=3D -ENOENT) { + kfree(digests_path); + return -EINVAL; + } + + return err; + } + + /* verify the root digests stem from a trusted file system */ + if (loadpin_read_file(file, READING_POLICY, true)) { + fput(file); + kfree(digests_path); + return -EPERM; + } + + rc =3D read_trusted_verity_root_digests(file); + fput(file); + if (rc) { + kfree(digests_path); + return rc; + } + + verity_digests_path =3D digests_path; + } else { + if (verity_digests_path) { + tbl.data =3D kzalloc(strlen(verity_digests_path) + 1, GFP_KERNEL); + strcpy(tbl.data, verity_digests_path); + } else { + tbl.data =3D kzalloc(1, GFP_KERNEL); + } + + proc_dostring(&tbl, write, buffer, lenp, ppos); + kfree(tbl.data); + } + + return 0; +} + +static struct ctl_table loadpin_sysctl_table_verity_digests[] =3D { + { + .procname =3D "trusted_verity_root_digests_path", + .maxlen =3D SZ_256, + .mode =3D 0644, + .proc_handler =3D proc_verity_digests, + }, + + { } +}; + +#else + +static struct ctl_table loadpin_sysctl_table_verity_digests[] =3D {}; + +#endif /* CONFIG_SECURITY_LOADPIN_VERITY */ + /* * This must be called after early kernel init, since then the rootdev * is available. @@ -118,6 +294,20 @@ static void loadpin_sb_free_security(struct super_bloc= k *mnt_sb) } } =20 +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; +} + static int loadpin_read_file(struct file *file, enum kernel_read_file_id i= d, bool contents) { @@ -174,7 +364,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 +431,13 @@ static int __init loadpin_init(void) enforce ? "" : "not "); parse_exclude(); security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin"); + + if (IS_ENABLED(CONFIG_SECURITY_LOADPIN_VERITY)) { + if (!register_sysctl_paths(loadpin_sysctl_path, + loadpin_sysctl_table_verity_digests)) + pr_notice("sysctl registration failed!\n"); + } + return 0; } =20 --=20 2.36.0.rc2.479.g8af0fa9b8e-goog From nobody Sun May 10 18:33:28 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 4B6E3C4332F for ; Tue, 26 Apr 2022 21:31:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355499AbiDZVek (ORCPT ); Tue, 26 Apr 2022 17:34:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355466AbiDZVea (ORCPT ); Tue, 26 Apr 2022 17:34:30 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA6BB25596 for ; Tue, 26 Apr 2022 14:31:21 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id u6-20020a17090a1f0600b001d86bd69427so118369pja.5 for ; Tue, 26 Apr 2022 14:31:21 -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=dWxaD7L0u/h0y2dhkzttG4l+Tb/Huddvi0NADO8Lp2s=; b=hdDvtXb8sW1W6Vb9Rl1EyT79gjqOgItMlyb6hUkULSIq7OBeaZAjDkb1GDFuCpTKwX 6amgRPyb8H6us1ZLwhP5WU3wpLB5m68cphTpxGKy7UfE1WavorSBcGyJnSyAn86RhwM2 rRm9wDZcuV4lvjKc/6RAtt7RcXFBuOMww+sFs= 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=dWxaD7L0u/h0y2dhkzttG4l+Tb/Huddvi0NADO8Lp2s=; b=yq7qXW2CaZ77IcuwzxvCdR5tddCn3ZgglClqH2YWwn6+FSLMXA8lxtcxWqoanUou1N FKmZXFkjq+U8cLOYKfCm4QfU4PntNsbAd/FTBfZtX/T67YD1cf4b1UqkTpGBrGewJxBS MhH2QWKzIHXxEVKVh+vB+K2N5Rr2wAUdGBGd1Umz/XCgokiBDQDw5GLjW7l8raCXKjrk RConl3vUjqQeFXw9+Y3X6fCWGQ+kJbaT1VHwxTQroFj8+wkrxwRyYGOt5Md7Tp2BpSeY fEbqBKjFFiTA7W/Ro2Ua2j3xoilLvo0nLOupdrbE2k6olBOfsp9zFUEN3MaswDy+7DHV A9uQ== X-Gm-Message-State: AOAM532oOfSR/R5kQuK/e1Xy9A2QAsInloo81ij0hqSpkeWREqhT/4sw 0g7p6XB89ykz6oHBLIUU38BWkw== X-Google-Smtp-Source: ABdhPJy4R5XtqAI2AVSwm6Ahikus8xa0rabLRQflKyRJPiBg1OerZzxzHWDe8RqGJ2mAUYofsSzeJw== X-Received: by 2002:a17:90b:2311:b0:1d9:277e:edad with SMTP id mt17-20020a17090b231100b001d9277eedadmr22722620pjb.190.1651008681490; Tue, 26 Apr 2022 14:31:21 -0700 (PDT) Received: from localhost ([2620:15c:202:201:eb63:4211:eb5d:f6b]) by smtp.gmail.com with UTF8SMTPSA id z8-20020aa785c8000000b005060d2d7085sm16015202pfn.151.2022.04.26.14.31.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 26 Apr 2022 14:31:21 -0700 (PDT) From: Matthias Kaehlcke To: Alasdair Kergon , Mike Snitzer , Kees Cook , James Morris , "Serge E . Hallyn" Cc: linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, dm-devel@redhat.com, linux-raid@vger.kernel.org, Douglas Anderson , Song Liu , Matthias Kaehlcke Subject: [PATCH v2 3/3] dm: verity-loadpin: Use CONFIG_SECURITY_LOADPIN_VERITY for conditional compilation Date: Tue, 26 Apr 2022 14:31:10 -0700 Message-Id: <20220426143059.v2.3.I5aca2dcc3b06de4bf53696cd21329dce8272b8aa@changeid> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog In-Reply-To: <20220426213110.3572568-1-mka@chromium.org> References: <20220426213110.3572568-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 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.rc2.479.g8af0fa9b8e-goog