All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/3] LoadPin: Enable loading from trusted dm-verity devices
@ 2022-05-04 19:54 ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-04 19:54 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris, Serge E . Hallyn
  Cc: dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module, Matthias Kaehlcke

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) [1] 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 [2] to
verify the integrity of the DLC content.

This series extends LoadPin to allow loading of kernel files from trusted
dm-verity devices. 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.

[1] https://chromium.googlesource.com/chromiumos/platform2/+/HEAD/dlcservice/docs/developer.md
[2] https://www.kernel.org/doc/html/latest/admin-guide/device-mapper/verity.html

Changes in v3:
- added securityfs for LoadPin (currently only populated when
  CONFIG_SECURITY_LOADPIN_VERITY=y)
- 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

Matthias Kaehlcke (3):
  dm: Add verity helpers for LoadPin
  LoadPin: Enable loading from trusted dm-verity devices
  dm: verity-loadpin: Use CONFIG_SECURITY_LOADPIN_VERITY for conditional
    compilation

 drivers/md/Makefile               |   1 +
 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 +++++
 include/uapi/linux/loadpin.h      |  19 +++
 security/loadpin/Kconfig          |  16 +++
 security/loadpin/loadpin.c        | 184 +++++++++++++++++++++++++++++-
 8 files changed, 363 insertions(+), 1 deletion(-)
 create mode 100644 drivers/md/dm-verity-loadpin.c
 create mode 100644 include/linux/dm-verity-loadpin.h
 create mode 100644 include/uapi/linux/loadpin.h

-- 
2.36.0.464.gb9c8b46e94-goog


^ permalink raw reply	[flat|nested] 42+ messages in thread

* [dm-devel] [PATCH v3 0/3] LoadPin: Enable loading from trusted dm-verity devices
@ 2022-05-04 19:54 ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-04 19:54 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris, Serge E . Hallyn
  Cc: Douglas Anderson, linux-security-module, linux-kernel,
	linux-raid, Song Liu, dm-devel, Matthias Kaehlcke

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) [1] 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 [2] to
verify the integrity of the DLC content.

This series extends LoadPin to allow loading of kernel files from trusted
dm-verity devices. 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.

[1] https://chromium.googlesource.com/chromiumos/platform2/+/HEAD/dlcservice/docs/developer.md
[2] https://www.kernel.org/doc/html/latest/admin-guide/device-mapper/verity.html

Changes in v3:
- added securityfs for LoadPin (currently only populated when
  CONFIG_SECURITY_LOADPIN_VERITY=y)
- 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

Matthias Kaehlcke (3):
  dm: Add verity helpers for LoadPin
  LoadPin: Enable loading from trusted dm-verity devices
  dm: verity-loadpin: Use CONFIG_SECURITY_LOADPIN_VERITY for conditional
    compilation

 drivers/md/Makefile               |   1 +
 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 +++++
 include/uapi/linux/loadpin.h      |  19 +++
 security/loadpin/Kconfig          |  16 +++
 security/loadpin/loadpin.c        | 184 +++++++++++++++++++++++++++++-
 8 files changed, 363 insertions(+), 1 deletion(-)
 create mode 100644 drivers/md/dm-verity-loadpin.c
 create mode 100644 include/linux/dm-verity-loadpin.h
 create mode 100644 include/uapi/linux/loadpin.h

-- 
2.36.0.464.gb9c8b46e94-goog

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* [PATCH v3 1/3] dm: Add verity helpers for LoadPin
  2022-05-04 19:54 ` [dm-devel] " Matthias Kaehlcke
@ 2022-05-04 19:54   ` Matthias Kaehlcke
  -1 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-04 19:54 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris, Serge E . Hallyn
  Cc: dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module, Matthias Kaehlcke

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 <mka@chromium.org>
---

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			+= 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 <linux/list.h>
+#include <linux/kernel.h>
+#include <linux/dm-verity-loadpin.h>
+
+#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..6f07b849fcb2 100644
--- a/drivers/md/dm-verity-target.c
+++ b/drivers/md/dm-verity-target.c
@@ -19,6 +19,7 @@
 #include <linux/module.h>
 #include <linux/reboot.h>
 #include <linux/scatterlist.h>
+#include <linux/string.h>
 
 #define DM_MSG_PREFIX			"verity"
 
@@ -1310,6 +1311,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 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, 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 <linux/list.h>
+
+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 */
-- 
2.36.0.464.gb9c8b46e94-goog


^ permalink raw reply related	[flat|nested] 42+ messages in thread

* [dm-devel] [PATCH v3 1/3] dm: Add verity helpers for LoadPin
@ 2022-05-04 19:54   ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-04 19:54 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris, Serge E . Hallyn
  Cc: Douglas Anderson, linux-security-module, linux-kernel,
	linux-raid, Song Liu, dm-devel, Matthias Kaehlcke

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 <mka@chromium.org>
---

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			+= 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 <linux/list.h>
+#include <linux/kernel.h>
+#include <linux/dm-verity-loadpin.h>
+
+#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..6f07b849fcb2 100644
--- a/drivers/md/dm-verity-target.c
+++ b/drivers/md/dm-verity-target.c
@@ -19,6 +19,7 @@
 #include <linux/module.h>
 #include <linux/reboot.h>
 #include <linux/scatterlist.h>
+#include <linux/string.h>
 
 #define DM_MSG_PREFIX			"verity"
 
@@ -1310,6 +1311,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 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, 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 <linux/list.h>
+
+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 */
-- 
2.36.0.464.gb9c8b46e94-goog

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply related	[flat|nested] 42+ messages in thread

* [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
  2022-05-04 19:54 ` [dm-devel] " Matthias Kaehlcke
@ 2022-05-04 19:54   ` Matthias Kaehlcke
  -1 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-04 19:54 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris, Serge E . Hallyn
  Cc: dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module, Matthias Kaehlcke

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/dlcservice/docs/developer.md

Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
---

Changes in v3:
- added securityfs for LoadPin (currently only populated when
  CONFIG_SECURITY_LOADPIN_VERITY=y)
- 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 verity 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, 0x00, 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=1".
+
+config SECURITY_LOADPIN_VERITY
+	bool "Allow reading files from certain other filesystems that use dm-verity"
+	depends on DM_VERITY=y && 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 <linux/path.h>
 #include <linux/sched.h>	/* current */
 #include <linux/string_helpers.h>
+#include <linux/device-mapper.h>
+#include <linux/dm-verity-loadpin.h>
+#include <uapi/linux/loadpin.h>
 
 static void report_load(const char *origin, struct file *file, char *operation)
 {
@@ -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
 
 #ifdef CONFIG_SYSCTL
 
@@ -118,6 +124,24 @@ static void loadpin_sb_free_security(struct super_block *mnt_sb)
 	}
 }
 
+#ifdef CONFIG_SECURITY_LOADPIN_VERITY
+static bool loadpin_is_fs_trusted(struct super_block *sb)
+{
+	struct mapped_device *md = dm_get_md(sb->s_bdev->bd_dev);
+	bool trusted;
+
+	if (!md)
+		return false;
+
+	trusted = 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 id,
 			     bool contents)
 {
@@ -174,7 +198,8 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
 		spin_unlock(&pinned_root_spinlock);
 	}
 
