[PATCH bpf-next v2] bpf: Upgrade bpf_{g,s}etsockopt return values

Ji Rongfeng posted 1 patch 1 year, 4 months ago
net/core/filter.c | 29 ++++++++++++++++-------------
1 file changed, 16 insertions(+), 13 deletions(-)
[PATCH bpf-next v2] bpf: Upgrade bpf_{g,s}etsockopt return values
Posted by Ji Rongfeng 1 year, 4 months ago
Returning -EINVAL almost all the time when error occurs is not very
helpful for the bpf prog to figure out what is wrong. This patch
upgrades some return values so that they will be much more helpful.

* return -ENOPROTOOPT when optname is unsupported

  The same as {g,s}etsockopt() syscall does. Before this patch,
  bpf_setsockopt(TCP_SAVED_SYN) already returns -ENOPROTOOPT, which
  may confuse the user, as -EINVAL is returned on other unsupported
  optnames. This patch also rejects TCP_SAVED_SYN right in
  sol_tcp_sockopt() when getopt is false, since do_tcp_setsockopt()
  is just the executor and it's not its duty to discover such error
  in bpf. We should maintain a precise allowlist to control whether
  an optname is supported and allowed to enter the executor or not.
  Functions like do_tcp_setsockopt(), their behaviour are not fully
  controllable by bpf. Imagine we let an optname pass, expecting
  -ENOPROTOOPT will be returned, but someday that optname is
  actually processed and unfortunately causes deadlock when calling
  from bpf. Thus, precise access control is essential.

* return -EOPNOTSUPP on level-related errors

  In do_ip_getsockopt(), -EOPNOTSUPP will be returned if level !=
  SOL_IP. In ipv6_getsockopt(), -ENOPROTOOPT will be returned if
  level != SOL_IPV6. To be distinguishable, the former is chosen.

* return -EBADFD when sk is not a full socket

  -EPERM or -EBUSY was an option, but in many cases one of them
  will be returned, especially under level SOL_TCP. -EBADFD is the
  better choice, since it is hardly returned in all cases. The bpf
  prog will be able to recognize it and decide what to do next.

Signed-off-by: Ji Rongfeng <SikoJobs@outlook.com>

From my point of view, changing these return values is acceptable,
because most of them are designed to be shown to the bpf prog
developer only and rarely shown in production environment.

I'll send another patch to update documentation in a proper way
after this patch is accepted, and add some tests if necessary.
---
 net/core/filter.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/net/core/filter.c b/net/core/filter.c
index 37baaa6b8fc3..44440b7d430c 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -5050,12 +5050,12 @@ static int sol_socket_sockopt(struct sock *sk, int optname,
 	case SO_BINDTODEVICE:
 		break;
 	default:
-		return -EINVAL;
+		return -ENOPROTOOPT;
 	}
 
 	if (getopt) {
 		if (optname == SO_BINDTODEVICE)
-			return -EINVAL;
+			return -ENOPROTOOPT;
 		return sk_getsockopt(sk, SOL_SOCKET, optname,
 				     KERNEL_SOCKPTR(optval),
 				     KERNEL_SOCKPTR(optlen));
@@ -5105,7 +5105,7 @@ static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname,
 		inet_csk(sk)->icsk_rto_min = timeout;
 		break;
 	default:
-		return -EINVAL;
+		return -ENOPROTOOPT;
 	}
 
 	return 0;
