Skip to content

Commit b6aa0ab

Browse files
author
Alexei Starovoitov
committed
Merge branch 'more-gen_loader-fixes'
Daniel Borkmann says: ==================== More gen_loader fixes Follow-up fixes for the signed loader, includes also the recent sashiko findings. v1->v2: - Fixed up verifier_map_ptr selftest - Added patch 1/2/6/7 with a new map-in-map fix and a redundant hash_buf memcpy cleanup as well as selftests ==================== Link: https://patch.msgid.link/20260601150248.394863-1-daniel@iogearbox.net Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 3d781ff + 32f7254 commit b6aa0ab

7 files changed

Lines changed: 121 additions & 31 deletions

File tree

include/linux/bpf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ struct bpf_map_ops {
111111
long (*map_pop_elem)(struct bpf_map *map, void *value);
112112
long (*map_peek_elem)(struct bpf_map *map, void *value);
113113
void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu);
114-
int (*map_get_hash)(struct bpf_map *map, u32 hash_buf_size, void *hash_buf);
114+
int (*map_get_hash)(struct bpf_map *map);
115115

116116
/* funcs called by prog_array and perf_event_array map */
117117
void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
@@ -296,6 +296,7 @@ struct bpf_map_owner {
296296

297297
struct bpf_map {
298298
u8 sha[SHA256_DIGEST_SIZE];
299+
u32 excl;
299300
const struct bpf_map_ops *ops;
300301
struct bpf_map *inner_map_meta;
301302
#ifdef CONFIG_SECURITY

kernel/bpf/arraymap.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,12 @@ static void *array_map_lookup_elem(struct bpf_map *map, void *key)
175175
return array->value + (u64)array->elem_size * (index & array->index_mask);
176176
}
177177

178-
static int array_map_get_hash(struct bpf_map *map, u32 hash_buf_size,
179-
void *hash_buf)
178+
static int array_map_get_hash(struct bpf_map *map)
180179
{
181180
struct bpf_array *array = container_of(map, struct bpf_array, map);
182181

183182
sha256(array->value, (u64)array->elem_size * array->map.max_entries,
184-
hash_buf);
185-
memcpy(array->map.sha, hash_buf, sizeof(array->map.sha));
183+
array->map.sha);
186184
return 0;
187185
}
188186

kernel/bpf/map_in_map.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd)
2020
/* Does not support >1 level map-in-map */
2121
if (inner_map->inner_map_meta)
2222
return ERR_PTR(-EINVAL);
23-
23+
if (inner_map->excl_prog_sha)
24+
return ERR_PTR(-ENOTSUPP);
2425
if (!inner_map->ops->map_meta_equal)
2526
return ERR_PTR(-ENOTSUPP);
2627