-	if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
+	if (IS_ERR_OR_NULL(pinned_root) ||
+	    ((load_root != 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;
 }
 
@@ -248,6 +274,162 @@ DEFINE_LSM(loadpin) = {
 	.init = loadpin_init,
 };
 
+#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 = fdget(fd);
+	if (!f.file)
+		return -EINVAL;
+
+	data = kzalloc(SZ_4K, GFP_KERNEL);
+	if (!data) {
+		rc = -ENOMEM;
+		goto err;
+	}
+
+	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
+	if (rc < 0)
+		goto err;
+
+	((char *)data)[rc] = '\0';
+
+	p = strim(data);
+	while ((d = strsep(&p, ",")) != NULL) {
+		int len = strlen(d);
+		struct trusted_root_digest *trd;
+
+		if (len % 2) {
+			rc = -EPROTO;
+			goto err;
+		}
+
+		len /= 2;
+
+		trd = kzalloc(sizeof(*trd), GFP_KERNEL);
+		if (!trd) {
+			rc = -ENOMEM;
+			goto err;
+		}
+
+		trd->data = kzalloc(len, GFP_KERNEL);
+		if (!trd->data) {
+			kfree(trd);
+			rc = -ENOMEM;
+			goto err;
+		}
+
+		if (hex2bin(trd->data, d, len)) {
+			kfree(trd);
+			kfree(trd->data);
+			rc = -EPROTO;
+			goto err;
+		}
+
+		trd->len = 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 = (void __user *)arg;
+	unsigned int fd;
+	int rc;
+
+	switch (cmd) {
+	case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
+		rc = 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 = {
+	.unlocked_ioctl = dm_verity_ioctl,
+	.compat_ioctl = 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 = 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 = 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 == 0). */
 module_param(enforce, int, 0);
 MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
-- 
2.36.0.464.gb9c8b46e94-goog


^ permalink raw reply related	[flat|nested] 42+ messages in thread

* [dm-devel] [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
@ 2022-05-04 19:54   ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-04 19:54 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris, Serge E . Hallyn
  Cc: Douglas Anderson, linux-security-module, linux-kernel,
	linux-raid, Song Liu, dm-devel, Matthias Kaehlcke

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/dlcservice/docs/developer.md

Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
---

Changes in v3:
- added securityfs for LoadPin (currently only populated when
  CONFIG_SECURITY_LOADPIN_VERITY=y)
- 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 verity 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, 0x00, 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=1".
+
+config SECURITY_LOADPIN_VERITY
+	bool "Allow reading files from certain other filesystems that use dm-verity"
+	depends on DM_VERITY=y && 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 <linux/path.h>
 #include <linux/sched.h>	/* current */
 #include <linux/string_helpers.h>
+#include <linux/device-mapper.h>
+#include <linux/dm-verity-loadpin.h>
+#include <uapi/linux/loadpin.h>
 
 static void report_load(const char *origin, struct file *file, char *operation)
 {
@@ -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
 
 #ifdef CONFIG_SYSCTL
 
@@ -118,6 +124,24 @@ static void loadpin_sb_free_security(struct super_block *mnt_sb)
 	}
 }
 
+#ifdef CONFIG_SECURITY_LOADPIN_VERITY
+static bool loadpin_is_fs_trusted(struct super_block *sb)
+{
+	struct mapped_device *md = dm_get_md(sb->s_bdev->bd_dev);
+	bool trusted;
+
+	if (!md)
+		return false;
+
+	trusted = 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 id,
 			     bool contents)
 {
@@ -174,7 +198,8 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
 		spin_unlock(&pinned_root_spinlock);
 	}
 
-	if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
+	if (IS_ERR_OR_NULL(pinned_root) ||
+	    ((load_root != 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;
 }
 
@@ -248,6 +274,162 @@ DEFINE_LSM(loadpin) = {
 	.init = loadpin_init,
 };
 
+#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 = fdget(fd);
+	if (!f.file)
+		return -EINVAL;
+
+	data = kzalloc(SZ_4K, GFP_KERNEL);
+	if (!data) {
+		rc = -ENOMEM;
+		goto err;
+	}
+
+	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
+	if (rc < 0)
+		goto err;
+
+	((char *)data)[rc] = '\0';
+
+	p = strim(data);
+	while ((d = strsep(&p, ",")) != NULL) {
+		int len = strlen(d);
+		struct trusted_root_digest *trd;
+
+		if (len % 2) {
+			rc = -EPROTO;
+			goto err;
+		}
+
+		len /= 2;
+
+		trd = kzalloc(sizeof(*trd), GFP_KERNEL);
+		if (!trd) {
+			rc = -ENOMEM;
+			goto err;
+		}
+
+		trd->data = kzalloc(len, GFP_KERNEL);
+		if (!trd->data) {
+			kfree(trd);
+			rc = -ENOMEM;
+			goto err;
+		}
+
+		if (hex2bin(trd->data, d, len)) {
+			kfree(trd);
+			kfree(trd->data);
+			rc = -EPROTO;
+			goto err;
+		}
+
+		trd->len = 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 = (void __user *)arg;
+	unsigned int fd;
+	int rc;
+
+	switch (cmd) {
+	case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
+		rc = 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 = {
+	.unlocked_ioctl = dm_verity_ioctl,
+	.compat_ioctl = 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 = 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 = 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 == 0). */
 module_param(enforce, int, 0);
 MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
-- 
2.36.0.464.gb9c8b46e94-goog

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply related	[flat|nested] 42+ messages in thread

* [PATCH v3 3/3] dm: verity-loadpin: Use CONFIG_SECURITY_LOADPIN_VERITY for conditional compilation
  2022-05-04 19:54 ` [dm-devel] " Matthias Kaehlcke
@ 2022-05-04 19:54   ` Matthias Kaehlcke
  -1 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-04 19:54 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris, Serge E . Hallyn
  Cc: dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module, Matthias Kaehlcke

The verity glue for LoadPin is only needed when CONFIG_SECURITY_LOADPIN_VERITY
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 <mka@chromium.org>
---

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)	+= dm-log-writes.o
 obj-$(CONFIG_DM_INTEGRITY)	+= dm-integrity.o
 obj-$(CONFIG_DM_ZONED)		+= dm-zoned.o
 obj-$(CONFIG_DM_WRITECACHE)	+= dm-writecache.o
+obj-$(CONFIG_SECURITY_LOADPIN_VERITY)	+= dm-verity-loadpin.o
 
 ifeq ($(CONFIG_DM_INIT),y)
 dm-mod-objs			+= dm-init.o
@@ -100,12 +101,6 @@ 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/include/linux/dm-verity-loadpin.h b/include/linux/dm-verity-loadpin.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;
 };
 
-#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
-- 
2.36.0.464.gb9c8b46e94-goog


^ permalink raw reply related	[flat|nested] 42+ messages in thread

* [dm-devel] [PATCH v3 3/3] dm: verity-loadpin: Use CONFIG_SECURITY_LOADPIN_VERITY for conditional compilation
@ 2022-05-04 19:54   ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-04 19:54 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris, Serge E . Hallyn
  Cc: Douglas Anderson, linux-security-module, linux-kernel,
	linux-raid, Song Liu, dm-devel, Matthias Kaehlcke

The verity glue for LoadPin is only needed when CONFIG_SECURITY_LOADPIN_VERITY
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 <mka@chromium.org>
---

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)	+= dm-log-writes.o
 obj-$(CONFIG_DM_INTEGRITY)	+= dm-integrity.o
 obj-$(CONFIG_DM_ZONED)		+= dm-zoned.o
 obj-$(CONFIG_DM_WRITECACHE)	+= dm-writecache.o
+obj-$(CONFIG_SECURITY_LOADPIN_VERITY)	+= dm-verity-loadpin.o
 
 ifeq ($(CONFIG_DM_INIT),y)
 dm-mod-objs			+= dm-init.o
@@ -100,12 +101,6 @@ 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/include/linux/dm-verity-loadpin.h b/include/linux/dm-verity-loadpin.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;
 };
 
-#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
-- 
2.36.0.464.gb9c8b46e94-goog

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply related	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
  2022-05-04 19:54   ` [dm-devel] " Matthias Kaehlcke
@ 2022-05-04 22:26     ` kernel test robot
  -1 siblings, 0 replies; 42+ messages in thread
From: kernel test robot @ 2022-05-04 22:26 UTC (permalink / raw)
  To: Matthias Kaehlcke, Alasdair Kergon, Mike Snitzer, Kees Cook,
	James Morris, Serge E . Hallyn
  Cc: kbuild-all, dm-devel, linux-kernel, linux-raid, Song Liu,
	Douglas Anderson, linux-security-module, Matthias Kaehlcke

Hi Matthias,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on device-mapper-dm/for-next]
[also build test WARNING on song-md/md-next kees/for-next/pstore linus/master v5.18-rc5 next-20220504]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Matthias-Kaehlcke/LoadPin-Enable-loading-from-trusted-dm-verity-devices/20220505-035620
base:   https://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm.git for-next
config: arc-allyesconfig (https://download.01.org/0day-ci/archive/20220505/202205050636.VNgSDFVz-lkp@intel.com/config)
compiler: arceb-elf-gcc (GCC) 11.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/56f912dcbb302f9a7e6694493529abd4e3f337af
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Matthias-Kaehlcke/LoadPin-Enable-loading-from-trusted-dm-verity-devices/20220505-035620
        git checkout 56f912dcbb302f9a7e6694493529abd4e3f337af
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross W=1 O=build_dir ARCH=arc SHELL=/bin/bash security/loadpin/

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   In file included from include/linux/kernel.h:29,
                    from include/linux/cpumask.h:10,
                    from include/linux/mm_types_task.h:14,
                    from include/linux/mm_types.h:5,
                    from include/linux/buildid.h:5,
                    from include/linux/module.h:14,
                    from security/loadpin/loadpin.c:12:
   security/loadpin/loadpin.c: In function 'init_loadpin_securityfs':
>> include/linux/kern_levels.h:5:25: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
       5 | #define KERN_SOH        "\001"          /* ASCII Start Of Header */
         |                         ^~~~~~
   include/linux/printk.h:418:25: note: in definition of macro 'printk_index_wrap'
     418 |                 _p_func(_fmt, ##__VA_ARGS__);                           \
         |                         ^~~~
   include/linux/printk.h:489:9: note: in expansion of macro 'printk'
     489 |         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
         |         ^~~~~~
   include/linux/kern_levels.h:11:25: note: in expansion of macro 'KERN_SOH'
      11 | #define KERN_ERR        KERN_SOH "3"    /* error conditions */
         |                         ^~~~~~~~
   include/linux/printk.h:489:16: note: in expansion of macro 'KERN_ERR'
     489 |         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
         |                ^~~~~~~~
   security/loadpin/loadpin.c:413:17: note: in expansion of macro 'pr_err'
     413 |                 pr_err("LoadPin: could not create securityfs dir: %d\n",
         |                 ^~~~~~
>> include/linux/kern_levels.h:5:25: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
       5 | #define KERN_SOH        "\001"          /* ASCII Start Of Header */
         |                         ^~~~~~
   include/linux/printk.h:418:25: note: in definition of macro 'printk_index_wrap'
     418 |                 _p_func(_fmt, ##__VA_ARGS__);                           \
         |                         ^~~~
   include/linux/printk.h:489:9: note: in expansion of macro 'printk'
     489 |         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
         |         ^~~~~~
   include/linux/kern_levels.h:11:25: note: in expansion of macro 'KERN_SOH'
      11 | #define KERN_ERR        KERN_SOH "3"    /* error conditions */
         |                         ^~~~~~~~
   include/linux/printk.h:489:16: note: in expansion of macro 'KERN_ERR'
     489 |         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
         |                ^~~~~~~~
   security/loadpin/loadpin.c:421:17: note: in expansion of macro 'pr_err'
     421 |                 pr_err("LoadPin: could not create securityfs entry 'dm-verity': %d\n",
         |                 ^~~~~~


vim +5 include/linux/kern_levels.h

314ba3520e513a Joe Perches 2012-07-30  4  
04d2c8c83d0e3a Joe Perches 2012-07-30 @5  #define KERN_SOH	"\001"		/* ASCII Start Of Header */
04d2c8c83d0e3a Joe Perches 2012-07-30  6  #define KERN_SOH_ASCII	'\001'
04d2c8c83d0e3a Joe Perches 2012-07-30  7  

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
@ 2022-05-04 22:26     ` kernel test robot
  0 siblings, 0 replies; 42+ messages in thread
From: kernel test robot @ 2022-05-04 22:26 UTC (permalink / raw)
  To: Matthias Kaehlcke, Alasdair Kergon, Mike Snitzer, Kees Cook,
	James Morris, Serge E . Hallyn
  Cc: linux-security-module, kbuild-all, linux-kernel,
	Douglas Anderson, linux-raid, Song Liu, dm-devel,
	Matthias Kaehlcke

Hi Matthias,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on device-mapper-dm/for-next]
[also build test WARNING on song-md/md-next kees/for-next/pstore linus/master v5.18-rc5 next-20220504]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Matthias-Kaehlcke/LoadPin-Enable-loading-from-trusted-dm-verity-devices/20220505-035620
base:   https://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm.git for-next
config: arc-allyesconfig (https://download.01.org/0day-ci/archive/20220505/202205050636.VNgSDFVz-lkp@intel.com/config)
compiler: arceb-elf-gcc (GCC) 11.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/56f912dcbb302f9a7e6694493529abd4e3f337af
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Matthias-Kaehlcke/LoadPin-Enable-loading-from-trusted-dm-verity-devices/20220505-035620
        git checkout 56f912dcbb302f9a7e6694493529abd4e3f337af
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross W=1 O=build_dir ARCH=arc SHELL=/bin/bash security/loadpin/

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

   In file included from include/linux/kernel.h:29,
                    from include/linux/cpumask.h:10,
                    from include/linux/mm_types_task.h:14,
                    from include/linux/mm_types.h:5,
                    from include/linux/buildid.h:5,
                    from include/linux/module.h:14,
                    from security/loadpin/loadpin.c:12:
   security/loadpin/loadpin.c: In function 'init_loadpin_securityfs':
>> include/linux/kern_levels.h:5:25: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
       5 | #define KERN_SOH        "\001"          /* ASCII Start Of Header */
         |                         ^~~~~~
   include/linux/printk.h:418:25: note: in definition of macro 'printk_index_wrap'
     418 |                 _p_func(_fmt, ##__VA_ARGS__);                           \
         |                         ^~~~
   include/linux/printk.h:489:9: note: in expansion of macro 'printk'
     489 |         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
         |         ^~~~~~
   include/linux/kern_levels.h:11:25: note: in expansion of macro 'KERN_SOH'
      11 | #define KERN_ERR        KERN_SOH "3"    /* error conditions */
         |                         ^~~~~~~~
   include/linux/printk.h:489:16: note: in expansion of macro 'KERN_ERR'
     489 |         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
         |                ^~~~~~~~
   security/loadpin/loadpin.c:413:17: note: in expansion of macro 'pr_err'
     413 |                 pr_err("LoadPin: could not create securityfs dir: %d\n",
         |                 ^~~~~~
>> include/linux/kern_levels.h:5:25: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
       5 | #define KERN_SOH        "\001"          /* ASCII Start Of Header */
         |                         ^~~~~~
   include/linux/printk.h:418:25: note: in definition of macro 'printk_index_wrap'
     418 |                 _p_func(_fmt, ##__VA_ARGS__);                           \
         |                         ^~~~
   include/linux/printk.h:489:9: note: in expansion of macro 'printk'
     489 |         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
         |         ^~~~~~
   include/linux/kern_levels.h:11:25: note: in expansion of macro 'KERN_SOH'
      11 | #define KERN_ERR        KERN_SOH "3"    /* error conditions */
         |                         ^~~~~~~~
   include/linux/printk.h:489:16: note: in expansion of macro 'KERN_ERR'
     489 |         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
         |                ^~~~~~~~
   security/loadpin/loadpin.c:421:17: note: in expansion of macro 'pr_err'
     421 |                 pr_err("LoadPin: could not create securityfs entry 'dm-verity': %d\n",
         |                 ^~~~~~


vim +5 include/linux/kern_levels.h

314ba3520e513a Joe Perches 2012-07-30  4  
04d2c8c83d0e3a Joe Perches 2012-07-30 @5  #define KERN_SOH	"\001"		/* ASCII Start Of Header */
04d2c8c83d0e3a Joe Perches 2012-07-30  6  #define KERN_SOH_ASCII	'\001'
04d2c8c83d0e3a Joe Perches 2012-07-30  7  

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 1/3] dm: Add verity helpers for LoadPin
  2022-05-04 19:54   ` [dm-devel] " Matthias Kaehlcke
@ 2022-05-11 20:54     ` Matthias Kaehlcke
  -1 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-11 20:54 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris, Serge E . Hallyn
  Cc: dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module

Alasdar/Mike, I'd be interested in your take on adding these functions
to verity/DM, to get an idea whether this series has a path forward to
landing upstream.

Thanks

Matthias

On Wed, May 04, 2022 at 12:54:17PM -0700, Matthias Kaehlcke wrote:
> 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 <mka@chromium.org>
> ---
> 
> 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			+= 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 <linux/list.h>
> +#include <linux/kernel.h>
> +#include <linux/dm-verity-loadpin.h>
> +
> +#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..6f07b849fcb2 100644
> --- a/drivers/md/dm-verity-target.c
> +++ b/drivers/md/dm-verity-target.c
> @@ -19,6 +19,7 @@
>  #include <linux/module.h>
>  #include <linux/reboot.h>
>  #include <linux/scatterlist.h>
> +#include <linux/string.h>
>  
>  #define DM_MSG_PREFIX			"verity"
>  
> @@ -1310,6 +1311,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 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, 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 <linux/list.h>
> +
> +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 */
> -- 
> 2.36.0.464.gb9c8b46e94-goog
> 

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 1/3] dm: Add verity helpers for LoadPin
@ 2022-05-11 20:54     ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-11 20:54 UTC (permalink / raw)
  To: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris, Serge E . Hallyn
  Cc: Douglas Anderson, linux-security-module, linux-kernel,
	linux-raid, Song Liu, dm-devel

Alasdar/Mike, I'd be interested in your take on adding these functions
to verity/DM, to get an idea whether this series has a path forward to
landing upstream.

Thanks

Matthias

On Wed, May 04, 2022 at 12:54:17PM -0700, Matthias Kaehlcke wrote:
> 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 <mka@chromium.org>
> ---
> 
> 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			+= 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 <linux/list.h>
> +#include <linux/kernel.h>
> +#include <linux/dm-verity-loadpin.h>
> +
> +#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..6f07b849fcb2 100644
> --- a/drivers/md/dm-verity-target.c
> +++ b/drivers/md/dm-verity-target.c
> @@ -19,6 +19,7 @@
>  #include <linux/module.h>
>  #include <linux/reboot.h>
>  #include <linux/scatterlist.h>
> +#include <linux/string.h>
>  
>  #define DM_MSG_PREFIX			"verity"
>  
> @@ -1310,6 +1311,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 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, 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 <linux/list.h>
> +
> +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 */
> -- 
> 2.36.0.464.gb9c8b46e94-goog
> 

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 1/3] dm: Add verity helpers for LoadPin
  2022-05-11 20:54     ` [dm-devel] " Matthias Kaehlcke
@ 2022-05-12 17:19       ` Mike Snitzer
  -1 siblings, 0 replies; 42+ messages in thread
From: Mike Snitzer @ 2022-05-12 17:19 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris,
	Serge E . Hallyn, dm-devel, linux-kernel, linux-raid, Song Liu,
	Douglas Anderson, linux-security-module

On Wed, May 11 2022 at  4:54P -0400,
Matthias Kaehlcke <mka@chromium.org> wrote:

> Alasdar/Mike, I'd be interested in your take on adding these functions
> to verity/DM, to get an idea whether this series has a path forward to
> landing upstream.

I'll be reviewing your patchset now. Comments inlined below.

> On Wed, May 04, 2022 at 12:54:17PM -0700, Matthias Kaehlcke wrote:
> > 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 <mka@chromium.org>
> > ---
> > 
> > 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			+= dm-ima.o
> >  endif
> >  
> > +ifeq ($(CONFIG_DM_VERITY),y)
> > +ifeq ($(CONFIG_SECURITY_LOADPIN),y)
> > +dm-mod-objs			+= dm-verity-loadpin.o
> > +endif
> > +endif
> > +

Why are you extending dm-mod-objs?  Why not dm-verity-objs?

> >  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 <linux/list.h>
> > +#include <linux/kernel.h>
> > +#include <linux/dm-verity-loadpin.h>
> > +
> > +#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");
> > +}

Would prefer you set a DM_MSG_PREFIX and use DMWARN() instead.

You never explicitly initialize trusted_root_digests to NULL.

Also, I'll have to look at the caller(s), but without locking this
branching is racey.

> > +
> > +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;

Again, where is the locking to protect trusted_root_digests?

> > +	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..6f07b849fcb2 100644
> > --- a/drivers/md/dm-verity-target.c
> > +++ b/drivers/md/dm-verity-target.c
> > @@ -19,6 +19,7 @@
> >  #include <linux/module.h>
> >  #include <linux/reboot.h>
> >  #include <linux/scatterlist.h>
> > +#include <linux/string.h>
> >  
> >  #define DM_MSG_PREFIX			"verity"
> >  
> > @@ -1310,6 +1311,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 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, 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 <linux/list.h>
> > +
> > +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 */
> > -- 
> > 2.36.0.464.gb9c8b46e94-goog
> > 
> 


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 1/3] dm: Add verity helpers for LoadPin
@ 2022-05-12 17:19       ` Mike Snitzer
  0 siblings, 0 replies; 42+ messages in thread
From: Mike Snitzer @ 2022-05-12 17:19 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: linux-security-module, Douglas Anderson, Kees Cook, Mike Snitzer,
	James Morris, linux-kernel, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn

On Wed, May 11 2022 at  4:54P -0400,
Matthias Kaehlcke <mka@chromium.org> wrote:

> Alasdar/Mike, I'd be interested in your take on adding these functions
> to verity/DM, to get an idea whether this series has a path forward to
> landing upstream.

I'll be reviewing your patchset now. Comments inlined below.

> On Wed, May 04, 2022 at 12:54:17PM -0700, Matthias Kaehlcke wrote:
> > 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 <mka@chromium.org>
> > ---
> > 
> > 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			+= dm-ima.o
> >  endif
> >  
> > +ifeq ($(CONFIG_DM_VERITY),y)
> > +ifeq ($(CONFIG_SECURITY_LOADPIN),y)
> > +dm-mod-objs			+= dm-verity-loadpin.o
> > +endif
> > +endif
> > +

Why are you extending dm-mod-objs?  Why not dm-verity-objs?

> >  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 <linux/list.h>
> > +#include <linux/kernel.h>
> > +#include <linux/dm-verity-loadpin.h>
> > +
> > +#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");
> > +}

Would prefer you set a DM_MSG_PREFIX and use DMWARN() instead.

You never explicitly initialize trusted_root_digests to NULL.

Also, I'll have to look at the caller(s), but without locking this
branching is racey.

> > +
> > +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;

Again, where is the locking to protect trusted_root_digests?

> > +	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..6f07b849fcb2 100644
> > --- a/drivers/md/dm-verity-target.c
> > +++ b/drivers/md/dm-verity-target.c
> > @@ -19,6 +19,7 @@
> >  #include <linux/module.h>
> >  #include <linux/reboot.h>
> >  #include <linux/scatterlist.h>
> > +#include <linux/string.h>
> >  
> >  #define DM_MSG_PREFIX			"verity"
> >  
> > @@ -1310,6 +1311,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 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, 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 <linux/list.h>
> > +
> > +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 */
> > -- 
> > 2.36.0.464.gb9c8b46e94-goog
> > 
> 

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 1/3] dm: Add verity helpers for LoadPin
  2022-05-12 17:19       ` [dm-devel] " Mike Snitzer
@ 2022-05-12 18:14         ` Matthias Kaehlcke
  -1 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-12 18:14 UTC (permalink / raw)
  To: Mike Snitzer
  Cc: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris,
	Serge E . Hallyn, dm-devel, linux-kernel, linux-raid, Song Liu,
	Douglas Anderson, linux-security-module

Hi Mike,

On Thu, May 12, 2022 at 01:19:12PM -0400, Mike Snitzer wrote:
> On Wed, May 11 2022 at  4:54P -0400,
> Matthias Kaehlcke <mka@chromium.org> wrote:
> 
> > Alasdar/Mike, I'd be interested in your take on adding these functions
> > to verity/DM, to get an idea whether this series has a path forward to
> > landing upstream.
> 
> I'll be reviewing your patchset now. Comments inlined below.

Thanks for the review!

> > On Wed, May 04, 2022 at 12:54:17PM -0700, Matthias Kaehlcke wrote:
> > > 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 <mka@chromium.org>
> > > ---
> > > 
> > > 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			+= dm-ima.o
> > >  endif
> > >  
> > > +ifeq ($(CONFIG_DM_VERITY),y)
> > > +ifeq ($(CONFIG_SECURITY_LOADPIN),y)
> > > +dm-mod-objs			+= dm-verity-loadpin.o
> > > +endif
> > > +endif
> > > +
> 
> Why are you extending dm-mod-objs?  Why not dm-verity-objs?
> 
> > >  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 <linux/list.h>
> > > +#include <linux/kernel.h>
> > > +#include <linux/dm-verity-loadpin.h>
> > > +
> > > +#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");
> > > +}
> 
> Would prefer you set a DM_MSG_PREFIX and use DMWARN() instead.

Sure, I'll change it to DMWARN().

> You never explicitly initialize trusted_root_digests to NULL.

That's what I had initially, however checkpatch didn't like it:

ERROR: do not initialise statics to NULL
#70: FILE: drivers/md/dm-verity-loadpin.c:10:
+static struct list_head *trusted_root_digests = NULL;

So I removed it.

> Also, I'll have to look at the caller(s), but without locking this
> branching is racey.

The list of trusted root digests can only be set once and is never
cleared. So if it is not set there is nothing to do, and if it is
set the list is immutable. We are trusting the caller to adhere to
that 'contract' and partially enforce it in dm_verity_loadpin_set_trusted_root_digests()
With that do you still think locking is needed?

> > > +
> > > +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;
> 
> Again, where is the locking to protect trusted_root_digests?

See above

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 1/3] dm: Add verity helpers for LoadPin
@ 2022-05-12 18:14         ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-12 18:14 UTC (permalink / raw)
  To: Mike Snitzer
  Cc: linux-security-module, Douglas Anderson, Kees Cook, Mike Snitzer,
	James Morris, linux-kernel, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn

Hi Mike,

On Thu, May 12, 2022 at 01:19:12PM -0400, Mike Snitzer wrote:
> On Wed, May 11 2022 at  4:54P -0400,
> Matthias Kaehlcke <mka@chromium.org> wrote:
> 
> > Alasdar/Mike, I'd be interested in your take on adding these functions
> > to verity/DM, to get an idea whether this series has a path forward to
> > landing upstream.
> 
> I'll be reviewing your patchset now. Comments inlined below.

Thanks for the review!

> > On Wed, May 04, 2022 at 12:54:17PM -0700, Matthias Kaehlcke wrote:
> > > 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 <mka@chromium.org>
> > > ---
> > > 
> > > 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			+= dm-ima.o
> > >  endif
> > >  
> > > +ifeq ($(CONFIG_DM_VERITY),y)
> > > +ifeq ($(CONFIG_SECURITY_LOADPIN),y)
> > > +dm-mod-objs			+= dm-verity-loadpin.o
> > > +endif
> > > +endif
> > > +
> 
> Why are you extending dm-mod-objs?  Why not dm-verity-objs?
> 
> > >  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 <linux/list.h>
> > > +#include <linux/kernel.h>
> > > +#include <linux/dm-verity-loadpin.h>
> > > +
> > > +#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");
> > > +}
> 
> Would prefer you set a DM_MSG_PREFIX and use DMWARN() instead.

Sure, I'll change it to DMWARN().

> You never explicitly initialize trusted_root_digests to NULL.

That's what I had initially, however checkpatch didn't like it:

ERROR: do not initialise statics to NULL
#70: FILE: drivers/md/dm-verity-loadpin.c:10:
+static struct list_head *trusted_root_digests = NULL;

So I removed it.

> Also, I'll have to look at the caller(s), but without locking this
> branching is racey.

The list of trusted root digests can only be set once and is never
cleared. So if it is not set there is nothing to do, and if it is
set the list is immutable. We are trusting the caller to adhere to
that 'contract' and partially enforce it in dm_verity_loadpin_set_trusted_root_digests()
With that do you still think locking is needed?

> > > +
> > > +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;
> 
> Again, where is the locking to protect trusted_root_digests?

See above

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 1/3] dm: Add verity helpers for LoadPin
  2022-05-12 17:19       ` [dm-devel] " Mike Snitzer
@ 2022-05-12 20:44         ` Matthias Kaehlcke
  -1 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-12 20:44 UTC (permalink / raw)
  To: Mike Snitzer
  Cc: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris,
	Serge E . Hallyn, dm-devel, linux-kernel, linux-raid, Song Liu,
	Douglas Anderson, linux-security-module

On Thu, May 12, 2022 at 01:19:12PM -0400, Mike Snitzer wrote:
> On Wed, May 11 2022 at  4:54P -0400,
> Matthias Kaehlcke <mka@chromium.org> wrote:
> 
> > Alasdar/Mike, I'd be interested in your take on adding these functions
> > to verity/DM, to get an idea whether this series has a path forward to
> > landing upstream.
> 
> I'll be reviewing your patchset now. Comments inlined below.
> 
> > On Wed, May 04, 2022 at 12:54:17PM -0700, Matthias Kaehlcke wrote:
> > > 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 <mka@chromium.org>
> > > ---
> > > 
> > > 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			+= dm-ima.o
> > >  endif
> > >  
> > > +ifeq ($(CONFIG_DM_VERITY),y)
> > > +ifeq ($(CONFIG_SECURITY_LOADPIN),y)
> > > +dm-mod-objs			+= dm-verity-loadpin.o
> > > +endif
> > > +endif
> > > +
> 
> Why are you extending dm-mod-objs?  Why not dm-verity-objs?

Sorry, I missed to address this comment in my earlier reply.

I don't recall why I chose dm-mod-objs initially, agreed that
dm-verity-objs seems a better fit.

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 1/3] dm: Add verity helpers for LoadPin
@ 2022-05-12 20:44         ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-12 20:44 UTC (permalink / raw)
  To: Mike Snitzer
  Cc: linux-security-module, Douglas Anderson, Kees Cook, Mike Snitzer,
	James Morris, linux-kernel, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn

On Thu, May 12, 2022 at 01:19:12PM -0400, Mike Snitzer wrote:
> On Wed, May 11 2022 at  4:54P -0400,
> Matthias Kaehlcke <mka@chromium.org> wrote:
> 
> > Alasdar/Mike, I'd be interested in your take on adding these functions
> > to verity/DM, to get an idea whether this series has a path forward to
> > landing upstream.
> 
> I'll be reviewing your patchset now. Comments inlined below.
> 
> > On Wed, May 04, 2022 at 12:54:17PM -0700, Matthias Kaehlcke wrote:
> > > 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 <mka@chromium.org>
> > > ---
> > > 
> > > 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			+= dm-ima.o
> > >  endif
> > >  
> > > +ifeq ($(CONFIG_DM_VERITY),y)
> > > +ifeq ($(CONFIG_SECURITY_LOADPIN),y)
> > > +dm-mod-objs			+= dm-verity-loadpin.o
> > > +endif
> > > +endif
> > > +
> 
> Why are you extending dm-mod-objs?  Why not dm-verity-objs?

Sorry, I missed to address this comment in my earlier reply.

I don't recall why I chose dm-mod-objs initially, agreed that
dm-verity-objs seems a better fit.

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 1/3] dm: Add verity helpers for LoadPin
  2022-05-12 20:44         ` [dm-devel] " Matthias Kaehlcke
@ 2022-05-13 16:29           ` Mike Snitzer
  -1 siblings, 0 replies; 42+ messages in thread
