1 // SPDX-License-Identifier: GPL-2.0
3 * Opening fs-verity files
5 * Copyright 2019 Google LLC
8 #include "fsverity_private.h"
11 #include <linux/slab.h>
13 static struct kmem_cache
*fsverity_info_cachep
;
16 * fsverity_init_merkle_tree_params() - initialize Merkle tree parameters
17 * @params: the parameters struct to initialize
18 * @inode: the inode for which the Merkle tree is being built
19 * @hash_algorithm: number of hash algorithm to use
20 * @log_blocksize: log base 2 of block size to use
21 * @salt: pointer to salt (optional)
22 * @salt_size: size of salt, possibly 0
24 * Validate the hash algorithm and block size, then compute the tree topology
25 * (num levels, num blocks in each level, etc.) and initialize @params.
27 * Return: 0 on success, -errno on failure
29 int fsverity_init_merkle_tree_params(struct merkle_tree_params
*params
,
30 const struct inode
*inode
,
31 unsigned int hash_algorithm
,
32 unsigned int log_blocksize
,
33 const u8
*salt
, size_t salt_size
)
35 const struct fsverity_hash_alg
*hash_alg
;
38 u64 blocks_in_level
[FS_VERITY_MAX_LEVELS
];
42 memset(params
, 0, sizeof(*params
));
44 hash_alg
= fsverity_get_hash_alg(inode
, hash_algorithm
);
46 return PTR_ERR(hash_alg
);
47 params
->hash_alg
= hash_alg
;
48 params
->digest_size
= hash_alg
->digest_size
;
50 params
->hashstate
= fsverity_prepare_hash_state(hash_alg
, salt
,
52 if (IS_ERR(params
->hashstate
)) {
53 err
= PTR_ERR(params
->hashstate
);
54 params
->hashstate
= NULL
;
55 fsverity_err(inode
, "Error %d preparing hash state", err
);
60 * fs/verity/ directly assumes that the Merkle tree block size is a
61 * power of 2 less than or equal to PAGE_SIZE. Another restriction
62 * arises from the interaction between fs/verity/ and the filesystems
63 * themselves: filesystems expect to be able to verify a single
64 * filesystem block of data at a time. Therefore, the Merkle tree block
65 * size must also be less than or equal to the filesystem block size.
67 * The above are the only hard limitations, so in theory the Merkle tree
68 * block size could be as small as twice the digest size. However,
69 * that's not useful, and it would result in some unusually deep and
70 * large Merkle trees. So we currently require that the Merkle tree
71 * block size be at least 1024 bytes. That's small enough to test the
72 * sub-page block case on systems with 4K pages, but not too small.
74 if (log_blocksize
< 10 || log_blocksize
> PAGE_SHIFT
||
75 log_blocksize
> inode
->i_blkbits
) {
76 fsverity_warn(inode
, "Unsupported log_blocksize: %u",
81 params
->log_blocksize
= log_blocksize
;
82 params
->block_size
= 1 << log_blocksize
;
83 params
->log_blocks_per_page
= PAGE_SHIFT
- log_blocksize
;
84 params
->blocks_per_page
= 1 << params
->log_blocks_per_page
;
86 if (WARN_ON_ONCE(!is_power_of_2(params
->digest_size
))) {
90 if (params
->block_size
< 2 * params
->digest_size
) {
92 "Merkle tree block size (%u) too small for hash algorithm \"%s\"",
93 params
->block_size
, hash_alg
->name
);
97 params
->log_digestsize
= ilog2(params
->digest_size
);
98 params
->log_arity
= log_blocksize
- params
->log_digestsize
;
99 params
->hashes_per_block
= 1 << params
->log_arity
;
102 * Compute the number of levels in the Merkle tree and create a map from
103 * level to the starting block of that level. Level 'num_levels - 1' is
104 * the root and is stored first. Level 0 is the level directly "above"
105 * the data blocks and is stored last.
108 /* Compute number of levels and the number of blocks in each level */
109 blocks
= ((u64
)inode
->i_size
+ params
->block_size
- 1) >> log_blocksize
;
111 if (params
->num_levels
>= FS_VERITY_MAX_LEVELS
) {
112 fsverity_err(inode
, "Too many levels in Merkle tree");
116 blocks
= (blocks
+ params
->hashes_per_block
- 1) >>
118 blocks_in_level
[params
->num_levels
++] = blocks
;
121 /* Compute the starting block of each level */
123 for (level
= (int)params
->num_levels
- 1; level
>= 0; level
--) {
124 params
->level_start
[level
] = offset
;
125 offset
+= blocks_in_level
[level
];
129 * With block_size != PAGE_SIZE, an in-memory bitmap will need to be
130 * allocated to track the "verified" status of hash blocks. Don't allow
131 * this bitmap to get too large. For now, limit it to 1 MiB, which
132 * limits the file size to about 4.4 TB with SHA-256 and 4K blocks.
134 * Together with the fact that the data, and thus also the Merkle tree,
135 * cannot have more than ULONG_MAX pages, this implies that hash block
136 * indices can always fit in an 'unsigned long'. But to be safe, we
137 * explicitly check for that too. Note, this is only for hash block
138 * indices; data block indices might not fit in an 'unsigned long'.
140 if ((params
->block_size
!= PAGE_SIZE
&& offset
> 1 << 23) ||
141 offset
> ULONG_MAX
) {
142 fsverity_err(inode
, "Too many blocks in Merkle tree");
147 params
->tree_size
= offset
<< log_blocksize
;
148 params
->tree_pages
= PAGE_ALIGN(params
->tree_size
) >> PAGE_SHIFT
;
152 kfree(params
->hashstate
);
153 memset(params
, 0, sizeof(*params
));
158 * Compute the file digest by hashing the fsverity_descriptor excluding the
159 * builtin signature and with the sig_size field set to 0.
161 static int compute_file_digest(const struct fsverity_hash_alg
*hash_alg
,
162 struct fsverity_descriptor
*desc
,
165 __le32 sig_size
= desc
->sig_size
;
169 err
= fsverity_hash_buffer(hash_alg
, desc
, sizeof(*desc
), file_digest
);
170 desc
->sig_size
= sig_size
;
176 * Create a new fsverity_info from the given fsverity_descriptor (with optional
177 * appended builtin signature), and check the signature if present. The
178 * fsverity_descriptor must have already undergone basic validation.
180 struct fsverity_info
*fsverity_create_info(const struct inode
*inode
,
181 struct fsverity_descriptor
*desc
)
183 struct fsverity_info
*vi
;
186 vi
= kmem_cache_zalloc(fsverity_info_cachep
, GFP_KERNEL
);
188 return ERR_PTR(-ENOMEM
);
191 err
= fsverity_init_merkle_tree_params(&vi
->tree_params
, inode
,
192 desc
->hash_algorithm
,
194 desc
->salt
, desc
->salt_size
);
197 "Error %d initializing Merkle tree parameters",
202 memcpy(vi
->root_hash
, desc
->root_hash
, vi
->tree_params
.digest_size
);
204 err
= compute_file_digest(vi
->tree_params
.hash_alg
, desc
,
207 fsverity_err(inode
, "Error %d computing file digest", err
);
211 err
= fsverity_verify_signature(vi
, desc
->signature
,
212 le32_to_cpu(desc
->sig_size
));
216 if (vi
->tree_params
.block_size
!= PAGE_SIZE
) {
218 * When the Merkle tree block size and page size differ, we use
219 * a bitmap to keep track of which hash blocks have been
220 * verified. This bitmap must contain one bit per hash block,
221 * including alignment to a page boundary at the end.
223 * Eventually, to support extremely large files in an efficient
224 * way, it might be necessary to make pages of this bitmap
225 * reclaimable. But for now, simply allocating the whole bitmap
226 * is a simple solution that works well on the files on which
227 * fsverity is realistically used. E.g., with SHA-256 and 4K
228 * blocks, a 100MB file only needs a 24-byte bitmap, and the
229 * bitmap for any file under 17GB fits in a 4K page.
231 unsigned long num_bits
=
232 vi
->tree_params
.tree_pages
<<
233 vi
->tree_params
.log_blocks_per_page
;
235 vi
->hash_block_verified
= kvcalloc(BITS_TO_LONGS(num_bits
),
236 sizeof(unsigned long),
238 if (!vi
->hash_block_verified
) {
247 fsverity_free_info(vi
);
251 void fsverity_set_info(struct inode
*inode
, struct fsverity_info
*vi
)
254 * Multiple tasks may race to set ->i_verity_info, so use
255 * cmpxchg_release(). This pairs with the smp_load_acquire() in
256 * fsverity_get_info(). I.e., here we publish ->i_verity_info with a
257 * RELEASE barrier so that other tasks can ACQUIRE it.
259 if (cmpxchg_release(&inode
->i_verity_info
, NULL
, vi
) != NULL
) {
260 /* Lost the race, so free the fsverity_info we allocated. */
261 fsverity_free_info(vi
);
263 * Afterwards, the caller may access ->i_verity_info directly,
264 * so make sure to ACQUIRE the winning fsverity_info.
266 (void)fsverity_get_info(inode
);
270 void fsverity_free_info(struct fsverity_info
*vi
)
274 kfree(vi
->tree_params
.hashstate
);
275 kvfree(vi
->hash_block_verified
);
276 kmem_cache_free(fsverity_info_cachep
, vi
);
279 static bool validate_fsverity_descriptor(struct inode
*inode
,
280 const struct fsverity_descriptor
*desc
,
283 if (desc_size
< sizeof(*desc
)) {
284 fsverity_err(inode
, "Unrecognized descriptor size: %zu bytes",
289 if (desc
->version
!= 1) {
290 fsverity_err(inode
, "Unrecognized descriptor version: %u",
295 if (memchr_inv(desc
->__reserved
, 0, sizeof(desc
->__reserved
))) {
296 fsverity_err(inode
, "Reserved bits set in descriptor");
300 if (desc
->salt_size
> sizeof(desc
->salt
)) {
301 fsverity_err(inode
, "Invalid salt_size: %u", desc
->salt_size
);
305 if (le64_to_cpu(desc
->data_size
) != inode
->i_size
) {
307 "Wrong data_size: %llu (desc) != %lld (inode)",
308 le64_to_cpu(desc
->data_size
), inode
->i_size
);
312 if (le32_to_cpu(desc
->sig_size
) > desc_size
- sizeof(*desc
)) {
313 fsverity_err(inode
, "Signature overflows verity descriptor");
321 * Read the inode's fsverity_descriptor (with optional appended builtin
322 * signature) from the filesystem, and do basic validation of it.
324 int fsverity_get_descriptor(struct inode
*inode
,
325 struct fsverity_descriptor
**desc_ret
)
328 struct fsverity_descriptor
*desc
;
330 res
= inode
->i_sb
->s_vop
->get_verity_descriptor(inode
, NULL
, 0);
333 "Error %d getting verity descriptor size", res
);
336 if (res
> FS_VERITY_MAX_DESCRIPTOR_SIZE
) {
337 fsverity_err(inode
, "Verity descriptor is too large (%d bytes)",
341 desc
= kmalloc(res
, GFP_KERNEL
);
344 res
= inode
->i_sb
->s_vop
->get_verity_descriptor(inode
, desc
, res
);
346 fsverity_err(inode
, "Error %d reading verity descriptor", res
);
351 if (!validate_fsverity_descriptor(inode
, desc
, res
)) {
360 /* Ensure the inode has an ->i_verity_info */
361 static int ensure_verity_info(struct inode
*inode
)
363 struct fsverity_info
*vi
= fsverity_get_info(inode
);
364 struct fsverity_descriptor
*desc
;
370 err
= fsverity_get_descriptor(inode
, &desc
);
374 vi
= fsverity_create_info(inode
, desc
);
380 fsverity_set_info(inode
, vi
);
387 int __fsverity_file_open(struct inode
*inode
, struct file
*filp
)
389 if (filp
->f_mode
& FMODE_WRITE
)
391 return ensure_verity_info(inode
);
393 EXPORT_SYMBOL_GPL(__fsverity_file_open
);
395 int __fsverity_prepare_setattr(struct dentry
*dentry
, struct iattr
*attr
)
397 if (attr
->ia_valid
& ATTR_SIZE
)
401 EXPORT_SYMBOL_GPL(__fsverity_prepare_setattr
);
403 void __fsverity_cleanup_inode(struct inode
*inode
)
405 fsverity_free_info(inode
->i_verity_info
);
406 inode
->i_verity_info
= NULL
;
408 EXPORT_SYMBOL_GPL(__fsverity_cleanup_inode
);
410 void __init
fsverity_init_info_cache(void)
412 fsverity_info_cachep
= KMEM_CACHE_USERCOPY(
414 SLAB_RECLAIM_ACCOUNT
| SLAB_PANIC
,