Skip to content

Commit a4bbf53

Browse files
committed
fsverity: simplify fsverity_get_digest()
Instead of looking up the algorithm by name in hash_algo_name[] to get its hash_algo ID, just store the hash_algo ID in the fsverity_hash_alg struct. Verify at boot time that every fsverity_hash_alg has a valid hash_algo ID with matching digest size. Remove an unnecessary memset() of the whole digest array to 0 before the digest is copied into it. Finally, remove the pr_debug statement. There is already a pr_debug for the fsverity digest when the file is opened. Signed-off-by: Eric Biggers <[email protected]> Reviewed-by: Mimi Zohar <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 98dc08b commit a4bbf53

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

fs/verity/fsverity_private.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ struct fsverity_hash_alg {
3232
unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */
3333
unsigned int block_size; /* block size in bytes, e.g. 64 for SHA-256 */
3434
mempool_t req_pool; /* mempool with a preallocated hash request */
35+
/*
36+
* The HASH_ALGO_* constant for this algorithm. This is different from
37+
* FS_VERITY_HASH_ALG_*, which uses a different numbering scheme.
38+
*/
39+
enum hash_algo algo_id;
3540
};
3641

3742
/* Merkle tree parameters: hash algorithm, initial hash state, and topology */

fs/verity/hash_algs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ struct fsverity_hash_alg fsverity_hash_algs[] = {
1616
.name = "sha256",
1717
.digest_size = SHA256_DIGEST_SIZE,
1818
.block_size = SHA256_BLOCK_SIZE,
19+
.algo_id = HASH_ALGO_SHA256,
1920
},
2021
[FS_VERITY_HASH_ALG_SHA512] = {
2122
.name = "sha512",
2223
.digest_size = SHA512_DIGEST_SIZE,
2324
.block_size = SHA512_BLOCK_SIZE,
25+
.algo_id = HASH_ALGO_SHA512,
2426
},
2527
};
2628

@@ -324,5 +326,9 @@ void __init fsverity_check_hash_algs(void)
324326
*/
325327
BUG_ON(!is_power_of_2(alg->digest_size));
326328
BUG_ON(!is_power_of_2(alg->block_size));
329+
330+
/* Verify that there is a valid mapping to HASH_ALGO_*. */
331+
BUG_ON(alg->algo_id == 0);
332+
BUG_ON(alg->digest_size != hash_digest_size[alg->algo_id]);
327333
}
328334
}

fs/verity/measure.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ EXPORT_SYMBOL_GPL(fsverity_ioctl_measure);
6565
* @alg: (out) pointer to the hash algorithm enumeration
6666
*
6767
* Return the file hash algorithm and digest of an fsverity protected file.
68-
* Assumption: before calling fsverity_get_digest(), the file must have been
69-
* opened.
68+
* Assumption: before calling this, the file must have been opened.
7069
*
7170
* Return: 0 on success, -errno on failure
7271
*/
@@ -76,27 +75,13 @@ int fsverity_get_digest(struct inode *inode,
7675
{
7776
const struct fsverity_info *vi;
7877
const struct fsverity_hash_alg *hash_alg;
79-
int i;
8078

8179
vi = fsverity_get_info(inode);
8280
if (!vi)
8381
return -ENODATA; /* not a verity file */
8482

8583
hash_alg = vi->tree_params.hash_alg;
86-
memset(digest, 0, FS_VERITY_MAX_DIGEST_SIZE);
87-
88-
/* convert the verity hash algorithm name to a hash_algo_name enum */
89-
i = match_string(hash_algo_name, HASH_ALGO__LAST, hash_alg->name);
90-
if (i < 0)
91-
return -EINVAL;
92-
*alg = i;
93-
94-
if (WARN_ON_ONCE(hash_alg->digest_size != hash_digest_size[*alg]))
95-
return -EINVAL;
9684
memcpy(digest, vi->file_digest, hash_alg->digest_size);
97-
98-
pr_debug("file digest %s:%*phN\n", hash_algo_name[*alg],
99-
hash_digest_size[*alg], digest);
100-
85+
*alg = hash_alg->algo_id;
10186
return 0;
10287
}

0 commit comments

Comments
 (0)