bpf: drop refcount if bpf_map_new_fd() fails in map_create()
authorPeng Sun <[email protected]>
Wed, 27 Feb 2019 14:36:25 +0000 (22:36 +0800)
committerDaniel Borkmann <[email protected]>
Fri, 1 Mar 2019 15:04:29 +0000 (16:04 +0100)
In bpf/syscall.c, map_create() first set map->usercnt to 1, a file
descriptor is supposed to return to userspace. When bpf_map_new_fd()
fails, drop the refcount.

Fixes: bd5f5f4ecb78 ("bpf: Add BPF_MAP_GET_FD_BY_ID")
Signed-off-by: Peng Sun <[email protected]>
Acked-by: Martin KaFai Lau <[email protected]>
Signed-off-by: Alexei Starovoitov <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
kernel/bpf/syscall.c

index f98328abe8fabea3ff34bb34d499278fc878e972..84470d1480aa4c4cfc4a5df70e4849d426bf922b 100644 (file)
@@ -559,12 +559,12 @@ static int map_create(union bpf_attr *attr)
        err = bpf_map_new_fd(map, f_flags);
        if (err < 0) {
                /* failed to allocate fd.
-                * bpf_map_put() is needed because the above
+                * bpf_map_put_with_uref() is needed because the above
                 * bpf_map_alloc_id() has published the map
                 * to the userspace and the userspace may
                 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
                 */
-               bpf_map_put(map);
+               bpf_map_put_with_uref(map);
                return err;
        }