diff mbox series

[net] net: Properly typecast int values to set sk_max_pacing_rate

Message ID 20201020003149.215357-1-keli@akamai.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net] net: Properly typecast int values to set sk_max_pacing_rate | expand

Commit Message

Li, Ke Oct. 20, 2020, 12:31 a.m. UTC
In setsockopt(SO_MAX_PACING_RATE) on 64bit systems, sk_max_pacing_rate,
after extended from 'u32' to 'unsigned long', takes unintentionally
hiked value whenever assigned from an 'int' value with MSB=1, due to
binary sign extension in promoting s32 to u64, e.g. 0x80000000 becomes
0xFFFFFFFF80000000.

Thus inflated sk_max_pacing_rate causes subsequent getsockopt to return
~0U unexpectedly. It may also result in increased pacing rate.

Fix by explicitly casting the 'int' value to 'unsigned int' before
assigning it to sk_max_pacing_rate, for zero extension to happen.

Fixes: 76a9ebe811fb ("net: extend sk_pacing_rate to unsigned long")
Signed-off-by: Ji Li <jli@akamai.com>
Signed-off-by: Ke Li <keli@akamai.com>
Cc: Eric Dumazet <edumazet@google.com>
---
 net/core/filter.c | 2 +-
 net/core/sock.c   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)


base-commit: 0e8b8d6a2d85344d80dda5beadd98f5f86e8d3d3

Comments