From: Mike Snitzer @ 2022-05-13 16:29 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris,
	Serge E . Hallyn, dm-devel, linux-kernel, linux-raid, Song Liu,
	Douglas Anderson, linux-security-module

On Thu, May 12 2022 at  4:44P -0400,
Matthias Kaehlcke <mka@chromium.org> wrote:

> On Thu, May 12, 2022 at 01:19:12PM -0400, Mike Snitzer wrote:
> > On Wed, May 11 2022 at  4:54P -0400,
> > Matthias Kaehlcke <mka@chromium.org> wrote:
> > 
> > > Alasdar/Mike, I'd be interested in your take on adding these functions
> > > to verity/DM, to get an idea whether this series has a path forward to
> > > landing upstream.
> > 
> > I'll be reviewing your patchset now. Comments inlined below.
> > 
> > > On Wed, May 04, 2022 at 12:54:17PM -0700, Matthias Kaehlcke wrote:
> > > > 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 <mka@chromium.org>
> > > > ---
> > > > 
> > > > 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			+= dm-ima.o
> > > >  endif
> > > >  
> > > > +ifeq ($(CONFIG_DM_VERITY),y)
> > > > +ifeq ($(CONFIG_SECURITY_LOADPIN),y)
> > > > +dm-mod-objs			+= dm-verity-loadpin.o
> > > > +endif
> > > > +endif
> > > > +
> > 
> > Why are you extending dm-mod-objs?  Why not dm-verity-objs?
> 
> Sorry, I missed to address this comment in my earlier reply.
> 
> I don't recall why I chose dm-mod-objs initially, agreed that
> dm-verity-objs seems a better fit.

Yes, should be fixed even though the 3rd patch removes this change.

