Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Verification of builtin signatures
4 *
5 * Copyright 2019 Google LLC
6 */
7
8/*
9 * This file implements verification of fs-verity builtin signatures. Please
10 * take great care before using this feature. It is not the only way to do
11 * signatures with fs-verity, and the alternatives (such as userspace signature
12 * verification, and IMA appraisal) can be much better. For details about the
13 * limitations of this feature, see Documentation/filesystems/fsverity.rst.
14 */
15
16#include "fsverity_private.h"
17
18#include <linux/cred.h>
19#include <linux/key.h>
20#include <linux/slab.h>
21#include <linux/verification.h>
22
23/*
24 * /proc/sys/fs/verity/require_signatures
25 * If 1, all verity files must have a valid builtin signature.
26 */
27int fsverity_require_signatures;
28
29/*
30 * Keyring that contains the trusted X.509 certificates.
31 *
32 * Only root (kuid=0) can modify this. Also, root may use
33 * keyctl_restrict_keyring() to prevent any more additions.
34 */
35static struct key *fsverity_keyring;
36
37/**
38 * fsverity_verify_signature() - check a verity file's signature
39 * @vi: the file's fsverity_info
40 * @signature: the file's built-in signature
41 * @sig_size: size of signature in bytes, or 0 if no signature
42 *
43 * If the file includes a signature of its fs-verity file digest, verify it
44 * against the certificates in the fs-verity keyring.
45 *
46 * Return: 0 on success (signature valid or not required); -errno on failure
47 */
48int fsverity_verify_signature(const struct fsverity_info *vi,
49 const u8 *signature, size_t sig_size)
50{
51 const struct inode *inode = vi->inode;
52 const struct fsverity_hash_alg *hash_alg = vi->tree_params.hash_alg;
53 struct fsverity_formatted_digest *d;
54 int err;
55
56 if (sig_size == 0) {
57 if (fsverity_require_signatures) {
58 fsverity_err(inode,
59 "require_signatures=1, rejecting unsigned file!");
60 return -EPERM;
61 }
62 return 0;
63 }
64
65 if (fsverity_keyring->keys.nr_leaves_on_tree == 0) {
66 /*
67 * The ".fs-verity" keyring is empty, due to builtin signatures
68 * being supported by the kernel but not actually being used.
69 * In this case, verify_pkcs7_signature() would always return an
70 * error, usually ENOKEY. It could also be EBADMSG if the
71 * PKCS#7 is malformed, but that isn't very important to
72 * distinguish. So, just skip to ENOKEY to avoid the attack
73 * surface of the PKCS#7 parser, which would otherwise be
74 * reachable by any task able to execute FS_IOC_ENABLE_VERITY.
75 */
76 fsverity_err(inode,
77 "fs-verity keyring is empty, rejecting signed file!");
78 return -ENOKEY;
79 }
80
81 d = kzalloc(sizeof(*d) + hash_alg->digest_size, GFP_KERNEL);
82 if (!d)
83 return -ENOMEM;
84 memcpy(d->magic, "FSVerity", 8);
85 d->digest_algorithm = cpu_to_le16(hash_alg - fsverity_hash_algs);
86 d->digest_size = cpu_to_le16(hash_alg->digest_size);
87 memcpy(d->digest, vi->file_digest, hash_alg->digest_size);
88
89 err = verify_pkcs7_signature(d, sizeof(*d) + hash_alg->digest_size,
90 signature, sig_size, fsverity_keyring,
91 VERIFYING_UNSPECIFIED_SIGNATURE,
92 NULL, NULL);
93 kfree(d);
94
95 if (err) {
96 if (err == -ENOKEY)
97 fsverity_err(inode,
98 "File's signing cert isn't in the fs-verity keyring");
99 else if (err == -EKEYREJECTED)
100 fsverity_err(inode, "Incorrect file signature");
101 else if (err == -EBADMSG)
102 fsverity_err(inode, "Malformed file signature");
103 else
104 fsverity_err(inode, "Error %d verifying file signature",
105 err);
106 return err;
107 }
108
109 return 0;
110}
111
112void __init fsverity_init_signature(void)
113{
114 fsverity_keyring =
115 keyring_alloc(".fs-verity", KUIDT_INIT(0), KGIDT_INIT(0),
116 current_cred(), KEY_POS_SEARCH |
117 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_WRITE |
118 KEY_USR_SEARCH | KEY_USR_SETATTR,
119 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
120 if (IS_ERR(fsverity_keyring))
121 panic("failed to allocate \".fs-verity\" keyring");
122}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * fs/verity/signature.c: verification of builtin signatures
4 *
5 * Copyright 2019 Google LLC
6 */
7
8#include "fsverity_private.h"
9
10#include <linux/cred.h>
11#include <linux/key.h>
12#include <linux/slab.h>
13#include <linux/verification.h>
14
15/*
16 * /proc/sys/fs/verity/require_signatures
17 * If 1, all verity files must have a valid builtin signature.
18 */
19static int fsverity_require_signatures;
20
21/*
22 * Keyring that contains the trusted X.509 certificates.
23 *
24 * Only root (kuid=0) can modify this. Also, root may use
25 * keyctl_restrict_keyring() to prevent any more additions.
26 */
27static struct key *fsverity_keyring;
28
29/**
30 * fsverity_verify_signature() - check a verity file's signature
31 *
32 * If the file's fs-verity descriptor includes a signature of the file
33 * measurement, verify it against the certificates in the fs-verity keyring.
34 *
35 * Return: 0 on success (signature valid or not required); -errno on failure
36 */
37int fsverity_verify_signature(const struct fsverity_info *vi,
38 const struct fsverity_descriptor *desc,
39 size_t desc_size)
40{
41 const struct inode *inode = vi->inode;
42 const struct fsverity_hash_alg *hash_alg = vi->tree_params.hash_alg;
43 const u32 sig_size = le32_to_cpu(desc->sig_size);
44 struct fsverity_signed_digest *d;
45 int err;
46
47 if (sig_size == 0) {
48 if (fsverity_require_signatures) {
49 fsverity_err(inode,
50 "require_signatures=1, rejecting unsigned file!");
51 return -EPERM;
52 }
53 return 0;
54 }
55
56 if (sig_size > desc_size - sizeof(*desc)) {
57 fsverity_err(inode, "Signature overflows verity descriptor");
58 return -EBADMSG;
59 }
60
61 d = kzalloc(sizeof(*d) + hash_alg->digest_size, GFP_KERNEL);
62 if (!d)
63 return -ENOMEM;
64 memcpy(d->magic, "FSVerity", 8);
65 d->digest_algorithm = cpu_to_le16(hash_alg - fsverity_hash_algs);
66 d->digest_size = cpu_to_le16(hash_alg->digest_size);
67 memcpy(d->digest, vi->measurement, hash_alg->digest_size);
68
69 err = verify_pkcs7_signature(d, sizeof(*d) + hash_alg->digest_size,
70 desc->signature, sig_size,
71 fsverity_keyring,
72 VERIFYING_UNSPECIFIED_SIGNATURE,
73 NULL, NULL);
74 kfree(d);
75
76 if (err) {
77 if (err == -ENOKEY)
78 fsverity_err(inode,
79 "File's signing cert isn't in the fs-verity keyring");
80 else if (err == -EKEYREJECTED)
81 fsverity_err(inode, "Incorrect file signature");
82 else if (err == -EBADMSG)
83 fsverity_err(inode, "Malformed file signature");
84 else
85 fsverity_err(inode, "Error %d verifying file signature",
86 err);
87 return err;
88 }
89
90 pr_debug("Valid signature for file measurement %s:%*phN\n",
91 hash_alg->name, hash_alg->digest_size, vi->measurement);
92 return 0;
93}
94
95#ifdef CONFIG_SYSCTL
96static struct ctl_table_header *fsverity_sysctl_header;
97
98static const struct ctl_path fsverity_sysctl_path[] = {
99 { .procname = "fs", },
100 { .procname = "verity", },
101 { }
102};
103
104static struct ctl_table fsverity_sysctl_table[] = {
105 {
106 .procname = "require_signatures",
107 .data = &fsverity_require_signatures,
108 .maxlen = sizeof(int),
109 .mode = 0644,
110 .proc_handler = proc_dointvec_minmax,
111 .extra1 = SYSCTL_ZERO,
112 .extra2 = SYSCTL_ONE,
113 },
114 { }
115};
116
117static int __init fsverity_sysctl_init(void)
118{
119 fsverity_sysctl_header = register_sysctl_paths(fsverity_sysctl_path,
120 fsverity_sysctl_table);
121 if (!fsverity_sysctl_header) {
122 pr_err("sysctl registration failed!\n");
123 return -ENOMEM;
124 }
125 return 0;
126}
127#else /* !CONFIG_SYSCTL */
128static inline int __init fsverity_sysctl_init(void)
129{
130 return 0;
131}
132#endif /* !CONFIG_SYSCTL */
133
134int __init fsverity_init_signature(void)
135{
136 struct key *ring;
137 int err;
138
139 ring = keyring_alloc(".fs-verity", KUIDT_INIT(0), KGIDT_INIT(0),
140 current_cred(), KEY_POS_SEARCH |
141 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_WRITE |
142 KEY_USR_SEARCH | KEY_USR_SETATTR,
143 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
144 if (IS_ERR(ring))
145 return PTR_ERR(ring);
146
147 err = fsverity_sysctl_init();
148 if (err)
149 goto err_put_ring;
150
151 fsverity_keyring = ring;
152 return 0;
153
154err_put_ring:
155 key_put(ring);
156 return err;
157}