Skip to content

Commit 432434c

Browse files
committed
fs-verity: support builtin file signatures
To meet some users' needs, add optional support for having fs-verity handle a portion of the authentication policy in the kernel. An ".fs-verity" keyring is created to which X.509 certificates can be added; then a sysctl 'fs.verity.require_signatures' can be set to cause the kernel to enforce that all fs-verity files contain a signature of their file measurement by a key in this keyring. See the "Built-in signature verification" section of Documentation/filesystems/fsverity.rst for the full documentation. Reviewed-by: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Eric Biggers <ebiggers@google.com>
1 parent add890c commit 432434c

8 files changed

Lines changed: 269 additions & 14 deletions

File tree

‎fs/verity/Kconfig‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,20 @@ config FS_VERITY_DEBUG
3636
Enable debugging messages related to fs-verity by default.
3737

3838
Say N unless you are an fs-verity developer.
39+
40+
config FS_VERITY_BUILTIN_SIGNATURES
41+
bool "FS Verity builtin signature support"
42+
depends on FS_VERITY
43+
select SYSTEM_DATA_VERIFICATION
44+
help
45+
Support verifying signatures of verity files against the X.509
46+
certificates that have been loaded into the ".fs-verity"
47+
kernel keyring.
48+
49+
This is meant as a relatively simple mechanism that can be
50+
used to provide an authenticity guarantee for verity files, as
51+
an alternative to IMA appraisal. Userspace programs still
52+
need to check that the verity bit is set in order to get an
53+
authenticity guarantee.
54+
55+
If unsure, say N.

‎fs/verity/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ obj-$(CONFIG_FS_VERITY) += enable.o \
66
measure.o \
77
open.o \
88
verify.o
9+
10+
obj-$(CONFIG_FS_VERITY_BUILTIN_SIGNATURES) += signature.o

‎fs/verity/enable.c‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static int enable_verity(struct file *filp,
161161
const struct fsverity_operations *vops = inode->i_sb->s_vop;
162162
struct merkle_tree_params params = { };
163163
struct fsverity_descriptor *desc;
164-
size_t desc_size = sizeof(*desc);
164+
size_t desc_size = sizeof(*desc) + arg->sig_size;
165165
struct fsverity_info *vi;
166166
int err;
167167

@@ -183,6 +183,16 @@ static int enable_verity(struct file *filp,
183183
}
184184
desc->salt_size = arg->salt_size;
185185

186+
/* Get the signature if the user provided one */
187+
if (arg->sig_size &&
188+
copy_from_user(desc->signature,
189+
(const u8 __user *)(uintptr_t)arg->sig_ptr,
190+
arg->sig_size)) {
191+
err = -EFAULT;
192+
goto out;
193+
}
194+
desc->sig_size = cpu_to_le32(arg->sig_size);
195+
186196
desc->data_size = cpu_to_le64(inode->i_size);
187197

188198
/* Prepare the Merkle tree parameters */
@@ -238,6 +248,10 @@ static int enable_verity(struct file *filp,
238248
goto rollback;
239249
}
240250

251+
if (arg->sig_size)
252+
pr_debug("Storing a %u-byte PKCS#7 signature alongside the file\n",
253+
arg->sig_size);
254+
241255
/*
242256
* Tell the filesystem to finish enabling verity on the file.
243257
* Serialized with ->begin_enable_verity() by the inode lock.
@@ -304,8 +318,8 @@ int fsverity_ioctl_enable(struct file *filp, const void __user *uarg)
304318
if (arg.salt_size > FIELD_SIZEOF(struct fsverity_descriptor, salt))
305319
return -EMSGSIZE;
306320

307-
if (arg.sig_size)
308-
return -EINVAL;
321+
if (arg.sig_size > FS_VERITY_MAX_SIGNATURE_SIZE)
322+
return -EMSGSIZE;
309323

310324
/*
311325
* Require a regular file with write access. But the actual fd must

‎fs/verity/fsverity_private.h‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,41 @@ struct fsverity_info {
7575
};
7676

7777
/*
78-
* Merkle tree properties. The file measurement is the hash of this structure.
78+
* Merkle tree properties. The file measurement is the hash of this structure
79+
* excluding the signature and with the sig_size field set to 0.
7980
*/
8081
struct fsverity_descriptor {
8182
__u8 version; /* must be 1 */
8283
__u8 hash_algorithm; /* Merkle tree hash algorithm */
8384
__u8 log_blocksize; /* log2 of size of data and tree blocks */
8485
__u8 salt_size; /* size of salt in bytes; 0 if none */
85-
__le32 sig_size; /* reserved, must be 0 */
86+
__le32 sig_size; /* size of signature in bytes; 0 if none */
8687
__le64 data_size; /* size of file the Merkle tree is built over */
8788
__u8 root_hash[64]; /* Merkle tree root hash */
8889
__u8 salt[32]; /* salt prepended to each hashed block */
8990
__u8 __reserved[144]; /* must be 0's */
91+
__u8 signature[]; /* optional PKCS#7 signature */
9092
};
9193

9294
/* Arbitrary limit to bound the kmalloc() size. Can be changed. */
9395
#define FS_VERITY_MAX_DESCRIPTOR_SIZE 16384
9496

97+
#define FS_VERITY_MAX_SIGNATURE_SIZE (FS_VERITY_MAX_DESCRIPTOR_SIZE - \
98+
sizeof(struct fsverity_descriptor))
99+
100+
/*
101+
* Format in which verity file measurements are signed. This is the same as
102+
* 'struct fsverity_digest', except here some magic bytes are prepended to
103+
* provide some context about what is being signed in case the same key is used
104+
* for non-fsverity purposes, and here the fields have fixed endianness.
105+
*/
106+
struct fsverity_signed_digest {
107+
char magic[8]; /* must be "FSVerity" */
108+
__le16 digest_algorithm;
109+
__le16 digest_size;
110+
__u8 digest[];
111+
};
112+
95113
/* hash_algs.c */
96114

