Skip to content

Commit c3558a6

Browse files
committed
Merge tag 'fsverity-for-linus' of git://git.kernel.org/pub/scm/fs/fsverity/linux
Pull fsverity updates from Eric Biggers: "Several cleanups and fixes for fs/verity/, including a couple minor fixes to the changes in 6.3 that added support for Merkle tree block sizes less than the page size" * tag 'fsverity-for-linus' of git://git.kernel.org/pub/scm/fs/fsverity/linux: fsverity: reject FS_IOC_ENABLE_VERITY on mode 3 fds fsverity: explicitly check for buffer overflow in build_merkle_tree() fsverity: use WARN_ON_ONCE instead of WARN_ON fs-verity: simplify sysctls with register_sysctl() fs/buffer.c: use b_folio for fsverity work
2 parents dbe0e78 + 0483913 commit c3558a6

File tree

6 files changed

+30
-21
lines changed

6 files changed

+30
-21
lines changed

fs/buffer.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,20 +308,19 @@ static void verify_bh(struct work_struct *work)
308308
struct buffer_head *bh = ctx->bh;
309309
bool valid;
310310

311-
valid = fsverity_verify_blocks(page_folio(bh->b_page), bh->b_size,
312-
bh_offset(bh));
311+
valid = fsverity_verify_blocks(bh->b_folio, bh->b_size, bh_offset(bh));
313312
end_buffer_async_read(bh, valid);
314313
kfree(ctx);
315314
}
316315

317316
static bool need_fsverity(struct buffer_head *bh)
318317
{
319-
struct page *page = bh->b_page;
320-
struct inode *inode = page->mapping->host;
318+
struct folio *folio = bh->b_folio;
319+
struct inode *inode = folio->mapping->host;
321320

322321
return fsverity_active(inode) &&
323322
/* needed by ext4 */
324-
page->index < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
323+
folio->index < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
325324
}
326325

327326
static void decrypt_bh(struct work_struct *work)

fs/verity/enable.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
struct block_buffer {
1515
u32 filled;
16+
bool is_root_hash;
1617
u8 *data;
1718
};
1819

@@ -24,6 +25,14 @@ static int hash_one_block(struct inode *inode,
2425
struct block_buffer *next = cur + 1;
2526
int err;
2627

28+
/*
29+
* Safety check to prevent a buffer overflow in case of a filesystem bug
30+
* that allows the file size to change despite deny_write_access(), or a
31+
* bug in the Merkle tree logic itself
32+
*/
33+
if (WARN_ON_ONCE(next->is_root_hash && next->filled != 0))
34+
return -EINVAL;
35+
2736
/* Zero-pad the block if it's shorter than the block size. */
2837
memset(&cur->data[cur->filled], 0, params->block_size - cur->filled);
2938

@@ -97,6 +106,7 @@ static int build_merkle_tree(struct file *filp,
97106
}
98107
}
99108
buffers[num_levels].data = root_hash;
109+
buffers[num_levels].is_root_hash = true;
100110