@@ -101,6 +102,8 @@ void *bpf_map_fd_get_ptr(struct bpf_map *map,
101102
inner_map = __bpf_map_get(f);
102103
if (IS_ERR(inner_map))
103104
return inner_map;
105+
if (inner_map->excl_prog_sha)
106+
return ERR_PTR(-ENOTSUPP);
104107

105108
inner_map_meta = map->inner_map_meta;
106109
if (inner_map_meta->ops->map_meta_equal(inner_map_meta, inner_map))

kernel/bpf/syscall.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,13 @@ static int map_create_alloc(union bpf_attr *attr, bpfptr_t uattr, struct bpf_ver
15881588
err = -EFAULT;
15891589
goto free_map;
15901590
}
1591+
1592+
/* See libbpf: emit_signature_match() */
1593+
BUILD_BUG_ON(offsetof(struct bpf_map, excl) != SHA256_DIGEST_SIZE);
1594+
BUILD_BUG_ON(!__same_type(map->excl, u32));
1595+
BUILD_BUG_ON(offsetof(struct bpf_map, sha) != 0);
1596+
BUILD_BUG_ON(!__same_type(map->sha, u8[SHA256_DIGEST_SIZE]));
1597+
map->excl = 1;
15911598
} else if (attr->excl_prog_hash_size) {
15921599
bpf_log(log, "Invalid excl_prog_hash_size.\n");
15931600
err = -EINVAL;
@@ -5434,18 +5441,16 @@ static int bpf_map_get_info_by_fd(struct file *file,
54345441

54355442
if (!map->ops->map_get_hash)
54365443
return -EINVAL;
5437-
5438-
if (info.hash_size != SHA256_DIGEST_SIZE)
5444+
if (info.hash_size != sizeof(map->sha))
54395445
return -EINVAL;
5440-
54415446
if (!READ_ONCE(map->frozen))
54425447
return -EPERM;
54435448

5444-
err = map->ops->map_get_hash(map, SHA256_DIGEST_SIZE, map->sha);
5449+
err = map->ops->map_get_hash(map);
54455450
if (err != 0)
54465451
return err;
54475452

5448-
if (copy_to_user(uhash, map->sha, SHA256_DIGEST_SIZE) != 0)
5453+
if (copy_to_user(uhash, map->sha, sizeof(map->sha)) != 0)
54495454
return -EFAULT;
54505455
} else if (info.hash_size) {
54515456
return -EINVAL;

tools/lib/bpf/gen_loader.c

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -546,13 +546,22 @@ void bpf_gen__map_create(struct bpf_gen *gen,
546546
default:
547547
break;
548548
}
549-
/* conditionally update max_entries */
550-
if (map_idx >= 0)
549+
550+
/*
551+
* Conditionally update max_entries from the host-supplied loader
552+
* ctx. This sizes the map at runtime, but for a signed loader
553+
* (gen_hash) it would let an untrusted host re-dimension the
554+
* program's maps after emit_signature_match(), outside what the
555+
* signature attests to. Keep the signer-provided max_entries
556+
* baked into the blob in that case.
557+
*/
558+
if (map_idx >= 0 && !OPTS_GET(gen->opts, gen_hash, false))
551559
move_ctx2blob(gen, attr_field(map_create_attr, max_entries), 4,
552560
sizeof(struct bpf_loader_ctx) +
553561
sizeof(struct bpf_map_desc) * map_idx +
554562
offsetof(struct bpf_map_desc, max_entries),
555563
true /* check that max_entries != 0 */);
564+
556565
/* emit MAP_CREATE command */
557566
emit_sys_bpf(gen, BPF_MAP_CREATE, map_create_attr, attr_size);
558567
debug_ret(gen, "map_create %s idx %d type %d value_size %d value_btf_id %d",
@@ -586,6 +595,23 @@ static void emit_signature_match(struct bpf_gen *gen)
586595
__s64 off;
587596
int i;
588597

598+
/*
599+
* Reject if the metadata map is not exclusive. Without exclusivity
600+
* the cached map->sha[] verified above can be stale: another BPF
601+
* program with map access could have mutated the contents between
602+
* BPF_OBJ_GET_INFO_BY_FD and loader execution.
603+
*/
604+
emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX,
605+
0, 0, 0, 0));
606+
emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_2, BPF_REG_1, SHA256_DIGEST_LENGTH));
607+
off = -(gen->insn_cur - gen->insn_start - gen->cleanup_label) / 8 - 2;
608+
if (is_simm16(off)) {
609+
emit(gen, BPF_MOV64_IMM(BPF_REG_7, -EINVAL));
610+
emit(gen, BPF_JMP_IMM(BPF_JNE, BPF_REG_2, 1, off));
611+
} else {
612+
gen->error = -ERANGE;
613+
}
614+
589615
for (i = 0; i < SHA256_DWORD_SIZE; i++) {
590616
emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX,
591617
0, 0, 0, 0));
@@ -1170,27 +1196,36 @@ void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *pvalue,
11701196
value = add_data(gen, pvalue, value_size);
11711197
key = add_data(gen, &zero, sizeof(zero));
11721198

1173-
/* if (map_desc[map_idx].initial_value) {
1199+
/*
1200+
* if (map_desc[map_idx].initial_value) {
11741201
* if (ctx->flags & BPF_SKEL_KERNEL)
11751202
* bpf_probe_read_kernel(value, value_size, initial_value);
11761203
* else
11771204
* bpf_copy_from_user(value, value_size, initial_value);
11781205
* }
1206+
*
1207+
* The runtime initial_value comes from the host-supplied loader
1208+
* ctx and would overwrite the blob value after emit_signature_match()
1209+
* has already validated map->sha[]. For a signed loader (gen_hash)
1210+
* the attested blob value must be authoritative, so skip the override
1211+
* and leave the hashed value in place.
11791212
*/
1180-
emit(gen, BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_6,
1181-
sizeof(struct bpf_loader_ctx) +
1182-
sizeof(struct bpf_map_desc) * map_idx +
1183-
offsetof(struct bpf_map_desc, initial_value)));
1184-
emit(gen, BPF_JMP_IMM(BPF_JEQ, BPF_REG_3, 0, 8));
1185-
emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
1186-
0, 0, 0, value));
1187-
emit(gen, BPF_MOV64_IMM(BPF_REG_2, value_size));
1188-
emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6,
1189-
offsetof(struct bpf_loader_ctx, flags)));
1190-
emit(gen, BPF_JMP_IMM(BPF_JSET, BPF_REG_0, BPF_SKEL_KERNEL, 2));
1191-
emit(gen, BPF_EMIT_CALL(BPF_FUNC_copy_from_user));
1192-
emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 1));
1193-
emit(gen, BPF_EMIT_CALL(BPF_FUNC_probe_read_kernel));
1213+
if (!OPTS_GET(gen->opts, gen_hash, false)) {
1214+
emit(gen, BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_6,
1215+
sizeof(struct bpf_loader_ctx) +
1216+
sizeof(struct bpf_map_desc) * map_idx +
1217+
offsetof(struct bpf_map_desc, initial_value)));
1218+
emit(gen, BPF_JMP_IMM(BPF_JEQ, BPF_REG_3, 0, 8));
1219+
emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
1220+
0, 0, 0, value));
1221+
emit(gen, BPF_MOV64_IMM(BPF_REG_2, value_size));
1222+
emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6,
1223+
offsetof(struct bpf_loader_ctx, flags)));
1224+
emit(gen, BPF_JMP_IMM(BPF_JSET, BPF_REG_0, BPF_SKEL_KERNEL, 2));
1225+
emit(gen, BPF_EMIT_CALL(BPF_FUNC_copy_from_user));
1226+
emit(gen, BPF_JMP_IMM(BPF_JA, 0, 0, 1));
1227+
emit(gen, BPF_EMIT_CALL(BPF_FUNC_probe_read_kernel));
1228+
}
11941229

