Skip to content

Commit 8d650cd

Browse files
edumazetdavem330
authored andcommitted
tcp: fix tcp_set_congestion_control() use from bpf hook
Neal reported incorrect use of ns_capable() from bpf hook. bpf_setsockopt(...TCP_CONGESTION...) -> tcp_set_congestion_control() -> ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN) -> ns_capable_common() -> current_cred() -> rcu_dereference_protected(current->cred, 1) Accessing 'current' in bpf context makes no sense, since packets are processed from softirq context. As Neal stated : The capability check in tcp_set_congestion_control() was written assuming a system call context, and then was reused from a BPF call site. The fix is to add a new parameter to tcp_set_congestion_control(), so that the ns_capable() call is only performed under the right context. Fixes: 91b5b21 ("bpf: Add support for changing congestion control") Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Lawrence Brakmo <brakmo@fb.com> Reported-by: Neal Cardwell <ncardwell@google.com> Acked-by: Neal Cardwell <ncardwell@google.com> Acked-by: Lawrence Brakmo <brakmo@fb.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 269b7c5 commit 8d650cd

4 files changed

Lines changed: 9 additions & 6 deletions

File tree

‎include/net/tcp.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,8 @@ void tcp_get_default_congestion_control(struct net *net, char *name);
10641064
void tcp_get_available_congestion_control(char *buf, size_t len);
10651065
void tcp_get_allowed_congestion_control(char *buf, size_t len);
10661066
int tcp_set_allowed_congestion_control(char *allowed);
1067-
int tcp_set_congestion_control(struct sock *sk, const char *name, bool load, bool reinit);
1067+
int tcp_set_congestion_control(struct sock *sk, const char *name, bool load,
1068+
bool reinit, bool cap_net_admin);
10681069
u32 tcp_slow_start(struct tcp_sock *tp, u32 acked);
10691070
void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked);
10701071

‎net/core/filter.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4335,7 +4335,7 @@ BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
43354335
TCP_CA_NAME_MAX-1));
43364336
name[TCP_CA_NAME_MAX-1] = 0;
43374337
ret = tcp_set_congestion_control(sk, name, false,
4338-
reinit);
4338+
reinit, true);
43394339
} else {
43404340
struct tcp_sock *tp = tcp_sk(sk);
43414341

‎net/ipv4/tcp.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2785,7 +2785,9 @@ static int do_tcp_setsockopt(struct sock *sk, int level,
27852785
name[val] = 0;
27862786

27872787
lock_sock(sk);
2788-
err = tcp_set_congestion_control(sk, name, true, true);
2788+
err = tcp_set_congestion_control(sk, name, true, true,
2789+
ns_capable(sock_net(sk)->user_ns,
2790+
CAP_NET_ADMIN));
27892791
release_sock(sk);
27902792
return err;
27912793
}

‎net/ipv4/tcp_cong.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ int tcp_set_allowed_congestion_control(char *val)
333333
* tcp_reinit_congestion_control (if the current congestion control was
334334
* already initialized.
335335
*/
336-
int tcp_set_congestion_control(struct sock *sk, const char *name, bool load, bool reinit)
336+
int tcp_set_congestion_control(struct sock *sk, const char *name, bool load,
337+
bool reinit, bool cap_net_admin)
337338
{
338339
struct inet_connection_sock *icsk = inet_csk(sk);
339340
const struct tcp_congestion_ops *ca;
@@ -369,8 +370,7 @@ int tcp_set_congestion_control(struct sock *sk, const char *name, bool load, boo
369370
} else {
370371
err = -EBUSY;
371372
}
372-
} else if (!((ca->flags & TCP_CONG_NON_RESTRICTED) ||
373-
ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))) {
373+
} else if (!((ca->flags & TCP_CONG_NON_RESTRICTED) || cap_net_admin)) {
374374
err = -EPERM;
375375
} else if (!try_module_get(ca->owner)) {
376376
err = -EBUSY;

0 commit comments

Comments
 (0)