101111
BUILD_BUG_ON(sizeof(level_offset) != sizeof(params->level_start));
102112
memcpy(level_offset, params->level_start, sizeof(level_offset));
@@ -165,7 +175,7 @@ static int build_merkle_tree(struct file *filp,
165175
}
166176
}
167177
/* The root hash was filled by the last call to hash_one_block(). */
168-
if (WARN_ON(buffers[num_levels].filled != params->digest_size)) {
178+
if (WARN_ON_ONCE(buffers[num_levels].filled != params->digest_size)) {
169179
err = -EINVAL;
170180
goto out;
171181
}
@@ -277,7 +287,7 @@ static int enable_verity(struct file *filp,
277287
fsverity_err(inode, "%ps() failed with err %d",
278288
vops->end_enable_verity, err);
279289
fsverity_free_info(vi);
280-
} else if (WARN_ON(!IS_VERITY(inode))) {
290+
} else if (WARN_ON_ONCE(!IS_VERITY(inode))) {
281291
err = -EINVAL;
282292
fsverity_free_info(vi);
283293
} else {
@@ -347,6 +357,13 @@ int fsverity_ioctl_enable(struct file *filp, const void __user *uarg)
347357
err = file_permission(filp, MAY_WRITE);
348358
if (err)
349359
return err;
360+
/*
361+
* __kernel_read() is used while building the Merkle tree. So, we can't
362+
* allow file descriptors that were opened for ioctl access only, using
363+
* the special nonstandard access mode 3. O_RDONLY only, please!
364+
*/
365+
if (!(filp->f_mode & FMODE_READ))
366+
return -EBADF;
350367

351368
if (IS_APPEND(inode))
352369
return -EPERM;

fs/verity/hash_algs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
8484
}
8585

8686
err = -EINVAL;
87-
if (WARN_ON(alg->digest_size != crypto_ahash_digestsize(tfm)))
87+
if (WARN_ON_ONCE(alg->digest_size != crypto_ahash_digestsize(tfm)))
8888
goto err_free_tfm;
89-
if (WARN_ON(alg->block_size != crypto_ahash_blocksize(tfm)))
89+
if (WARN_ON_ONCE(alg->block_size != crypto_ahash_blocksize(tfm)))
9090
goto err_free_tfm;
9191

9292
err = mempool_init_kmalloc_pool(&alg->req_pool, 1,

fs/verity/open.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int fsverity_init_merkle_tree_params(struct merkle_tree_params *params,
8383
params->log_blocks_per_page = PAGE_SHIFT - log_blocksize;
8484
params->blocks_per_page = 1 << params->log_blocks_per_page;
8585

86-
if (WARN_ON(!is_power_of_2(params->digest_size))) {
86+
if (WARN_ON_ONCE(!is_power_of_2(params->digest_size))) {
8787
err = -EINVAL;
8888
goto out_err;
8989
}

fs/verity/signature.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,6 @@ int fsverity_verify_signature(const struct fsverity_info *vi,
8888
#ifdef CONFIG_SYSCTL
8989
static struct ctl_table_header *fsverity_sysctl_header;
9090

91-
static const struct ctl_path fsverity_sysctl_path[] = {
92-
{ .procname = "fs", },
93-
{ .procname = "verity", },
94-
{ }
95-
};
96-
9791
static struct ctl_table fsverity_sysctl_table[] = {
9892
{
9993
.procname = "require_signatures",
@@ -109,8 +103,7 @@ static struct ctl_table fsverity_sysctl_table[] = {
109103

110104
static int __init fsverity_sysctl_init(void)
111105
{
112-
fsverity_sysctl_header = register_sysctl_paths(fsverity_sysctl_path,
113-
fsverity_sysctl_table);
106+
fsverity_sysctl_header = register_sysctl("fs/verity", fsverity_sysctl_table);
114107
if (!fsverity_sysctl_header) {
115108
pr_err("sysctl registration failed!\n");
116109
return -ENOMEM;

include/linux/fsverity.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,18 @@ static inline int fsverity_ioctl_read_metadata(struct file *filp,
233233
static inline bool fsverity_verify_blocks(struct folio *folio, size_t len,
234234
size_t offset)
235235
{
236-
WARN_ON(1);
236+
WARN_ON_ONCE(1);
237237
return false;
238238
}
239239

240240
static inline void fsverity_verify_bio(struct bio *bio)
241241
{
242-
WARN_ON(1);
242+
WARN_ON_ONCE(1);
243243
}
244244

245245
static inline void fsverity_enqueue_verify_work(struct work_struct *work)
246246
{
247-
WARN_ON(1);
247+
WARN_ON_ONCE(1);
248248
}
249249

250250
#endif /* !CONFIG_FS_VERITY */

0 commit comments

Comments
 (0)