BTW, looking at the 2nd patch's loadpin_is_fs_trusted().  Seems to me
you'd do well to pass a 'struct block_device *' to a DM helper rather
than force security/loadpin/loadpin.c to mess around with DM device
refcounting, etc.


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 1/3] dm: Add verity helpers for LoadPin
@ 2022-05-13 16:29           ` Mike Snitzer
  0 siblings, 0 replies; 42+ messages in thread
From: Mike Snitzer @ 2022-05-13 16:29 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: linux-security-module, Douglas Anderson, Kees Cook, Mike Snitzer,
	James Morris, linux-kernel, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn

On Thu, May 12 2022 at  4:44P -0400,
Matthias Kaehlcke <mka@chromium.org> wrote:

> On Thu, May 12, 2022 at 01:19:12PM -0400, Mike Snitzer wrote:
> > On Wed, May 11 2022 at  4:54P -0400,
> > Matthias Kaehlcke <mka@chromium.org> wrote:
> > 
> > > Alasdar/Mike, I'd be interested in your take on adding these functions
> > > to verity/DM, to get an idea whether this series has a path forward to
> > > landing upstream.
> > 
> > I'll be reviewing your patchset now. Comments inlined below.
> > 
> > > On Wed, May 04, 2022 at 12:54:17PM -0700, Matthias Kaehlcke wrote:
> > > > 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 <mka@chromium.org>
> > > > ---
> > > > 
> > > > 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			+= dm-ima.o
> > > >  endif
> > > >  
> > > > +ifeq ($(CONFIG_DM_VERITY),y)
> > > > +ifeq ($(CONFIG_SECURITY_LOADPIN),y)
> > > > +dm-mod-objs			+= dm-verity-loadpin.o
> > > > +endif
> > > > +endif
> > > > +
> > 
> > Why are you extending dm-mod-objs?  Why not dm-verity-objs?
> 
> Sorry, I missed to address this comment in my earlier reply.
> 
> I don't recall why I chose dm-mod-objs initially, agreed that
> dm-verity-objs seems a better fit.

Yes, should be fixed even though the 3rd patch removes this change.

BTW, looking at the 2nd patch's loadpin_is_fs_trusted().  Seems to me
you'd do well to pass a 'struct block_device *' to a DM helper rather
than force security/loadpin/loadpin.c to mess around with DM device
refcounting, etc.

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
  2022-05-04 19:54   ` [dm-devel] " Matthias Kaehlcke
@ 2022-05-13 16:32     ` Mike Snitzer
  -1 siblings, 0 replies; 42+ messages in thread
From: Mike Snitzer @ 2022-05-13 16:32 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris,
	Serge E . Hallyn, dm-devel, linux-kernel, linux-raid, Song Liu,
	Douglas Anderson, linux-security-module

On Wed, May 04 2022 at  3:54P -0400,
Matthias Kaehlcke <mka@chromium.org> wrote:

> 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/dlcservice/docs/developer.md
> 
> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> ---
> 
> Changes in v3:
> - added securityfs for LoadPin (currently only populated when
>   CONFIG_SECURITY_LOADPIN_VERITY=y)
> - 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

I would certainly need some Reviewed-by:s from security and/or loadpin
experts if I were to pick this patch up.

Did you see the issues the kernel test robot emailed about?

You'd do well to fix those issues up when submitting another revision
of this patchset.

Mike


> 
> 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 verity 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, 0x00, 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=1".
> +
> +config SECURITY_LOADPIN_VERITY
> +	bool "Allow reading files from certain other filesystems that use dm-verity"
> +	depends on DM_VERITY=y && 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 <linux/path.h>
>  #include <linux/sched.h>	/* current */
>  #include <linux/string_helpers.h>
> +#include <linux/device-mapper.h>
> +#include <linux/dm-verity-loadpin.h>
> +#include <uapi/linux/loadpin.h>
>  
>  static void report_load(const char *origin, struct file *file, char *operation)
>  {
> @@ -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
>  
>  #ifdef CONFIG_SYSCTL
>  
> @@ -118,6 +124,24 @@ static void loadpin_sb_free_security(struct super_block *mnt_sb)
>  	}
>  }
>  
> +#ifdef CONFIG_SECURITY_LOADPIN_VERITY
> +static bool loadpin_is_fs_trusted(struct super_block *sb)
> +{
> +	struct mapped_device *md = dm_get_md(sb->s_bdev->bd_dev);
> +	bool trusted;
> +
> +	if (!md)
> +		return false;
> +
> +	trusted = 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 id,
>  			     bool contents)
>  {
> @@ -174,7 +198,8 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
>  		spin_unlock(&pinned_root_spinlock);
>  	}
>  
> -	if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
> +	if (IS_ERR_OR_NULL(pinned_root) ||
> +	    ((load_root != 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;
>  }
>  
> @@ -248,6 +274,162 @@ DEFINE_LSM(loadpin) = {
>  	.init = loadpin_init,
>  };
>  
> +#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 = fdget(fd);
> +	if (!f.file)
> +		return -EINVAL;
> +
> +	data = kzalloc(SZ_4K, GFP_KERNEL);
> +	if (!data) {
> +		rc = -ENOMEM;
> +		goto err;
> +	}
> +
> +	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
> +	if (rc < 0)
> +		goto err;
> +
> +	((char *)data)[rc] = '\0';
> +
> +	p = strim(data);
> +	while ((d = strsep(&p, ",")) != NULL) {
> +		int len = strlen(d);
> +		struct trusted_root_digest *trd;
> +
> +		if (len % 2) {
> +			rc = -EPROTO;
> +			goto err;
> +		}
> +
> +		len /= 2;
> +
> +		trd = kzalloc(sizeof(*trd), GFP_KERNEL);
> +		if (!trd) {
> +			rc = -ENOMEM;
> +			goto err;
> +		}
> +
> +		trd->data = kzalloc(len, GFP_KERNEL);
> +		if (!trd->data) {
> +			kfree(trd);
> +			rc = -ENOMEM;
> +			goto err;
> +		}
> +
> +		if (hex2bin(trd->data, d, len)) {
> +			kfree(trd);
> +			kfree(trd->data);
> +			rc = -EPROTO;
> +			goto err;
> +		}
> +
> +		trd->len = 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 = (void __user *)arg;
> +	unsigned int fd;
> +	int rc;
> +
> +	switch (cmd) {
> +	case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
> +		rc = 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 = {
> +	.unlocked_ioctl = dm_verity_ioctl,
> +	.compat_ioctl = 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 = 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 = 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 == 0). */
>  module_param(enforce, int, 0);
>  MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
> -- 
> 2.36.0.464.gb9c8b46e94-goog
> 


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
@ 2022-05-13 16:32     ` Mike Snitzer
  0 siblings, 0 replies; 42+ messages in thread
From: Mike Snitzer @ 2022-05-13 16:32 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: linux-security-module, Douglas Anderson, Kees Cook, Mike Snitzer,
	James Morris, linux-kernel, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn

On Wed, May 04 2022 at  3:54P -0400,
Matthias Kaehlcke <mka@chromium.org> wrote:

> 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/dlcservice/docs/developer.md
> 
> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> ---
> 
> Changes in v3:
> - added securityfs for LoadPin (currently only populated when
>   CONFIG_SECURITY_LOADPIN_VERITY=y)
> - 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

I would certainly need some Reviewed-by:s from security and/or loadpin
experts if I were to pick this patch up.

Did you see the issues the kernel test robot emailed about?

You'd do well to fix those issues up when submitting another revision
of this patchset.

Mike


