Skip to content

Commit d3b968b

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Daniel Borkmann says: ==================== pull-request: bpf 2020-05-22 The following pull-request contains BPF updates for your *net* tree. We've added 3 non-merge commits during the last 3 day(s) which contain a total of 5 files changed, 69 insertions(+), 11 deletions(-). The main changes are: 1) Fix to reject mmap()'ing read-only array maps as writable since BPF verifier relies on such map content to be frozen, from Andrii Nakryiko. 2) Fix breaking audit from secid_to_secctx() LSM hook by avoiding to use call_int_hook() since this hook is not stackable, from KP Singh. 3) Fix BPF flow dissector program ref leak on netns cleanup, from Jakub Sitnicki. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents b4024c9 + 5cf6592 commit d3b968b

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

kernel/bpf/syscall.c

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

624624
mutex_lock(&map->freeze_mutex);
625625

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

631642
/* set default open/close callbacks */

net/core/flow_dissector.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,10 @@ int skb_flow_dissector_bpf_prog_attach(const union bpf_attr *attr,
160160
return ret;
161161
}
162162

163-
int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
163+
static int flow_dissector_bpf_prog_detach(struct net *net)
164164
{
165165
struct bpf_prog *attached;
166-
struct net *net;
167166

168-
net = current->nsproxy->net_ns;
169167
mutex_lock(&flow_dissector_mutex);
170168
attached = rcu_dereference_protected(net->flow_dissector_prog,
171169
lockdep_is_held(&flow_dissector_mutex));
@@ -179,6 +177,24 @@ int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
179177
return 0;
180178
}
181179

180+
int skb_flow_dissector_bpf_prog_detach(const union bpf_attr *attr)
181+
{
182+
return flow_dissector_bpf_prog_detach(current->nsproxy->net_ns);
183+
}
184+
185+
static void __net_exit flow_dissector_pernet_pre_exit(struct net *net)
186+
{
187+
/* We're not racing with attach/detach because there are no
188+
* references to netns left when pre_exit gets called.
189+
*/
190+
if (rcu_access_pointer(net->flow_dissector_prog))
191+
flow_dissector_bpf_prog_detach(net);
192+
}
193+
194+
static struct pernet_operations flow_dissector_pernet_ops __net_initdata = {
195+
.pre_exit = flow_dissector_pernet_pre_exit,
196+
};
197+
182198
/**
183199
* __skb_flow_get_ports - extract the upper layer ports and return them
184200
* @skb: sk_buff to extract the ports from
@@ -1836,7 +1852,7 @@ static int __init init_default_flow_dissectors(void)
18361852
skb_flow_dissector_init(&flow_keys_basic_dissector,
18371853
flow_keys_basic_dissector_keys,
18381854
ARRAY_SIZE(flow_keys_basic_dissector_keys));
1839-
return 0;
1840-
}
18411855

1856+
return register_pernet_subsys(&flow_dissector_pernet_ops);
1857+
}
18421858
core_initcall(init_default_flow_dissectors);

security/security.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,8 +1965,20 @@ EXPORT_SYMBOL(security_ismaclabel);
19651965

19661966
int security_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
19671967
{
1968-
return call_int_hook(secid_to_secctx, -EOPNOTSUPP, secid, secdata,
1969-
seclen);
1968+
struct security_hook_list *hp;
1969+
int rc;
1970+
1971+
/*
1972+
* Currently, only one LSM can implement secid_to_secctx (i.e this
1973+
* LSM hook is not "stackable").
1974+
*/
1975+
hlist_for_each_entry(hp, &security_hook_heads.secid_to_secctx, list) {
1976+
rc = hp->hook.secid_to_secctx(secid, secdata, seclen);
1977+
if (rc != LSM_RET_DEFAULT(secid_to_secctx))
1978+
return rc;
1979+
}
1980+
1981+
return LSM_RET_DEFAULT(secid_to_secctx);
19701982
}
19711983
EXPORT_SYMBOL(security_secid_to_secctx);
19721984

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, data_map_id, tmp_fd;
22+
int err, duration = 0, i, data_map_fd, data_map_id, tmp_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;
@@ -37,6 +37,17 @@ void test_mmap(void)
3737
data_map = skel->maps.data_map;
3838
data_map_fd = bpf_map__fd(data_map);
3939

40+
rdmap_fd = bpf_map__fd(skel->maps.rdonly_map);
41+
tmp1 = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, rdmap_fd, 0);
42+
if (CHECK(tmp1 != MAP_FAILED, "rdonly_write_mmap", "unexpected success\n")) {
43+
munmap(tmp1, 4096);
44+
goto cleanup;
45+
}
46+
/* now double-check if it's mmap()'able at all */
47+
tmp1 = mmap(NULL, 4096, PROT_READ, MAP_SHARED, rdmap_fd, 0);
48+
if (CHECK(tmp1 == MAP_FAILED, "rdonly_read_mmap", "failed: %d\n", errno))
49+
goto cleanup;
50+
4051
/* get map's ID */
4152
memset(&map_info, 0, map_info_sz);
4253
err = bpf_obj_get_info_by_fd(data_map_fd, &map_info, &map_info_sz);

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)