1 // SPDX-License-Identifier: GPL-2.0
3 * Verification of builtin signatures
5 * Copyright 2019 Google LLC
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.
16 #include "fsverity_private.h"
18 #include <linux/cred.h>
19 #include <linux/key.h>
20 #include <linux/slab.h>
21 #include <linux/verification.h>
24 * /proc/sys/fs/verity/require_signatures
25 * If 1, all verity files must have a valid builtin signature.
27 int fsverity_require_signatures;
30 * Keyring that contains the trusted X.509 certificates.
32 * Only root (kuid=0) can modify this. Also, root may use
33 * keyctl_restrict_keyring() to prevent any more additions.
35 static struct key *fsverity_keyring;
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
43 * If the file includes a signature of its fs-verity file digest, verify it
44 * against the certificates in the fs-verity keyring.
46 * Return: 0 on success (signature valid or not required); -errno on failure
48 int fsverity_verify_signature(const struct fsverity_info *vi,
49 const u8 *signature, size_t sig_size)
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;
57 if (fsverity_require_signatures) {
59 "require_signatures=1, rejecting unsigned file!");
65 if (fsverity_keyring->keys.nr_leaves_on_tree == 0) {
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.
77 "fs-verity keyring is empty, rejecting signed file!");
81 d = kzalloc(sizeof(*d) + hash_alg->digest_size, GFP_KERNEL);
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);
89 err = verify_pkcs7_signature(d, sizeof(*d) + hash_alg->digest_size,
90 signature, sig_size, fsverity_keyring,
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",
112 void __init fsverity_init_signature(void)
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");