Skip to content

Commit cf4620f

Browse files
sinkapKernel Patches Daemon
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]>
1 parent cde4d9b commit cf4620f

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
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: 6 additions & 7 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>
@@ -295,13 +296,12 @@ void __bpf_prog_free(struct bpf_prog *fp)
295296

296297
int bpf_prog_calc_tag(struct bpf_prog *fp)
297298
{
298-
size_t size = bpf_prog_insn_size(fp);
299-
u8 digest[SHA1_DIGEST_SIZE];
299+
u32 insn_size = bpf_prog_insn_size(fp);
300300
struct bpf_insn *dst;
301301
bool was_ld_map;
302-
u32 i;
302+
int i, ret = 0;
303303

304-
dst = vmalloc(size);
304+
dst = vmalloc(insn_size);
305305
if (!dst)
306306
return -ENOMEM;
307307

@@ -327,10 +327,9 @@ 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, insn_size, fp->digest);
332331
vfree(dst);
333-
return 0;
332+
return ret;
334333
}
335334

336335
static int bpf_adj_delta_to_imm(struct bpf_insn *insn, u32 pos, s32 end_old,

0 commit comments

Comments
 (0)