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 d
= kzalloc(sizeof(*d
) + hash_alg
->digest_size
, GFP_KERNEL
);
67 memcpy(d
->magic
, "FSVerity", 8);
68 d
->digest_algorithm
= cpu_to_le16(hash_alg
- fsverity_hash_algs
);
69 d
->digest_size
= cpu_to_le16(hash_alg
->digest_size
);
70 memcpy(d
->digest
, vi
->measurement
, hash_alg
->digest_size
);
72 err
= verify_pkcs7_signature(d
, sizeof(*d
) + hash_alg
->digest_size
,
73 desc
->signature
, sig_size
,
75 VERIFYING_UNSPECIFIED_SIGNATURE
,
82 "File's signing cert isn't in the fs-verity keyring");
83 else if (err
== -EKEYREJECTED
)
84 fsverity_err(inode
, "Incorrect file signature");
85 else if (err
== -EBADMSG
)
86 fsverity_err(inode
, "Malformed file signature");
88 fsverity_err(inode
, "Error %d verifying file signature",
93 pr_debug("Valid signature for file measurement %s:%*phN\n",
94 hash_alg
->name
, hash_alg
->digest_size
, vi
->measurement
);
99 static struct ctl_table_header
*fsverity_sysctl_header
;
101 static const struct ctl_path fsverity_sysctl_path
[] = {
102 { .procname
= "fs", },
103 { .procname
= "verity", },
107 static struct ctl_table fsverity_sysctl_table
[] = {
109 .procname
= "require_signatures",
110 .data
= &fsverity_require_signatures
,
111 .maxlen
= sizeof(int),
113 .proc_handler
= proc_dointvec_minmax
,
114 .extra1
= SYSCTL_ZERO
,
115 .extra2
= SYSCTL_ONE
,
120 static int __init
fsverity_sysctl_init(void)
122 fsverity_sysctl_header
= register_sysctl_paths(fsverity_sysctl_path
,
123 fsverity_sysctl_table
);
124 if (!fsverity_sysctl_header
) {
125 pr_err("sysctl registration failed!\n");
130 #else /* !CONFIG_SYSCTL */
131 static inline int __init
fsverity_sysctl_init(void)
135 #endif /* !CONFIG_SYSCTL */
137 int __init
fsverity_init_signature(void)
142 ring
= keyring_alloc(".fs-verity", KUIDT_INIT(0), KGIDT_INIT(0),
143 current_cred(), KEY_POS_SEARCH
|
144 KEY_USR_VIEW
| KEY_USR_READ
| KEY_USR_WRITE
|
145 KEY_USR_SEARCH
| KEY_USR_SETATTR
,
146 KEY_ALLOC_NOT_IN_QUOTA
, NULL
, NULL
);
148 return PTR_ERR(ring
);
150 err
= fsverity_sysctl_init();
154 fsverity_keyring
= ring
;