Skip to content

Commit e137eeb

Browse files
borkmanngregkh
authored andcommitted
tun, bpf: fix suspicious RCU usage in tun_{attach, detach}_filter
[ Upstream commit 5a5abb1 ] Sasha Levin reported a suspicious rcu_dereference_protected() warning found while fuzzing with trinity that is similar to this one: [ 52.765684] net/core/filter.c:2262 suspicious rcu_dereference_protected() usage! [ 52.765688] other info that might help us debug this: [ 52.765695] rcu_scheduler_active = 1, debug_locks = 1 [ 52.765701] 1 lock held by a.out/1525: [ 52.765704] #0: (rtnl_mutex){+.+.+.}, at: [<ffffffff816a64b7>] rtnl_lock+0x17/0x20 [ 52.765721] stack backtrace: [ 52.765728] CPU: 1 PID: 1525 Comm: a.out Not tainted 4.5.0+ #264 [...] [ 52.765768] Call Trace: [ 52.765775] [<ffffffff813e488d>] dump_stack+0x85/0xc8 [ 52.765784] [<ffffffff810f2fa5>] lockdep_rcu_suspicious+0xd5/0x110 [ 52.765792] [<ffffffff816afdc2>] sk_detach_filter+0x82/0x90 [ 52.765801] [<ffffffffa0883425>] tun_detach_filter+0x35/0x90 [tun] [ 52.765810] [<ffffffffa0884ed4>] __tun_chr_ioctl+0x354/0x1130 [tun] [ 52.765818] [<ffffffff8136fed0>] ? selinux_file_ioctl+0x130/0x210 [ 52.765827] [<ffffffffa0885ce3>] tun_chr_ioctl+0x13/0x20 [tun] [ 52.765834] [<ffffffff81260ea6>] do_vfs_ioctl+0x96/0x690 [ 52.765843] [<ffffffff81364af3>] ? security_file_ioctl+0x43/0x60 [ 52.765850] [<ffffffff81261519>] SyS_ioctl+0x79/0x90 [ 52.765858] [<ffffffff81003ba2>] do_syscall_64+0x62/0x140 [ 52.765866] [<ffffffff817d563f>] entry_SYSCALL64_slow_path+0x25/0x25 Same can be triggered with PROVE_RCU (+ PROVE_RCU_REPEATEDLY) enabled from tun_attach_filter() when user space calls ioctl(tun_fd, TUN{ATTACH, DETACH}FILTER, ...) for adding/removing a BPF filter on tap devices. Since the fix in f91ff5b ("net: sk_{detach|attach}_filter() rcu fixes") sk_attach_filter()/sk_detach_filter() now dereferences the filter with rcu_dereference_protected(), checking whether socket lock is held in control path. Since its introduction in 9940516 ("tun: socket filter support"), tap filters are managed under RTNL lock from __tun_chr_ioctl(). Thus the sock_owned_by_user(sk) doesn't apply in this specific case and therefore triggers the false positive. Extend the BPF API with __sk_attach_filter()/__sk_detach_filter() pair that is used by tap filters and pass in lockdep_rtnl_is_held() for the rcu_dereference_protected() checks instead. Reported-by: Sasha Levin <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: David S. Miller <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 18baf0e commit e137eeb

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

drivers/net/tun.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,8 @@ static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filte
621621

622622
/* Re-attach the filter to persist device */
623623
if (!skip_filter && (tun->filter_attached == true)) {
624-
err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
624+
err = __sk_attach_filter(&tun->fprog, tfile->socket.sk,
625+
lockdep_rtnl_is_held());
625626
if (!err)
626627
goto out;
627628
}
@@ -1804,7 +1805,7 @@ static void tun_detach_filter(struct tun_struct *tun, int n)
18041805

18051806
for (i = 0; i < n; i++) {
18061807
tfile = rtnl_dereference(tun->tfiles[i]);
1807-
sk_detach_filter(tfile->socket.sk);
1808+
__sk_detach_filter(tfile->socket.sk, lockdep_rtnl_is_held());
18081809
}
18091810

18101811
tun->filter_attached = false;
@@ -1817,7 +1818,8 @@ static int tun_attach_filter(struct tun_struct *tun)
18171818

