From nobody Tue May 7 10:53:25 2024 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 92E9FC433F5 for ; Mon, 23 May 2022 21:14:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230173AbiEWVOY (ORCPT ); Mon, 23 May 2022 17:14:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229951AbiEWVOG (ORCPT ); Mon, 23 May 2022 17:14:06 -0400 Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6431E7DE08 for ; Mon, 23 May 2022 14:14:05 -0700 (PDT) Received: by mail-pg1-x536.google.com with SMTP id g184so14717008pgc.1 for ; Mon, 23 May 2022 14:14:05 -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=nFCWx+ylAY8dgCt25JkKf17aOAoMoBf41nquHMuShDE=; b=DrxRx3lcJlrmz3dLTTbA/BGYwGlTnpRvbUn3+FYGbiyqc4fVVCmnHeaYh9oRRylTqf mwVuGqA66Wk5YOx4+E+11K5zMPz+fQWvA+SOglWu8QqlKYDd1D9QrrfWjtQVi8VEtz4D RCMsaWnJLfHfLrQLi2TMwruH5HBHe/jWUmoiA= 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=nFCWx+ylAY8dgCt25JkKf17aOAoMoBf41nquHMuShDE=; b=56qQB/2obBxzZTiiCzk7YRY1/1XDxbCvg+8j9Rl0vlyIRR2HFhXEqexvNo3IVQF3Z7 yiydzFP0kFd1Q7mAMz3Td1zvMfq/zxwiTfZMj6iwTjEOrxxcQsVK4DF7HzZIJLYfk2iz 9OJA27fYq3Skfp2gKUj8wQxp/B1au58Y2vhT0e+jgw/W5wvqx861tCOBmVQu27Fcvl6M G24PQZ4KeZmjcWGG6R58PiQaHuCB1kK0WNBmYEqMTL8J4jK+i6thEERGUGL3j3pEzrH0 2EARXXnrlwA862AuO1hVgHSUeizLaJPOLERSUHDvecSOhy19EMXZkAhKJgBsiJmz2HWd 9ukA== X-Gm-Message-State: AOAM532qwfgaSZTw/Z0w6Tg+gXA4dBN7l0CN61NwZgddo41h9cWli6+v 8mg++0KjRrFAZCNuO79f1PZRKA== X-Google-Smtp-Source: ABdhPJzFBI8sZMoLwz+YBgyqx5St60xio7LcpPb0/AquoAtcXVQBdrULJtODVLNJRd6PnonV4Oef3Q== X-Received: by 2002:a63:8bc9:0:b0:3f8:4817:880b with SMTP id j192-20020a638bc9000000b003f84817880bmr15353756pge.87.1653340444919; Mon, 23 May 2022 14:14:04 -0700 (PDT) Received: from localhost ([2620:15c:11a:202:d9e2:8472:9ac:8532]) by smtp.gmail.com with UTF8SMTPSA id v17-20020a17090ad59100b001deb92de665sm118439pju.46.2022.05.23.14.14.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 23 May 2022 14:14:04 -0700 (PDT) From: Matthias Kaehlcke To: Alasdair Kergon , Mike Snitzer , Kees Cook , James Morris , "Serge E . Hallyn" Cc: Milan Broz , dm-devel@redhat.com, linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Douglas Anderson , linux-security-module@vger.kernel.org, Matthias Kaehlcke Subject: [PATCH v6 1/3] dm: Add verity helpers for LoadPin Date: Mon, 23 May 2022 14:13:58 -0700 Message-Id: <20220523141325.v6.1.I3e928575a23481121e73286874c4c2bdb403355d@changeid> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog In-Reply-To: <20220523211400.290537-1-mka@chromium.org> References: <20220523211400.290537-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 Acked-by: Kees Cook --- Changes in v6: - s/loadpin_trusted_verity_root_digests/dm_verity_loadpin_trusted_root_dige= sts/ - s/trusted_root_digest/dm_verity_loadpin_trusted_root_digest/ - removed unnecessary symbol exports Changes in v5: - changed dm_verity_loadpin_is_sb_trusted() to dm_verity_loadpin_is_bdev_trusted() - bumped version number to 1.8.1 - deleted bad semicolon in declaration of stub for dm_verity_loadpin_is_bdev_trusted() - added 'Acked-by' tag from Kees Changes in v4: - a trusted verity device must have a single target of type 'verity' - share list of verity digests with loadpin, deleted dm_verity_loadpin_set_trusted_root_digests() - dm_verity_loadpin_is_md_trusted() is now dm_verity_loadpin_is_sb_trusted(= ), it receives a super_block instead of mapped_device. Updated kernel doc. - changed struct trusted_root_digest to have an unsized u8 array instead of a pointer - extend 'dm-verity-objs' instead of 'dm-mod-objs' Changes in v3: - none Changes in v2: - none drivers/md/Makefile | 6 +++ drivers/md/dm-verity-loadpin.c | 74 +++++++++++++++++++++++++++++++ drivers/md/dm-verity-target.c | 33 +++++++++++++- drivers/md/dm-verity.h | 4 ++ include/linux/dm-verity-loadpin.h | 27 +++++++++++ 5 files changed, 143 insertions(+), 1 deletion(-) 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..71771901c823 100644 --- a/drivers/md/Makefile +++ b/drivers/md/Makefile @@ -108,6 +108,12 @@ ifeq ($(CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG),y) dm-verity-objs +=3D dm-verity-verify-sig.o endif =20 +ifeq ($(CONFIG_DM_VERITY),y) +ifeq ($(CONFIG_SECURITY_LOADPIN),y) +dm-verity-objs +=3D dm-verity-loadpin.o +endif +endif + ifeq ($(CONFIG_DM_AUDIT),y) dm-mod-objs +=3D dm-audit.o endif diff --git a/drivers/md/dm-verity-loadpin.c b/drivers/md/dm-verity-loadpin.c new file mode 100644 index 000000000000..10c18bc1652c --- /dev/null +++ b/drivers/md/dm-verity-loadpin.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include + +#include "dm.h" +#include "dm-verity.h" + +#define DM_MSG_PREFIX "verity-loadpin" + +LIST_HEAD(dm_verity_loadpin_trusted_root_digests); + +static bool is_trusted_verity_target(struct dm_target *ti) +{ + u8 *root_digest; + unsigned int digest_size; + struct dm_verity_loadpin_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, &dm_verity_loadpin_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 the file system of a superblock is located on + * a verity device that is trusted by LoadPin. + */ +bool dm_verity_loadpin_is_bdev_trusted(struct block_device *bdev) +{ + struct mapped_device *md; + struct dm_table *table; + struct dm_target *ti; + int srcu_idx; + bool trusted =3D false; + + if (list_empty(&dm_verity_loadpin_trusted_root_digests)) + return false; + + md =3D dm_get_md(bdev->bd_dev); + if (!md) + return false; + + table =3D dm_get_live_table(md, &srcu_idx); + + if (dm_table_get_num_targets(table) !=3D 1) + goto out; + + ti =3D dm_table_get_target(table, 0); + + if (is_trusted_verity_target(ti)) + trusted =3D true; + +out: + dm_put_live_table(md, srcu_idx); + dm_put(md); + + return trusted; +} diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c index 80133aae0db3..c5395e93525a 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,9 +1311,39 @@ 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; +} + +/* + * 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; +} + static struct target_type verity_target =3D { .name =3D "verity", - .version =3D {1, 8, 0}, + .version =3D {1, 8, 1}, .module =3D THIS_MODULE, .ctr =3D verity_ctr, .dtr =3D verity_dtr, 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..fb695ecaa5d5 --- /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 block_device; + +extern struct list_head dm_verity_loadpin_trusted_root_digests; + +struct dm_verity_loadpin_trusted_root_digest { + struct list_head node; + unsigned int len; + u8 data[]; +}; + +#if IS_ENABLED(CONFIG_SECURITY_LOADPIN) && IS_BUILTIN(CONFIG_DM_VERITY) +bool dm_verity_loadpin_is_bdev_trusted(struct block_device *bdev); +#else +static inline bool dm_verity_loadpin_is_bdev_trusted(struct block_device *= bdev) +{ + return false; +} +#endif + +#endif /* __LINUX_DM_VERITY_LOADPIN_H */ --=20 2.36.1.124.g0e6072fb45-goog From nobody Tue May 7 10:53:25 2024 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 C221CC433FE for ; Mon, 23 May 2022 21:14:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230091AbiEWVOa (ORCPT ); Mon, 23 May 2022 17:14:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230084AbiEWVOJ (ORCPT ); Mon, 23 May 2022 17:14:09 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13D627E1D5 for ; Mon, 23 May 2022 14:14:07 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id t11-20020a17090a6a0b00b001df6f318a8bso331623pjj.4 for ; Mon, 23 May 2022 14:14: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=C4be8YT79bQW6yC5eFeQ+eAHdjRsLKAj3D88vIx7x5g=; b=RrHDAeXdrIcCF9w2t2VYd6Mq2xkhvxnx7XSAaYRuhNhRJu5qFDvdTmFQA+DPrVWlop qTBpGSIfFZPxkbpy1zlhtJIdFmdVKvrSTtzKI0AsmHQMuY44/JS8oAI9Fq+VwlmQfxPd +8jW5zFHm4fUuAHOyPOHAfYd1/S2spiSqQjkg= 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=C4be8YT79bQW6yC5eFeQ+eAHdjRsLKAj3D88vIx7x5g=; b=XMNrl35Xiw+OBkaYfcDWRzFi6Qp4jatwskGPKVMYqY7LUUrQiLySLaNfxuLStspAgw 2dNoeyScP0k6GqCuXuObbmp6DQVRCxkRiwZE8PQmK70YbU7tsbSotqzvawVTvxB346of Tu62A1XI3uzDTiy9iy4CgNqWBnrRAVX3RPzK+3aZNooGErx3EAXMyNcUNYu5UQu40mP6 HQWaJ3Qm88exvuCjNJl88hZeZH4Y6JxqzUyVlFd3YJgj7qWrQo4Wpmd+dZ93VfJifUdx p4KnLKyr1OKR5CTCstP2zP29kewRdkcq/WA26snrjNMeICiwP3wgTUDfP5g+9fv84KFX 4juA== X-Gm-Message-State: AOAM530jPYl8gQEZcTPlf1Yjdfw3rO5B9t38Kp6PGZPlFEm2vnqIpo// BjQTtz2CYH/cxhMkLAm0erRliw== X-Google-Smtp-Source: ABdhPJyiErmSjZFLBqU69WQDgLHMb8qxS+dDfqBCkBXqNDsDP5dbRxlkLCUj6w7r3yXRWk88gYDEFA== X-Received: by 2002:a17:90b:19c8:b0:1df:2b09:ce99 with SMTP id nm8-20020a17090b19c800b001df2b09ce99mr932582pjb.185.1653340446500; Mon, 23 May 2022 14:14:06 -0700 (PDT) Received: from localhost ([2620:15c:11a:202:d9e2:8472:9ac:8532]) by smtp.gmail.com with UTF8SMTPSA id u18-20020a63a912000000b003c14af50626sm5088092pge.62.2022.05.23.14.14.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 23 May 2022 14:14:06 -0700 (PDT) From: Matthias Kaehlcke To: Alasdair Kergon , Mike Snitzer , Kees Cook , James Morris , "Serge E . Hallyn" Cc: Milan Broz , dm-devel@redhat.com, linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Douglas Anderson , linux-security-module@vger.kernel.org, Matthias Kaehlcke Subject: [PATCH v6 2/3] LoadPin: Enable loading from trusted dm-verity devices Date: Mon, 23 May 2022 14:13:59 -0700 Message-Id: <20220523141325.v6.2.I01c67af41d2f6525c6d023101671d7339a9bc8b5@changeid> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog In-Reply-To: <20220523211400.290537-1-mka@chromium.org> References: <20220523211400.290537-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 digest file must contain one digest per line. 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 Acked-by: Kees Cook --- Changes in v6: - added missing dependency on CONFIG_SECURITY_LOADPIN - s/loadpin_trusted_verity_root_digests/dm_verity_loadpin_trusted_root_dige= sts/ Changes in v5: - call dm_verity_loadpin_is_sb_trusted() instead of dm_verity_loadpin_is_bdev_trusted() - added 'Acked-by' tag from Kees Changes in v4: - use newline as separator in digest file instead of comma - after reading an invalid/corrupt digest file deny further attempts of setting up the list of digests - added comment to read_trusted_verity_root_digests() explaining that an invalid digests entry invalidates the entire list of digests - refactored read_trusted_verity_root_digests() to avoid cast of 'data' at assignment - add the format of the digest file and the path of the securityfs attribute to the ioctl comment. - adapted to struct trusted_root_digest with unsized array 'data' - call dm_verity_loadpin_is_sb_trusted() instead of loadpin_is_fs_trusted() - deleted loadpin_is_fs_trusted() - use '%ld' in format string for PTR_ERR() - added note about digest file format to the commit message 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 | 22 +++++ security/loadpin/Kconfig | 16 ++++ security/loadpin/loadpin.c | 167 ++++++++++++++++++++++++++++++++++- 3 files changed, 204 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..daa6dbb8bb02 --- /dev/null +++ b/include/uapi/linux/loadpin.h @@ -0,0 +1,22 @@ +/* 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. The file + * is expected to contain a list of digests in ASCII format, with one line= per digest. The ioctl + * must be issued on the securityfs attribute 'loadpin/dm-verity' (which c= an be typically found + * under /sys/kernel/security/loadpin/dm-verity). + */ +#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..70e7985b2561 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 SECURITY_LOADPIN && 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..ef6c28e1dba0 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,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 bool deny_reading_verity_digests; +#endif =20 #ifdef CONFIG_SYSCTL =20 @@ -174,7 +179,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) && !dm_verity_loadpin_is_bdev_trusted(l= oad_root->s_bdev))) { if (unlikely(!enforce)) { report_load(origin, file, "pinning-ignored"); return 0; @@ -240,6 +246,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 +255,164 @@ 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; + + if (deny_reading_verity_digests) + return -EPERM; + + /* The list of trusted root digests can only be set up once */ + if (!list_empty(&dm_verity_loadpin_trusted_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, (void **)&data, SZ_4K - 1, NULL, READI= NG_POLICY); + if (rc < 0) + goto err; + + p =3D data; + p[rc] =3D '\0'; + p =3D strim(p); + + p =3D strim(data); + while ((d =3D strsep(&p, "\n")) !=3D NULL) { + int len =3D strlen(d); + struct dm_verity_loadpin_trusted_root_digest *trd; + + if (len % 2) { + rc =3D -EPROTO; + goto err; + } + + len /=3D 2; + + trd =3D kzalloc(struct_size(trd, data, len), GFP_KERNEL); + if (!trd) { + rc =3D -ENOMEM; + goto err; + } + + if (hex2bin(trd->data, d, len)) { + kfree(trd); + rc =3D -EPROTO; + goto err; + } + + trd->len =3D len; + + list_add_tail(&trd->node, &dm_verity_loadpin_trusted_root_digests); + } + + if (list_empty(&dm_verity_loadpin_trusted_root_digests)) { + rc =3D -EPROTO; + goto err; + } + + kfree(data); + fdput(f); + + return 0; + +err: + kfree(data); + + /* any failure in loading/parsing invalidates the entire list */ + { + struct dm_verity_loadpin_trusted_root_digest *trd, *tmp; + + list_for_each_entry_safe(trd, tmp, &dm_verity_loadpin_trusted_root_diges= ts, node) { + list_del(&trd->node); + kfree(trd); + } + } + + /* disallow further attempts after reading a corrupt/invalid file */ + deny_reading_verity_digests =3D true; + + 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: %ld\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': %ld\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.1.124.g0e6072fb45-goog From nobody Tue May 7 10:53:25 2024 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 9965DC433F5 for ; Mon, 23 May 2022 21:14:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230272AbiEWVOd (ORCPT ); Mon, 23 May 2022 17:14:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230132AbiEWVOW (ORCPT ); Mon, 23 May 2022 17:14:22 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8318B7DE09 for ; Mon, 23 May 2022 14:14:08 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id f21so2396379pfa.3 for ; Mon, 23 May 2022 14:14:08 -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=7sQ9oR+xbg8R35wvoreY201E08AeVxeiXWdeAF7cXRM=; b=iNUjyOKjFHBZM89PlCmEcdOGkthobgiSyYUS457qK0QaLGGsWpcgsGmcklWVDWuzmI KqTv3dF1RgTfgEThOWi4q26pvTJeVo4ejWLwnmJ56VsIl0HRjxD2PvQphSv7alg9+vrg idzlpqgNvFzuYyIGn7F56jBsDWN1CAUUn0bY8= 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=7sQ9oR+xbg8R35wvoreY201E08AeVxeiXWdeAF7cXRM=; b=qPrP9p+BI70WjZ++csuNYMzTZEvqeeH6y+7G+yFuortepuuiZxKS5zJ5jiKh6qYUrQ 0gKc2ZF7YJhxItqW9i21TnXkbaVOixC5ZNzvVFxIk0f9/w6lrYgm+bRTzCiLwWs+YfYO j/fA3h9FokD0MJICkbK9LYN8fxDuJYTw3RiT/Sg0s24C8WqKmmGYX8G4+K9luQBSPeWv knu7AO+bHGQ3peezyr/0fjGMw81i7rA5LoL+GGuRjTvYMw3bfYxHZeql8c+VyhQ4fMh/ LZMLmqj++T1bbhwOpeQY+A5YPSp2gy7D2mVw/5hUXnAM2B5buel109BJShCaMVUt94MY 5b2g== X-Gm-Message-State: AOAM530KiUyKywiTxz9LGj/igD6inppAU3Tbb/Bweu0eH9mfaiH1k7I1 bo6pEtS0+tQz3SBiP05CywBfjA== X-Google-Smtp-Source: ABdhPJwjJqrTLcWA+nZYZEnU+6VaXKXbwTOJORpnOZ2WXLEU4bYD4BuxvIye0UbjX4g50uxPfvLoDg== X-Received: by 2002:a63:82c7:0:b0:3f9:e153:6a54 with SMTP id w190-20020a6382c7000000b003f9e1536a54mr12840592pgd.409.1653340448097; Mon, 23 May 2022 14:14:08 -0700 (PDT) Received: from localhost ([2620:15c:11a:202:d9e2:8472:9ac:8532]) by smtp.gmail.com with UTF8SMTPSA id s17-20020a170903215100b0015e8d4eb1b7sm5571230ple.1.2022.05.23.14.14.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 23 May 2022 14:14:07 -0700 (PDT) From: Matthias Kaehlcke To: Alasdair Kergon , Mike Snitzer , Kees Cook , James Morris , "Serge E . Hallyn" Cc: Milan Broz , dm-devel@redhat.com, linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, Song Liu , Douglas Anderson , linux-security-module@vger.kernel.org, Matthias Kaehlcke Subject: [PATCH v6 3/3] dm: verity-loadpin: Use CONFIG_SECURITY_LOADPIN_VERITY for conditional compilation Date: Mon, 23 May 2022 14:14:00 -0700 Message-Id: <20220523141325.v6.3.I5aca2dcc3b06de4bf53696cd21329dce8272b8aa@changeid> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog In-Reply-To: <20220523211400.290537-1-mka@chromium.org> References: <20220523211400.290537-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 Acked-by: Kees Cook --- Changes in v6: - none Changes in v5: - added 'Acked-by' tag from Kees Changes in v4: - none 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 71771901c823..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 @@ -108,12 +109,6 @@ ifeq ($(CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG),y) dm-verity-objs +=3D dm-verity-verify-sig.o endif =20 -ifeq ($(CONFIG_DM_VERITY),y) -ifeq ($(CONFIG_SECURITY_LOADPIN),y) -dm-verity-objs +=3D dm-verity-loadpin.o -endif -endif - ifeq ($(CONFIG_DM_AUDIT),y) dm-mod-objs +=3D dm-audit.o endif diff --git a/include/linux/dm-verity-loadpin.h b/include/linux/dm-verity-lo= adpin.h index fb695ecaa5d5..552b817ab102 100644 --- a/include/linux/dm-verity-loadpin.h +++ b/include/linux/dm-verity-loadpin.h @@ -15,7 +15,7 @@ struct dm_verity_loadpin_trusted_root_digest { u8 data[]; }; =20 -#if IS_ENABLED(CONFIG_SECURITY_LOADPIN) && IS_BUILTIN(CONFIG_DM_VERITY) +#if IS_ENABLED(CONFIG_SECURITY_LOADPIN_VERITY) bool dm_verity_loadpin_is_bdev_trusted(struct block_device *bdev); #else static inline bool dm_verity_loadpin_is_bdev_trusted(struct block_device *= bdev) --=20 2.36.1.124.g0e6072fb45-goog