Skip to content

Commit 13e2408

Browse files
committed
fsverity: simplify error handling in verify_data_block()
Clean up the error handling in verify_data_block() to (a) eliminate the 'err' variable which has caused some confusion because the function actually returns a bool, (b) reduce the compiled code size slightly, and (c) execute one fewer branch in the success case. Link: https://lore.kernel.org/r/20230604022312.48532-1-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@google.com>
1 parent d1f0c5e commit 13e2408

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

fs/verity/verify.c

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,6 @@
1212

1313
static struct workqueue_struct *fsverity_read_workqueue;
1414

15-
static inline int cmp_hashes(const struct fsverity_info *vi,
16-
const u8 *want_hash, const u8 *real_hash,
17-
u64 data_pos, int level)
18-
{
19-
const unsigned int hsize = vi->tree_params.digest_size;
20-
21-
if (memcmp(want_hash, real_hash, hsize) == 0)
22-
return 0;
23-
24-
fsverity_err(vi->inode,
25-
"FILE CORRUPTED! pos=%llu, level=%d, want_hash=%s:%*phN, real_hash=%s:%*phN",
26-
data_pos, level,
27-
vi->tree_params.hash_alg->name, hsize, want_hash,
28-
vi->tree_params.hash_alg->name, hsize, real_hash);
29-
return -EBADMSG;
30-
}
31-
3215
/*
3316
* Returns true if the hash block with index @hblock_idx in the tree, located in
3417
* @hpage, has already been verified.
@@ -131,7 +114,6 @@ verify_data_block(struct inode *inode, struct fsverity_info *vi,
131114
* index of that block's hash within the current level.
132115
*/
133116
u64 hidx = data_pos >> params->log_blocksize;
134-
int err;
135117

136118
/* Up to 1 + FS_VERITY_MAX_LEVELS pages may be mapped at once */
137119
BUILD_BUG_ON(1 + FS_VERITY_MAX_LEVELS > KM_MAX_IDX);
@@ -191,11 +173,10 @@ verify_data_block(struct inode *inode, struct fsverity_info *vi,
191173
hpage_idx, level == 0 ? min(max_ra_pages,
192174
params->tree_pages - hpage_idx) : 0);
193175
if (IS_ERR(hpage)) {
194-
err = PTR_ERR(hpage);
195176
fsverity_err(inode,
196-
"Error %d reading Merkle tree page %lu",
197-
err, hpage_idx);
198-
goto out;
177+
"Error %ld reading Merkle tree page %lu",
178+
PTR_ERR(hpage), hpage_idx);
179+
goto error;
199180
}
200181
haddr = kmap_local_page(hpage) + hblock_offset_in_page;
201182
if (is_hash_block_verified(vi, hpage, hblock_idx)) {
@@ -221,12 +202,10 @@ verify_data_block(struct inode *inode, struct fsverity_info *vi,
221202
unsigned long hblock_idx = hblocks[level - 1].index;
222203
unsigned int hoffset = hblocks[level - 1].hoffset;
223204

224-
err = fsverity_hash_block(params, inode, haddr, real_hash);
225-
if (err)
226-
goto out;
227-
err = cmp_hashes(vi, want_hash, real_hash, data_pos, level - 1);
228-
if (err)
229-
goto out;
205+
if (fsverity_hash_block(params, inode, haddr, real_hash) != 0)
206+
goto error;
207+
if (memcmp(want_hash, real_hash, hsize) != 0)
208+
goto corrupted;
230209
/*
231210
* Mark the hash block as verified. This must be atomic and
232211
* idempotent, as the same hash block might be verified by
@@ -243,16 +222,24 @@ verify_data_block(struct inode *inode, struct fsverity_info *vi,
243222
}
244223

245224
/* Finally, verify the data block. */
246-
err = fsverity_hash_block(params, inode, data, real_hash);
247-
if (err)
248-
goto out;
249-
err = cmp_hashes(vi, want_hash, real_hash, data_pos, -1);
250-
out:
225+
if (fsverity_hash_block(params, inode, data, real_hash) != 0)
226+
goto error;
227+
if (memcmp(want_hash, real_hash, hsize) != 0)
228+
goto corrupted;
229+
return true;
230+
231+
corrupted:
232+
fsverity_err(inode,
233+
"FILE CORRUPTED! pos=%llu, level=%d, want_hash=%s:%*phN, real_hash=%s:%*phN",
234+
data_pos, level - 1,
235+
params->hash_alg->name, hsize, want_hash,
236+
params->hash_alg->name, hsize, real_hash);
237+
error:
251238
for (; level > 0; level--) {
252239
kunmap_local(hblocks[level - 1].addr);
253240
put_page(hblocks[level - 1].page);
254241
}
255-
return err == 0;
242+
return false;
256243
}
257244

258245
static bool

0 commit comments

Comments
 (0)