bpf: don't return EINVAL from {get,set}sockopt when optlen > PAGE_SIZE
Subsystems (6)
PATCH v5 2/3 selftests/bpf: make sure optvals > PAGE_SIZE are bypassed
In reply to Stanislav FomichevWe are relying on the fact, that we can pass > sizeof(int) optvals to the SOL_IP+IP_FREEBIND option (the kernel will take first 4 bytes). In the BPF program we check that we can only touch PAGE_SIZE bytes, but the real optlen is PAGE_SIZE * 2. In both cases, we override it to some predefined value and trim the optlen.
Also, let's modify exiting IP_TOS usecase to test optlen=0 case where BPF program just bypasses the data as is.
--- .../selftests/bpf/prog_tests/sockopt_sk.c | 46 +++++++++++++--- .../testing/selftests/bpf/progs/sockopt_sk.c | 54 ++++++++++++++++++- 2 files changed, 91 insertions(+), 9 deletions(-)
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +39 -7
@@ -13,6 +13,7 @@ static int getsetsockopt(void)char cc[16 ];/* TCP_CA_NAME_MAX */ } buf = {};socklen_t optlen; +char *big_buf =NULL ; fd =socket (AF_INET ,SOCK_STREAM ,0 );if (fd <0 ) {
@@ -22,24 +23,31 @@ static int getsetsockopt(void)/* IP_TOS - BPF bypass */ - buf.u8[0 ] =0x08 ; - err =setsockopt (fd,SOL_IP ,IP_TOS , &buf,1 ); + optlen =getpagesize () *2 ; + big_buf =calloc (1 , optlen); +if (!big_buf) { +log_err ("Couldn't allocate two pages" ); + goto err; + } + + *(int *)big_buf =0x08 ; + err =setsockopt (fd,SOL_IP ,IP_TOS , big_buf, optlen);if (err) {log_err ("Failed to call setsockopt(IP_TOS)" ); goto err; } - buf.u8[0 ] =0x00 ; +memset (big_buf,0 , optlen); optlen =1 ; - err =getsockopt (fd,SOL_IP ,IP_TOS , &buf, &optlen); + err =getsockopt (fd,SOL_IP ,IP_TOS , big_buf, &optlen);if (err) {log_err ("Failed to call getsockopt(IP_TOS)" ); goto err; } -if (buf.u8[0 ] !=0x08 ) { -log_err ("Unexpected getsockopt(IP_TOS) buf[0] 0x%02x != 0x08" , - buf.u8[0 ]); +if (*(int *)big_buf !=0x08 ) { +log_err ("Unexpected getsockopt(IP_TOS) optval 0x%x != 0x08" , + *(int *)big_buf); goto err; }
@@ -78,6 +86,28 @@ static int getsetsockopt(void) goto err; } +/* IP_FREEBIND - BPF can't access optval past PAGE_SIZE */ + + optlen =getpagesize () *2 ; +memset (big_buf,0 , optlen); + + err =setsockopt (fd,SOL_IP ,IP_FREEBIND , big_buf, optlen); +if (err !=0 ) { +log_err ("Failed to call setsockopt, ret=%d" , err); + goto err; + } + + err =getsockopt (fd,SOL_IP ,IP_FREEBIND , big_buf, &optlen); +if (err !=0 ) { +log_err ("Failed to call getsockopt, ret=%d" , err); + goto err; + } + +if (optlen !=1 || *(__u8 *)big_buf !=0x55 ) { +log_err ("Unexpected IP_FREEBIND getsockopt, optlen=%d, optval=0x%x" , + optlen, *(__u8 *)big_buf); + } +/* SO_SNDBUF is overwritten */ buf.u32 =0x01010101 ;
@@ -124,9 +154,11 @@ static int getsetsockopt(void) goto err; } +free (big_buf);close (fd);return 0 ; err: +free (big_buf);close (fd);return -1 ; }
tools/testing/selftests/bpf/progs/sockopt_sk.c +52 -2
94 lines not on this page · raw message
Signature · 1 line
2.27.0.290.gba653c62da-goog
PATCH v5 3/3 bpf: document optval > PAGE_SIZE behavior for sockopt hooks
In reply to Stanislav FomichevExtend existing doc with more details about requiring ctx->optlen = 0 for handling optval > PAGE_SIZE.
--- Documentation/bpf/prog_cgroup_sockopt.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
Documentation/bpf/prog_cgroup_sockopt.rst +14 -0
@@ -86,6 +86,20 @@ then the next program in the chain (A) will see those changes, *not* the original input ``setsockopt`` arguments. The potentially modified values will be then passed down to the kernel. +Large optval +============ +When the ``optval`` is greater than the ``PAGE_SIZE``, the BPF program +can access only the first ``PAGE_SIZE`` of that data. So it has to options: + +* Set ``optlen`` to zero, which indicates that the kernel should + use the original buffer from the userspace. Any modifications + done by the BPF program to the ``optval`` are ignored. +* Set ``optlen`` to the value less than ``PAGE_SIZE``, which + indicates that the kernel should use BPF's trimmed ``optval``. + +When the BPF program returns with the ``optlen`` greater than +``PAGE_SIZE``, the userspace will receive ``EFAULT`` errno. + Example =======
Signature · 1 line
2.27.0.290.gba653c62da-goog
PATCH v5 1/3 bpf: don't return EINVAL from {get,set}sockopt when optlen > PAGE_SIZE
Attaching to these hooks can break iptables because its optval is usually quite big, or at least bigger than the current PAGE_SIZE limit. David also mentioned some SCTP options can be big (around 256k).
For such optvals we expose only the first PAGE_SIZE bytes to the BPF program. BPF program has two options:
1. Set ctx->optlen to 0 to indicate that the BPF's optval should be ignored and the kernel should use original userspace value.
2. Set ctx->optlen to something that's smaller than the PAGE_SIZE.
v5:
* use ctx->optlen == 0 with trimmed buffer (Alexei Starovoitov)
* update the docs accordingly
v4:
* use temporary buffer to avoid optval == optval_end == NULL; this removes the corner case in the verifier that might assume non-zero PTR_TO_PACKET/PTR_TO_PACKET_END.
v3:
* don't increase the limit, bypass the argument
v2:
* proper comments formatting (Jakub Kicinski)
--- kernel/bpf/cgroup.c | 53 ++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 20 deletions(-)
kernel/bpf/cgroup.c +33 -20
@@ -1276,16 +1276,23 @@ static bool __cgroup_bpf_prog_array_is_empty(struct cgroup *cgrp,static int sockopt_alloc_buf (struct bpf_sockopt_kern *ctx,int max_optlen) { -if (unlikely (max_optlen >PAGE_SIZE ) || max_optlen <0 ) +if (unlikely (max_optlen <0 ))return -EINVAL ; +if (unlikely (max_optlen >PAGE_SIZE )) { +/* We don't expose optvals that are greater than PAGE_SIZE +* to the BPF program. +*/ + max_optlen =PAGE_SIZE ; + } + ctx->optval =kzalloc (max_optlen,GFP_USER );if (!ctx->optval)return -ENOMEM ; ctx->optval_end = ctx->optval + max_optlen; -return 0 ; +return max_optlen; }static void sockopt_free_buf (struct bpf_sockopt_kern *ctx)
@@ -1319,13 +1326,13 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,*/ max_optlen =max_t (int,16 , *optlen); - ret =sockopt_alloc_buf (&ctx, max_optlen); -if (ret) -return ret; + max_optlen =sockopt_alloc_buf (&ctx, max_optlen); +if (max_optlen <0 ) +return max_optlen; ctx.optlen = *optlen; -if (copy_from_user (ctx.optval, optval, *optlen) !=0 ) { +if (copy_from_user (ctx.optval, optval,min (*optlen, max_optlen)) !=0 ) { ret = -EFAULT ; goto out; }
@@ -1353,8 +1360,14 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,/* export any potential modifications */ *level = ctx.level; *optname = ctx.optname; - *optlen = ctx.optlen; - *kernel_optval = ctx.optval; + +/* optlen == 0 from BPF indicates that we should +* use original userspace data. +*/ +if (ctx.optlen !=0 ) { + *optlen = ctx.optlen; + *kernel_optval = ctx.optval; + } } out:
@@ -1385,12 +1398,12 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, __cgroup_bpf_prog_array_is_empty(cgrp, BPF_CGROUP_GETSOCKOPT))return retval; - ret =sockopt_alloc_buf (&ctx, max_optlen); -if (ret) -return ret; - ctx.optlen = max_optlen; + max_optlen =sockopt_alloc_buf (&ctx, max_optlen); +if (max_optlen <0 ) +return max_optlen; +if (!retval) {/* If kernel getsockopt finished successfully, * copy whatever was returned to the user back
@@ -1404,10 +1417,8 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, goto out; } -if (ctx.optlen > max_optlen) - ctx.optlen = max_optlen; - -if (copy_from_user (ctx.optval, optval, ctx.optlen) !=0 ) { +if (copy_from_user (ctx.optval, optval, +min (ctx.optlen, max_optlen)) !=0 ) { ret = -EFAULT ; goto out; }
@@ -1436,10 +1447,12 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, goto out; } -if (copy_to_user (optval, ctx.optval, ctx.optlen) || -put_user (ctx.optlen, optlen)) { - ret = -EFAULT ; - goto out; +if (ctx.optlen !=0 ) { +if (copy_to_user (optval, ctx.optval, ctx.optlen) || +put_user (ctx.optlen, optlen)) { + ret = -EFAULT ; + goto out; + } } ret = ctx.retval;
Signature · 1 line
2.27.0.290.gba653c62da-goog
Quoted 83 lines · Stanislav Fomichev
Attaching to these hooks can break iptables because its optval is usually quite big, or at least bigger than the current
PAGE_SIZElimit. David also mentioned some SCTP options can be big (around 256k).For such optvals we expose only the first
PAGE_SIZEbytes to the BPF program. BPF program has two options:
1. Setctx->optlento 0 to indicate that the BPF's optval should be ignored and the kernel should use original userspace value.
2. Setctx->optlento something that's smaller than thePAGE_SIZE.v5:
* usectx->optlen== 0 with trimmed buffer (Alexei Starovoitov)
* update the docs accordinglyv4:
* use temporary buffer to avoid optval ==optval_end== NULL; this removes the corner case in the verifier that might assume non-zero PTR_TO_PACKET/PTR_TO_PACKET_END.v3:
* don't increase the limit, bypass the argumentv2:
* proper comments formatting (Jakub Kicinski)Fixes 0d01da6afc54 ("bpf: implement getsockopt and setsockopt hooks")Cc David Laight <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- kernel/bpf/cgroup.c | 53 ++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 20 deletions(-)kernel/bpf/cgroup.c +13
-6@@ -1276,16 +1276,23 @@ static bool __cgroup_bpf_prog_array_is_empty(struct cgroup *cgrp,static int sockopt_alloc_buf (struct bpf_sockopt_kern *ctx,int max_optlen) { -if (unlikely (max_optlen >PAGE_SIZE ) || max_optlen <0 ) +if (unlikely (max_optlen <0 ))return -EINVAL ; +if (unlikely (max_optlen >PAGE_SIZE )) { +/* We don't expose optvals that are greater than PAGE_SIZE +* to the BPF program. +*/ + max_optlen =PAGE_SIZE ; + } + ctx->optval =kzalloc (max_optlen,GFP_USER );if (!ctx->optval)return -ENOMEM ; ctx->optval_end = ctx->optval + max_optlen; -return 0 ; +return max_optlen; }static void sockopt_free_buf (struct bpf_sockopt_kern *ctx)@@ -1319,13 +1326,13 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,*/ max_optlen =max_t (int,16 , *optlen); - ret =sockopt_alloc_buf (&ctx, max_optlen); -if (ret) -return ret; + max_optlen =sockopt_alloc_buf (&ctx, max_optlen); +if (max_optlen <0 ) +return max_optlen; ctx.optlen = *optlen; -if (copy_from_user (ctx.optval, optval, *optlen) !=0 ) { +if (copy_from_user (ctx.optval, optval,min (*optlen, max_optlen)) !=0 ) { ret = -EFAULT ; goto out; }@@ -1353,8 +1360,14 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,/* export any potential modifications */
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, · line 1368
Quoted 10 lines
kernel/bpf/cgroup.c +6
-2*level = ctx.level; *optname = ctx.optname; - *optlen = ctx.optlen; - *kernel_optval = ctx.optval; + +/* optlen == 0 from BPF indicates that we should +* use original userspace data. +*/ +if (ctx.optlen !=0 ) { + *optlen = ctx.optlen;
I think it should be:
*optlen = min(ctx.optlen, max_optlen);
Otherwise when bpf prog doesn't adjust ctx.oplen the kernel will see 4k only in kernel_optval whereas optlen will be > 4k.
I suspect iptables sockopt should have crashed at this point. How did you test it?
Quoted 4 lines
kernel/bpf/cgroup.c +2
-0+ *kernel_optval = ctx.optval; + } }
Quoted 88 lines · Alexei Starovoitov
kernel/bpf/cgroup.c static bool
Quoted 38 lines · Stanislav Fomichev
Attaching to these hooks can break iptables because its optval is usually quite big, or at least bigger than the current
PAGE_SIZElimit. David also mentioned some SCTP options can be big (around 256k).For such optvals we expose only the first
PAGE_SIZEbytes to the BPF program. BPF program has two options:
1. Setctx->optlento 0 to indicate that the BPF's optval should be ignored and the kernel should use original userspace value.
2. Setctx->optlento something that's smaller than thePAGE_SIZE.v5:
* usectx->optlen== 0 with trimmed buffer (Alexei Starovoitov)
* update the docs accordinglyv4:
* use temporary buffer to avoid optval ==optval_end== NULL; this removes the corner case in the verifier that might assume non-zero PTR_TO_PACKET/PTR_TO_PACKET_END.v3:
* don't increase the limit, bypass the argumentv2:
* proper comments formatting (Jakub Kicinski)Fixes 0d01da6afc54 ("bpf: implement getsockopt and setsockopt hooks")Cc David Laight <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- kernel/bpf/cgroup.c | 53 ++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 20 deletions(-)kernel/bpf/cgroup.c +0
-0@@ -1276,16 +1276,23 @@ static bool__cgroup_bpf_prog_array_is_empty(struct cgroup *cgrp,
kernel/bpf/cgroup.c static bool · line 1276
Quoted 2 lines
kernel/bpf/cgroup.c +0
-0static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int
max_optlen)kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_setsockopt(struct
Quoted 24 lines
kernel/bpf/cgroup.c +9
-2{ -if (unlikely (max_optlen >PAGE_SIZE ) || max_optlen <0 ) +if (unlikely (max_optlen <0 ))return -EINVAL ; +if (unlikely (max_optlen >PAGE_SIZE )) { +/* We don't expose optvals that are greater than PAGE_SIZE +* to the BPF program. +*/ + max_optlen =PAGE_SIZE ; + } + ctx->optval =kzalloc (max_optlen,GFP_USER );if (!ctx->optval)return -ENOMEM ; ctx->optval_end = ctx->optval + max_optlen; -return 0 ; +return max_optlen; }static void sockopt_free_buf (struct bpf_sockopt_kern *ctx)@@ -1319,13 +1326,13 @@ int __cgroup_bpf_run_filter_setsockopt(structsock *sk, int *level,
Quoted 4 lines
kernel/bpf/cgroup.c +0
-1*/ max_optlen =max_t (int,16 , *optlen); - ret =sockopt_alloc_buf (&ctx, max_optlen);kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
Quoted 10 lines
kernel/bpf/cgroup.c +4
-3-if (ret) -return ret; + max_optlen =sockopt_alloc_buf (&ctx, max_optlen); +if (max_optlen <0 ) +return max_optlen; ctx.optlen = *optlen; -if (copy_from_user (ctx.optval, optval, *optlen) !=0 ) { + if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) !=0) {
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_setsockopt(struct
Quoted 4 lines
kernel/bpf/cgroup.c +0
-0ret = -EFAULT ; goto out; }@@ -1353,8 +1360,14 @@ int __cgroup_bpf_run_filter_setsockopt(structsock *sk, int *level,
Quoted 14 lines
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, · line 1368
Quoted 11 lines
kernel/bpf/cgroup.c +6
-2/* export any potential modifications */ *level = ctx.level; *optname = ctx.optname; - *optlen = ctx.optlen; - *kernel_optval = ctx.optval; + +/* optlen == 0 from BPF indicates that we should +* use original userspace data. +*/ +if (ctx.optlen !=0 ) { + *optlen = ctx.optlen;I think it should be:
*optlen =min(ctx.optlen, max_optlen);
We do have the following (existing) check above:
} else if (ctx.optlen > max_optlen || ctx.optlen < -1) {
/* optlen is out of bounds */
ret = -EFAULT;
} else {So we shouldn't need any min here? Or am I missing something?
Quoted 4 lines
Otherwise when bpf prog doesn't adjust ctx.oplen the kernel will see 4k only in
kernel_optvalwhereas optlen will be > 4k.
I suspect iptables sockopt should have crashed at this point. How did you test it?
The selftests that I've attached in the series. The test is passing two pages and for IP_TOS we bypass the value via optlen=0 and for IP_FREEBIND we trim the buffer to 1 byte. I think this should cover this check here.
One thing I didn't really test is getsockopt when the kernel returns really large buffer (iptables). Right now, the test gets 4 bytes (trimmed) from the kernel. I think that's the only place that I didn't properly test. I wonder whether I should do a real iptables-like setsockopt/getsockopt :-/
Quoted 103 lines · On Wed, Jun 17, 2020 at 10:45:08AM -0700, [email protected] wrote:
Quoted 88 lines · Alexei Starovoitov
kernel/bpf/cgroup.c static bool
Quoted 38 lines · Stanislav Fomichev
Attaching to these hooks can break iptables because its optval is usually quite big, or at least bigger than the current
PAGE_SIZElimit. David also mentioned some SCTP options can be big (around 256k).For such optvals we expose only the first
PAGE_SIZEbytes to the BPF program. BPF program has two options:
1. Setctx->optlento 0 to indicate that the BPF's optval should be ignored and the kernel should use original userspace value.
2. Setctx->optlento something that's smaller than thePAGE_SIZE.v5:
* usectx->optlen== 0 with trimmed buffer (Alexei Starovoitov)
* update the docs accordinglyv4:
* use temporary buffer to avoid optval ==optval_end== NULL; this removes the corner case in the verifier that might assume non-zero PTR_TO_PACKET/PTR_TO_PACKET_END.v3:
* don't increase the limit, bypass the argumentv2:
* proper comments formatting (Jakub Kicinski)Fixes 0d01da6afc54 ("bpf: implement getsockopt and setsockopt hooks")Cc David Laight <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- kernel/bpf/cgroup.c | 53 ++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 20 deletions(-)kernel/bpf/cgroup.c +0
-0@@ -1276,16 +1276,23 @@ static bool__cgroup_bpf_prog_array_is_empty(struct cgroup *cgrp,
kernel/bpf/cgroup.c static bool · line 1276
Quoted 2 lines
kernel/bpf/cgroup.c +0
-0static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int
max_optlen)kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_setsockopt(struct
Quoted 24 lines
kernel/bpf/cgroup.c +9
-2{ -if (unlikely (max_optlen >PAGE_SIZE ) || max_optlen <0 ) +if (unlikely (max_optlen <0 ))return -EINVAL ; +if (unlikely (max_optlen >PAGE_SIZE )) { +/* We don't expose optvals that are greater than PAGE_SIZE +* to the BPF program. +*/ + max_optlen =PAGE_SIZE ; + } + ctx->optval =kzalloc (max_optlen,GFP_USER );if (!ctx->optval)return -ENOMEM ; ctx->optval_end = ctx->optval + max_optlen; -return 0 ; +return max_optlen; }static void sockopt_free_buf (struct bpf_sockopt_kern *ctx)@@ -1319,13 +1326,13 @@ int __cgroup_bpf_run_filter_setsockopt(structsock *sk, int *level,
Quoted 4 lines
kernel/bpf/cgroup.c +0
-1*/ max_optlen =max_t (int,16 , *optlen); - ret =sockopt_alloc_buf (&ctx, max_optlen);kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
Quoted 10 lines
kernel/bpf/cgroup.c +4
-3-if (ret) -return ret; + max_optlen =sockopt_alloc_buf (&ctx, max_optlen); +if (max_optlen <0 ) +return max_optlen; ctx.optlen = *optlen; -if (copy_from_user (ctx.optval, optval, *optlen) !=0 ) { + if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) !=0) {
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_setsockopt(struct
Quoted 4 lines
kernel/bpf/cgroup.c +0
-0ret = -EFAULT ; goto out; }@@ -1353,8 +1360,14 @@ int __cgroup_bpf_run_filter_setsockopt(structsock *sk, int *level,
Quoted 14 lines
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, · line 1368
Quoted 11 lines
kernel/bpf/cgroup.c +6
-2/* export any potential modifications */ *level = ctx.level; *optname = ctx.optname; - *optlen = ctx.optlen; - *kernel_optval = ctx.optval; + +/* optlen == 0 from BPF indicates that we should +* use original userspace data. +*/ +if (ctx.optlen !=0 ) { + *optlen = ctx.optlen;I think it should be:
*optlen =min(ctx.optlen, max_optlen);
Quoted 7 lines
We do have the following (existing) check above:
} else if (ctx.optlen > max_optlen || ctx.optlen < -1) { /* optlen is out of bounds */ ret = -EFAULT; } else {So we shouldn't need any min here? Or am I missing something?
ahh. you're right.
Applied to bpf tree.
Quoted 9 lines
Quoted 4 lines
Otherwise when bpf prog doesn't adjust ctx.oplen the kernel will see 4k only in
kernel_optvalwhereas optlen will be > 4k.
I suspect iptables sockopt should have crashed at this point. How did you test it?The selftests that I've attached in the series. The test is passing two pages and for
IP_TOSwe bypass the value via optlen=0 and forIP_FREEBINDwe trim the buffer to 1 byte. I think this should cover this check here.
Quoted 5 lines
One thing I didn't really test is getsockopt when the kernel returns really large buffer (iptables). Right now, the test gets 4 bytes (trimmed) from the kernel. I think that's the only place that I didn't properly test. I wonder whether I should do a real iptables-like setsockopt/getsockopt :-/
would be nice :)