1 // SPDX-License-Identifier: GPL-2.0
3 * fs/verity/signature.c: verification of builtin signatures
5 * Copyright 2019 Google LLC
8 #include "fsverity_private.h"
10 #include <linux/cred.h>
11 #include <linux/key.h>
12 #include <linux/slab.h>
13 #include <linux/verification.h>
16 * /proc/sys/fs/verity/require_signatures
17 * If 1, all verity files must have a valid builtin signature.
19 static int fsverity_require_signatures;
22 * Keyring that contains the trusted X.509 certificates.
24 * Only root (kuid=0) can modify this. Also, root may use
25 * keyctl_restrict_keyring() to prevent any more additions.
27 static struct key *fsverity_keyring;
30 * fsverity_verify_signature() - check a verity file's signature
31 * @vi: the file's fsverity_info
32 * @desc: the file's fsverity_descriptor
33 * @desc_size: size of @desc
35 * If the file's fs-verity descriptor includes a signature of the file
36 * measurement, verify it against the certificates in the fs-verity keyring.
38 * Return: 0 on success (signature valid or not required); -errno on failure
40 int fsverity_verify_signature(const struct fsverity_info *vi,
41 const struct fsverity_descriptor *desc,
44 const struct inode *inode = vi->inode;
45 const struct fsverity_hash_alg *hash_alg = vi->tree_params.hash_alg;
46 const u32 sig_size = le32_to_cpu(desc->sig_size);
47 struct fsverity_signed_digest *d;
51 if (fsverity_require_signatures) {
53 "require_signatures=1, rejecting unsigned file!");
59 if (sig_size > desc_size - sizeof(*desc)) {
60 fsverity_err(inode, "Signature overflows verity descriptor");
64 if (fsverity_keyring->keys.nr_leaves_on_tree == 0) {
66 * The ".fs-verity" keyring is empty, due to builtin signatures
67 * being supported by the kernel but not actually being used.
68 * In this case, verify_pkcs7_signature() would always return an
69 * error, usually ENOKEY. It could also be EBADMSG if the
70 * PKCS#7 is malformed, but that isn't very important to
71 * distinguish. So, just skip to ENOKEY to avoid the attack
72 * surface of the PKCS#7 parser, which would otherwise be
73 * reachable by any task able to execute FS_IOC_ENABLE_VERITY.
76 "fs-verity keyring is empty, rejecting signed file!");
80 d = kzalloc(sizeof(*d) + hash_alg->digest_size, GFP_KERNEL);
83 memcpy(d->magic, "FSVerity", 8);
84 d->digest_algorithm = cpu_to_le16(hash_alg - fsverity_hash_algs);
85 d->digest_size = cpu_to_le16(hash_alg->digest_size);
86 memcpy(d->digest, vi->measurement, hash_alg->digest_size);
88 err = verify_pkcs7_signature(d, sizeof(*d) + hash_alg->digest_size,
89 desc->signature, sig_size,
91 VERIFYING_UNSPECIFIED_SIGNATURE,
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");
104 fsverity_err(inode, "Error %d verifying file signature",
109 pr_debug("Valid signature for file measurement %s:%*phN\n",
110 hash_alg->name, hash_alg->digest_size, vi->measurement);
115 static struct ctl_table_header *fsverity_sysctl_header;
117 static const struct ctl_path fsverity_sysctl_path[] = {
118 { .procname = "fs", },
119 { .procname = "verity", },
123 static struct ctl_table fsverity_sysctl_table[] = {
125 .procname = "require_signatures",
126 .data = &fsverity_require_signatures,
127 .maxlen = sizeof(int),
129 .proc_handler = proc_dointvec_minmax,
130 .extra1 = SYSCTL_ZERO,
131 .extra2 = SYSCTL_ONE,
136 static int __init fsverity_sysctl_init(void)
138 fsverity_sysctl_header = register_sysctl_paths(fsverity_sysctl_path,
139 fsverity_sysctl_table);
140 if (!fsverity_sysctl_header) {
141 pr_err("sysctl registration failed!\n");
146 #else /* !CONFIG_SYSCTL */
147 static inline int __init fsverity_sysctl_init(void)
151 #endif /* !CONFIG_SYSCTL */
153 int __init fsverity_init_signature(void)
158 ring = keyring_alloc(".fs-verity", KUIDT_INIT(0), KGIDT_INIT(0),
159 current_cred(), KEY_POS_SEARCH |
160 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_WRITE |
161 KEY_USR_SEARCH | KEY_USR_SETATTR,
162 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
164 return PTR_ERR(ring);
166 err = fsverity_sysctl_init();
170 fsverity_keyring = ring;