Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

bpf: Use sha1() instead of sha1_transform() in bpf_prog_calc_tag()

Now that there's a proper SHA-1 library API, just use that instead of
the low-level SHA-1 compression function. This eliminates the need for
bpf_prog_calc_tag() to implement the SHA-1 padding itself. No
functional change; the computed tags remain the same.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/bpf/20250811201615.564461-1-ebiggers@kernel.org

authored by

Eric Biggers and committed by
Andrii Nakryiko
d47cc4de 0780f54a

+9 -47
-6
include/linux/filter.h
··· 997 997 return prog->len * sizeof(struct bpf_insn); 998 998 } 999 999 1000 - static inline u32 bpf_prog_tag_scratch_size(const struct bpf_prog *prog) 1001 - { 1002 - return round_up(bpf_prog_insn_size(prog) + 1003 - sizeof(__be64) + 1, SHA1_BLOCK_SIZE); 1004 - } 1005 - 1006 1000 static inline unsigned int bpf_prog_size(unsigned int proglen) 1007 1001 { 1008 1002 return max(sizeof(struct bpf_prog),
+9 -41
kernel/bpf/core.c
··· 18 18 */ 19 19 20 20 #include <uapi/linux/btf.h> 21 + #include <crypto/sha1.h> 21 22 #include <linux/filter.h> 22 23 #include <linux/skbuff.h> 23 24 #include <linux/vmalloc.h> ··· 294 293 295 294 int bpf_prog_calc_tag(struct bpf_prog *fp) 296 295 { 297 - const u32 bits_offset = SHA1_BLOCK_SIZE - sizeof(__be64); 298 - u32 raw_size = bpf_prog_tag_scratch_size(fp); 299 - u32 digest[SHA1_DIGEST_WORDS]; 300 - u32 ws[SHA1_WORKSPACE_WORDS]; 301 - u32 i, bsize, psize, blocks; 296 + size_t size = bpf_prog_insn_size(fp); 297 + u8 digest[SHA1_DIGEST_SIZE]; 302 298 struct bpf_insn *dst; 303 299 bool was_ld_map; 304 - u8 *raw, *todo; 305 - __be32 *result; 306 - __be64 *bits; 300 + u32 i; 307 301 308 - raw = vmalloc(raw_size); 309 - if (!raw) 302 + dst = vmalloc(size); 303 + if (!dst) 310 304 return -ENOMEM; 311 - 312 - sha1_init_raw(digest); 313 - memset(ws, 0, sizeof(ws)); 314 305 315 306 /* We need to take out the map fd for the digest calculation 316 307 * since they are unstable from user space side. 317 308 */ 318 - dst = (void *)raw; 319 309 for (i = 0, was_ld_map = false; i < fp->len; i++) { 320 310 dst[i] = fp->insnsi[i]; 321 311 if (!was_ld_map && ··· 326 334 was_ld_map = false; 327 335 } 328 336 } 329 - 330 - psize = bpf_prog_insn_size(fp); 331 - memset(&raw[psize], 0, raw_size - psize); 332 - raw[psize++] = 0x80; 333 - 334 - bsize = round_up(psize, SHA1_BLOCK_SIZE); 335 - blocks = bsize / SHA1_BLOCK_SIZE; 336 - todo = raw; 337 - if (bsize - psize >= sizeof(__be64)) { 338 - bits = (__be64 *)(todo + bsize - sizeof(__be64)); 339 - } else { 340 - bits = (__be64 *)(todo + bsize + bits_offset); 341 - blocks++; 342 - } 343 - *bits = cpu_to_be64((psize - 1) << 3); 344 - 345 - while (blocks--) { 346 - sha1_transform(digest, todo, ws); 347 - todo += SHA1_BLOCK_SIZE; 348 - } 349 - 350 - result = (__force __be32 *)digest; 351 - for (i = 0; i < SHA1_DIGEST_WORDS; i++) 352 - result[i] = cpu_to_be32(digest[i]); 353 - memcpy(fp->tag, result, sizeof(fp->tag)); 354 - 355 - vfree(raw); 337 + sha1((const u8 *)dst, size, digest); 338 + memcpy(fp->tag, digest, sizeof(fp->tag)); 339 + vfree(dst); 356 340 return 0; 357 341 } 358 342