Skip to content

Commit 33fe044

Browse files
borkmanngregkh
authored andcommitted
bpf: Fix toctou on read-only map's constant scalar tracking
commit 353050b upstream. Commit a23740e ("bpf: Track contents of read-only maps as scalars") is checking whether maps are read-only both from BPF program side and user space side, and then, given their content is constant, reading out their data via map->ops->map_direct_value_addr() which is then subsequently used as known scalar value for the register, that is, it is marked as __mark_reg_known() with the read value at verification time. Before a23740e, the register content was marked as an unknown scalar so the verifier could not make any assumptions about the map content. The current implementation however is prone to a TOCTOU race, meaning, the value read as known scalar for the register is not guaranteed to be exactly the same at a later point when the program is executed, and as such, the prior made assumptions of the verifier with regards to the program will be invalid which can cause issues such as OOB access, etc. While the BPF_F_RDONLY_PROG map flag is always fixed and required to be specified at map creation time, the map->frozen property is initially set to false for the map given the map value needs to be populated, e.g. for global data sections. Once complete, the loader "freezes" the map from user space such that no subsequent updates/deletes are possible anymore. For the rest of the lifetime of the map, this freeze one-time trigger cannot be undone anymore after a successful BPF_MAP_FREEZE cmd return. Meaning, any new BPF_* cmd calls which would update/delete map entries will be rejected with -EPERM since map_get_sys_perms() removes the FMODE_CAN_WRITE permission. This also means that pending update/delete map entries must still complete before this guarantee is given. This corner case is not an issue for loaders since they create and prepare such program private map in successive steps. However, a malicious user is able to trigger this TOCTOU race in two different ways: i) via userfaultfd, and ii) via batched updates. For i) userfaultfd is used to expand the competition interval, so that map_update_elem() can modify the contents of the map after map_freeze() and bpf_prog_load() were executed. This works, because userfaultfd halts the parallel thread which triggered a map_update_elem() at the time where we copy key/value from the user buffer and this already passed the FMODE_CAN_WRITE capability test given at that time the map was not "frozen". Then, the main thread performs the map_freeze() and bpf_prog_load(), and once that had completed successfully, the other thread is woken up to complete the pending map_update_elem() which then changes the map content. For ii) the idea of the batched update is similar, meaning, when there are a large number of updates to be processed, it can increase the competition interval between the two. It is therefore possible in practice to modify the contents of the map after executing map_freeze() and bpf_prog_load(). One way to fix both i) and ii) at the same time is to expand the use of the map's map->writecnt. The latter was introduced in fc97022 ("bpf: Add mmap() support for BPF_MAP_TYPE_ARRAY") and further refined in 1f6cb19 ("bpf: Prevent re-mmap()'ing BPF map as writable for initially r/o mapping") with the rationale to make a writable mmap()'ing of a map mutually exclusive with read-only freezing. The counter indicates writable mmap() mappings and then prevents/fails the freeze operation. Its semantics can be expanded beyond just mmap() by generally indicating ongoing write phases. This would essentially span any parallel regular and batched flavor of update/delete operation and then also have map_freeze() fail with -EBUSY. For the check_mem_access() in the verifier we expand upon the bpf_map_is_rdonly() check ensuring that all last pending writes have completed via bpf_map_write_active() test. Once the map->frozen is set and bpf_map_write_active() indicates a map->writecnt of 0 only then we are really guaranteed to use the map's data as known constants. For map->frozen being set and pending writes in process of still being completed we fall back to marking that register as unknown scalar so we don't end up making assumptions about it. With this, both TOCTOU reproducers from i) and ii) are fixed. Note that the map->writecnt has been converted into a atomic64 in the fix in order to avoid a double freeze_mutex mutex_{un,}lock() pair when updating map->writecnt in the various map update/delete BPF_* cmd flavors. Spanning the freeze_mutex over entire map update/delete operations in syscall side would not be possible due to then causing everything to be serialized. Similarly, something like synchronize_rcu() after setting map->frozen to wait for update/deletes to complete is not possible either since it would also have to span the user copy which can sleep. On the libbpf side, this won't break d66562f ("libbpf: Add BPF object skeleton support") as the anonymous mmap()-ed "map initialization image" is remapped as a BPF map-backed mmap()-ed memory where for .rodata it's non-writable. Fixes: a23740e ("bpf: Track contents of read-only maps as scalars") Reported-by: w1tcher.bupt@gmail.com Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> [fix conflict to call bpf_map_write_active_dec() in err_put block. fix conflict to insert new functions after find_and_alloc_map().] Reference: CVE-2021-4001 Signed-off-by: Masami Ichikawa(CIP) <masami.ichikawa@cybertrust.co.jp> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d5259a9 commit 33fe044

