#include "fsverity_private.h"
#include <linux/bio.h>
#include <linux/export.h>
#define FS_VERITY_MAX_PENDING_BLOCKS 2
struct fsverity_pending_block {
const void *data;
u64 pos;
u8 real_hash[FS_VERITY_MAX_DIGEST_SIZE];
};
struct fsverity_verification_context {
struct fsverity_info *vi;
int num_pending;
int max_pending;
struct fsverity_pending_block
pending_blocks[FS_VERITY_MAX_PENDING_BLOCKS];
};
static struct workqueue_struct *fsverity_read_workqueue;
void fsverity_readahead(struct fsverity_info *vi, pgoff_t index,
unsigned long nr_pages)
{
struct inode *inode = vi->inode;
const struct merkle_tree_params *params = &vi->tree_params;
u64 start_hidx = (u64)index << params->log_blocks_per_page;
u64 end_hidx =
(((u64)index + nr_pages) << params->log_blocks_per_page) - 1;
int level;
if (!inode->i_sb->s_vop->readahead_merkle_tree)
return;
for (level = 0; level < params->num_levels; level++) {
unsigned long level_start = params->level_start[level];
unsigned long next_start_hidx = start_hidx >> params->log_arity;
unsigned long next_end_hidx = end_hidx >> params->log_arity;
pgoff_t start_idx = (level_start + next_start_hidx) >>
params->log_blocks_per_page;
pgoff_t end_idx = (level_start + next_end_hidx) >>
params->log_blocks_per_page;
inode->i_sb->s_vop->readahead_merkle_tree(
inode, start_idx, end_idx - start_idx + 1);
start_hidx = next_start_hidx;
end_hidx = next_end_hidx;
}
}
EXPORT_SYMBOL_GPL(fsverity_readahead);
static bool is_hash_block_verified(struct fsverity_info *vi, struct page *hpage,
unsigned long hblock_idx)
{
unsigned int blocks_per_page;
unsigned int i;
if (!vi->hash_block_verified)
return PageChecked(hpage);
if (PageChecked(hpage)) {
smp_rmb();
return test_bit(hblock_idx, vi->hash_block_verified);
}
blocks_per_page = vi->tree_params.blocks_per_page;
hblock_idx = round_down(hblock_idx, blocks_per_page);
for (i = 0; i < blocks_per_page; i++)
clear_bit(hblock_idx + i, vi->hash_block_verified);
smp_wmb();
SetPageChecked(hpage);
return false;
}
static bool verify_data_block(struct fsverity_info *vi,
const struct fsverity_pending_block *dblock)
{
struct inode *inode = vi->inode;
const u64 data_pos = dblock->pos;
const struct merkle_tree_params *params = &vi->tree_params;
const unsigned int hsize = params->digest_size;
int level;
u8 _want_hash[FS_VERITY_MAX_DIGEST_SIZE];
const u8 *want_hash;
u8 real_hash[FS_VERITY_MAX_DIGEST_SIZE];
struct {
struct page *page;
const void *addr;
unsigned long index;
unsigned int hoffset;
} hblocks[FS_VERITY_MAX_LEVELS];
trace_fsverity_verify_data_block(inode, params, data_pos);
u64 hidx = data_pos >> params->log_blocksize;
static_assert(FS_VERITY_MAX_PENDING_BLOCKS + FS_VERITY_MAX_LEVELS <=
KM_MAX_IDX);
if (unlikely(data_pos >= inode->i_size)) {
if (memchr_inv(dblock->data, 0, params->block_size)) {
fsverity_err(inode,
"FILE CORRUPTED! Data past EOF is not zeroed");
return false;
}
return true;
}
for (level = 0; level < params->num_levels; level++) {
unsigned long next_hidx;
unsigned long hblock_idx;
pgoff_t hpage_idx;
unsigned int hblock_offset_in_page;
unsigned int hoffset;
struct page *hpage;
const void *haddr;
next_hidx = hidx >> params->log_arity;
hblock_idx = params->level_start[level] + next_hidx;
hpage_idx = hblock_idx >> params->log_blocks_per_page;
hblock_offset_in_page =
(hblock_idx << params->log_blocksize) & ~PAGE_MASK;
hoffset = (hidx << params->log_digestsize) &
(params->block_size - 1);
hpage = inode->i_sb->s_vop->read_merkle_tree_page(inode,
hpage_idx);
if (IS_ERR(hpage)) {
fsverity_err(inode,
"Error %ld reading Merkle tree page %lu",
PTR_ERR(hpage), hpage_idx);
goto error;
}
haddr = kmap_local_page(hpage) + hblock_offset_in_page;
if (is_hash_block_verified(vi, hpage, hblock_idx)) {
memcpy(_want_hash, haddr + hoffset, hsize);
want_hash = _want_hash;
kunmap_local(haddr);
put_page(hpage);
trace_fsverity_merkle_hit(inode, data_pos, hblock_idx,
level,
hoffset >> params->log_digestsize);
goto descend;
}
hblocks[level].page = hpage;
hblocks[level].addr = haddr;
hblocks[level].index = hblock_idx;
hblocks[level].hoffset = hoffset;
hidx = next_hidx;
}
want_hash = vi->root_hash;
descend:
for (; level > 0; level--) {
struct page *hpage = hblocks[level - 1].page;
const void *haddr = hblocks[level - 1].addr;
unsigned long hblock_idx = hblocks[level - 1].index;
unsigned int hoffset = hblocks[level - 1].hoffset;
trace_fsverity_verify_merkle_block(inode, hblock_idx,
level, hoffset >> params->log_digestsize);
fsverity_hash_block(params, haddr, real_hash);
if (memcmp(want_hash, real_hash, hsize) != 0)
goto corrupted;
if (vi->hash_block_verified)
set_bit(hblock_idx, vi->hash_block_verified);
else
SetPageChecked(hpage);
memcpy(_want_hash, haddr + hoffset, hsize);
want_hash = _want_hash;
kunmap_local(haddr);
put_page(hpage);
}
if (memcmp(want_hash, dblock->real_hash, hsize) != 0)
goto corrupted;
return true;
corrupted:
fsverity_err(
inode,
"FILE CORRUPTED! pos=%llu, level=%d, want_hash=%s:%*phN, real_hash=%s:%*phN",
data_pos, level - 1, params->hash_alg->name, hsize, want_hash,
params->hash_alg->name, hsize,
level == 0 ? dblock->real_hash : real_hash);
error:
for (; level > 0; level--) {
kunmap_local(hblocks[level - 1].addr);
put_page(hblocks[level - 1].page);
}
return false;
}
static void
fsverity_init_verification_context(struct fsverity_verification_context *ctx,
struct fsverity_info *vi)
{
ctx->vi = vi;
ctx->num_pending = 0;
if (vi->tree_params.hash_alg->algo_id == HASH_ALGO_SHA256 &&
sha256_finup_2x_is_optimized())
ctx->max_pending = 2;
else
ctx->max_pending = 1;
}
static void
fsverity_clear_pending_blocks(struct fsverity_verification_context *ctx)
{
int i;
for (i = ctx->num_pending - 1; i >= 0; i--) {
kunmap_local(ctx->pending_blocks[i].data);
ctx->pending_blocks[i].data = NULL;
}
ctx->num_pending = 0;
}
static bool
fsverity_verify_pending_blocks(struct fsverity_verification_context *ctx)
{
struct fsverity_info *vi = ctx->vi;
const struct merkle_tree_params *params = &vi->tree_params;
int i;
if (ctx->num_pending == 2) {
sha256_finup_2x(params->hashstate ? ¶ms->hashstate->sha256 :
NULL,
ctx->pending_blocks[0].data,
ctx->pending_blocks[1].data, params->block_size,
ctx->pending_blocks[0].real_hash,
ctx->pending_blocks[1].real_hash);
} else {
for (i = 0; i < ctx->num_pending; i++)
fsverity_hash_block(params, ctx->pending_blocks[i].data,
ctx->pending_blocks[i].real_hash);
}
for (i = 0; i < ctx->num_pending; i++) {
if (!verify_data_block(vi, &ctx->pending_blocks[i]))
return false;
}
fsverity_clear_pending_blocks(ctx);
return true;
}
static bool fsverity_add_data_blocks(struct fsverity_verification_context *ctx,
struct folio *data_folio, size_t len,
size_t offset)
{
struct fsverity_info *vi = ctx->vi;
const struct merkle_tree_params *params = &vi->tree_params;
const unsigned int block_size = params->block_size;
u64 pos = (u64)data_folio->index << PAGE_SHIFT;
if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offset, block_size)))
return false;
if (WARN_ON_ONCE(!folio_test_locked(data_folio) ||
folio_test_uptodate(data_folio)))
return false;
do {
ctx->pending_blocks[ctx->num_pending].data =
kmap_local_folio(data_folio, offset);
ctx->pending_blocks[ctx->num_pending].pos = pos + offset;
if (++ctx->num_pending == ctx->max_pending &&
!fsverity_verify_pending_blocks(ctx))
return false;
offset += block_size;
len -= block_size;
} while (len);
return true;
}
bool fsverity_verify_blocks(struct fsverity_info *vi, struct folio *folio,
size_t len, size_t offset)
{
struct fsverity_verification_context ctx;
fsverity_init_verification_context(&ctx, vi);
if (fsverity_add_data_blocks(&ctx, folio, len, offset) &&
fsverity_verify_pending_blocks(&ctx))
return true;
fsverity_clear_pending_blocks(&ctx);
return false;
}
EXPORT_SYMBOL_GPL(fsverity_verify_blocks);
#ifdef CONFIG_BLOCK
void fsverity_verify_bio(struct fsverity_info *vi, struct bio *bio)
{
struct fsverity_verification_context ctx;
struct folio_iter fi;
fsverity_init_verification_context(&ctx, vi);
bio_for_each_folio_all(fi, bio) {
if (!fsverity_add_data_blocks(&ctx, fi.folio, fi.length,
fi.offset))
goto ioerr;
}
if (!fsverity_verify_pending_blocks(&ctx))
goto ioerr;
return;
ioerr:
fsverity_clear_pending_blocks(&ctx);
bio->bi_status = BLK_STS_IOERR;
}
EXPORT_SYMBOL_GPL(fsverity_verify_bio);
#endif
void fsverity_enqueue_verify_work(struct work_struct *work)
{
queue_work(fsverity_read_workqueue, work);
}
EXPORT_SYMBOL_GPL(fsverity_enqueue_verify_work);
void __init fsverity_init_workqueue(void)
{
fsverity_read_workqueue = alloc_workqueue("fsverity_read_queue",
WQ_HIGHPRI | WQ_PERCPU,
num_online_cpus());
if (!fsverity_read_workqueue)
panic("failed to allocate fsverity_read_queue");
}