Skip to content

Commit 033724b

Browse files
braunerMikulas Patocka
authored andcommitted
dm-verity: add dm-verity keyring
Add a dedicated ".dm-verity" keyring for root hash signature verification, similar to the ".fs-verity" keyring used by fs-verity. By default the keyring is unused retaining the exact same old behavior. For systems that provision additional keys only intended for dm-verity images during boot, the dm_verity.keyring_unsealed=1 kernel parameter leaves the keyring open. We want to use this in systemd as a way add keys during boot that are only used for creating dm-verity devices for later mounting and nothing else. The discoverable disk image (DDI) spec at [1] heavily relies on dm-verity and we would like to expand this even more. This will allow us to do that in a fully backward compatible way. Once provisioning is complete, userspace restricts and activates it for dm-verity verification. If userspace fully seals the keyring then it gains the guarantee that no new keys can be added. Link: https://uapi-group.org/specifications/specs/discoverable_partitions_specification [1] Co-developed-by: Aleksa Sarai <cyphar@cyphar.com> Signed-off-by: Aleksa Sarai <cyphar@cyphar.com> Signed-off-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
1 parent fb8a6c1 commit 033724b

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,13 @@ Kernel parameters
13701370
For details see:
13711371
Documentation/admin-guide/hw-vuln/reg-file-data-sampling.rst
13721372

1373+
dm_verity.keyring_unsealed=
1374+
[KNL] When set to 1, leave the dm-verity keyring
1375+
unsealed after initialization so userspace can
1376+
provision keys. Once the keyring is restricted
1377+
it becomes active and is searched during signature
1378+
verification.
1379+
13731380
driver_async_probe= [KNL]
13741381
List of driver names to be probed asynchronously. *
13751382
matches with all driver names. If * is specified, the

drivers/md/dm-verity-target.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,31 @@ static struct target_type verity_target = {
18021802
.preresume = verity_preresume,
18031803
#endif /* CONFIG_SECURITY */
18041804
};
1805-
module_dm(verity);
1805+
1806+
static int __init dm_verity_init(void)
1807+
{
1808+
int r;
1809+
1810+
r = dm_verity_verify_sig_init();
1811+
if (r)
1812+
return r;
1813+
1814+
r = dm_register_target(&verity_target);
1815+
if (r) {
1816+
dm_verity_verify_sig_exit();
1817+
return r;
1818+
}
1819+
1820+
return 0;
1821+
}
1822+
module_init(dm_verity_init);
1823+
1824+
static void __exit dm_verity_exit(void)
1825+
{
1826+
dm_unregister_target(&verity_target);
1827+
dm_verity_verify_sig_exit();
1828+
}
1829+
module_exit(dm_verity_exit);
18061830

18071831
/*
18081832
* Check whether a DM target is a verity target.

drivers/md/dm-verity-verify-sig.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@
77
*/
88
#include <linux/device-mapper.h>
99
#include <linux/verification.h>
10+
#include <linux/key.h>
1011
#include <keys/user-type.h>
1112
#include <linux/module.h>
1213
#include "dm-verity.h"
1314
#include "dm-verity-verify-sig.h"
1415

1516
#define DM_VERITY_VERIFY_ERR(s) DM_VERITY_ROOT_HASH_VERIFICATION " " s
1617

18+
static struct key *dm_verity_keyring;
19+
20+
static bool dm_verity_keyring_unsealed __ro_after_init;
21+
module_param_named(keyring_unsealed, dm_verity_keyring_unsealed, bool, 0444);
22+
MODULE_PARM_DESC(keyring_unsealed, "Leave the dm-verity keyring unsealed");
23+
1724
static bool require_signatures;
1825
module_param(require_signatures, bool, 0444);
1926
MODULE_PARM_DESC(require_signatures,
@@ -143,6 +150,17 @@ int verity_verify_root_hash(const void *root_hash, size_t root_hash_len,
143150
VERIFYING_UNSPECIFIED_SIGNATURE, NULL, NULL);
144151
#endif
145152

153+
if (ret != -ENOKEY && ret != -EKEYREJECTED)
154+
return ret;
155+
156+
if (dm_verity_keyring->keys.nr_leaves_on_tree &&
157+
dm_verity_keyring->restrict_link)
158+
ret = verify_pkcs7_signature(root_hash, root_hash_len,
159+
sig_data, sig_len,
160+
dm_verity_keyring,
161+
VERIFYING_UNSPECIFIED_SIGNATURE,
162+
NULL, NULL);
163+
146164
return ret;
147165
}
148166

@@ -152,3 +170,30 @@ void verity_verify_sig_opts_cleanup(struct dm_verity_sig_opts *sig_opts)
152170
sig_opts->sig = NULL;
153171
sig_opts->sig_size = 0;
154172
}
173+
174+
int __init dm_verity_verify_sig_init(void)
175+
{
176+
dm_verity_keyring = keyring_alloc(".dm-verity",
177+
GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
178+
current_cred(),
179+
KEY_POS_SEARCH |
180+
KEY_USR_VIEW | KEY_USR_READ |
181+
KEY_USR_WRITE | KEY_USR_SEARCH |
182+
KEY_USR_SETATTR,
183+
KEY_ALLOC_NOT_IN_QUOTA,
184+
NULL, NULL);
185+
if (IS_ERR(dm_verity_keyring))
186+
panic("dm-verity can't allocate keyring\n");
187+
188+
if (!dm_verity_keyring_unsealed &&
189+
keyring_restrict(make_key_ref(dm_verity_keyring, true), NULL, NULL))
190+
panic("dm-verity can't seal keyring\n");
191+
192+
return 0;
193+
}
194+
195+
void __exit dm_verity_verify_sig_exit(void)
196+
{
197+
key_revoke(dm_verity_keyring);
198+
key_put(dm_verity_keyring);
199+
}

drivers/md/dm-verity-verify-sig.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ int verity_verify_sig_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
3030

3131
void verity_verify_sig_opts_cleanup(struct dm_verity_sig_opts *sig_opts);
3232

33+
int __init dm_verity_verify_sig_init(void);
34+
void __exit dm_verity_verify_sig_exit(void);
35+
3336
#else
3437

3538
#define DM_VERITY_ROOT_HASH_VERIFICATION_OPTS 0
@@ -56,5 +59,14 @@ static inline void verity_verify_sig_opts_cleanup(struct dm_verity_sig_opts *sig
5659
{
5760
}
5861

62+
static inline int dm_verity_verify_sig_init(void)
63+
{
64+
return 0;
65+
}
66+
67+
static inline void dm_verity_verify_sig_exit(void)
68+
{
69+
}
70+
5971
#endif /* CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG */
6072
#endif /* DM_VERITY_SIG_VERIFICATION_H */

0 commit comments

Comments
 (0)