> 
> 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 verity 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, 0x00, 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=1".
> +
> +config SECURITY_LOADPIN_VERITY
> +	bool "Allow reading files from certain other filesystems that use dm-verity"
> +	depends on DM_VERITY=y && 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 <linux/path.h>
>  #include <linux/sched.h>	/* current */
>  #include <linux/string_helpers.h>
> +#include <linux/device-mapper.h>
> +#include <linux/dm-verity-loadpin.h>
> +#include <uapi/linux/loadpin.h>
>  
>  static void report_load(const char *origin, struct file *file, char *operation)
>  {
> @@ -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
>  
>  #ifdef CONFIG_SYSCTL
>  
> @@ -118,6 +124,24 @@ static void loadpin_sb_free_security(struct super_block *mnt_sb)
>  	}
>  }
>  
> +#ifdef CONFIG_SECURITY_LOADPIN_VERITY
> +static bool loadpin_is_fs_trusted(struct super_block *sb)
> +{
> +	struct mapped_device *md = dm_get_md(sb->s_bdev->bd_dev);
> +	bool trusted;
> +
> +	if (!md)
> +		return false;
> +
> +	trusted = 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 id,
>  			     bool contents)
>  {
> @@ -174,7 +198,8 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
>  		spin_unlock(&pinned_root_spinlock);
>  	}
>  
> -	if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
> +	if (IS_ERR_OR_NULL(pinned_root) ||
> +	    ((load_root != 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;
>  }
>  
> @@ -248,6 +274,162 @@ DEFINE_LSM(loadpin) = {
>  	.init = loadpin_init,
>  };
>  
> +#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 = fdget(fd);
> +	if (!f.file)
> +		return -EINVAL;
> +
> +	data = kzalloc(SZ_4K, GFP_KERNEL);
> +	if (!data) {
> +		rc = -ENOMEM;
> +		goto err;
> +	}
> +
> +	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
> +	if (rc < 0)
> +		goto err;
> +
> +	((char *)data)[rc] = '\0';
> +
> +	p = strim(data);
> +	while ((d = strsep(&p, ",")) != NULL) {
> +		int len = strlen(d);
> +		struct trusted_root_digest *trd;
> +
> +		if (len % 2) {
> +			rc = -EPROTO;
> +			goto err;
> +		}
> +
> +		len /= 2;
> +
> +		trd = kzalloc(sizeof(*trd), GFP_KERNEL);
> +		if (!trd) {
> +			rc = -ENOMEM;
> +			goto err;
> +		}
> +
> +		trd->data = kzalloc(len, GFP_KERNEL);
> +		if (!trd->data) {
> +			kfree(trd);
> +			rc = -ENOMEM;
> +			goto err;
> +		}
> +
> +		if (hex2bin(trd->data, d, len)) {
> +			kfree(trd);
> +			kfree(trd->data);
> +			rc = -EPROTO;
> +			goto err;
> +		}
> +
> +		trd->len = 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 = (void __user *)arg;
> +	unsigned int fd;
> +	int rc;
> +
> +	switch (cmd) {
> +	case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
> +		rc = 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 = {
> +	.unlocked_ioctl = dm_verity_ioctl,
> +	.compat_ioctl = 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 = 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 = 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 == 0). */
>  module_param(enforce, int, 0);
>  MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
> -- 
> 2.36.0.464.gb9c8b46e94-goog
> 

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 1/3] dm: Add verity helpers for LoadPin
  2022-05-13 16:29           ` [dm-devel] " Mike Snitzer
@ 2022-05-13 16:53             ` Matthias Kaehlcke
  -1 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-13 16:53 UTC (permalink / raw)
  To: Mike Snitzer
  Cc: linux-security-module, Douglas Anderson, Kees Cook, Mike Snitzer,
	James Morris, linux-kernel, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn

On Fri, May 13, 2022 at 12:29:29PM -0400, Mike Snitzer wrote:
> On Thu, May 12 2022 at  4:44P -0400,
> Matthias Kaehlcke <mka@chromium.org> wrote:
> 
> > On Thu, May 12, 2022 at 01:19:12PM -0400, Mike Snitzer wrote:
> > > On Wed, May 11 2022 at  4:54P -0400,
> > > Matthias Kaehlcke <mka@chromium.org> wrote:
> > > 
> > > > Alasdar/Mike, I'd be interested in your take on adding these functions
> > > > to verity/DM, to get an idea whether this series has a path forward to
> > > > landing upstream.
> > > 
> > > I'll be reviewing your patchset now. Comments inlined below.
> > > 
> > > > On Wed, May 04, 2022 at 12:54:17PM -0700, Matthias Kaehlcke wrote:
> > > > > 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 <mka@chromium.org>
> > > > > ---
> > > > > 
> > > > > 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			+= dm-ima.o
> > > > >  endif
> > > > >  
> > > > > +ifeq ($(CONFIG_DM_VERITY),y)
> > > > > +ifeq ($(CONFIG_SECURITY_LOADPIN),y)
> > > > > +dm-mod-objs			+= dm-verity-loadpin.o
> > > > > +endif
> > > > > +endif
> > > > > +
> > > 
> > > Why are you extending dm-mod-objs?  Why not dm-verity-objs?
> > 
> > Sorry, I missed to address this comment in my earlier reply.
> > 
> > I don't recall why I chose dm-mod-objs initially, agreed that
> > dm-verity-objs seems a better fit.
> 
> Yes, should be fixed even though the 3rd patch removes this change.

Sure

> BTW, looking at the 2nd patch's loadpin_is_fs_trusted().  Seems to me
> you'd do well to pass a 'struct block_device *' to a DM helper rather
> than force security/loadpin/loadpin.c to mess around with DM device
> refcounting, etc.

Sounds good to me. Thanks for the suggestion!

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 1/3] dm: Add verity helpers for LoadPin
@ 2022-05-13 16:53             ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-13 16:53 UTC (permalink / raw)
  To: Mike Snitzer
  Cc: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris,
	Serge E . Hallyn, dm-devel, linux-kernel, linux-raid, Song Liu,
	Douglas Anderson, linux-security-module

On Fri, May 13, 2022 at 12:29:29PM -0400, Mike Snitzer wrote:
> On Thu, May 12 2022 at  4:44P -0400,
> Matthias Kaehlcke <mka@chromium.org> wrote:
> 
> > On Thu, May 12, 2022 at 01:19:12PM -0400, Mike Snitzer wrote:
> > > On Wed, May 11 2022 at  4:54P -0400,
> > > Matthias Kaehlcke <mka@chromium.org> wrote:
> > > 
> > > > Alasdar/Mike, I'd be interested in your take on adding these functions
> > > > to verity/DM, to get an idea whether this series has a path forward to
> > > > landing upstream.
> > > 
> > > I'll be reviewing your patchset now. Comments inlined below.
> > > 
> > > > On Wed, May 04, 2022 at 12:54:17PM -0700, Matthias Kaehlcke wrote:
> > > > > 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 <mka@chromium.org>
> > > > > ---
> > > > > 
> > > > > 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			+= dm-ima.o
> > > > >  endif
> > > > >  
> > > > > +ifeq ($(CONFIG_DM_VERITY),y)
> > > > > +ifeq ($(CONFIG_SECURITY_LOADPIN),y)
> > > > > +dm-mod-objs			+= dm-verity-loadpin.o
> > > > > +endif
> > > > > +endif
> > > > > +
> > > 
> > > Why are you extending dm-mod-objs?  Why not dm-verity-objs?
> > 
> > Sorry, I missed to address this comment in my earlier reply.
> > 
> > I don't recall why I chose dm-mod-objs initially, agreed that
> > dm-verity-objs seems a better fit.
> 
> Yes, should be fixed even though the 3rd patch removes this change.

Sure

> BTW, looking at the 2nd patch's loadpin_is_fs_trusted().  Seems to me
> you'd do well to pass a 'struct block_device *' to a DM helper rather
> than force security/loadpin/loadpin.c to mess around with DM device
> refcounting, etc.

Sounds good to me. Thanks for the suggestion!

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
  2022-05-13 16:32     ` [dm-devel] " Mike Snitzer
@ 2022-05-13 17:01       ` Matthias Kaehlcke
  -1 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-13 17:01 UTC (permalink / raw)
  To: Mike Snitzer
  Cc: Alasdair Kergon, Mike Snitzer, Kees Cook, James Morris,
	Serge E . Hallyn, dm-devel, linux-kernel, linux-raid, Song Liu,
	Douglas Anderson, linux-security-module

On Fri, May 13, 2022 at 12:32:12PM -0400, Mike Snitzer wrote:
> On Wed, May 04 2022 at  3:54P -0400,
> Matthias Kaehlcke <mka@chromium.org> wrote:
> 
> > 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/dlcservice/docs/developer.md
> > 
> > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > ---
> > 
> > Changes in v3:
> > - added securityfs for LoadPin (currently only populated when
> >   CONFIG_SECURITY_LOADPIN_VERITY=y)
> > - 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
> 
> I would certainly need some Reviewed-by:s from security and/or loadpin
> experts if I were to pick this patch up.

Yes, I think Kees (LoadPin maintainer) is generally on board with the idea,
but a more in depth review is still pending.

I'll send out a new revision which addresses the current outstanding
comments soon.

> Did you see the issues the kernel test robot emailed about?
> 
> You'd do well to fix those issues up when submitting another revision
> of this patchset.

Yes, I plan to address those in the next revision. Thanks for the reminder!

> > 
> > 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 verity 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, 0x00, 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=1".
> > +
> > +config SECURITY_LOADPIN_VERITY
> > +	bool "Allow reading files from certain other filesystems that use dm-verity"
> > +	depends on DM_VERITY=y && 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 <linux/path.h>
> >  #include <linux/sched.h>	/* current */
> >  #include <linux/string_helpers.h>
> > +#include <linux/device-mapper.h>
> > +#include <linux/dm-verity-loadpin.h>
> > +#include <uapi/linux/loadpin.h>
> >  
> >  static void report_load(const char *origin, struct file *file, char *operation)
> >  {
> > @@ -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
> >  
> >  #ifdef CONFIG_SYSCTL
> >  
> > @@ -118,6 +124,24 @@ static void loadpin_sb_free_security(struct super_block *mnt_sb)
> >  	}
> >  }
> >  
> > +#ifdef CONFIG_SECURITY_LOADPIN_VERITY
> > +static bool loadpin_is_fs_trusted(struct super_block *sb)
> > +{
> > +	struct mapped_device *md = dm_get_md(sb->s_bdev->bd_dev);
> > +	bool trusted;
> > +
> > +	if (!md)
> > +		return false;
> > +
> > +	trusted = 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 id,
> >  			     bool contents)
> >  {
> > @@ -174,7 +198,8 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
> >  		spin_unlock(&pinned_root_spinlock);
> >  	}
> >  
> > -	if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
> > +	if (IS_ERR_OR_NULL(pinned_root) ||
> > +	    ((load_root != 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;
> >  }
> >  
> > @@ -248,6 +274,162 @@ DEFINE_LSM(loadpin) = {
> >  	.init = loadpin_init,
> >  };
> >  
> > +#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 = fdget(fd);
> > +	if (!f.file)
> > +		return -EINVAL;
> > +
> > +	data = kzalloc(SZ_4K, GFP_KERNEL);
> > +	if (!data) {
> > +		rc = -ENOMEM;
> > +		goto err;
> > +	}
> > +
> > +	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
> > +	if (rc < 0)
> > +		goto err;
> > +
> > +	((char *)data)[rc] = '\0';
> > +
> > +	p = strim(data);
> > +	while ((d = strsep(&p, ",")) != NULL) {
> > +		int len = strlen(d);
> > +		struct trusted_root_digest *trd;
> > +
> > +		if (len % 2) {
> > +			rc = -EPROTO;
> > +			goto err;
> > +		}
> > +
> > +		len /= 2;
> > +
> > +		trd = kzalloc(sizeof(*trd), GFP_KERNEL);
> > +		if (!trd) {
> > +			rc = -ENOMEM;
> > +			goto err;
> > +		}
> > +
> > +		trd->data = kzalloc(len, GFP_KERNEL);
> > +		if (!trd->data) {
> > +			kfree(trd);
> > +			rc = -ENOMEM;
> > +			goto err;
> > +		}
> > +
> > +		if (hex2bin(trd->data, d, len)) {
> > +			kfree(trd);
> > +			kfree(trd->data);
> > +			rc = -EPROTO;
> > +			goto err;
> > +		}
> > +
> > +		trd->len = 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 = (void __user *)arg;
> > +	unsigned int fd;
> > +	int rc;
> > +
> > +	switch (cmd) {
> > +	case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
> > +		rc = 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 = {
> > +	.unlocked_ioctl = dm_verity_ioctl,
> > +	.compat_ioctl = 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 = 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 = 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 == 0). */
> >  module_param(enforce, int, 0);
> >  MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
> > -- 
> > 2.36.0.464.gb9c8b46e94-goog
> > 
> 

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
@ 2022-05-13 17:01       ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-13 17:01 UTC (permalink / raw)
  To: Mike Snitzer
  Cc: linux-security-module, Douglas Anderson, Kees Cook, Mike Snitzer,
	James Morris, linux-kernel, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn

On Fri, May 13, 2022 at 12:32:12PM -0400, Mike Snitzer wrote:
> On Wed, May 04 2022 at  3:54P -0400,
> Matthias Kaehlcke <mka@chromium.org> wrote:
> 
> > 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/dlcservice/docs/developer.md
> > 
> > Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> > ---
> > 
> > Changes in v3:
> > - added securityfs for LoadPin (currently only populated when
> >   CONFIG_SECURITY_LOADPIN_VERITY=y)
> > - 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
> 
> I would certainly need some Reviewed-by:s from security and/or loadpin
> experts if I were to pick this patch up.

Yes, I think Kees (LoadPin maintainer) is generally on board with the idea,
but a more in depth review is still pending.

I'll send out a new revision which addresses the current outstanding
comments soon.

> Did you see the issues the kernel test robot emailed about?
> 
> You'd do well to fix those issues up when submitting another revision
> of this patchset.

Yes, I plan to address those in the next revision. Thanks for the reminder!

> > 
> > 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 verity 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, 0x00, 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=1".
> > +
> > +config SECURITY_LOADPIN_VERITY
> > +	bool "Allow reading files from certain other filesystems that use dm-verity"
> > +	depends on DM_VERITY=y && 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 <linux/path.h>
> >  #include <linux/sched.h>	/* current */
> >  #include <linux/string_helpers.h>
> > +#include <linux/device-mapper.h>
> > +#include <linux/dm-verity-loadpin.h>
> > +#include <uapi/linux/loadpin.h>
> >  
> >  static void report_load(const char *origin, struct file *file, char *operation)
> >  {
> > @@ -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
> >  
> >  #ifdef CONFIG_SYSCTL
> >  
> > @@ -118,6 +124,24 @@ static void loadpin_sb_free_security(struct super_block *mnt_sb)
> >  	}
> >  }
> >  
> > +#ifdef CONFIG_SECURITY_LOADPIN_VERITY
> > +static bool loadpin_is_fs_trusted(struct super_block *sb)
> > +{
> > +	struct mapped_device *md = dm_get_md(sb->s_bdev->bd_dev);
> > +	bool trusted;
> > +
> > +	if (!md)
> > +		return false;
> > +
> > +	trusted = 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 id,
> >  			     bool contents)
> >  {
> > @@ -174,7 +198,8 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
> >  		spin_unlock(&pinned_root_spinlock);
> >  	}
> >  
> > -	if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
> > +	if (IS_ERR_OR_NULL(pinned_root) ||
> > +	    ((load_root != 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;
> >  }
> >  
> > @@ -248,6 +274,162 @@ DEFINE_LSM(loadpin) = {
> >  	.init = loadpin_init,
> >  };
> >  
> > +#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 = fdget(fd);
> > +	if (!f.file)
> > +		return -EINVAL;
> > +
> > +	data = kzalloc(SZ_4K, GFP_KERNEL);
> > +	if (!data) {
> > +		rc = -ENOMEM;
> > +		goto err;
> > +	}
> > +
> > +	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
> > +	if (rc < 0)
> > +		goto err;
> > +
> > +	((char *)data)[rc] = '\0';
> > +
> > +	p = strim(data);
> > +	while ((d = strsep(&p, ",")) != NULL) {
> > +		int len = strlen(d);
> > +		struct trusted_root_digest *trd;
> > +
> > +		if (len % 2) {
> > +			rc = -EPROTO;
> > +			goto err;
> > +		}
> > +
> > +		len /= 2;
> > +
> > +		trd = kzalloc(sizeof(*trd), GFP_KERNEL);
> > +		if (!trd) {
> > +			rc = -ENOMEM;
> > +			goto err;
> > +		}
> > +
> > +		trd->data = kzalloc(len, GFP_KERNEL);
> > +		if (!trd->data) {
> > +			kfree(trd);
> > +			rc = -ENOMEM;
> > +			goto err;
> > +		}
> > +
> > +		if (hex2bin(trd->data, d, len)) {
> > +			kfree(trd);
> > +			kfree(trd->data);
> > +			rc = -EPROTO;
> > +			goto err;
> > +		}
> > +
> > +		trd->len = 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 = (void __user *)arg;
> > +	unsigned int fd;
> > +	int rc;
> > +
> > +	switch (cmd) {
> > +	case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
> > +		rc = 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 = {
> > +	.unlocked_ioctl = dm_verity_ioctl,
> > +	.compat_ioctl = 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 = 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 = 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 == 0). */
> >  module_param(enforce, int, 0);
> >  MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");
> > -- 
> > 2.36.0.464.gb9c8b46e94-goog
> > 
> 

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
  2022-05-13 16:32     ` [dm-devel] " Mike Snitzer
@ 2022-05-13 18:26       ` Kees Cook
  -1 siblings, 0 replies; 42+ messages in thread
From: Kees Cook @ 2022-05-13 18:26 UTC (permalink / raw)
  To: Mike Snitzer, Matthias Kaehlcke
  Cc: Alasdair Kergon, Mike Snitzer, James Morris, Serge E . Hallyn,
	dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module



On May 13, 2022 9:32:12 AM PDT, Mike Snitzer <snitzer@redhat.com> wrote:
>On Wed, May 04 2022 at  3:54P -0400,
>Matthias Kaehlcke <mka@chromium.org> wrote:
>
>> 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/dlcservice/docs/developer.md
>> 
>> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
>> ---
>> 
>> Changes in v3:
>> - added securityfs for LoadPin (currently only populated when
>>   CONFIG_SECURITY_LOADPIN_VERITY=y)
>> - 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
>
>I would certainly need some Reviewed-by:s from security and/or loadpin
>experts if I were to pick this patch up.

Alternatively, since it's mostly touching loadpin, I can carry it in my tree, as long as you've Acked the dm bits. :)

>Did you see the issues the kernel test robot emailed about?
>
>You'd do well to fix those issues up when submitting another revision
>of this patchset.

Agreed.


-- 
Kees Cook

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
@ 2022-05-13 18:26       ` Kees Cook
  0 siblings, 0 replies; 42+ messages in thread
From: Kees Cook @ 2022-05-13 18:26 UTC (permalink / raw)
  To: Mike Snitzer, Matthias Kaehlcke
  Cc: linux-security-module, Douglas Anderson, Mike Snitzer,
	linux-kernel, James Morris, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn



On May 13, 2022 9:32:12 AM PDT, Mike Snitzer <snitzer@redhat.com> wrote:
>On Wed, May 04 2022 at  3:54P -0400,
>Matthias Kaehlcke <mka@chromium.org> wrote:
>
>> 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/dlcservice/docs/developer.md
>> 
>> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
>> ---
>> 
>> Changes in v3:
>> - added securityfs for LoadPin (currently only populated when
>>   CONFIG_SECURITY_LOADPIN_VERITY=y)
>> - 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
>
>I would certainly need some Reviewed-by:s from security and/or loadpin
>experts if I were to pick this patch up.

Alternatively, since it's mostly touching loadpin, I can carry it in my tree, as long as you've Acked the dm bits. :)

>Did you see the issues the kernel test robot emailed about?
>
>You'd do well to fix those issues up when submitting another revision
>of this patchset.

Agreed.


-- 
Kees Cook

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 1/3] dm: Add verity helpers for LoadPin
  2022-05-04 19:54   ` [dm-devel] " Matthias Kaehlcke
@ 2022-05-13 22:15     ` Kees Cook
  -1 siblings, 0 replies; 42+ messages in thread
From: Kees Cook @ 2022-05-13 22:15 UTC (permalink / raw)
  To: Matthias Kaehlcke, Alasdair Kergon, Mike Snitzer, James Morris,
	Serge E . Hallyn
  Cc: dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module



On May 4, 2022 12:54:17 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
>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 <mka@chromium.org>
> [...]
>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 <linux/list.h>
>+#include <linux/kernel.h>
>+#include <linux/dm-verity-loadpin.h>
>+
>+#include "dm.h"
>+#include "dm-verity.h"
>+
>+static struct list_head *trusted_root_digests;

Does this need to exist in two places? (i.e. why can't dm and loadpin share this instead of needing dm_verity_loadpin_set_trusted_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;
>+		}
>+	}

Pardon my lack of dm vocabulary, but what is "target" vs "table" here? I was only thinking of "whole device", so I must not understand what this is examining.

> [...]
>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 <linux/list.h>
>+
>+struct mapped_device;
>+
>+struct trusted_root_digest {
>+	u8 *data;
>+	unsigned int len;
>+	struct list_head node;
>+};

To avoid the double-alloc in patch 2 (and save 1 pointer size of memory), this could just be:

struct trusted_root_digest {
	struct list_head node;
	unsigned int len;
	u8 data[];
};

Otherwise, looks good to me!

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 1/3] dm: Add verity helpers for LoadPin
@ 2022-05-13 22:15     ` Kees Cook
  0 siblings, 0 replies; 42+ messages in thread
From: Kees Cook @ 2022-05-13 22:15 UTC (permalink / raw)
  To: Matthias Kaehlcke, Alasdair Kergon, Mike Snitzer, James Morris,
	Serge E . Hallyn
  Cc: Douglas Anderson, linux-security-module, linux-kernel,
	linux-raid, Song Liu, dm-devel



On May 4, 2022 12:54:17 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
>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 <mka@chromium.org>
> [...]
>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 <linux/list.h>
>+#include <linux/kernel.h>
>+#include <linux/dm-verity-loadpin.h>
>+
>+#include "dm.h"
>+#include "dm-verity.h"
>+
>+static struct list_head *trusted_root_digests;

Does this need to exist in two places? (i.e. why can't dm and loadpin share this instead of needing dm_verity_loadpin_set_trusted_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;
>+		}
>+	}

Pardon my lack of dm vocabulary, but what is "target" vs "table" here? I was only thinking of "whole device", so I must not understand what this is examining.

> [...]
>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 <linux/list.h>
>+
>+struct mapped_device;
>+
>+struct trusted_root_digest {
>+	u8 *data;
>+	unsigned int len;
>+	struct list_head node;
>+};

To avoid the double-alloc in patch 2 (and save 1 pointer size of memory), this could just be:

struct trusted_root_digest {
	struct list_head node;
	unsigned int len;
	u8 data[];
};

Otherwise, looks good to me!

-- 
Kees Cook

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
  2022-05-04 19:54   ` [dm-devel] " Matthias Kaehlcke
@ 2022-05-13 22:36     ` Kees Cook
  -1 siblings, 0 replies; 42+ messages in thread
From: Kees Cook @ 2022-05-13 22:36 UTC (permalink / raw)
  To: Matthias Kaehlcke, Alasdair Kergon, Mike Snitzer, James Morris,
	Serge E . Hallyn
  Cc: dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module



On May 4, 2022 12:54:18 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
>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

I think this should be "... on an already trusted device ..."

>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/dlcservice/docs/developer.md
>
>Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
>---
>
>Changes in v3:
>- added securityfs for LoadPin (currently only populated when
>  CONFIG_SECURITY_LOADPIN_VERITY=y)
>- 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 verity devices
>+ *                                          that loadpin should trust.
>+ *
>+ * Takes a file descriptor from which to read the root digests of trusted verity devices.

Maybe add to the comment the securityfs node path here as a helpful hint to the reader, and mention the format (comma separated?)

>+ */
>+#define LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS _IOW(LOADPIN_IOC_MAGIC, 0x00, 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=1".
>+
>+config SECURITY_LOADPIN_VERITY
>+	bool "Allow reading files from certain other filesystems that use dm-verity"
>+	depends on DM_VERITY=y && 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 <linux/path.h>
> #include <linux/sched.h>	/* current */
> #include <linux/string_helpers.h>
>+#include <linux/device-mapper.h>
>+#include <linux/dm-verity-loadpin.h>
>+#include <uapi/linux/loadpin.h>
> 
> static void report_load(const char *origin, struct file *file, char *operation)
> {
>@@ -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
> 
> #ifdef CONFIG_SYSCTL
> 
>@@ -118,6 +124,24 @@ static void loadpin_sb_free_security(struct super_block *mnt_sb)
> 	}
> }
> 
>+#ifdef CONFIG_SECURITY_LOADPIN_VERITY
>+static bool loadpin_is_fs_trusted(struct super_block *sb)
>+{
>+	struct mapped_device *md = dm_get_md(sb->s_bdev->bd_dev);
>+	bool trusted;
>+
>+	if (!md)
>+		return false;
>+
>+	trusted = 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 id,
> 			     bool contents)
> {
>@@ -174,7 +198,8 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
> 		spin_unlock(&pinned_root_spinlock);
> 	}
> 
>-	if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
>+	if (IS_ERR_OR_NULL(pinned_root) ||
>+	    ((load_root != 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;
> }
> 
>@@ -248,6 +274,162 @@ DEFINE_LSM(loadpin) = {
> 	.init = loadpin_init,
> };
> 
>+#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;

Probably easier if this is u8 *?

>+	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 = fdget(fd);
>+	if (!f.file)
>+		return -EINVAL;
>+
>+	data = kzalloc(SZ_4K, GFP_KERNEL);
>+	if (!data) {
>+		rc = -ENOMEM;
>+		goto err;
>+	}
>+
>+	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
>+	if (rc < 0)
>+		goto err;
>+
>+	((char *)data)[rc] = '\0';
>+
>+	p = strim(data);
>+	while ((d = strsep(&p, ",")) != NULL) {

Maybe be flexible and add newline as a separator too?

>+		int len = strlen(d);
>+		struct trusted_root_digest *trd;
>+
>+		if (len % 2) {
>+			rc = -EPROTO;
>+			goto err;
>+		}
>+
>+		len /= 2;
>+
>+		trd = kzalloc(sizeof(*trd), GFP_KERNEL);

With the struct change, this could be:

kzalloc(struct_size(trd, data, len), ...)

>+		if (!trd) {
>+			rc = -ENOMEM;
>+			goto err;
>+		}
>+
>+		trd->data = kzalloc(len, GFP_KERNEL);
>+		if (!trd->data) {
>+			kfree(trd);
>+			rc = -ENOMEM;
>+			goto err;
>+		}
>+
>+		if (hex2bin(trd->data, d, len)) {
>+			kfree(trd);
>+			kfree(trd->data);
>+			rc = -EPROTO;
>+			goto err;
>+		}
>+
>+		trd->len = 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);
>+

Maybe add a comment that any load failure will invalidate the entire list?

>+	{
>+		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 = (void __user *)arg;
>+	unsigned int fd;
>+	int rc;
>+
>+	switch (cmd) {
>+	case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
>+		rc = 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 = {
>+	.unlocked_ioctl = dm_verity_ioctl,
>+	.compat_ioctl = 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 = 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 = 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 == 0). */
> module_param(enforce, int, 0);
> MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");

Otherwise looks good! The only other thing I can think of is pondering more about more carefully failing closed. E.g. instead of just throwing away all the other hashes on a file load failure, maybe lock out future attempts to set it too? I'm not sure this is actually useful, though. :P it shouldn't be possible to corrupt the file, etc. But in the universe where things like DirtyCOW happens, I've gotten even more paranoid. ;)

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
@ 2022-05-13 22:36     ` Kees Cook
  0 siblings, 0 replies; 42+ messages in thread
From: Kees Cook @ 2022-05-13 22:36 UTC (permalink / raw)
  To: Matthias Kaehlcke, Alasdair Kergon, Mike Snitzer, James Morris,
	Serge E . Hallyn
  Cc: Douglas Anderson, linux-security-module, linux-kernel,
	linux-raid, Song Liu, dm-devel



On May 4, 2022 12:54:18 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
>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

I think this should be "... on an already trusted device ..."

>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/dlcservice/docs/developer.md
>
>Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
>---
>
>Changes in v3:
>- added securityfs for LoadPin (currently only populated when
>  CONFIG_SECURITY_LOADPIN_VERITY=y)
>- 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 verity devices
>+ *                                          that loadpin should trust.
>+ *
>+ * Takes a file descriptor from which to read the root digests of trusted verity devices.

Maybe add to the comment the securityfs node path here as a helpful hint to the reader, and mention the format (comma separated?)

>+ */
>+#define LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS _IOW(LOADPIN_IOC_MAGIC, 0x00, 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=1".
>+
>+config SECURITY_LOADPIN_VERITY
>+	bool "Allow reading files from certain other filesystems that use dm-verity"
>+	depends on DM_VERITY=y && 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 <linux/path.h>
> #include <linux/sched.h>	/* current */
> #include <linux/string_helpers.h>
>+#include <linux/device-mapper.h>
>+#include <linux/dm-verity-loadpin.h>
>+#include <uapi/linux/loadpin.h>
> 
> static void report_load(const char *origin, struct file *file, char *operation)
> {
>@@ -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
> 
> #ifdef CONFIG_SYSCTL
> 
>@@ -118,6 +124,24 @@ static void loadpin_sb_free_security(struct super_block *mnt_sb)
> 	}
> }
> 
>+#ifdef CONFIG_SECURITY_LOADPIN_VERITY
>+static bool loadpin_is_fs_trusted(struct super_block *sb)
>+{
>+	struct mapped_device *md = dm_get_md(sb->s_bdev->bd_dev);
>+	bool trusted;
>+
>+	if (!md)
>+		return false;
>+
>+	trusted = 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 id,
> 			     bool contents)
> {
>@@ -174,7 +198,8 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
> 		spin_unlock(&pinned_root_spinlock);
> 	}
> 
>-	if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
>+	if (IS_ERR_OR_NULL(pinned_root) ||
>+	    ((load_root != 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;
> }
> 
>@@ -248,6 +274,162 @@ DEFINE_LSM(loadpin) = {
> 	.init = loadpin_init,
> };
> 
>+#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;

Probably easier if this is u8 *?

>+	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 = fdget(fd);
>+	if (!f.file)
>+		return -EINVAL;
>+
>+	data = kzalloc(SZ_4K, GFP_KERNEL);
>+	if (!data) {
>+		rc = -ENOMEM;
>+		goto err;
>+	}
>+
>+	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
>+	if (rc < 0)
>+		goto err;
>+
>+	((char *)data)[rc] = '\0';
>+
>+	p = strim(data);
>+	while ((d = strsep(&p, ",")) != NULL) {

Maybe be flexible and add newline as a separator too?

>+		int len = strlen(d);
>+		struct trusted_root_digest *trd;
>+
>+		if (len % 2) {
>+			rc = -EPROTO;
>+			goto err;
>+		}
>+
>+		len /= 2;
>+
>+		trd = kzalloc(sizeof(*trd), GFP_KERNEL);

With the struct change, this could be:

kzalloc(struct_size(trd, data, len), ...)

>+		if (!trd) {
>+			rc = -ENOMEM;
>+			goto err;
>+		}
>+
>+		trd->data = kzalloc(len, GFP_KERNEL);
>+		if (!trd->data) {
>+			kfree(trd);
>+			rc = -ENOMEM;
>+			goto err;
>+		}
>+
>+		if (hex2bin(trd->data, d, len)) {
>+			kfree(trd);
>+			kfree(trd->data);
>+			rc = -EPROTO;
>+			goto err;
>+		}
>+
>+		trd->len = 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);
>+

Maybe add a comment that any load failure will invalidate the entire list?

>+	{
>+		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 = (void __user *)arg;
>+	unsigned int fd;
>+	int rc;
>+
>+	switch (cmd) {
>+	case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
>+		rc = 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 = {
>+	.unlocked_ioctl = dm_verity_ioctl,
>+	.compat_ioctl = 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 = 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 = 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 == 0). */
> module_param(enforce, int, 0);
> MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");

Otherwise looks good! The only other thing I can think of is pondering more about more carefully failing closed. E.g. instead of just throwing away all the other hashes on a file load failure, maybe lock out future attempts to set it too? I'm not sure this is actually useful, though. :P it shouldn't be possible to corrupt the file, etc. But in the universe where things like DirtyCOW happens, I've gotten even more paranoid. ;)

-- 
Kees Cook

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
  2022-05-13 22:36     ` [dm-devel] " Kees Cook
@ 2022-05-16 18:17       ` Matthias Kaehlcke
  -1 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-16 18:17 UTC (permalink / raw)
  To: Kees Cook
  Cc: Alasdair Kergon, Mike Snitzer, James Morris, Serge E . Hallyn,
	dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module

Hi Kees,

thanks for the review!

On Fri, May 13, 2022 at 03:36:26PM -0700, Kees Cook wrote:
> 
> 
> On May 4, 2022 12:54:18 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
> >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
> 
> I think this should be "... on an already trusted device ..."

It's not entirely clear which part you want me to substitute. 'an already
trusted device' makes me wonder whether you are thinking about reading the
list of digests, and not the general case of reading a kernel file, which
this paragraph intends to describe.

> >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/dlcservice/docs/developer.md
> >
> >Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> >---
> >
> >Changes in v3:
> >- added securityfs for LoadPin (currently only populated when
> >  CONFIG_SECURITY_LOADPIN_VERITY=y)
> >- 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/security/loadpin/loadpin.c b/security/loadpin/loadpin.c
> >index b12f7d986b1e..c29ce562a366 100644
> >--- a/security/loadpin/loadpin.c
> >+++ b/security/loadpin/loadpin.c
>
> ...
>
> >+static int read_trusted_verity_root_digests(unsigned int fd)
> >+{
> >+	struct fd f;
> >+	void *data;
> 
> Probably easier if this is u8 *?

Maybe slightly, it would then require a cast when passing it to
kernel_read_file()

> >+	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 = fdget(fd);
> >+	if (!f.file)
> >+		return -EINVAL;
> >+
> >+	data = kzalloc(SZ_4K, GFP_KERNEL);
> >+	if (!data) {
> >+		rc = -ENOMEM;
> >+		goto err;
> >+	}
> >+
> >+	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
> >+	if (rc < 0)
> >+		goto err;
> >+
> >+	((char *)data)[rc] = '\0';
> >+
> >+	p = strim(data);
> >+	while ((d = strsep(&p, ",")) != NULL) {
> 
> Maybe be flexible and add newline as a separator too?

Sure, I can add that. I'd also be fine with just allowing a newline as
separator, which seems a reasonable format for a sysfs file.

> >+		int len = strlen(d);
> >+		struct trusted_root_digest *trd;
> >+
> >+		if (len % 2) {
> >+			rc = -EPROTO;
> >+			goto err;
> >+		}
> >+
> >+		len /= 2;
> >+
> >+		trd = kzalloc(sizeof(*trd), GFP_KERNEL);
> 
> With the struct change, this could be:
> 
> kzalloc(struct_size(trd, data, len), ...)

Will change

> >+		if (!trd) {
> >+			rc = -ENOMEM;
> >+			goto err;
> >+		}
> >+
> >+		trd->data = kzalloc(len, GFP_KERNEL);
> >+		if (!trd->data) {
> >+			kfree(trd);
> >+			rc = -ENOMEM;
> >+			goto err;
> >+		}
> >+
> >+		if (hex2bin(trd->data, d, len)) {
> >+			kfree(trd);
> >+			kfree(trd->data);
> >+			rc = -EPROTO;
> >+			goto err;
> >+		}
> >+
> >+		trd->len = 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);
> >+
> 
> Maybe add a comment that any load failure will invalidate the entire list?

ok

> Otherwise looks good! The only other thing I can think of is pondering more
> about more carefully failing closed. E.g. instead of just throwing away all
> the other hashes on a file load failure, maybe lock out future attempts to
> set it too? I'm not sure this is actually useful, though. :P it shouldn't be
> possible to corrupt the file, etc. But in the universe where things like
> DirtyCOW happens, I've gotten even more paranoid. ;)

Sure, we can do that

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
@ 2022-05-16 18:17       ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-16 18:17 UTC (permalink / raw)
  To: Kees Cook
  Cc: linux-security-module, Douglas Anderson, Mike Snitzer,
	linux-kernel, James Morris, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn

Hi Kees,

thanks for the review!

On Fri, May 13, 2022 at 03:36:26PM -0700, Kees Cook wrote:
> 
> 
> On May 4, 2022 12:54:18 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
> >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
> 
> I think this should be "... on an already trusted device ..."

It's not entirely clear which part you want me to substitute. 'an already
trusted device' makes me wonder whether you are thinking about reading the
list of digests, and not the general case of reading a kernel file, which
this paragraph intends to describe.

> >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/dlcservice/docs/developer.md
> >
> >Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> >---
> >
> >Changes in v3:
> >- added securityfs for LoadPin (currently only populated when
> >  CONFIG_SECURITY_LOADPIN_VERITY=y)
> >- 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/security/loadpin/loadpin.c b/security/loadpin/loadpin.c
> >index b12f7d986b1e..c29ce562a366 100644
> >--- a/security/loadpin/loadpin.c
> >+++ b/security/loadpin/loadpin.c
>
> ...
>
> >+static int read_trusted_verity_root_digests(unsigned int fd)
> >+{
> >+	struct fd f;
> >+	void *data;
> 
> Probably easier if this is u8 *?

Maybe slightly, it would then require a cast when passing it to
kernel_read_file()

> >+	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 = fdget(fd);
> >+	if (!f.file)
> >+		return -EINVAL;
> >+
> >+	data = kzalloc(SZ_4K, GFP_KERNEL);
> >+	if (!data) {
> >+		rc = -ENOMEM;
> >+		goto err;
> >+	}
> >+
> >+	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
> >+	if (rc < 0)
> >+		goto err;
> >+
> >+	((char *)data)[rc] = '\0';
> >+
> >+	p = strim(data);
> >+	while ((d = strsep(&p, ",")) != NULL) {
> 
> Maybe be flexible and add newline as a separator too?

Sure, I can add that. I'd also be fine with just allowing a newline as
separator, which seems a reasonable format for a sysfs file.

> >+		int len = strlen(d);
> >+		struct trusted_root_digest *trd;
> >+
> >+		if (len % 2) {
> >+			rc = -EPROTO;
> >+			goto err;
> >+		}
> >+
> >+		len /= 2;
> >+
> >+		trd = kzalloc(sizeof(*trd), GFP_KERNEL);
> 
> With the struct change, this could be:
> 
> kzalloc(struct_size(trd, data, len), ...)

Will change

> >+		if (!trd) {
> >+			rc = -ENOMEM;
> >+			goto err;
> >+		}
> >+
> >+		trd->data = kzalloc(len, GFP_KERNEL);
> >+		if (!trd->data) {
> >+			kfree(trd);
> >+			rc = -ENOMEM;
> >+			goto err;
> >+		}
> >+
> >+		if (hex2bin(trd->data, d, len)) {
> >+			kfree(trd);
> >+			kfree(trd->data);
> >+			rc = -EPROTO;
> >+			goto err;
> >+		}
> >+
> >+		trd->len = 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);
> >+
> 
> Maybe add a comment that any load failure will invalidate the entire list?

ok

> Otherwise looks good! The only other thing I can think of is pondering more
> about more carefully failing closed. E.g. instead of just throwing away all
> the other hashes on a file load failure, maybe lock out future attempts to
> set it too? I'm not sure this is actually useful, though. :P it shouldn't be
> possible to corrupt the file, etc. But in the universe where things like
> DirtyCOW happens, I've gotten even more paranoid. ;)

Sure, we can do that

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 1/3] dm: Add verity helpers for LoadPin
  2022-05-13 22:15     ` [dm-devel] " Kees Cook
@ 2022-05-16 18:51       ` Matthias Kaehlcke
  -1 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-16 18:51 UTC (permalink / raw)
  To: Kees Cook
  Cc: Alasdair Kergon, Mike Snitzer, James Morris, Serge E . Hallyn,
	dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module

On Fri, May 13, 2022 at 03:15:53PM -0700, Kees Cook wrote:
> 
> 
> On May 4, 2022 12:54:17 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
> >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 <mka@chromium.org>
> > [...]
> >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 <linux/list.h>
> >+#include <linux/kernel.h>
> >+#include <linux/dm-verity-loadpin.h>
> >+
> >+#include "dm.h"
> >+#include "dm-verity.h"
> >+
> >+static struct list_head *trusted_root_digests;
> 
> Does this need to exist in two places? (i.e. why can't dm and loadpin share
> this instead of needing dm_verity_loadpin_set_trusted_digests()?)

We could share it. Probably it should then be defined here, since this is
the first patch of the series, we could add an extern declaration to
dm-verity-loadpin.h.

> >+
> >+/*
> >+ * 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;
> >+		}
> >+	}
> 
> Pardon my lack of dm vocabulary, but what is "target" vs "table" here?
> I was only thinking of "whole device", so I must not understand what this is
> examining.

'targets' are different types of DM mappings like 'linear' or 'verity'. A
device mapper table contains has one or more targets that define the mapping
of the blocks of the mapped device.

Having spelled that out I realize that the above check is wrong. It would
consider a device like this trusted:

0 10000000 linear 8:1
10000000 10001000 verity <params>

In the above case only a small part of the DM device would be backed by verity.

I think we want a table with a single entry that is a verity target.

> > [...]
> >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 <linux/list.h>
> >+
> >+struct mapped_device;
> >+
> >+struct trusted_root_digest {
> >+	u8 *data;
> >+	unsigned int len;
> >+	struct list_head node;
> >+};
> 
> To avoid the double-alloc in patch 2 (and save 1 pointer size of memory), this could just be:
> 
> struct trusted_root_digest {
> 	struct list_head node;
> 	unsigned int len;
> 	u8 data[];
> };

Looks good to me, will change

> Otherwise, looks good to me!

Excellent, thanks for the review!

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 1/3] dm: Add verity helpers for LoadPin
@ 2022-05-16 18:51       ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-16 18:51 UTC (permalink / raw)
  To: Kees Cook
  Cc: linux-security-module, Douglas Anderson, Mike Snitzer,
	linux-kernel, James Morris, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn

On Fri, May 13, 2022 at 03:15:53PM -0700, Kees Cook wrote:
> 
> 
> On May 4, 2022 12:54:17 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
> >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 <mka@chromium.org>
> > [...]
> >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 <linux/list.h>
> >+#include <linux/kernel.h>
> >+#include <linux/dm-verity-loadpin.h>
> >+
> >+#include "dm.h"
> >+#include "dm-verity.h"
> >+
> >+static struct list_head *trusted_root_digests;
> 
> Does this need to exist in two places? (i.e. why can't dm and loadpin share
> this instead of needing dm_verity_loadpin_set_trusted_digests()?)

We could share it. Probably it should then be defined here, since this is
the first patch of the series, we could add an extern declaration to
dm-verity-loadpin.h.

> >+
> >+/*
> >+ * 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;
> >+		}
> >+	}
> 
> Pardon my lack of dm vocabulary, but what is "target" vs "table" here?
> I was only thinking of "whole device", so I must not understand what this is
> examining.

'targets' are different types of DM mappings like 'linear' or 'verity'. A
device mapper table contains has one or more targets that define the mapping
of the blocks of the mapped device.

Having spelled that out I realize that the above check is wrong. It would
consider a device like this trusted:

0 10000000 linear 8:1
10000000 10001000 verity <params>

In the above case only a small part of the DM device would be backed by verity.

I think we want a table with a single entry that is a verity target.

> > [...]
> >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 <linux/list.h>
> >+
> >+struct mapped_device;
> >+
> >+struct trusted_root_digest {
> >+	u8 *data;
> >+	unsigned int len;
> >+	struct list_head node;
> >+};
> 
> To avoid the double-alloc in patch 2 (and save 1 pointer size of memory), this could just be:
> 
> struct trusted_root_digest {
> 	struct list_head node;
> 	unsigned int len;
> 	u8 data[];
> };

Looks good to me, will change

> Otherwise, looks good to me!

Excellent, thanks for the review!

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 1/3] dm: Add verity helpers for LoadPin
  2022-05-16 18:51       ` [dm-devel] " Matthias Kaehlcke
@ 2022-05-17  3:38         ` Kees Cook
  -1 siblings, 0 replies; 42+ messages in thread
From: Kees Cook @ 2022-05-17  3:38 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: Alasdair Kergon, Mike Snitzer, James Morris, Serge E . Hallyn,
	dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module

On Mon, May 16, 2022 at 11:51:54AM -0700, Matthias Kaehlcke wrote:
> 'targets' are different types of DM mappings like 'linear' or 'verity'. A
> device mapper table contains has one or more targets that define the mapping
> of the blocks of the mapped device.
> 
> Having spelled that out I realize that the above check is wrong. It would
> consider a device like this trusted:
> 
> 0 10000000 linear 8:1
> 10000000 10001000 verity <params>
> 
> In the above case only a small part of the DM device would be backed by verity.
> 
> I think we want a table with a single entry that is a verity target.

Ah-ha! Okay, that's what I was worried about. Yes, a device made up
of only trusted verity targets should be the only trusted device. (So,
technically it could be more than 1 verity target, but each would need
to be trusted. Supporting that arrangement, though, may be overkill --
I would expect a 1:1 mapping as you suggest.

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 1/3] dm: Add verity helpers for LoadPin
@ 2022-05-17  3:38         ` Kees Cook
  0 siblings, 0 replies; 42+ messages in thread
From: Kees Cook @ 2022-05-17  3:38 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: linux-security-module, Douglas Anderson, Mike Snitzer,
	linux-kernel, James Morris, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn

On Mon, May 16, 2022 at 11:51:54AM -0700, Matthias Kaehlcke wrote:
> 'targets' are different types of DM mappings like 'linear' or 'verity'. A
> device mapper table contains has one or more targets that define the mapping
> of the blocks of the mapped device.
> 
> Having spelled that out I realize that the above check is wrong. It would
> consider a device like this trusted:
> 
> 0 10000000 linear 8:1
> 10000000 10001000 verity <params>
> 
> In the above case only a small part of the DM device would be backed by verity.
> 
> I think we want a table with a single entry that is a verity target.

Ah-ha! Okay, that's what I was worried about. Yes, a device made up
of only trusted verity targets should be the only trusted device. (So,
technically it could be more than 1 verity target, but each would need
to be trusted. Supporting that arrangement, though, may be overkill --
I would expect a 1:1 mapping as you suggest.

-- 
Kees Cook

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
  2022-05-16 18:17       ` [dm-devel] " Matthias Kaehlcke
@ 2022-05-17  3:44         ` Kees Cook
  -1 siblings, 0 replies; 42+ messages in thread
From: Kees Cook @ 2022-05-17  3:44 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: Alasdair Kergon, Mike Snitzer, James Morris, Serge E . Hallyn,
	dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module

On Mon, May 16, 2022 at 11:17:44AM -0700, Matthias Kaehlcke wrote:
> On Fri, May 13, 2022 at 03:36:26PM -0700, Kees Cook wrote:
> > 
> > 
> > On May 4, 2022 12:54:18 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
> > >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
> > 
> > I think this should be "... on an already trusted device ..."
> 
> It's not entirely clear which part you want me to substitute. 'an already
> trusted device' makes me wonder whether you are thinking about reading the
> list of digests, and not the general case of reading a kernel file, which
> this paragraph intends to describe.

Sorry, I think I confused myself while reading what you'd written. I
think it's fine as is. I think I had skipped around in my mind thinking
about the trusted verity hashes file coming from the pinned root, but
you basically already said that. :) Nevermind!

> > >+static int read_trusted_verity_root_digests(unsigned int fd)
> > >+{
> > >+	struct fd f;
> > >+	void *data;
> > 
> > Probably easier if this is u8 *?
> 
> Maybe slightly, it would then require a cast when passing it to
> kernel_read_file()

Oh, good point. That is a kinda weird API.

> 
> > >+	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 = fdget(fd);
> > >+	if (!f.file)
> > >+		return -EINVAL;
> > >+
> > >+	data = kzalloc(SZ_4K, GFP_KERNEL);
> > >+	if (!data) {
> > >+		rc = -ENOMEM;
> > >+		goto err;
> > >+	}
> > >+
> > >+	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
> > >+	if (rc < 0)
> > >+		goto err;

So maybe, here, you could do:

	p = data;
	p[rc] '\0';
	p = strim(p);

etc... (the void * -> char * cast in the assignment should be accepted
without warning?)

> > >+
> > >+	((char *)data)[rc] = '\0';
> > >+
> > >+	p = strim(data);
> > >+	while ((d = strsep(&p, ",")) != NULL) {
> > 
> > Maybe be flexible and add newline as a separator too?
> 
> Sure, I can add that. I'd also be fine with just allowing a newline as
> separator, which seems a reasonable format for a sysfs file.

Yeah, that was my thinking too. And easier to parse for command line
tools, etc. Not a requirement at all, but might make testing easier,
etc.

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
@ 2022-05-17  3:44         ` Kees Cook
  0 siblings, 0 replies; 42+ messages in thread
From: Kees Cook @ 2022-05-17  3:44 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: linux-security-module, Douglas Anderson, Mike Snitzer,
	linux-kernel, James Morris, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn

On Mon, May 16, 2022 at 11:17:44AM -0700, Matthias Kaehlcke wrote:
> On Fri, May 13, 2022 at 03:36:26PM -0700, Kees Cook wrote:
> > 
> > 
> > On May 4, 2022 12:54:18 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
> > >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
> > 
> > I think this should be "... on an already trusted device ..."
> 
> It's not entirely clear which part you want me to substitute. 'an already
> trusted device' makes me wonder whether you are thinking about reading the
> list of digests, and not the general case of reading a kernel file, which
> this paragraph intends to describe.

Sorry, I think I confused myself while reading what you'd written. I
think it's fine as is. I think I had skipped around in my mind thinking
about the trusted verity hashes file coming from the pinned root, but
you basically already said that. :) Nevermind!

> > >+static int read_trusted_verity_root_digests(unsigned int fd)
> > >+{
> > >+	struct fd f;
> > >+	void *data;
> > 
> > Probably easier if this is u8 *?
> 
> Maybe slightly, it would then require a cast when passing it to
> kernel_read_file()

Oh, good point. That is a kinda weird API.

> 
> > >+	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 = fdget(fd);
> > >+	if (!f.file)
> > >+		return -EINVAL;
> > >+
> > >+	data = kzalloc(SZ_4K, GFP_KERNEL);
> > >+	if (!data) {
> > >+		rc = -ENOMEM;
> > >+		goto err;
> > >+	}
> > >+
> > >+	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
> > >+	if (rc < 0)
> > >+		goto err;

So maybe, here, you could do:

	p = data;
	p[rc] '\0';
	p = strim(p);

etc... (the void * -> char * cast in the assignment should be accepted
without warning?)

> > >+
> > >+	((char *)data)[rc] = '\0';
> > >+
> > >+	p = strim(data);
> > >+	while ((d = strsep(&p, ",")) != NULL) {
> > 
> > Maybe be flexible and add newline as a separator too?
> 
> Sure, I can add that. I'd also be fine with just allowing a newline as
> separator, which seems a reasonable format for a sysfs file.

Yeah, that was my thinking too. And easier to parse for command line
tools, etc. Not a requirement at all, but might make testing easier,
etc.

-- 
Kees Cook

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
  2022-05-17  3:44         ` [dm-devel] " Kees Cook
@ 2022-05-17 19:28           ` Matthias Kaehlcke
  -1 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-17 19:28 UTC (permalink / raw)
  To: Kees Cook
  Cc: Alasdair Kergon, Mike Snitzer, James Morris, Serge E . Hallyn,
	dm-devel, linux-kernel, linux-raid, Song Liu, Douglas Anderson,
	linux-security-module

On Mon, May 16, 2022 at 08:44:37PM -0700, Kees Cook wrote:
> On Mon, May 16, 2022 at 11:17:44AM -0700, Matthias Kaehlcke wrote:
> > On Fri, May 13, 2022 at 03:36:26PM -0700, Kees Cook wrote:
> > > 
> > > 
> > > On May 4, 2022 12:54:18 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
> > > >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
> > > 
> > > I think this should be "... on an already trusted device ..."
> > 
> > It's not entirely clear which part you want me to substitute. 'an already
> > trusted device' makes me wonder whether you are thinking about reading the
> > list of digests, and not the general case of reading a kernel file, which
> > this paragraph intends to describe.
> 
> Sorry, I think I confused myself while reading what you'd written. I
> think it's fine as is. I think I had skipped around in my mind thinking
> about the trusted verity hashes file coming from the pinned root, but
> you basically already said that. :) Nevermind!
> 
> > > >+static int read_trusted_verity_root_digests(unsigned int fd)
> > > >+{
> > > >+	struct fd f;
> > > >+	void *data;
> > > 
> > > Probably easier if this is u8 *?
> > 
> > Maybe slightly, it would then require a cast when passing it to
> > kernel_read_file()
> 
> Oh, good point. That is a kinda weird API.
> 
> > 
> > > >+	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 = fdget(fd);
> > > >+	if (!f.file)
> > > >+		return -EINVAL;
> > > >+
> > > >+	data = kzalloc(SZ_4K, GFP_KERNEL);
> > > >+	if (!data) {
> > > >+		rc = -ENOMEM;
> > > >+		goto err;
> > > >+	}
> > > >+
> > > >+	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
> > > >+	if (rc < 0)
> > > >+		goto err;
> 
> So maybe, here, you could do:
> 
> 	p = data;
> 	p[rc] '\0';
> 	p = strim(p);
> 
> etc... (the void * -> char * cast in the assignment should be accepted
> without warning?)

Yes, that would work, I'll change it accordingly, thanks!

> > > >+
> > > >+	((char *)data)[rc] = '\0';
> > > >+
> > > >+	p = strim(data);
> > > >+	while ((d = strsep(&p, ",")) != NULL) {
> > > 
> > > Maybe be flexible and add newline as a separator too?
> > 
> > Sure, I can add that. I'd also be fine with just allowing a newline as
> > separator, which seems a reasonable format for a sysfs file.
> 
> Yeah, that was my thinking too. And easier to parse for command line
> tools, etc. Not a requirement at all, but might make testing easier,
> etc.

Ok, I'll change it to use newline as the only separator.

^ permalink raw reply	[flat|nested] 42+ messages in thread

* Re: [dm-devel] [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices
@ 2022-05-17 19:28           ` Matthias Kaehlcke
  0 siblings, 0 replies; 42+ messages in thread
From: Matthias Kaehlcke @ 2022-05-17 19:28 UTC (permalink / raw)
  To: Kees Cook
  Cc: linux-security-module, Douglas Anderson, Mike Snitzer,
	linux-kernel, James Morris, linux-raid, Song Liu, dm-devel,
	Alasdair Kergon, Serge E . Hallyn

On Mon, May 16, 2022 at 08:44:37PM -0700, Kees Cook wrote:
> On Mon, May 16, 2022 at 11:17:44AM -0700, Matthias Kaehlcke wrote:
> > On Fri, May 13, 2022 at 03:36:26PM -0700, Kees Cook wrote:
> > > 
> > > 
> > > On May 4, 2022 12:54:18 PM PDT, Matthias Kaehlcke <mka@chromium.org> wrote:
> > > >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
> > > 
> > > I think this should be "... on an already trusted device ..."
> > 
> > It's not entirely clear which part you want me to substitute. 'an already
> > trusted device' makes me wonder whether you are thinking about reading the
> > list of digests, and not the general case of reading a kernel file, which
> > this paragraph intends to describe.
> 
> Sorry, I think I confused myself while reading what you'd written. I
> think it's fine as is. I think I had skipped around in my mind thinking
> about the trusted verity hashes file coming from the pinned root, but
> you basically already said that. :) Nevermind!
> 
> > > >+static int read_trusted_verity_root_digests(unsigned int fd)
> > > >+{
> > > >+	struct fd f;
> > > >+	void *data;
> > > 
> > > Probably easier if this is u8 *?
> > 
> > Maybe slightly, it would then require a cast when passing it to
> > kernel_read_file()
> 
> Oh, good point. That is a kinda weird API.
> 
> > 
> > > >+	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 = fdget(fd);
> > > >+	if (!f.file)
> > > >+		return -EINVAL;
> > > >+
> > > >+	data = kzalloc(SZ_4K, GFP_KERNEL);
> > > >+	if (!data) {
> > > >+		rc = -ENOMEM;
> > > >+		goto err;
> > > >+	}
> > > >+
> > > >+	rc = kernel_read_file(f.file, 0, &data, SZ_4K - 1, NULL, READING_POLICY);
> > > >+	if (rc < 0)
> > > >+		goto err;
> 
> So maybe, here, you could do:
> 
> 	p = data;
> 	p[rc] '\0';
> 	p = strim(p);
> 
> etc... (the void * -> char * cast in the assignment should be accepted
> without warning?)

Yes, that would work, I'll change it accordingly, thanks!

> > > >+
> > > >+	((char *)data)[rc] = '\0';
> > > >+
> > > >+	p = strim(data);
> > > >+	while ((d = strsep(&p, ",")) != NULL) {
> > > 
> > > Maybe be flexible and add newline as a separator too?
> > 
> > Sure, I can add that. I'd also be fine with just allowing a newline as
> > separator, which seems a reasonable format for a sysfs file.
> 
> Yeah, that was my thinking too. And easier to parse for command line
> tools, etc. Not a requirement at all, but might make testing easier,
> etc.

Ok, I'll change it to use newline as the only separator.

--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


^ permalink raw reply	[flat|nested] 42+ messages in thread

end of thread, other threads:[~2022-05-17 19:28 UTC | newest]

Thread overview: 42+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-04 19:54 [PATCH v3 0/3] LoadPin: Enable loading from trusted dm-verity devices Matthias Kaehlcke
2022-05-04 19:54 ` [dm-devel] " Matthias Kaehlcke
2022-05-04 19:54 ` [PATCH v3 1/3] dm: Add verity helpers for LoadPin Matthias Kaehlcke
2022-05-04 19:54   ` [dm-devel] " Matthias Kaehlcke
2022-05-11 20:54   ` Matthias Kaehlcke
2022-05-11 20:54     ` [dm-devel] " Matthias Kaehlcke
2022-05-12 17:19     ` Mike Snitzer
2022-05-12 17:19       ` [dm-devel] " Mike Snitzer
2022-05-12 18:14       ` Matthias Kaehlcke
2022-05-12 18:14         ` [dm-devel] " Matthias Kaehlcke
2022-05-12 20:44       ` Matthias Kaehlcke
2022-05-12 20:44         ` [dm-devel] " Matthias Kaehlcke
2022-05-13 16:29         ` Mike Snitzer
2022-05-13 16:29           ` [dm-devel] " Mike Snitzer
2022-05-13 16:53           ` Matthias Kaehlcke
2022-05-13 16:53             ` Matthias Kaehlcke
2022-05-13 22:15   ` Kees Cook
2022-05-13 22:15     ` [dm-devel] " Kees Cook
2022-05-16 18:51     ` Matthias Kaehlcke
2022-05-16 18:51       ` [dm-devel] " Matthias Kaehlcke
2022-05-17  3:38       ` Kees Cook
2022-05-17  3:38         ` [dm-devel] " Kees Cook
2022-05-04 19:54 ` [PATCH v3 2/3] LoadPin: Enable loading from trusted dm-verity devices Matthias Kaehlcke
2022-05-04 19:54   ` [dm-devel] " Matthias Kaehlcke
2022-05-04 22:26   ` kernel test robot
2022-05-04 22:26     ` [dm-devel] " kernel test robot
2022-05-13 16:32   ` Mike Snitzer
2022-05-13 16:32     ` [dm-devel] " Mike Snitzer
2022-05-13 17:01     ` Matthias Kaehlcke
2022-05-13 17:01       ` [dm-devel] " Matthias Kaehlcke
2022-05-13 18:26     ` Kees Cook
2022-05-13 18:26       ` [dm-devel] " Kees Cook
2022-05-13 22:36   ` Kees Cook
2022-05-13 22:36     ` [dm-devel] " Kees Cook
2022-05-16 18:17     ` Matthias Kaehlcke
2022-05-16 18:17       ` [dm-devel] " Matthias Kaehlcke
2022-05-17  3:44       ` Kees Cook
2022-05-17  3:44         ` [dm-devel] " Kees Cook
2022-05-17 19:28         ` Matthias Kaehlcke
2022-05-17 19:28           ` [dm-devel] " Matthias Kaehlcke
2022-05-04 19:54 ` [PATCH v3 3/3] dm: verity-loadpin: Use CONFIG_SECURITY_LOADPIN_VERITY for conditional compilation Matthias Kaehlcke
2022-05-04 19:54   ` [dm-devel] " Matthias Kaehlcke

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.