From patchwork Mon Apr 18 21:15:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Kaehlcke X-Patchwork-Id: 12817105 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 947BAC433EF for ; Mon, 18 Apr 2022 21:16:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240926AbiDRVS5 (ORCPT ); Mon, 18 Apr 2022 17:18:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347976AbiDRVSs (ORCPT ); Mon, 18 Apr 2022 17:18:48 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFD752CE28 for ; Mon, 18 Apr 2022 14:16:07 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id u2so21092146pgq.10 for ; Mon, 18 Apr 2022 14:16:07 -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=3NUaPl7T9VDjPF5pSPc597gsFG5v6iWsZQaLgLfeQww=; b=UBDOLR+Mh24DHdsNmC7ttDSdUYYhK71ZxlqJFIvEiPoTYFmr87XRbhqUuJe0zKDfii TXW8I3yWCcvYyiqCHPxV7WfB/4n0bNzymbHzIArdU4+e6O3lq47ucuRYaCgHGlvDjSGO B20sYxYIBFP2mUCRci3ZuhiR27q5GeyeAYpPg= 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=3NUaPl7T9VDjPF5pSPc597gsFG5v6iWsZQaLgLfeQww=; b=V+QKtRiGcYF4sQQRR78aEH/yMg2bv3I4qPTAGGl0AMeASZlRvC/Hym70++kFdH9gB9 zUoh1Wddoesoy/d8O+MgsPRsvYF9QB1COevPsSoHcQ2FS5Sb0FpQh2ts6Unbdvq0bMSa aCtDTv58SVsS2KpXFyokr9vr8LR3t/PJsfj0DzkLk8LLzzFiqkK3bx6fKeRwC5g8fnUg r9OTfqQlP6K866kABsQYBNr2EU9d/LNFLtbVzsWt2hXdzmLKDN+UOl0etc/C5jRkOv9c 0NOxCsHGOTWeiac/MsYkonNgnG9QG2vNhqfoyGIRbZCmkW9RG4zqw73Z6+qETnMAqEBs QJMA== X-Gm-Message-State: AOAM530rlWzxIFw/G3VzkAcGThz9WnWB2vUQJ5TIpZGqXFc0Uq4p7BoW VJ0g4PcJkgMsiQrkYTmOxZGO/Q== X-Google-Smtp-Source: ABdhPJyQQEFDCs5IVHAM/UnOWzfEHFGUzBQrHu06Pf6Bm6srYRBFZ5pue0X3KWczqh0V440cxpFgFA== X-Received: by 2002:a05:6a00:1786:b0:50a:7f5a:e280 with SMTP id s6-20020a056a00178600b0050a7f5ae280mr4803956pfg.25.1650316567469; Mon, 18 Apr 2022 14:16:07 -0700 (PDT) Received: from localhost ([2620:15c:202:201:6b32:a0a5:ec32:c287]) by smtp.gmail.com with UTF8SMTPSA id w13-20020a17090a5e0d00b001c7d4099670sm13858457pjf.28.2022.04.18.14.16.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Apr 2022 14:16:06 -0700 (PDT) From: Matthias Kaehlcke To: Alasdair Kergon , Mike Snitzer , Kees Cook , James Morris , "Serge E . Hallyn" Cc: linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , linux-security-module@vger.kernel.org, Douglas Anderson , dm-devel@redhat.com, Matthias Kaehlcke Subject: [PATCH 1/3] dm: Add verity helpers for LoadPin Date: Mon, 18 Apr 2022 14:15:57 -0700 Message-Id: <20220418141545.1.I3e928575a23481121e73286874c4c2bdb403355d@changeid> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog In-Reply-To: <20220418211559.3832724-1-mka@chromium.org> References: <20220418211559.3832724-1-mka@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: 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 --- drivers/md/Makefile | 6 +++ drivers/md/dm-verity-loadpin.c | 80 +++++++++++++++++++++++++++++++ drivers/md/dm-verity-target.c | 34 +++++++++++++ drivers/md/dm-verity.h | 4 ++ include/linux/dm-verity-loadpin.h | 27 +++++++++++ 5 files changed, 151 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 += dm-ima.o endif +ifeq ($(CONFIG_DM_VERITY),y) +ifeq ($(CONFIG_SECURITY_LOADPIN),y) +dm-mod-objs += dm-verity-loadpin.o +endif +endif + ifeq ($(CONFIG_DM_VERITY_FEC),y) dm-verity-objs += 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 trusted. + * + * 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 = 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 = 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 == digest_size) && + !memcmp(trd->data, root_digest, digest_size)) { + trusted = 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 = false; + int i; + + if (!trusted_root_digests || list_empty(trusted_root_digests)) + return false; + + table = dm_get_live_table(md, &srcu_idx); + num_targets = dm_table_get_num_targets(table); + for (i = 0; i < num_targets; i++) { + struct dm_target *ti = dm_table_get_target(table, i); + + if (is_trusted_verity_target(ti)) { + trusted = 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..6bea9692cd39 100644 --- a/drivers/md/dm-verity-target.c +++ b/drivers/md/dm-verity-target.c @@ -16,9 +16,11 @@ #include "dm-verity.h" #include "dm-verity-fec.h" #include "dm-verity-verify-sig.h" +#include #include #include #include +#include #define DM_MSG_PREFIX "verity" @@ -1310,6 +1312,38 @@ static int verity_ctr(struct dm_target *ti, unsigned argc, char **argv) return r; } +/* + * Check whether a DM target is a verity target. + */ +bool dm_is_verity_target(struct dm_target *ti) +{ + return ti->type->module == 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, unsigned int *digest_size) +{ + struct dm_verity *v = ti->private; + + if (!dm_is_verity_target(ti)) + return -EINVAL; + + *root_digest = kmemdup(v->root_digest, v->digest_size, GFP_KERNEL); + if (*root_digest == NULL) + return -ENOMEM; + + *digest_size = v->digest_size; + + return 0; +} +EXPORT_SYMBOL_GPL(dm_verity_get_root_digest); + static struct target_type verity_target = { .name = "verity", .version = {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 ahash_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); +extern bool dm_is_verity_target(struct dm_target *ti); +extern int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest, + unsigned int *digest_size); + #endif /* DM_VERITY_H */ diff --git a/include/linux/dm-verity-loadpin.h b/include/linux/dm-verity-loadpin.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 *md) +{ + return false; +} +#endif + +#endif /* __LINUX_DM_LOADPIN_H */