97115
extern struct fsverity_hash_alg fsverity_hash_algs[];
@@ -127,7 +145,7 @@ int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
127145
const u8 *salt, size_t salt_size);
128146

129147
struct fsverity_info *fsverity_create_info(const struct inode *inode,
130-
const void *desc, size_t desc_size);
148+
void *desc, size_t desc_size);
131149

132150
void fsverity_set_info(struct inode *inode, struct fsverity_info *vi);
133151

@@ -136,8 +154,32 @@ void fsverity_free_info(struct fsverity_info *vi);
136154
int __init fsverity_init_info_cache(void);
137155
void __init fsverity_exit_info_cache(void);
138156

157+
/* signature.c */
158+
159+
#ifdef CONFIG_FS_VERITY_BUILTIN_SIGNATURES
160+
int fsverity_verify_signature(const struct fsverity_info *vi,
161+
const struct fsverity_descriptor *desc,
162+
size_t desc_size);
163+
164+
int __init fsverity_init_signature(void);
165+
#else /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
166+
static inline int
167+
fsverity_verify_signature(const struct fsverity_info *vi,
168+
const struct fsverity_descriptor *desc,
169+
size_t desc_size)
170+
{
171+
return 0;
172+
}
173+
174+
static inline int fsverity_init_signature(void)
175+
{
176+
return 0;
177+
}
178+
#endif /* !CONFIG_FS_VERITY_BUILTIN_SIGNATURES */
179+
139180
/* verify.c */
140181

141182
int __init fsverity_init_workqueue(void);
183+
void __init fsverity_exit_workqueue(void);
142184

143185
#endif /* _FSVERITY_PRIVATE_H */

‎fs/verity/init.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@ static int __init fsverity_init(void)
4545
if (err)
4646
goto err_exit_info_cache;
4747

48+
err = fsverity_init_signature();
49+
if (err)
50+
goto err_exit_workqueue;
51+
4852
pr_debug("Initialized fs-verity\n");
4953
return 0;
5054

55+
err_exit_workqueue:
56+
fsverity_exit_workqueue();
5157
err_exit_info_cache:
5258
fsverity_exit_info_cache();
5359
return err;

‎fs/verity/open.c‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,32 @@ int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
122122
return err;
123123
}
124124

125-
/* Compute the file measurement by hashing the fsverity_descriptor. */
125+
/*
126+
* Compute the file measurement by hashing the fsverity_descriptor excluding the
127+
* signature and with the sig_size field set to 0.
128+
*/
126129
static int compute_file_measurement(const struct fsverity_hash_alg *hash_alg,
127-
const struct fsverity_descriptor *desc,
130+
struct fsverity_descriptor *desc,
128131
u8 *measurement)
129132
{
130-
return fsverity_hash_buffer(hash_alg, desc, sizeof(*desc), measurement);
133+
__le32 sig_size = desc->sig_size;
134+
int err;
135+
136+
desc->sig_size = 0;
137+
err = fsverity_hash_buffer(hash_alg, desc, sizeof(*desc), measurement);
138+
desc->sig_size = sig_size;
139+
140+
return err;
131141
}
132142

133143
/*
134144
* Validate the given fsverity_descriptor and create a new fsverity_info from
135-
* it.
145+
* it. The signature (if present) is also checked.
136146
*/
137147
struct fsverity_info *fsverity_create_info(const struct inode *inode,
138-
const void *_desc, size_t desc_size)
148+
void *_desc, size_t desc_size)
139149
{
140-
const struct fsverity_descriptor *desc = _desc;
150+
struct fsverity_descriptor *desc = _desc;
141151
struct fsverity_info *vi;
142152
int err;
143153

@@ -153,8 +163,7 @@ struct fsverity_info *fsverity_create_info(const struct inode *inode,
153163
return ERR_PTR(-EINVAL);
154164
}
155165

156-
if (desc->sig_size ||
157-
memchr_inv(desc->__reserved, 0, sizeof(desc->__reserved))) {
166+
if (memchr_inv(desc->__reserved, 0, sizeof(desc->__reserved))) {
158167
fsverity_err(inode, "Reserved bits set in descriptor");
159168
return ERR_PTR(-EINVAL);
160169
}
@@ -198,6 +207,8 @@ struct fsverity_info *fsverity_create_info(const struct inode *inode,
198207
pr_debug("Computed file measurement: %s:%*phN\n",
199208
vi->tree_params.hash_alg->name,
200209
vi->tree_params.digest_size, vi->measurement);
210+
211+
err = fsverity_verify_signature(vi, desc, desc_size);
201212
out:
202213
if (err) {
203214
fsverity_free_info(vi);

‎fs/verity/signature.c‎

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
*/
19+
static 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+
*/
27+
static 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+
*/
37+
int 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
96+
static struct ctl_table_header *fsverity_sysctl_header;
97+
98+
static const struct ctl_path fsverity_sysctl_path[] = {
99+
{ .procname = "fs", },
100+
{ .procname = "verity", },
101+
{ }
102+
};
103+
104+
static 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+
117+
static 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 */
128+
static inline int __init fsverity_sysctl_init(void)
129+
{
130+
return 0;
131+
}
132+
#endif /* !CONFIG_SYSCTL */
133+
134+
int __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+
154+
err_put_ring:
155+
key_put(ring);
156+
return err;
157+
}

0 commit comments

Comments
 (0)