Skip to content

Commit f469812

Browse files
anakryikogregkh
authored andcommitted
bpf: Prevent mmap()'ing read-only maps as writable
[ Upstream commit dfeb376 ] As discussed in [0], it's dangerous to allow mapping BPF map, that's meant to be frozen and is read-only on BPF program side, because that allows user-space to actually store a writable view to the page even after it is frozen. This is exacerbated by BPF verifier making a strong assumption that contents of such frozen map will remain unchanged. To prevent this, disallow mapping BPF_F_RDONLY_PROG mmap()'able BPF maps as writable, ever. [0] https://lore.kernel.org/bpf/CAEf4BzYGWYhXdp6BJ7_=9OQPJxQpgug080MMjdSB72i9R+5c6g@mail.gmail.com/ Fixes: fc97022 ("bpf: Add mmap() support for BPF_MAP_TYPE_ARRAY") Suggested-by: Jann Horn <jannh@google.com> Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Reviewed-by: Jann Horn <jannh@google.com> Link: https://lore.kernel.org/bpf/20200519053824.1089415-1-andriin@fb.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent f8acf49 commit f469812

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

‎kernel/bpf/syscall.c‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,20 @@ static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
629629

630630
mutex_lock(&map->freeze_mutex);
631631

632-
if ((vma->vm_flags & VM_WRITE) && map->frozen) {
633-
err = -EPERM;
634-
goto out;
632+
if (vma->vm_flags & VM_WRITE) {
633+
if (map->frozen) {
634+
err = -EPERM;
635+
goto out;
636+
}
637+
/* map is meant to be read-only, so do not allow mapping as
638+
* writable, because it's possible to leak a writable page
639+
* reference and allows user-space to still modify it after
640+
* freezing, while verifier will assume contents do not change
641+
*/
642+
if (map->map_flags & BPF_F_RDONLY_PROG) {
643+
err = -EACCES;
644+
goto out;
645+
}
635646
}
636647

637648
/* set default open/close callbacks */

‎tools/testing/selftests/bpf/prog_tests/mmap.c‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void test_mmap(void)
1919
const size_t map_sz = roundup_page(sizeof(struct map_data));
2020
const int zero = 0, one = 1, two = 2, far = 1500;
2121
const long page_size = sysconf(_SC_PAGE_SIZE);
22-
int err, duration = 0, i, data_map_fd;
22+
int err, duration = 0, i, data_map_fd, rdmap_fd;
2323
struct bpf_map *data_map, *bss_map;
2424
void *bss_mmaped = NULL, *map_mmaped = NULL, *tmp1, *tmp2;
2525
struct test_mmap__bss *bss_data;
@@ -36,6 +36,17 @@ void test_mmap(void)
3636
data_map = skel->maps.data_map;
3737
data_map_fd = bpf_map__fd(data_map);
3838

39+
rdmap_fd = bpf_map__fd(skel->maps.rdonly_map);
40+
tmp1 = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, rdmap_fd, 0);
41+
if (CHECK(tmp1 != MAP_FAILED, "rdonly_write_mmap", "unexpected success\n")) {
42+
munmap(tmp1, 4096);
43+
goto cleanup;
44+
}
45+
/* now double-check if it's mmap()'able at all */
46+
tmp1 = mmap(NULL, 4096, PROT_READ, MAP_SHARED, rdmap_fd, 0);
47+
if (CHECK(tmp1 == MAP_FAILED, "rdonly_read_mmap", "failed: %d\n", errno))
48+
goto cleanup;
49+
3950
bss_mmaped = mmap(NULL, bss_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
4051
bpf_map__fd(bss_map), 0);
4152
if (CHECK(bss_mmaped == MAP_FAILED, "bss_mmap",

‎tools/testing/selftests/bpf/progs/test_mmap.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
char _license[] SEC("license") = "GPL";
99

10+
struct {
11+
__uint(type, BPF_MAP_TYPE_ARRAY);
12+
__uint(max_entries, 4096);
13+
__uint(map_flags, BPF_F_MMAPABLE | BPF_F_RDONLY_PROG);
14+
__type(key, __u32);
15+
__type(value, char);
16+
} rdonly_map SEC(".maps");
17+
1018
struct {
1119
__uint(type, BPF_MAP_TYPE_ARRAY);
1220
__uint(max_entries, 512 * 4); /* at least 4 pages of data */

0 commit comments

Comments
 (0)