11951230
map_update_attr = add_data(gen, &attr, attr_size);
11961231
pr_debug("gen: map_update_elem: idx %d, value: off %d size %d, attr: off %d size %d\n",

tools/testing/selftests/bpf/prog_tests/map_excl.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#include "map_excl.skel.h"
1010

11+
#ifndef SHA256_DIGEST_SIZE
12+
#define SHA256_DIGEST_SIZE 32
13+
#endif
14+
1115
static void test_map_excl_allowed(void)
1216
{
1317
struct map_excl *skel = map_excl__open();
@@ -45,10 +49,52 @@ static void test_map_excl_denied(void)
4549

4650
}
4751

52+
static void test_map_excl_no_map_in_map(void)
53+
{
54+
__u8 hash[SHA256_DIGEST_SIZE] = {};
55+
LIBBPF_OPTS(bpf_map_create_opts, excl_opts,
56+
.excl_prog_hash = hash,
57+
.excl_prog_hash_size = sizeof(hash));
58+
LIBBPF_OPTS(bpf_map_create_opts, outer_opts);
59+
int excl_fd, tmpl_fd = -1, outer_fd = -1, err;
60+
__u32 key = 0;
61+
62+
excl_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "excl_inner", 4, 4, 1, &excl_opts);
63+
if (!ASSERT_OK_FD(excl_fd, "create exclusive map"))
64+
return;
65+
66+
outer_opts.inner_map_fd = excl_fd;
67+
err = bpf_map_create(BPF_MAP_TYPE_ARRAY_OF_MAPS, "outer_from_excl",
68+
4, 4, 1, &outer_opts);
69+
if (err >= 0)
70+
close(err);
71+
ASSERT_EQ(err, -ENOTSUPP, "reject exclusive map as map-in-map template");
72+
73+
tmpl_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "tmpl", 4, 4, 1, NULL);
74+
if (!ASSERT_OK_FD(tmpl_fd, "create inner template"))
75+
goto out;
76+
77+
outer_opts.inner_map_fd = tmpl_fd;
78+
outer_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY_OF_MAPS, "outer", 4, 4, 1, &outer_opts);
79+
if (!ASSERT_OK_FD(outer_fd, "create map-of-maps"))
80+
goto out;
81+
82+
err = bpf_map_update_elem(outer_fd, &key, &excl_fd, 0);
83+
ASSERT_EQ(err, -ENOTSUPP, "reject exclusive map as map-in-map element");
84+
out:
85+
if (outer_fd >= 0)
86+
close(outer_fd);
87+
if (tmpl_fd >= 0)
88+
close(tmpl_fd);
89+
close(excl_fd);
90+
}
91+
4892
void test_map_excl(void)
4993
{
5094
if (test__start_subtest("map_excl_allowed"))
5195
test_map_excl_allowed();
5296
if (test__start_subtest("map_excl_denied"))
5397
test_map_excl_denied();
98+
if (test__start_subtest("map_excl_no_map_in_map"))
99+
test_map_excl_no_map_in_map();
54100
}

tools/testing/selftests/bpf/progs/verifier_map_ptr.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ __naked void bpf_map_ptr_write_rejected(void)
7070
: __clobber_all);
7171
}
7272

73-
/* The first element of struct bpf_map is a SHA256 hash of 32 bytes, accessing
74-
* into this array is valid. The opts field is now at offset 33.
73+
/*
74+
* struct bpf_map starts with the SHA256 hash sha[32] at offset 0 (a readable
75+
* byte array), followed by the u32 excl field at offset 32. Reading a u32 at
76+
* offset 33 runs past the end of excl and is rejected.
7577
*/
7678
SEC("socket")
7779
__description("bpf_map_ptr: read non-existent field rejected")
7880
__failure
79-
__msg("cannot access ptr member ops with moff 32 in struct bpf_map with off 33 size 4")
81+
__msg("access beyond the end of member excl (mend:36) in struct bpf_map with off 33 size 4")
8082
__failure_unpriv
8183
__msg_unpriv("access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN")
8284
__flag(BPF_F_ANY_ALIGNMENT)

0 commit comments

Comments
 (0)