@@ -5169,7 +5169,7 @@ static int sol_tcp_sockopt(struct sock *sk, int optname,
 			   bool getopt)
 {
 	if (sk->sk_prot->setsockopt != tcp_setsockopt)
-		return -EINVAL;
+		return -EOPNOTSUPP;
 
 	switch (optname) {
 	case TCP_NODELAY:
@@ -5194,7 +5194,7 @@ static int sol_tcp_sockopt(struct sock *sk, int optname,
 		break;
 	default:
 		if (getopt)
-			return -EINVAL;
+			return -ENOPROTOOPT;
 		return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen);
 	}
 
@@ -5215,6 +5215,9 @@ static int sol_tcp_sockopt(struct sock *sk, int optname,
 		return do_tcp_getsockopt(sk, SOL_TCP, optname,
 					 KERNEL_SOCKPTR(optval),
 					 KERNEL_SOCKPTR(optlen));
+	} else {
+		if (optname == TCP_SAVED_SYN)
+			return -ENOPROTOOPT;
 	}
 
 	return do_tcp_setsockopt(sk, SOL_TCP, optname,
@@ -5226,7 +5229,7 @@ static int sol_ip_sockopt(struct sock *sk, int optname,
 			  bool getopt)
 {
 	if (sk->sk_family != AF_INET)
-		return -EINVAL;
+		return -EOPNOTSUPP;
 
 	switch (optname) {
 	case IP_TOS:
@@ -5234,7 +5237,7 @@ static int sol_ip_sockopt(struct sock *sk, int optname,
 			return -EINVAL;
 		break;
 	default:
-		return -EINVAL;
+		return -ENOPROTOOPT;
 	}
 
 	if (getopt)
@@ -5251,7 +5254,7 @@ static int sol_ipv6_sockopt(struct sock *sk, int optname,
 			    bool getopt)
 {
 	if (sk->sk_family != AF_INET6)
-		return -EINVAL;
+		return -EOPNOTSUPP;
 
 	switch (optname) {
 	case IPV6_TCLASS:
@@ -5260,7 +5263,7 @@ static int sol_ipv6_sockopt(struct sock *sk, int optname,
 			return -EINVAL;
 		break;
 	default:
-		return -EINVAL;
+		return -ENOPROTOOPT;
 	}
 
 	if (getopt)
@@ -5276,7 +5279,7 @@ static int __bpf_setsockopt(struct sock *sk, int level, int optname,
 			    char *optval, int optlen)
 {
 	if (!sk_fullsock(sk))
-		return -EINVAL;
+		return -EBADFD;
 
 	if (level == SOL_SOCKET)
 		return sol_socket_sockopt(sk, optname, optval, &optlen, false);
@@ -5287,7 +5290,7 @@ static int __bpf_setsockopt(struct sock *sk, int level, int optname,
 	else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP)
 		return sol_tcp_sockopt(sk, optname, optval, &optlen, false);
 
-	return -EINVAL;
+	return -EOPNOTSUPP;
 }
 
 static int _bpf_setsockopt(struct sock *sk, int level, int optname,
@@ -5304,7 +5307,7 @@ static int __bpf_getsockopt(struct sock *sk, int level, int optname,
 	int err, saved_optlen = optlen;
 
 	if (!sk_fullsock(sk)) {
-		err = -EINVAL;
+		err = -EBADFD;
 		goto done;
 	}
 
@@ -5317,7 +5320,7 @@ static int __bpf_getsockopt(struct sock *sk, int level, int optname,
 	else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6)
 		err = sol_ipv6_sockopt(sk, optname, optval, &optlen, true);
 	else
-		err = -EINVAL;
+		err = -EOPNOTSUPP;
 
 done:
 	if (err)
-- 
2.30.2
Re: [PATCH bpf-next v2] bpf: Upgrade bpf_{g,s}etsockopt return values
Posted by Martin KaFai Lau 1 year, 4 months ago
On 12/2/22 9:39 AM, Ji Rongfeng wrote:
> Returning -EINVAL almost all the time when error occurs is not very
> helpful for the bpf prog to figure out what is wrong. This patch
> upgrades some return values so that they will be much more helpful.
> 
> * return -ENOPROTOOPT when optname is unsupported
> 
>    The same as {g,s}etsockopt() syscall does. Before this patch,
>    bpf_setsockopt(TCP_SAVED_SYN) already returns -ENOPROTOOPT, which
>    may confuse the user, as -EINVAL is returned on other unsupported
>    optnames. This patch also rejects TCP_SAVED_SYN right in
>    sol_tcp_sockopt() when getopt is false, since do_tcp_setsockopt()
>    is just the executor and it's not its duty to discover such error
>    in bpf. We should maintain a precise allowlist to control whether
>    an optname is supported and allowed to enter the executor or not.
>    Functions like do_tcp_setsockopt(), their behaviour are not fully
>    controllable by bpf. Imagine we let an optname pass, expecting
>    -ENOPROTOOPT will be returned, but someday that optname is
>    actually processed and unfortunately causes deadlock when calling
>    from bpf. Thus, precise access control is essential.

Please leave the current -EINVAL to distinguish between optnames rejected by bpf 
and optnames rejected by the do_*_{get,set}sockopt().

> 
> * return -EOPNOTSUPP on level-related errors
> 
>    In do_ip_getsockopt(), -EOPNOTSUPP will be returned if level !=
>    SOL_IP. In ipv6_getsockopt(), -ENOPROTOOPT will be returned if
>    level != SOL_IPV6. To be distinguishable, the former is chosen.

I would leave this one as is also.  Are you sure the do_ip_*sockopt cannot 
handle sk_family == AF_INET6?  afaict, bpf is rejecting those optnames instead.

> 
> * return -EBADFD when sk is not a full socket
> 
>    -EPERM or -EBUSY was an option, but in many cases one of them
>    will be returned, especially under level SOL_TCP. -EBADFD is the
>    better choice, since it is hardly returned in all cases. The bpf
>    prog will be able to recognize it and decide what to do next.

This one makes sense and is useful.
Re: [PATCH bpf-next v2] bpf: Upgrade bpf_{g,s}etsockopt return values
Posted by Ji Rongfeng 1 year, 4 months ago
On 2022/12/7 2:36, Martin KaFai Lau wrote:
> On 12/2/22 9:39 AM, Ji Rongfeng wrote:
>> Returning -EINVAL almost all the time when error occurs is not very
>> helpful for the bpf prog to figure out what is wrong. This patch
>> upgrades some return values so that they will be much more helpful.
>>
>> * return -ENOPROTOOPT when optname is unsupported
>>
>>    The same as {g,s}etsockopt() syscall does. Before this patch,
>>    bpf_setsockopt(TCP_SAVED_SYN) already returns -ENOPROTOOPT, which
>>    may confuse the user, as -EINVAL is returned on other unsupported
>>    optnames. This patch also rejects TCP_SAVED_SYN right in
>>    sol_tcp_sockopt() when getopt is false, since do_tcp_setsockopt()
>>    is just the executor and it's not its duty to discover such error
>>    in bpf. We should maintain a precise allowlist to control whether
>>    an optname is supported and allowed to enter the executor or not.
>>    Functions like do_tcp_setsockopt(), their behaviour are not fully
>>    controllable by bpf. Imagine we let an optname pass, expecting
>>    -ENOPROTOOPT will be returned, but someday that optname is
>>    actually processed and unfortunately causes deadlock when calling
>>    from bpf. Thus, precise access control is essential.
> 
> Please leave the current -EINVAL to distinguish between optnames 
> rejected by bpf and optnames rejected by the do_*_{get,set}sockopt().

To reach that goal, it would be better for us to pick a value other than 
-ENOPROTOOPT or -EINVAL. This patch actually makes sk-related errors, 
level-reletad errors, optname-related errors and opt{val,len}-related 
errors distinguishable, as they should be, by leaving -EINVAL to 
opt{val,len}-related errors only. man setsockopt:

 > EINVAL optlen invalid in setsockopt().  In some cases this error
 >        can also occur for an invalid value in optval (e.g., for
 >        the IP_ADD_MEMBERSHIP option described in ip(7)).

With an unique return value, the bpf prog developer will be able to know 
that the error is "unsupported or unknown optname" for sure, saving time 
on figuring the actual cause of the error. In production environment, 
the bpf prog will be able to test whether an optname is available in 
current bpf env and decide what to do next also, which is very useful.

> 
>>
>> * return -EOPNOTSUPP on level-related errors
>>
>>    In do_ip_getsockopt(), -EOPNOTSUPP will be returned if level !=
>>    SOL_IP. In ipv6_getsockopt(), -ENOPROTOOPT will be returned if
>>    level != SOL_IPV6. To be distinguishable, the former is chosen.
> 
> I would leave this one as is also.  Are you sure the do_ip_*sockopt 
> cannot handle sk_family == AF_INET6?  afaict, bpf is rejecting those 
> optnames instead.

-EOPNOTSUPP is just picked here as an unique return value representing 
"unknown level or unsupported sk_family or mismatched protocol in 
bpf_{g,s}etsockopt()". I'm ok if you want to pick another unique value 
for them or pick three unique values for each type of error : )

> 
>>
>> * return -EBADFD when sk is not a full socket
>>
>>    -EPERM or -EBUSY was an option, but in many cases one of them
>>    will be returned, especially under level SOL_TCP. -EBADFD is the
>>    better choice, since it is hardly returned in all cases. The bpf
>>    prog will be able to recognize it and decide what to do next.
> 
> This one makes sense and is useful.
> 

Re: [PATCH bpf-next v2] bpf: Upgrade bpf_{g,s}etsockopt return values
Posted by Martin KaFai Lau 1 year, 4 months ago
On 12/7/22 3:19 AM, Ji Rongfeng wrote:
> On 2022/12/7 2:36, Martin KaFai Lau wrote:
>> On 12/2/22 9:39 AM, Ji Rongfeng wrote:
>>> Returning -EINVAL almost all the time when error occurs is not very
>>> helpful for the bpf prog to figure out what is wrong. This patch
>>> upgrades some return values so that they will be much more helpful.
>>>
>>> * return -ENOPROTOOPT when optname is unsupported
>>>
>>>    The same as {g,s}etsockopt() syscall does. Before this patch,
>>>    bpf_setsockopt(TCP_SAVED_SYN) already returns -ENOPROTOOPT, which
>>>    may confuse the user, as -EINVAL is returned on other unsupported
>>>    optnames. This patch also rejects TCP_SAVED_SYN right in
>>>    sol_tcp_sockopt() when getopt is false, since do_tcp_setsockopt()
>>>    is just the executor and it's not its duty to discover such error
>>>    in bpf. We should maintain a precise allowlist to control whether
>>>    an optname is supported and allowed to enter the executor or not.
>>>    Functions like do_tcp_setsockopt(), their behaviour are not fully
>>>    controllable by bpf. Imagine we let an optname pass, expecting
>>>    -ENOPROTOOPT will be returned, but someday that optname is
>>>    actually processed and unfortunately causes deadlock when calling
>>>    from bpf. Thus, precise access control is essential.
>>
>> Please leave the current -EINVAL to distinguish between optnames rejected by 
>> bpf and optnames rejected by the do_*_{get,set}sockopt().
> 
> To reach that goal, it would be better for us to pick a value other than 
> -ENOPROTOOPT or -EINVAL. This patch actually makes sk-related errors, 
> level-reletad errors, optname-related errors and opt{val,len}-related errors 
> distinguishable, as they should be, by leaving -EINVAL to opt{val,len}-related 
> errors only. man setsockopt:
> 
>  > EINVAL optlen invalid in setsockopt().  In some cases this error
>  >        can also occur for an invalid value in optval (e.g., for
>  >        the IP_ADD_MEMBERSHIP option described in ip(7)).
> 
> With an unique return value, the bpf prog developer will be able to know that 
> the error is "unsupported or unknown optname" for sure, saving time on figuring 
> the actual cause of the error. In production environment, the bpf prog will be 
> able to test whether an optname is available in current bpf env and decide what 
> to do next also, which is very useful.

It should be a non-goal for bpf_{set,get}sockopt to provide this level of 
granularity (optlevel vs optname vs optlen) at this point when it requires this 
kind of churns on the existing return values.  It is not like there is a lot of 
optname that supports non integer optlen.  This should not be a limit factor if 
the userspace wants to probe what optname is supported.  Hence, not convinced it 
is needed.  I would like to hear how others think.

If optname is not something that bpf_{set,get}sockopt rejects, it should then 
rely on the do_*_{get,set}sockopt for handling which includes the error value.

For TCP_SAVED_SYN, tbh, it is too hypothetical that the do_tcp_setsockopt will 
have a reasonable use case to ever support it.  If it did, we would make it 
available to the bpf also because the bpf_getsockopt(TCP_SAVED_SYN) has already 
been supported instead of now worrying about blacklisting it for the future.

-EBADFD will be useful because the same level, optname, optval, and optlen will 
fail because of different sk.
Re: [PATCH bpf-next v2] bpf: Upgrade bpf_{g,s}etsockopt return values
Posted by Ji Rongfeng 1 year, 4 months ago
I have noticed that this patch has been marked as "Changes Requested" 
for a few days, but there's no comment so far, which is abnormal and 
confusing. I will resend this patch with updated documentation a few 
days later. Please let me know if there're any suggestions. Thanks!
Re: [PATCH bpf-next v2] bpf: Upgrade bpf_{g,s}etsockopt return values
Posted by Martin KaFai Lau 1 year, 4 months ago
On 12/6/22 8:35 AM, Ji Rongfeng wrote:
> I have noticed that this patch has been marked as "Changes Requested" for a few 
> days, but there's no comment so far, which is abnormal and confusing.

It is obvious that a test is needed for this change.
Re: [PATCH bpf-next v2] bpf: Upgrade bpf_{g,s}etsockopt return values
Posted by Ji Rongfeng 1 year, 4 months ago
On 2022/12/7 2:37, Martin KaFai Lau wrote:
> On 12/6/22 8:35 AM, Ji Rongfeng wrote:
>> I have noticed that this patch has been marked as "Changes Requested" 
>> for a few days, but there's no comment so far, which is abnormal and 
>> confusing.
> 
> It is obvious that a test is needed for this change.
> 

Sure. These unique return values will be written into documentation and 
widely used in production environment. So I will add some tests, to make 
sure that they won't be changed accidentally in the future.