Jakub Kicinski Oct. 22, 2020, 3:53 a.m. UTC | #1
On Mon, 19 Oct 2020 20:31:49 -0400 Ke Li wrote:
> In setsockopt(SO_MAX_PACING_RATE) on 64bit systems, sk_max_pacing_rate,
> after extended from 'u32' to 'unsigned long', takes unintentionally
> hiked value whenever assigned from an 'int' value with MSB=1, due to
> binary sign extension in promoting s32 to u64, e.g. 0x80000000 becomes
> 0xFFFFFFFF80000000.
> 
> Thus inflated sk_max_pacing_rate causes subsequent getsockopt to return
> ~0U unexpectedly. It may also result in increased pacing rate.
> 
> Fix by explicitly casting the 'int' value to 'unsigned int' before
> assigning it to sk_max_pacing_rate, for zero extension to happen.
> 
> Fixes: 76a9ebe811fb ("net: extend sk_pacing_rate to unsigned long")
> Signed-off-by: Ji Li <jli@akamai.com>
> Signed-off-by: Ke Li <keli@akamai.com>
> Cc: Eric Dumazet <edumazet@google.com>
> ---
>  net/core/filter.c | 2 +-
>  net/core/sock.c   | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/net/core/filter.c b/net/core/filter.c
> index c5e2a1c5fd8d..43f20c14864c 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -4693,7 +4693,7 @@ static int _bpf_setsockopt(struct sock *sk, int level, int optname,
>  				cmpxchg(&sk->sk_pacing_status,
>  					SK_PACING_NONE,
>  					SK_PACING_NEEDED);
> -			sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val;
> +			sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : (unsigned int)val;

Looks good, but please wrap this to 80 chars.

>  			sk->sk_pacing_rate = min(sk->sk_pacing_rate,
>  						 sk->sk_max_pacing_rate);
>  			break;
> diff --git a/net/core/sock.c b/net/core/sock.c
> index 4e8729357122..727ea1cc633c 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -1163,7 +1163,7 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
>  
>  	case SO_MAX_PACING_RATE:
>  		{
> -		unsigned long ulval = (val == ~0U) ? ~0UL : val;
> +		unsigned long ulval = (val == ~0U) ? ~0UL : (unsigned int)val;
>  
>  		if (sizeof(ulval) != sizeof(val) &&
>  		    optlen >= sizeof(ulval) &&
> 
> base-commit: 0e8b8d6a2d85344d80dda5beadd98f5f86e8d3d3
Li, Ke Oct. 22, 2020, 6:31 a.m. UTC | #2
Thanks for review and comment, Jakub!
Please check my v2 patch.

Best,
-Ke

On 10/21/20, 8:53 PM, "Jakub Kicinski" <kuba@kernel.org> wrote:

    On Mon, 19 Oct 2020 20:31:49 -0400 Ke Li wrote:
    > In setsockopt(SO_MAX_PACING_RATE) on 64bit systems, sk_max_pacing_rate,
    > after extended from 'u32' to 'unsigned long', takes unintentionally
    > hiked value whenever assigned from an 'int' value with MSB=1, due to
    > binary sign extension in promoting s32 to u64, e.g. 0x80000000 becomes
    > 0xFFFFFFFF80000000.
    > 
    > Thus inflated sk_max_pacing_rate causes subsequent getsockopt to return
    > ~0U unexpectedly. It may also result in increased pacing rate.
    > 
    > Fix by explicitly casting the 'int' value to 'unsigned int' before
    > assigning it to sk_max_pacing_rate, for zero extension to happen.
    > 
    > Fixes: 76a9ebe811fb ("net: extend sk_pacing_rate to unsigned long")
    > Signed-off-by: Ji Li <jli@akamai.com>
    > Signed-off-by: Ke Li <keli@akamai.com>
    > Cc: Eric Dumazet <edumazet@google.com>
    > ---
    >  net/core/filter.c | 2 +-
    >  net/core/sock.c   | 2 +-
    >  2 files changed, 2 insertions(+), 2 deletions(-)
    > 
    > diff --git a/net/core/filter.c b/net/core/filter.c
    > index c5e2a1c5fd8d..43f20c14864c 100644
    > --- a/net/core/filter.c
    > +++ b/net/core/filter.c
    > @@ -4693,7 +4693,7 @@ static int _bpf_setsockopt(struct sock *sk, int level, int optname,
    >  				cmpxchg(&sk->sk_pacing_status,
    >  					SK_PACING_NONE,
    >  					SK_PACING_NEEDED);
    > -			sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val;
    > +			sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : (unsigned int)val;

    Looks good, but please wrap this to 80 chars.

    >  			sk->sk_pacing_rate = min(sk->sk_pacing_rate,
    >  						 sk->sk_max_pacing_rate);
    >  			break;
    > diff --git a/net/core/sock.c b/net/core/sock.c
    > index 4e8729357122..727ea1cc633c 100644
    > --- a/net/core/sock.c
    > +++ b/net/core/sock.c
    > @@ -1163,7 +1163,7 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
    >  
    >  	case SO_MAX_PACING_RATE:
    >  		{
    > -		unsigned long ulval = (val == ~0U) ? ~0UL : val;
    > +		unsigned long ulval = (val == ~0U) ? ~0UL : (unsigned int)val;
    >  
    >  		if (sizeof(ulval) != sizeof(val) &&
    >  		    optlen >= sizeof(ulval) &&
    > 
    > base-commit: 0e8b8d6a2d85344d80dda5beadd98f5f86e8d3d3
diff mbox series

Patch

diff --git a/net/core/filter.c b/net/core/filter.c
index c5e2a1c5fd8d..43f20c14864c 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -4693,7 +4693,7 @@  static int _bpf_setsockopt(struct sock *sk, int level, int optname,
 				cmpxchg(&sk->sk_pacing_status,
 					SK_PACING_NONE,
 					SK_PACING_NEEDED);
-			sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val;
+			sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : (unsigned int)val;
 			sk->sk_pacing_rate = min(sk->sk_pacing_rate,
 						 sk->sk_max_pacing_rate);
 			break;
diff --git a/net/core/sock.c b/net/core/sock.c
index 4e8729357122..727ea1cc633c 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -1163,7 +1163,7 @@  int sock_setsockopt(struct socket *sock, int level, int optname,
 
 	case SO_MAX_PACING_RATE:
 		{
-		unsigned long ulval = (val == ~0U) ? ~0UL : val;
+		unsigned long ulval = (val == ~0U) ? ~0UL : (unsigned int)val;
 
 		if (sizeof(ulval) != sizeof(val) &&
 		    optlen >= sizeof(ulval) &&