bpf: properly check TCP_CONGESTION optlen
Subsystems (4)
PATCH 1/1 bpf: properly check TCP_CONGESTION optlen
If caller of bpf_setsockopt() is silly passing a negative optlen bad things happen.
--- net/core/filter.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
net/core/filter.c +3 -2
@@ -4194,8 +4194,9 @@ BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,char name[TCP_CA_NAME_MAX ];bool reinit = bpf_sock->op >BPF_SOCK_OPS_NEEDS_ECN ; -strncpy (name, optval,min_t (long, optlen, -TCP_CA_NAME_MAX -1 )); +if (optlen <0 ) +return -EINVAL ; +strncpy (name, optval,min (optlen,TCP_CA_NAME_MAX -1 )); name[TCP_CA_NAME_MAX -1 ] =0 ; ret =tcp_set_congestion_control (sk, name, false, reinit);
Signature · 1 line
2.21.0.rc0.258.g878e2cd30e-goog
Quoted 15 lines · Eric Dumazet
If caller of
bpf_setsockopt()is silly passing a negative optlen bad things happen.Fixes 91b5b21c7c16 ("bpf: Add support for changing congestion control")Signed-off-by Eric Dumazet <[email protected]>Cc Lawrence Brakmo <[email protected]>--- net/core/filter.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)net/core/filter.c +0
-0
net/core/filter.c BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, · line 4199
Quoted 8 lines
net/core/filter.c +3
-2@@ -4194,8 +4194,9 @@ BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,char name[TCP_CA_NAME_MAX ];bool reinit = bpf_sock->op >BPF_SOCK_OPS_NEEDS_ECN ; -strncpy (name, optval,min_t (long, optlen, -TCP_CA_NAME_MAX -1 )); +if (optlen <0 ) +return -EINVAL ; +strncpy (name, optval,min (optlen,TCP_CA_NAME_MAX -1 ));
Unnecessary.
The verifier guarantees that optlen > 0 because static const struct bpf_func_proto bpf_setsockopt_proto = {
.func = bpf_setsockopt,
...
.arg5_type = ARG_CONST_SIZE,
};
Quoted 19 lines · Alexei Starovoitov
Quoted 18 lines · Eric Dumazet
If caller of
bpf_setsockopt()is silly passing a negative optlen bad things happen.Fixes 91b5b21c7c16 ("bpf: Add support for changing congestion control")Signed-off-by Eric Dumazet <[email protected]>Cc Lawrence Brakmo <[email protected]>--- net/core/filter.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)net/core/filter.c +0
-0@@ -4194,8 +4194,9 @@ BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,char name[TCP_CA_NAME_MAX ];bool reinit = bpf_sock->op >BPF_SOCK_OPS_NEEDS_ECN ;
Quoted 14 lines
net/core/filter.c BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, · line 4199
Quoted 5 lines
net/core/filter.c +3
-2-strncpy (name, optval,min_t (long, optlen, -TCP_CA_NAME_MAX -1 )); +if (optlen <0 ) +return -EINVAL ; +strncpy (name, optval,min (optlen,TCP_CA_NAME_MAX -1 ));Unnecessary.
The verifier guarantees that optlen > 0 because static const structbpf_func_protobpf_setsockopt_proto= {
.func =bpf_setsockopt,
...
.arg5_type =ARG_CONST_SIZE,
};
Even on 32bit kernel ?
The suspect thing to me is the min_t(long, ....)
optlen is an integer, why do we need to promote to a long ?
Quoted 37 lines · Eric Dumazet
Quoted 19 lines · Alexei Starovoitov
Quoted 18 lines · Eric Dumazet
If caller of
bpf_setsockopt()is silly passing a negative optlen bad things happen.Fixes 91b5b21c7c16 ("bpf: Add support for changing congestion control")Signed-off-by Eric Dumazet <[email protected]>Cc Lawrence Brakmo <[email protected]>--- net/core/filter.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)net/core/filter.c +0
-0@@ -4194,8 +4194,9 @@ BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,char name[TCP_CA_NAME_MAX ];bool reinit = bpf_sock->op >BPF_SOCK_OPS_NEEDS_ECN ;Quoted 14 lines
net/core/filter.c BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, · line 4199
Quoted 5 lines
net/core/filter.c +3
-2-strncpy (name, optval,min_t (long, optlen, -TCP_CA_NAME_MAX -1 )); +if (optlen <0 ) +return -EINVAL ; +strncpy (name, optval,min (optlen,TCP_CA_NAME_MAX -1 ));Unnecessary.
The verifier guarantees that optlen > 0 because static const structbpf_func_protobpf_setsockopt_proto= {
.func =bpf_setsockopt,
...
.arg5_type =ARG_CONST_SIZE,
};
Quoted 5 lines
Even on 32bit kernel ?
The suspect thing to me is the
min_t(long, ....)optlen is an integer, why do we need to promote to a long ?
I think the code is actually fine as-is.
I bet it was copy pasted from do_tcp_setsockopt
where similar min_t(long) is used to match strncpy_from_user() declaration. Here min_t(long) or min_t(int) or min() doesn't matter.
I would keep it as-is to avoid noisy patches.
Daniel Borkmann
maintainerQuoted 39 lines · Alexei Starovoitov
Quoted 38 lines · Eric Dumazet
Quoted 19 lines · Alexei Starovoitov
Quoted 18 lines · Eric Dumazet
If caller of
bpf_setsockopt()is silly passing a negative optlen bad things happen.Fixes 91b5b21c7c16 ("bpf: Add support for changing congestion control")Signed-off-by Eric Dumazet <[email protected]>Cc Lawrence Brakmo <[email protected]>--- net/core/filter.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)net/core/filter.c +0
-0@@ -4194,8 +4194,9 @@ BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,char name[TCP_CA_NAME_MAX ];bool reinit = bpf_sock->op >BPF_SOCK_OPS_NEEDS_ECN ;Quoted 13 lines
net/core/filter.c BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, · line 4199
Quoted 5 lines
net/core/filter.c +3
-2-strncpy (name, optval,min_t (long, optlen, -TCP_CA_NAME_MAX -1 )); +if (optlen <0 ) +return -EINVAL ; +strncpy (name, optval,min (optlen,TCP_CA_NAME_MAX -1 ));Unnecessary.
The verifier guarantees that optlen > 0 because static const structbpf_func_protobpf_setsockopt_proto= {
.func =bpf_setsockopt,
...
.arg5_type =ARG_CONST_SIZE,
};Even on 32bit kernel ?
The suspect thing to me is the
min_t(long, ....)
Quoted 7 lines
Quoted 1 line
optlen is an integer, why do we need to promote to a long ?
I think the code is actually fine as-is.
I bet it was copy pasted fromdo_tcp_setsockopt
where similarmin_t(long)is used to matchstrncpy_from_user()declaration. Heremin_t(long)ormin_t(int)ormin()doesn't matter.
I would keep it as-is to avoid noisy patches.
Max allowed input from verifier should be BPF_MAX_VAR_SIZ which is 1 << 29, but I totally agree that the bpf_setsockopt() and bpf_getsockopt() signature should change into u32 optlen as it's just confusing otherwise, same with the long in strncpy(). Agree with Alexei that this might have been a copy-paste kind of thing. Lawrence can probably clarify best?
The same wrong assumption is in commit 1e215300f138 ("bpf: add TCP_SAVE_SYN/ TCP_SAVED_SYN options for bpf_(set|get)sockopt") which tests for optlen <= 0. Neither can it be negative nor zero here due to ARG_CONST_SIZE. Given it's also confusing others, cleanup might still be worth considering. Maybe Lawrence can spin this into one of his next patches, if noone else gets to it first.
Thanks,
Daniel
Quoted 55 lines · Daniel Borkmann
Quoted 39 lines · Alexei Starovoitov
Quoted 38 lines · Eric Dumazet
Quoted 19 lines · Alexei Starovoitov
Quoted 18 lines · Eric Dumazet
If caller of
bpf_setsockopt()is silly passing a negative optlen bad things happen.Fixes 91b5b21c7c16 ("bpf: Add support for changing congestion control")Signed-off-by Eric Dumazet <[email protected]>Cc Lawrence Brakmo <[email protected]>--- net/core/filter.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)net/core/filter.c +0
-0@@ -4194,8 +4194,9 @@ BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,char name[TCP_CA_NAME_MAX ];bool reinit = bpf_sock->op >BPF_SOCK_OPS_NEEDS_ECN ;Quoted 13 lines
net/core/filter.c BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, · line 4199
Quoted 5 lines
net/core/filter.c +3
-2-strncpy (name, optval,min_t (long, optlen, -TCP_CA_NAME_MAX -1 )); +if (optlen <0 ) +return -EINVAL ; +strncpy (name, optval,min (optlen,TCP_CA_NAME_MAX -1 ));Unnecessary.
The verifier guarantees that optlen > 0 because static const structbpf_func_protobpf_setsockopt_proto= {
.func =bpf_setsockopt,
...
.arg5_type =ARG_CONST_SIZE,
};Even on 32bit kernel ?
The suspect thing to me is the
min_t(long, ....)Quoted 7 lines
Quoted 1 line
optlen is an integer, why do we need to promote to a long ?
I think the code is actually fine as-is.
I bet it was copy pasted fromdo_tcp_setsockopt
where similarmin_t(long)is used to matchstrncpy_from_user()declaration. Heremin_t(long)ormin_t(int)ormin()doesn't matter.
I would keep it as-is to avoid noisy patches.Max allowed input from verifier should be
BPF_MAX_VAR_SIZwhich is 1 << 29, but I totally agree that thebpf_setsockopt()andbpf_getsockopt()signature should change into u32 optlen as it's just confusing otherwise, same with the long instrncpy(). Agree with Alexei that this might have been a copy-paste kind of thing. Lawrence can probably clarify best?
Quoted 5 lines
The same wrong assumption is in commit
1e215300f138("bpf: add TCP_SAVE_SYN/TCP_SAVED_SYNoptions for bpf_(set|get)sockopt") which tests for optlen <= 0. Neither can it be negative nor zero here due toARG_CONST_SIZE. Given it's also confusing others, cleanup might still be worth considering. Maybe Lawrence can spin this into one of his next patches, if noone else gets to it first.
Yes, and considering the err_clear: error path does
memset(optval, 0, optlen);
This is even more confusing to think that optlen could be negative or out of bound ;)