Skip to content

Commit 7a87d53

Browse files
committed
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 bff9b8b commit 7a87d53

File tree

4 files changed

+14
-50
lines changed

4 files changed

+14
-50
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;
@@ -1711,7 +1712,10 @@ struct bpf_prog {
17111712
enum bpf_attach_type expected_attach_type; /* For some prog types */
17121713
u32 len; /* Number of filter blocks */
17131714
u32 jited_len; /* Size of jited insns in bytes */
1714-
u8 tag[BPF_TAG_SIZE];
1715+
union {
1716+
u8 digest[SHA256_DIGEST_SIZE];
1717+
u8 tag[BPF_TAG_SIZE];
1718+
};
17151719
struct bpf_prog_stats __percpu *stats;
17161720
int __percpu *active;
17171721
unsigned int (*bpf_func)(const void *ctx,

include/linux/filter.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -997,12 +997,6 @@ static inline u32 bpf_prog_insn_size(const struct bpf_prog *prog)
997997
return prog->len * sizeof(struct bpf_insn);
998998
}
999999

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-
10061000
static inline unsigned int bpf_prog_size(unsigned int proglen)
10071001
{
10081002
return max(sizeof(struct bpf_prog),

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: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <linux/bpf_mem_alloc.h>
3939
#include <linux/memcontrol.h>
4040
#include <linux/execmem.h>
41+
#include <crypto/sha2.h>
4142

4243
#include <asm/barrier.h>
4344
#include <linux/unaligned.h>
@@ -293,28 +294,18 @@ void __bpf_prog_free(struct bpf_prog *fp)
293294

294295
int bpf_prog_calc_tag(struct bpf_prog *fp)
295296
{
296-
const u32 bits_offset = SHA1_BLOCK_SIZE - sizeof(__be64);
297-
u32 raw_size = bpf_prog_tag_scratch_size(fp);
298-
u32 digest[SHA1_DIGEST_WORDS];
299-
u32 ws[SHA1_WORKSPACE_WORDS];
300-
u32 i, bsize, psize, blocks;
297+
u32 insn_size = bpf_prog_insn_size(fp);
301298
struct bpf_insn *dst;
302299
bool was_ld_map;
303-
u8 *raw, *todo;
304-
__be32 *result;
305-
__be64 *bits;
300+
int i, ret = 0;
306301

307-
raw = vmalloc(raw_size);
308-
if (!raw)
302+
dst = vmalloc(insn_size);
303+
if (!dst)
309304
return -ENOMEM;
310305

311-
sha1_init_raw(digest);
312-
memset(ws, 0, sizeof(ws));
313-
314306
/* We need to take out the map fd for the digest calculation
315307
* since they are unstable from user space side.
316308
*/
317-
dst = (void *)raw;
318309
for (i = 0, was_ld_map = false; i < fp->len; i++) {
319310
dst[i] = fp->insnsi[i];
320311
if (!was_ld_map &&
@@ -334,34 +325,9 @@ int bpf_prog_calc_tag(struct bpf_prog *fp)
334325
was_ld_map = false;
335326
}
336327
}
337-
338-
psize = bpf_prog_insn_size(fp);
339-
memset(&raw[psize], 0, raw_size - psize);
340-
raw[psize++] = 0x80;
341-
342-
bsize = round_up(psize, SHA1_BLOCK_SIZE);
343-
blocks = bsize / SHA1_BLOCK_SIZE;
344-
todo = raw;
345-
if (bsize - psize >= sizeof(__be64)) {
346-
bits = (__be64 *)(todo + bsize - sizeof(__be64));
347-
} else {
348-
bits = (__be64 *)(todo + bsize + bits_offset);
349-
blocks++;
350-
}
351-
*bits = cpu_to_be64((psize - 1) << 3);
352-
353-
while (blocks--) {
354-
sha1_transform(digest, todo, ws);
355-
todo += SHA1_BLOCK_SIZE;
356-
}
357-
358-
result = (__force __be32 *)digest;
359-
for (i = 0; i < SHA1_DIGEST_WORDS; i++)
360-
result[i] = cpu_to_be32(digest[i]);
361-
memcpy(fp->tag, result, sizeof(fp->tag));
362-
363-
vfree(raw);
364-
return 0;
328+
sha256((u8 *)dst, insn_size, fp->digest);
329+
vfree(dst);
330+
return ret;
365331
}
366332

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

0 commit comments

Comments
 (0)