Skip to content

Commit 3f805f8

Browse files
Matthias Kaehlckekees
authored andcommitted
LoadPin: Enable loading from trusted dm-verity devices
Extend LoadPin to allow loading of kernel files from trusted dm-verity [1] devices. This change adds the concept of trusted verity devices to LoadPin. LoadPin maintains a list of root digests of verity devices it considers trusted. Userspace can populate this list through an ioctl on the new LoadPin securityfs entry 'dm-verity'. The ioctl receives a file descriptor of a file with verity digests as parameter. Verity reads the digests from this file after confirming that the file is located on the pinned root. The digest file must contain one digest per line. The list of trusted digests can only be set up once, which is typically done at boot time. When a kernel file is read LoadPin first checks (as usual) whether the file is located on the pinned root, if so the file can be loaded. Otherwise, if the verity extension is enabled, LoadPin determines whether the file is located on a verity backed device and whether the root digest of that device is in the list of trusted digests. The file can be loaded if the verity device has a trusted root digest. Background: As of now LoadPin restricts loading of kernel files to a single pinned filesystem, typically the rootfs. This works for many systems, however it can result in a bloated rootfs (and OTA updates) on platforms where multiple boards with different hardware configurations use the same rootfs image. Especially when 'optional' files are large it may be preferable to download/install them only when they are actually needed by a given board. Chrome OS uses Downloadable Content (DLC) [2] to deploy certain 'packages' at runtime. As an example a DLC package could contain firmware for a peripheral that is not present on all boards. DLCs use dm-verity to verify the integrity of the DLC content. [1] https://www.kernel.org/doc/html/latest/admin-guide/device-mapper/verity.html [2] https://chromium.googlesource.com/chromiumos/platform2/+/HEAD/dlcservice/docs/developer.md Signed-off-by: Matthias Kaehlcke <[email protected]> Acked-by: Mike Snitzer <[email protected]> Link: https://lore.kernel.org/lkml/20220627083512.v7.2.I01c67af41d2f6525c6d023101671d7339a9bc8b5@changeid Signed-off-by: Kees Cook <[email protected]>
1 parent b6c1c57 commit 3f805f8

File tree

3 files changed

+204
-1
lines changed

3 files changed

+204
-1
lines changed

include/uapi/linux/loadpin.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2+
/*
3+
* Copyright (c) 2022, Google LLC
4+
*/
5+
6+
#ifndef _UAPI_LINUX_LOOP_LOADPIN_H
7+
#define _UAPI_LINUX_LOOP_LOADPIN_H
8+
9+
#define LOADPIN_IOC_MAGIC 'L'
10+
11+
/**
12+
* LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS - Set up the root digests of verity devices
13+
* that loadpin should trust.
14+
*
15+
* Takes a file descriptor from which to read the root digests of trusted verity devices. The file
16+
* is expected to contain a list of digests in ASCII format, with one line per digest. The ioctl
17+
* must be issued on the securityfs attribute 'loadpin/dm-verity' (which can be typically found
18+
* under /sys/kernel/security/loadpin/dm-verity).
19+
*/
20+
#define LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS _IOW(LOADPIN_IOC_MAGIC, 0x00, unsigned int)
21+
22+
#endif /* _UAPI_LINUX_LOOP_LOADPIN_H */

security/loadpin/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,19 @@ config SECURITY_LOADPIN_ENFORCE
1818
If selected, LoadPin will enforce pinning at boot. If not
1919
selected, it can be enabled at boot with the kernel parameter
2020
"loadpin.enforce=1".
21+
22+
config SECURITY_LOADPIN_VERITY
23+
bool "Allow reading files from certain other filesystems that use dm-verity"
24+
depends on SECURITY_LOADPIN && DM_VERITY=y && SECURITYFS
25+
help
26+
If selected LoadPin can allow reading files from filesystems
27+
that use dm-verity. LoadPin maintains a list of verity root
28+
digests it considers trusted. A verity backed filesystem is
29+
considered trusted if its root digest is found in the list
30+
of trusted digests.
31+
32+
The list of trusted verity can be populated through an ioctl
33+
on the LoadPin securityfs entry 'dm-verity'. The ioctl
34+
expects a file descriptor of a file with verity digests as
35+
parameter. The file must be located on the pinned root and
36+
contain a comma separated list of digests.

security/loadpin/loadpin.c

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <linux/path.h>
1919
#include <linux/sched.h> /* current */
2020
#include <linux/string_helpers.h>
21+
#include <linux/dm-verity-loadpin.h>
22+
#include <uapi/linux/loadpin.h>
2123

2224
static void report_load(const char *origin, struct file *file, char *operation)
2325
{
@@ -43,6 +45,9 @@ static char *exclude_read_files[READING_MAX_ID];
4345
static int ignore_read_file_id[READING_MAX_ID] __ro_after_init;
4446
static struct super_block *pinned_root;
4547
static DEFINE_SPINLOCK(pinned_root_spinlock);
48+
#ifdef CONFIG_SECURITY_LOADPIN_VERITY
49+
static bool deny_reading_verity_digests;
50+
#endif
4651

4752
#ifdef CONFIG_SYSCTL
4853

@@ -171,7 +176,8 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id,
171176
spin_unlock(&pinned_root_spinlock);
172177
}
173178