18181819
for (i = 0; i < tun->numqueues; i++) {
18191820
tfile = rtnl_dereference(tun->tfiles[i]);
1820-
ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
1821+
ret = __sk_attach_filter(&tun->fprog, tfile->socket.sk,
1822+
lockdep_rtnl_is_held());
18211823
if (ret) {
18221824
tun_detach_filter(tun, i);
18231825
return ret;

include/linux/filter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,12 @@ int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
446446
void bpf_prog_destroy(struct bpf_prog *fp);
447447

448448
int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
449+
int __sk_attach_filter(struct sock_fprog *fprog, struct sock *sk,
450+
bool locked);
449451
int sk_attach_bpf(u32 ufd, struct sock *sk);
450452
int sk_detach_filter(struct sock *sk);
453+
int __sk_detach_filter(struct sock *sk, bool locked);
454+
451455
int sk_get_filter(struct sock *sk, struct sock_filter __user *filter,
452456
unsigned int len);
453457

net/core/filter.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,8 @@ void bpf_prog_destroy(struct bpf_prog *fp)
11391139
}
11401140
EXPORT_SYMBOL_GPL(bpf_prog_destroy);
11411141

1142-
static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1142+
static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk,
1143+
bool locked)
11431144
{
11441145
struct sk_filter *fp, *old_fp;
11451146

@@ -1155,10 +1156,8 @@ static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
11551156
return -ENOMEM;
11561157
}
11571158

1158-
old_fp = rcu_dereference_protected(sk->sk_filter,
1159-
sock_owned_by_user(sk));
1159+
old_fp = rcu_dereference_protected(sk->sk_filter, locked);
11601160
rcu_assign_pointer(sk->sk_filter, fp);
1161-
11621161
if (old_fp)
11631162
sk_filter_uncharge(sk, old_fp);
11641163

@@ -1175,7 +1174,8 @@ static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
11751174
* occurs or there is insufficient memory for the filter a negative
11761175
* errno code is returned. On success the return is zero.
11771176
*/
1178-
int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1177+
int __sk_attach_filter(struct sock_fprog *fprog, struct sock *sk,
1178+
bool locked)
11791179
{
11801180
unsigned int fsize = bpf_classic_proglen(fprog);
11811181
unsigned int bpf_fsize = bpf_prog_size(fprog->len);
@@ -1213,15 +1213,20 @@ int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
12131213
if (IS_ERR(prog))
12141214
return PTR_ERR(prog);
12151215

1216-
err = __sk_attach_prog(prog, sk);
1216+
err = __sk_attach_prog(prog, sk, locked);
12171217
if (err < 0) {
12181218
__bpf_prog_release(prog);
12191219
return err;
12201220
}
12211221

12221222
return 0;
12231223
}
1224-
EXPORT_SYMBOL_GPL(sk_attach_filter);
1224+
EXPORT_SYMBOL_GPL(__sk_attach_filter);
1225+
1226+
int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1227+
{
1228+
return __sk_attach_filter(fprog, sk, sock_owned_by_user(sk));
1229+
}
12251230

12261231
int sk_attach_bpf(u32 ufd, struct sock *sk)
12271232
{
@@ -1240,7 +1245,7 @@ int sk_attach_bpf(u32 ufd, struct sock *sk)
12401245
return -EINVAL;
12411246
}
12421247

1243-
err = __sk_attach_prog(prog, sk);
1248+
err = __sk_attach_prog(prog, sk, sock_owned_by_user(sk));
12441249
if (err < 0) {
12451250
bpf_prog_put(prog);
12461251
return err;
@@ -1913,16 +1918,15 @@ static int __init register_sk_filter_ops(void)
19131918
}
19141919
late_initcall(register_sk_filter_ops);
19151920

1916-
int sk_detach_filter(struct sock *sk)
1921+
int __sk_detach_filter(struct sock *sk, bool locked)
19171922
{
19181923
int ret = -ENOENT;
19191924
struct sk_filter *filter;
19201925

19211926
if (sock_flag(sk, SOCK_FILTER_LOCKED))
19221927
return -EPERM;
19231928

1924-
filter = rcu_dereference_protected(sk->sk_filter,
1925-
sock_owned_by_user(sk));
1929+
filter = rcu_dereference_protected(sk->sk_filter, locked);
19261930
if (filter) {
19271931
RCU_INIT_POINTER(sk->sk_filter, NULL);
19281932
sk_filter_uncharge(sk, filter);
@@ -1931,7 +1935,12 @@ int sk_detach_filter(struct sock *sk)
19311935

19321936
return ret;
19331937
}
1934-
EXPORT_SYMBOL_GPL(sk_detach_filter);
1938+
EXPORT_SYMBOL_GPL(__sk_detach_filter);
1939+
1940+
int sk_detach_filter(struct sock *sk)
1941+
{
1942+
return __sk_detach_filter(sk, sock_owned_by_user(sk));
1943+
}
19351944

19361945
int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
19371946
unsigned int len)

0 commit comments

Comments
 (0)