3 files changed

Lines changed: 54 additions & 23 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ struct bpf_map {
173173
atomic64_t usercnt;
174174
struct work_struct work;
175175
struct mutex freeze_mutex;
176-
u64 writecnt; /* writable mmap cnt; protected by freeze_mutex */
176+
atomic64_t writecnt;
177177
};
178178

179179
static inline bool map_value_has_spin_lock(const struct bpf_map *map)
@@ -1252,6 +1252,7 @@ void bpf_map_charge_move(struct bpf_map_memory *dst,
12521252
void *bpf_map_area_alloc(u64 size, int numa_node);
12531253
void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
12541254
void bpf_map_area_free(void *base);
1255+
bool bpf_map_write_active(const struct bpf_map *map);
12551256
void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
12561257
int generic_map_lookup_batch(struct bpf_map *map,
12571258
const union bpf_attr *attr,

‎kernel/bpf/syscall.c‎

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
127127
return map;
128128
}
129129

130+
static void bpf_map_write_active_inc(struct bpf_map *map)
131+
{
132+
atomic64_inc(&map->writecnt);
133+
}
134+
135+
static void bpf_map_write_active_dec(struct bpf_map *map)
136+
{
137+
atomic64_dec(&map->writecnt);
138+
}
139+
140+
bool bpf_map_write_active(const struct bpf_map *map)
141+
{
142+
return atomic64_read(&map->writecnt) != 0;
143+
}
144+
130145
static u32 bpf_map_value_size(struct bpf_map *map)
131146
{
132147
if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
@@ -588,23 +603,17 @@ static void bpf_map_mmap_open(struct vm_area_struct *vma)
588603
{
589604
struct bpf_map *map = vma->vm_file->private_data;
590605

591-
if (vma->vm_flags & VM_MAYWRITE) {
592-
mutex_lock(&map->freeze_mutex);
593-
map->writecnt++;
594-
mutex_unlock(&map->freeze_mutex);
595-
}
606+
if (vma->vm_flags & VM_MAYWRITE)
607+
bpf_map_write_active_inc(map);
596608
}
597609

598610
/* called for all unmapped memory region (including initial) */
599611
static void bpf_map_mmap_close(struct vm_area_struct *vma)
600612
{
601613
struct bpf_map *map = vma->vm_file->private_data;
602614

603-
if (vma->vm_flags & VM_MAYWRITE) {
604-
mutex_lock(&map->freeze_mutex);
605-
map->writecnt--;
606-
mutex_unlock(&map->freeze_mutex);
607-
}
615+
if (vma->vm_flags & VM_MAYWRITE)
616+
bpf_map_write_active_dec(map);
608617
}
609618

610619
static const struct vm_operations_struct bpf_map_default_vmops = {
@@ -654,7 +663,7 @@ static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
654663
goto out;
655664

656665
if (vma->vm_flags & VM_MAYWRITE)
657-
map->writecnt++;
666+
bpf_map_write_active_inc(map);
658667
out:
659668
mutex_unlock(&map->freeze_mutex);
660669
return err;
@@ -1086,6 +1095,7 @@ static int map_update_elem(union bpf_attr *attr)
10861095
map = __bpf_map_get(f);
10871096
if (IS_ERR(map))
10881097
return PTR_ERR(map);
1098+
bpf_map_write_active_inc(map);
10891099
if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
10901100
err = -EPERM;
10911101
goto err_put;
@@ -1127,6 +1137,7 @@ static int map_update_elem(union bpf_attr *attr)
11271137
free_key:
11281138
kfree(key);
11291139
err_put:
1140+
bpf_map_write_active_dec(map);
11301141
fdput(f);
11311142
return err;
11321143
}
@@ -1149,6 +1160,7 @@ static int map_delete_elem(union bpf_attr *attr)
11491160
map = __bpf_map_get(f);
11501161
if (IS_ERR(map))
11511162
return PTR_ERR(map);
1163+
bpf_map_write_active_inc(map);
11521164
if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
11531165
err = -EPERM;
11541166
goto err_put;
@@ -1179,6 +1191,7 @@ static int map_delete_elem(union bpf_attr *attr)
11791191
out:
11801192
kfree(key);
11811193
err_put:
1194+
bpf_map_write_active_dec(map);
11821195
fdput(f);
11831196
return err;
11841197
}
@@ -1483,6 +1496,7 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)
14831496
map = __bpf_map_get(f);
14841497
if (IS_ERR(map))
14851498
return PTR_ERR(map);
1499+
bpf_map_write_active_inc(map);
14861500
if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
14871501
!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
14881502
err = -EPERM;
@@ -1524,6 +1538,7 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)
15241538
free_key:
15251539
kfree(key);
15261540
err_put:
1541+
bpf_map_write_active_dec(map);
15271542
fdput(f);
15281543
return err;
15291544
}
@@ -1550,8 +1565,7 @@ static int map_freeze(const union bpf_attr *attr)
15501565
}
15511566