174-
if (IS_ERR_OR_NULL(pinned_root) || load_root != pinned_root) {
179+
if (IS_ERR_OR_NULL(pinned_root) ||
180+
((load_root != pinned_root) && !dm_verity_loadpin_is_bdev_trusted(load_root->s_bdev))) {
175181
if (unlikely(!enforce)) {
176182
report_load(origin, file, "pinning-ignored");
177183
return 0;
@@ -237,6 +243,7 @@ static int __init loadpin_init(void)
237243
enforce ? "" : "not ");
238244
parse_exclude();
239245
security_add_hooks(loadpin_hooks, ARRAY_SIZE(loadpin_hooks), "loadpin");
246+
240247
return 0;
241248
}
242249

@@ -245,6 +252,164 @@ DEFINE_LSM(loadpin) = {
245252
.init = loadpin_init,
246253
};
247254

255+
#ifdef CONFIG_SECURITY_LOADPIN_VERITY
256+
257+
enum loadpin_securityfs_interface_index {
258+
LOADPIN_DM_VERITY,
259+
};
260+
261+
static int read_trusted_verity_root_digests(unsigned int fd)
262+
{
263+
struct fd f;
264+
void *data;
265+
int rc;
266+
char *p, *d;
267+
268+
if (deny_reading_verity_digests)
269+
return -EPERM;
270+
271+
/* The list of trusted root digests can only be set up once */
272+
if (!list_empty(&dm_verity_loadpin_trusted_root_digests))
273+
return -EPERM;
274+
275+
f = fdget(fd);
276+
if (!f.file)
277+
return -EINVAL;
278+
279+
data = kzalloc(SZ_4K, GFP_KERNEL);
280+
if (!data) {
281+
rc = -ENOMEM;
282+
goto err;
283+
}
284+
285+
rc = kernel_read_file(f.file, 0, (void **)&data, SZ_4K - 1, NULL, READING_POLICY);
286+
if (rc < 0)
287+
goto err;
288+
289+
p = data;
290+
p[rc] = '\0';
291+
p = strim(p);
292+
293+
p = strim(data);
294+
while ((d = strsep(&p, "\n")) != NULL) {
295+
int len = strlen(d);
296+
struct dm_verity_loadpin_trusted_root_digest *trd;
297+
298+
if (len % 2) {
299+
rc = -EPROTO;
300+
goto err;
301+
}
302+
303+
len /= 2;
304+
305+
trd = kzalloc(struct_size(trd, data, len), GFP_KERNEL);
306+
if (!trd) {
307+
rc = -ENOMEM;
308+
goto err;
309+
}
310+
311+
if (hex2bin(trd->data, d, len)) {
312+
kfree(trd);
313+
rc = -EPROTO;
314+
goto err;
315+
}
316+
317+
trd->len = len;
318+
319+
list_add_tail(&trd->node, &dm_verity_loadpin_trusted_root_digests);
320+
}
321+
322+
if (list_empty(&dm_verity_loadpin_trusted_root_digests)) {
323+
rc = -EPROTO;
324+
goto err;
325+
}
326+
327+
kfree(data);
328+
fdput(f);
329+
330+
return 0;
331+
332+
err:
333+
kfree(data);
334+
335+
/* any failure in loading/parsing invalidates the entire list */
336+
{
337+
struct dm_verity_loadpin_trusted_root_digest *trd, *tmp;
338+
339+
list_for_each_entry_safe(trd, tmp, &dm_verity_loadpin_trusted_root_digests, node) {
340+
list_del(&trd->node);
341+
kfree(trd);
342+
}
343+
}
344+
345+
/* disallow further attempts after reading a corrupt/invalid file */
346+
deny_reading_verity_digests = true;
347+
348+
fdput(f);
349+
350+
return rc;
351+
}
352+
353+
/******************************** securityfs ********************************/
354+
355+
static long dm_verity_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
356+
{
357+
void __user *uarg = (void __user *)arg;
358+
unsigned int fd;
359+
int rc;
360+
361+
switch (cmd) {
362+
case LOADPIN_IOC_SET_TRUSTED_VERITY_DIGESTS:
363+
rc = copy_from_user(&fd, uarg, sizeof(fd));
364+
if (rc)
365+
return rc;
366+
367+
return read_trusted_verity_root_digests(fd);
368+
369+
default:
370+
return -EINVAL;
371+
}
372+
}
373+
374+
static const struct file_operations loadpin_dm_verity_ops = {
375+
.unlocked_ioctl = dm_verity_ioctl,
376+
.compat_ioctl = compat_ptr_ioctl,
377+
};
378+
379+
/**
380+
* init_loadpin_securityfs - create the securityfs directory for LoadPin
381+
*
382+
* We can not put this method normally under the loadpin_init() code path since
383+
* the security subsystem gets initialized before the vfs caches.
384+
*
385+
* Returns 0 if the securityfs directory creation was successful.
386+
*/
387+
static int __init init_loadpin_securityfs(void)
388+
{
389+
struct dentry *loadpin_dir, *dentry;
390+
391+
loadpin_dir = securityfs_create_dir("loadpin", NULL);
392+
if (IS_ERR(loadpin_dir)) {
393+
pr_err("LoadPin: could not create securityfs dir: %ld\n",
394+
PTR_ERR(loadpin_dir));
395+
return PTR_ERR(loadpin_dir);
396+
}
397+
398+
dentry = securityfs_create_file("dm-verity", 0600, loadpin_dir,
399+
(void *)LOADPIN_DM_VERITY, &loadpin_dm_verity_ops);
400+
if (IS_ERR(dentry)) {
401+
pr_err("LoadPin: could not create securityfs entry 'dm-verity': %ld\n",
402+
PTR_ERR(dentry));
403+
return PTR_ERR(dentry);
404+
}
405+
406+
return 0;
407+
}
408+
409+
fs_initcall(init_loadpin_securityfs);
410+
411+
#endif /* CONFIG_SECURITY_LOADPIN_VERITY */
412+
248413
/* Should not be mutable after boot, so not listed in sysfs (perm == 0). */
249414
module_param(enforce, int, 0);
250415
MODULE_PARM_DESC(enforce, "Enforce module/firmware pinning");

0 commit comments

Comments
 (0)