sol_tcp_sockopt_congestion() checks the name in optval and rejects "cdg". do_tcp_setsockopt() then copies the name from optval for lookup. If optval points into a shared BPF map, an update between the check and the copy can select CDG even though the check passed. Copy the name into a bounded local buffer and use that copy for both the CDG check and the TCP_CONGESTION request. Keep the check before the recursion guard so a request for CDG still returns -ENOTSUPP. Fixes: eb18b49ea758 ("bpf: tcp: Allow bpf-tcp-cc to call bpf_(get|set)sockopt") Cc: stable@vger.kernel.org Signed-off-by: Paul Moses --- net/core/filter.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index b7d38b075ef3..87d4191339b5 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5692,8 +5692,9 @@ static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname, static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval, int *optlen, bool getopt) { + char name[TCP_CA_NAME_MAX]; struct tcp_sock *tp; - int ret; + int len, ret; if (*optlen < 2) return -EINVAL; @@ -5708,11 +5709,15 @@ static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval, KERNEL_SOCKPTR(optlen)); } + len = min_t(int, *optlen, sizeof(name) - 1); + memcpy(name, optval, len); + name[len] = '\0'; + /* "cdg" is the only cc that alloc a ptr * in inet_csk_ca area. The bpf-tcp-cc may * overwrite this ptr after switching to cdg. */ - if (*optlen >= sizeof("cdg") - 1 && !strncmp("cdg", optval, *optlen)) + if (!strcmp(name, "cdg")) return -ENOTSUPP; /* It stops this looping @@ -5737,7 +5742,7 @@ static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval, tp->bpf_chg_cc_inprogress = 1; ret = do_tcp_setsockopt(sk, SOL_TCP, TCP_CONGESTION, - KERNEL_SOCKPTR(optval), *optlen); + KERNEL_SOCKPTR(name), len); tp->bpf_chg_cc_inprogress = 0; return ret; } -- 2.56.0.lol.gf7de81456940