Skip to content

Commit 603b441

Browse files
sinkapAlexei Starovoitov
authored andcommitted
bpf: Update the bpf_prog_calc_tag to use SHA256
Exclusive maps restrict map access to specific programs using a hash. The current hash used for this is SHA1, which is prone to collisions. This patch uses SHA256, which is more resilient against collisions. This new hash is stored in bpf_prog and used by the verifier to determine if a program can access a given exclusive map. The original 64-bit tags are kept, as they are used by users as a short, possibly colliding program identifier for non-security purposes. Signed-off-by: KP Singh <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 3547a61 commit 603b441

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

include/linux/bpf.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/memcontrol.h>
3232
#include <linux/cfi.h>
3333
#include <asm/rqspinlock.h>
34+
#include <crypto/sha2.h>
3435

3536
struct bpf_verifier_env;
3637
struct bpf_verifier_log;
@@ -1717,7 +1718,10 @@ struct bpf_prog {
17171718
enum bpf_attach_type expected_attach_type; /* For some prog types */
17181719
u32 len; /* Number of filter blocks */
17191720
u32 jited_len; /* Size of jited insns in bytes */
1720-
u8 tag[BPF_TAG_SIZE];
1721+
union {
1722+
u8 digest[SHA256_DIGEST_SIZE];
1723+
u8 tag[BPF_TAG_SIZE];
1724+
};
17211725
struct bpf_prog_stats __percpu *stats;
17221726
int __percpu *active;
17231727
unsigned int (*bpf_func)(const void *ctx,

kernel/bpf/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# BPF interpreter that, for example, classic socket filters depend on.
44
config BPF
55
bool
6-
select CRYPTO_LIB_SHA1
6+
select CRYPTO_LIB_SHA256
77

88
# Used by archs to tell that they support BPF JIT compiler plus which
99
# flavour. Only one of the two can be selected for a specific arch since

kernel/bpf/core.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <linux/bpf_mem_alloc.h>
4040
#include <linux/memcontrol.h>
4141
#include <linux/execmem.h>
42+
#include <crypto/sha2.h>
4243

4344
#include <asm/barrier.h>
4445
#include <linux/unaligned.h>
@@ -296,7 +297,6 @@ void __bpf_prog_free(struct bpf_prog *fp)
296297
int bpf_prog_calc_tag(struct bpf_prog *fp)
297298
{
298299
size_t size = bpf_prog_insn_size(fp);
299-
u8 digest[SHA1_DIGEST_SIZE];
300300
struct bpf_insn *dst;
301301
bool was_ld_map;
302302
u32 i;
@@ -327,8 +327,7 @@ int bpf_prog_calc_tag(struct bpf_prog *fp)
327327
was_ld_map = false;
328328
}
329329
}
330-
sha1((const u8 *)dst, size, digest);
331-
memcpy(fp->tag, digest, sizeof(fp->tag));
330+
sha256((u8 *)dst, size, fp->digest);
332331
vfree(dst);
333332
return 0;
334333
}

0 commit comments

Comments
 (0)