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
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.
35 * Return: 0 on success (signature valid or not required); -errno on failure
37 int fsverity_verify_signature(const struct fsverity_info
*vi
,
38 const struct fsverity_descriptor
*desc
,
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
;
48 if (fsverity_require_signatures
) {
50 "require_signatures=1, rejecting unsigned file!");
56 if (sig_size
> desc_size
- sizeof(*desc
)) {
57 fsverity_err(inode
, "Signature overflows verity descriptor");
61 d
= kzalloc(sizeof(*d
) + hash_alg
->digest_size
, GFP_KERNEL
);
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
);
69 err
= verify_pkcs7_signature(d
, sizeof(*d
) + hash_alg
->digest_size
,
70 desc
->signature
, sig_size
,
72 VERIFYING_UNSPECIFIED_SIGNATURE
,
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");
85 fsverity_err(inode
, "Error %d verifying file signature",
90 pr_debug("Valid signature for file measurement %s:%*phN\n",
91 hash_alg
->name
, hash_alg
->digest_size
, vi
->measurement
);
96 static struct ctl_table_header
*fsverity_sysctl_header
;
98 static const struct ctl_path fsverity_sysctl_path
[] = {
99 { .procname
= "fs", },
100 { .procname
= "verity", },
104 static struct ctl_table fsverity_sysctl_table
[] = {
106 .procname
= "require_signatures",
107 .data
= &fsverity_require_signatures
,
108 .maxlen
= sizeof(int),
110 .proc_handler
= proc_dointvec_minmax
,
111 .extra1
= SYSCTL_ZERO
,
112 .extra2
= SYSCTL_ONE
,
117 static int __init
fsverity_sysctl_init(void)
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");
127 #else /* !CONFIG_SYSCTL */
128 static inline int __init
fsverity_sysctl_init(void)
132 #endif /* !CONFIG_SYSCTL */
134 int __init
fsverity_init_signature(void)
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
);
145 return PTR_ERR(ring
);
147 err
= fsverity_sysctl_init();
151 fsverity_keyring
= ring
;