Skip to contentSkip to conversation

fsverity: Fix bpf_get_fsverity_digest() dynptr assumptions

PATCH Applied Eric Biggers 4 participants 9 messages Jul 31 – Aug 4

Subsystems (3)

v1 of 2 Applied 1/1 Latest is v2 →

lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

PATCH 1/1 fsverity: Fix bpf_get_fsverity_digest() dynptr assumptions

The BPF verifier and the dynptr abstraction ensure that the memory space referenced by a dynptr remains valid. They do not, however, provide any guarantee that the contents of the memory are stable. kfuncs are expected to remain memory-safe even if concurrent modifications occur.

bpf_get_fsverity_digest() didn't follow that: it could crash if arg->digest_size was concurrently modified.

Fix that by using the known-good value hash_alg->digest_size instead.

Also correctly handle sizes over INT_MAX, which previously caused an integer overflow and crash. __bpf_dynptr_size() returns a u64.

Fixes 67814c00de31 ("bpf, fsverity: Add kfunc bpf_get_fsverity_digest")
---
 fs/verity/measure.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/verity/measure.c +6 -4
index cfe2d5e535f9..f8b3526af004 100644
@@ -122,11 +122,11 @@ __bpf_kfunc int bpf_get_fsverity_digest(struct file *file, const struct bpf_dynp
 {
 	const struct bpf_dynptr_kern *digest_ptr = (struct bpf_dynptr_kern *)digest_p;
 	const struct inode *inode = file_inode(file);
-	u32 dynptr_sz = __bpf_dynptr_size(digest_ptr);
+	u64 dynptr_sz = __bpf_dynptr_size(digest_ptr);
 	struct fsverity_digest *arg;
 	const struct fsverity_info *vi;
 	const struct fsverity_hash_alg *hash_alg;
-	int out_digest_sz;
+	u64 out_digest_sz;
 
 	if (dynptr_sz < sizeof(struct fsverity_digest))
 		return -EINVAL;
@@ -150,11 +150,13 @@ __bpf_kfunc int bpf_get_fsverity_digest(struct file *file, const struct bpf_dynp
 	out_digest_sz = dynptr_sz - sizeof(struct fsverity_digest);
 
 	/* copy digest */
-	memcpy(arg->digest, vi->file_digest,  min_t(int, hash_alg->digest_size, out_digest_sz));
+	memcpy(arg->digest, vi->file_digest,
+	       min(hash_alg->digest_size, out_digest_sz));
 
 	/* fill the extra buffer with zeros */
 	if (out_digest_sz > hash_alg->digest_size)
-		memset(arg->digest + arg->digest_size, 0, out_digest_sz - hash_alg->digest_size);
+		memset(arg->digest + hash_alg->digest_size, 0,
+		       out_digest_sz - hash_alg->digest_size);
 
 	return 0;
 }

base-commit: f5098b6bae761e346ebcd9da7f95622c04733cff

Signature · 1 line
2.55.0.508.g3f0d502094-goog
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Eric Biggers

The BPF verifier and the dynptr abstraction ensure that the memory space referenced by a dynptr remains valid. They do not, however, provide any guarantee that the contents of the memory are stable. kfuncs are expected to remain memory-safe even if concurrent modifications occur.

bpf_get_fsverity_digest() didn't follow that: it could crash if arg->digest_size was concurrently modified.

Fix that by using the known-good value hash_alg->digest_size instead.

This makes sense.

Quoted 3 lines

Also correctly handle sizes over INT_MAX, which previously caused an integer overflow and crash. __bpf_dynptr_size() returns a u64.

I guess this occurs when using file dynptrs? Otherwise the size is capped to DYNPTR_SIZE_MASK.

Quoted 4 lines
Fixes 67814c00de31 ("bpf, fsverity: Add kfunc bpf_get_fsverity_digest")

---

Quoted 1 line

[...]

author maintainer
#2 lore raw
Quoted 6 lines · Kumar Kartikeya Dwivedi
Quoted 3 lines

Also correctly handle sizes over INT_MAX, which previously caused an integer overflow and crash. __bpf_dynptr_size() returns a u64.

I guess this occurs when using file dynptrs? Otherwise the size is capped to DYNPTR_SIZE_MASK.

Yes, though it looks like __bpf_dynptr_data_rw() excludes file dynptrs. So this particular issue might be unreachable. Still seems like a good idea to match the u64 type that __bpf_dynptr_size() returns though.

- Eric

#3 lore raw
Quoted 5 lines · Eric Biggers
Quoted 4 lines · Kumar Kartikeya Dwivedi
Quoted 3 lines

Also correctly handle sizes over INT_MAX, which previously caused an integer overflow and crash. __bpf_dynptr_size() returns a u64.

Quoted 7 lines
Quoted 2 lines

I guess this occurs when using file dynptrs? Otherwise the size is capped to DYNPTR_SIZE_MASK.

Yes, though it looks like __bpf_dynptr_data_rw() excludes file dynptrs. So this particular issue might be unreachable. Still seems like a good idea to match the u64 type that __bpf_dynptr_size() returns though.

I don't see this in patchwork, so you might have to resend this with [PATCH bpf-next] subject again to trigger CI if you want this to go through bpf tree.

Quoted 1 line

- Eric

#4 lore raw
Quoted 9 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · Eric Biggers
Quoted 6 lines · Kumar Kartikeya Dwivedi
Quoted 3 lines

Also correctly handle sizes over INT_MAX, which previously caused an integer overflow and crash. __bpf_dynptr_size() returns a u64.

I guess this occurs when using file dynptrs? Otherwise the size is capped to DYNPTR_SIZE_MASK.

Quoted 8 lines
Quoted 4 lines

Yes, though it looks like __bpf_dynptr_data_rw() excludes file dynptrs. So this particular issue might be unreachable. Still seems like a good idea to match the u64 type that __bpf_dynptr_size() returns though.

I don't see this in patchwork, so you might have to resend this with [PATCH bpf-next] subject again to trigger CI if you want this to go through bpf tree.

Sorry, [PATCH bpf], rather.

Quoted 1 line
Quoted 1 line

- Eric

author maintainer
#5 lore raw
Quoted 15 lines · Kumar Kartikeya Dwivedi
Quoted 14 lines · Kumar Kartikeya Dwivedi
Quoted 12 lines · Eric Biggers
Quoted 6 lines · Kumar Kartikeya Dwivedi
Quoted 3 lines

Also correctly handle sizes over INT_MAX, which previously caused an integer overflow and crash. __bpf_dynptr_size() returns a u64.

I guess this occurs when using file dynptrs? Otherwise the size is capped to DYNPTR_SIZE_MASK.

Yes, though it looks like __bpf_dynptr_data_rw() excludes file dynptrs. So this particular issue might be unreachable. Still seems like a good idea to match the u64 type that __bpf_dynptr_size() returns though.

Quoted 5 lines
Quoted 3 lines

I don't see this in patchwork, so you might have to resend this with [PATCH bpf-next] subject again to trigger CI if you want this to go through bpf tree.

Sorry, [PATCH bpf], rather.

Sure I can do that.

Would it be okay to also make this function return -EOVERFLOW if out_digest_sz is too small, similar to FS_IOC_MEASURE_VERITY, instead of silently truncating? The silent truncation is a footgun.

- Eric

#6 lore raw
Quoted 21 lines · Eric Biggers
Quoted 19 lines · Kumar Kartikeya Dwivedi
Quoted 17 lines · Kumar Kartikeya Dwivedi
Quoted 5 lines · Eric Biggers
Quoted 4 lines · Kumar Kartikeya Dwivedi
Quoted 3 lines

Also correctly handle sizes over INT_MAX, which previously caused an integer overflow and crash. __bpf_dynptr_size() returns a u64.

Quoted 7 lines
Quoted 2 lines

I guess this occurs when using file dynptrs? Otherwise the size is capped to DYNPTR_SIZE_MASK.

Yes, though it looks like __bpf_dynptr_data_rw() excludes file dynptrs. So this particular issue might be unreachable. Still seems like a good idea to match the u64 type that __bpf_dynptr_size() returns though.

I don't see this in patchwork, so you might have to resend this with [PATCH bpf-next] subject again to trigger CI if you want this to go through bpf tree.

Quoted 7 lines
Quoted 1 line

Sorry, [PATCH bpf], rather.

Sure I can do that.

Would it be okay to also make this function return -EOVERFLOW if out_digest_sz is too small, similar to FS_IOC_MEASURE_VERITY, instead of silently truncating? The silent truncation is a footgun.

Yeah, I think it makes sense. I will also ping Song to ack this on Monday (or your new version).
Thanks

Quoted 2 lines

- Eric

reviewer Acked-by on v1 1/1
#7 lore raw
In reply to Eric Biggers
Quoted 9 lines · Eric Biggers

The BPF verifier and the dynptr abstraction ensure that the memory space referenced by a dynptr remains valid. They do not, however, provide any guarantee that the contents of the memory are stable. kfuncs are expected to remain memory-safe even if concurrent modifications occur.

bpf_get_fsverity_digest() didn't follow that: it could crash if arg->digest_size was concurrently modified.

Quoted 8 lines

Fix that by using the known-good value hash_alg->digest_size instead.

Also correctly handle sizes over INT_MAX, which previously caused an integer overflow and crash. __bpf_dynptr_size() returns a u64.

Fixes 67814c00de31 ("bpf, fsverity: Add kfunc bpf_get_fsverity_digest")

Looks good to me. Thanks for the fix!

#8 lore raw
In reply to Eric Biggers
This patch was applied to bpf/bpf.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:

Hello:

This patch was applied to bpf/bpf.git (master)
by Kumar Kartikeya Dwivedi <[email protected]>:

Quoted 9 lines · On Fri, 31 Jul 2026 00:39:25 +0000 you wrote:

The BPF verifier and the dynptr abstraction ensure that the memory space referenced by a dynptr remains valid. They do not, however, provide any guarantee that the contents of the memory are stable. kfuncs are expected to remain memory-safe even if concurrent modifications occur.

bpf_get_fsverity_digest() didn't follow that: it could crash if arg->digest_size was concurrently modified.

[...]

Here is the summary with links:
- fsverity: Fix bpf_get_fsverity_digest() dynptr assumptions

    https://git.kernel.org/bpf/bpf/c/3e8ec7c03872

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html