15521567
mutex_lock(&map->freeze_mutex);
1553-
1554-
if (map->writecnt) {
1568+
if (bpf_map_write_active(map)) {
15551569
err = -EBUSY;
15561570
goto err_put;
15571571
}
@@ -3976,6 +3990,9 @@ static int bpf_map_do_batch(const union bpf_attr *attr,
39763990
union bpf_attr __user *uattr,
39773991
int cmd)
39783992
{
3993+
bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
3994+
cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
3995+
bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
39793996
struct bpf_map *map;
39803997
int err, ufd;
39813998
struct fd f;
@@ -3988,16 +4005,13 @@ static int bpf_map_do_batch(const union bpf_attr *attr,
39884005
map = __bpf_map_get(f);
39894006
if (IS_ERR(map))
39904007
return PTR_ERR(map);
3991-
3992-
if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3993-
cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
3994-
!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4008+
if (has_write)
4009+
bpf_map_write_active_inc(map);
4010+
if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
39954011
err = -EPERM;
39964012
goto err_put;
39974013
}
3998-
3999-
if (cmd != BPF_MAP_LOOKUP_BATCH &&
4000-
!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4014+
if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
40014015
err = -EPERM;
40024016
goto err_put;
40034017
}
@@ -4010,8 +4024,9 @@ static int bpf_map_do_batch(const union bpf_attr *attr,
40104024
BPF_DO_BATCH(map->ops->map_update_batch);
40114025
else
40124026
BPF_DO_BATCH(map->ops->map_delete_batch);
4013-
40144027
err_put:
4028+
if (has_write)
4029+
bpf_map_write_active_dec(map);
40154030
fdput(f);
40164031
return err;
40174032
}

‎kernel/bpf/verifier.c‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3486,7 +3486,22 @@ static void coerce_reg_to_size(struct bpf_reg_state *reg, int size)
34863486

34873487
static bool bpf_map_is_rdonly(const struct bpf_map *map)
34883488
{
3489-
return (map->map_flags & BPF_F_RDONLY_PROG) && map->frozen;
3489+
/* A map is considered read-only if the following condition are true:
3490+
*
3491+
* 1) BPF program side cannot change any of the map content. The
3492+
* BPF_F_RDONLY_PROG flag is throughout the lifetime of a map
3493+
* and was set at map creation time.
3494+
* 2) The map value(s) have been initialized from user space by a
3495+
* loader and then "frozen", such that no new map update/delete
3496+
* operations from syscall side are possible for the rest of
3497+
* the map's lifetime from that point onwards.
3498+
* 3) Any parallel/pending map update/delete operations from syscall
3499+
* side have been completed. Only after that point, it's safe to
3500+
* assume that map value(s) are immutable.
3501+
*/
3502+
return (map->map_flags & BPF_F_RDONLY_PROG) &&
3503+
READ_ONCE(map->frozen) &&
3504+
!bpf_map_write_active(map);
34903505
}
34913506

34923507
static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val)

0 commit comments

Comments
 (0)