bpf: btf: Avoid WARN_ON when CONFIG_REFCOUNT_FULL=y
authorMartin KaFai Lau <kafai@fb.com>
Fri, 4 May 2018 21:49:50 +0000 (14:49 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 9 May 2018 15:25:13 +0000 (17:25 +0200)
commit82e9697250977f3f87cd42e71e8daa8810e64520
tree1e10f9af9a4b3ccd1d5e063d8a0a45c6dcfaa34a
parent53a7bdfb2a2756cce8003b90817f8a6fb4d830d9
bpf: btf: Avoid WARN_ON when CONFIG_REFCOUNT_FULL=y

If CONFIG_REFCOUNT_FULL=y, refcount_inc() WARN when refcount is 0.
When creating a new btf, the initial btf->refcnt is 0 and
triggered the following:

[   34.855452] refcount_t: increment on 0; use-after-free.
[   34.856252] WARNING: CPU: 6 PID: 1857 at lib/refcount.c:153 refcount_inc+0x26/0x30
....
[   34.868809] Call Trace:
[   34.869168]  btf_new_fd+0x1af6/0x24d0
[   34.869645]  ? btf_type_seq_show+0x200/0x200
[   34.870212]  ? lock_acquire+0x3b0/0x3b0
[   34.870726]  ? security_capable+0x54/0x90
[   34.871247]  __x64_sys_bpf+0x1b2/0x310
[   34.871761]  ? __ia32_sys_bpf+0x310/0x310
[   34.872285]  ? bad_area_access_error+0x310/0x310
[   34.872894]  do_syscall_64+0x95/0x3f0

This patch uses refcount_set() instead.

Reported-by: Yonghong Song <yhs@fb.com>
Tested-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
kernel/bpf/btf.c