diff mbox series

can: raw: raw_getsockopt(): reduce scope of err

Message ID 20240220-raw-setsockopt-v1-1-7d34cb1377fc@pengutronix.de (mailing list archive)
State Awaiting Upstream
Delegated to: Netdev Maintainers
Headers show
Series can: raw: raw_getsockopt(): reduce scope of err | expand

Checks

Context Check Description
netdev/tree_selection success Series ignored based on subject

Commit Message

Marc Kleine-Budde Feb. 20, 2024, 8:16 a.m. UTC
Reduce the scope of the variable "err" to the individual cases. This
is to avoid the mistake of setting "err" in the mistaken belief that
it will be evaluated later.

Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 net/can/raw.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)


---
base-commit: c8fba5d6df5e476aa791db4f1f014dad2bb5e904
change-id: 20240220-raw-setsockopt-f6e173cdbbbb

Best regards,

Comments

Eric Dumazet Feb. 20, 2024, 8:25 a.m. UTC | #1
On Tue, Feb 20, 2024 at 9:16 AM Marc Kleine-Budde <mkl@pengutronix.de> wrote:
>
> Reduce the scope of the variable "err" to the individual cases. This
> is to avoid the mistake of setting "err" in the mistaken belief that
> it will be evaluated later.
>
> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
> ---
>  net/can/raw.c | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/net/can/raw.c b/net/can/raw.c
> index 897ffc17d850..2bb3eab98af0 100644
> --- a/net/can/raw.c
> +++ b/net/can/raw.c
> @@ -756,7 +756,6 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>         struct raw_sock *ro = raw_sk(sk);
>         int len;
>         void *val;
> -       int err = 0;
>
>         if (level != SOL_CAN_RAW)
>                 return -EINVAL;
> @@ -766,7 +765,9 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>                 return -EINVAL;
>
>         switch (optname) {
> -       case CAN_RAW_FILTER:
> +       case CAN_RAW_FILTER: {
> +               int err = 0;
> +
>                 lock_sock(sk);
>                 if (ro->count > 0) {
>                         int fsize = ro->count * sizeof(struct can_filter);
> @@ -791,7 +792,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>                 if (!err)
>                         err = put_user(len, optlen);
>                 return err;
> -
> +       }
>         case CAN_RAW_ERR_FILTER:
>                 if (len > sizeof(can_err_mask_t))
>                         len = sizeof(can_err_mask_t);
> @@ -822,7 +823,9 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>                 val = &ro->xl_frames;
>                 break;
>
> -       case CAN_RAW_XL_VCID_OPTS:
> +       case CAN_RAW_XL_VCID_OPTS: {
> +               int err = 0;
> +
>                 /* user space buffer to small for VCID opts? */
>                 if (len < sizeof(ro->raw_vcid_opts)) {
>                         /* return -ERANGE and needed space in optlen */
> @@ -839,6 +842,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>                         err = put_user(len, optlen);
>                 return err;
>
> +       }
>         case CAN_RAW_JOIN_FILTERS:
>                 if (len > sizeof(int))
>                         len = sizeof(int);
>
> ---
> base-commit: c8fba5d6df5e476aa791db4f1f014dad2bb5e904
> change-id: 20240220-raw-setsockopt-f6e173cdbbbb

What is the target tree ?

In net-next tree, syzbot complained about a bug added in

commit c83c22ec1493c0b7cc77327bedbd387e295872b6
Author: Oliver Hartkopp <socketcan@hartkopp.net>
Date:   Mon Feb 12 22:35:50 2024 +0100

    can: canxl: add virtual CAN network identifier support

Patch to fix the issue has not been sent yet ?

diff --git a/net/can/raw.c b/net/can/raw.c
index cb8e6f788af84ac65830399baac6d1cf3d093e08..897ffc17d850670003e5cf3402477e8fc201f61e
100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -835,7 +835,9 @@ static int raw_getsockopt(struct socket *sock, int
level, int optname,
                        if (copy_to_user(optval, &ro->raw_vcid_opts, len))
                                err = -EFAULT;
                }
-               break;
+               if (!err)
+                       err = put_user(len, optlen);
+               return err;

        case CAN_RAW_JOIN_FILTERS:
                if (len > sizeof(int))
Vincent Mailhol Feb. 20, 2024, 8:27 a.m. UTC | #2
On Tue. 20 Feb. 2024 at 17:16, Marc Kleine-Budde <mkl@pengutronix.de> wrote:
> Reduce the scope of the variable "err" to the individual cases. This
> is to avoid the mistake of setting "err" in the mistaken belief that
> it will be evaluated later.
>
> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

One nitpick to remove a newline, but aside from that:

Reviewed-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>

(as usual, I do not mind if the nitpick gets resolved while applying).

> ---
>  net/can/raw.c | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/net/can/raw.c b/net/can/raw.c
> index 897ffc17d850..2bb3eab98af0 100644
> --- a/net/can/raw.c
> +++ b/net/can/raw.c
> @@ -756,7 +756,6 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>         struct raw_sock *ro = raw_sk(sk);
>         int len;
>         void *val;
> -       int err = 0;
>
>         if (level != SOL_CAN_RAW)
>                 return -EINVAL;
> @@ -766,7 +765,9 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>                 return -EINVAL;
>
>         switch (optname) {
> -       case CAN_RAW_FILTER:
> +       case CAN_RAW_FILTER: {

Aesthetically speaking, I do not like turning the cases into compound
statement, but this removes a pitfall, so let go for it!

> +               int err = 0;
> +
>                 lock_sock(sk);
>                 if (ro->count > 0) {
>                         int fsize = ro->count * sizeof(struct can_filter);
> @@ -791,7 +792,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>                 if (!err)
>                         err = put_user(len, optlen);
>                 return err;
> -
> +       }
>         case CAN_RAW_ERR_FILTER:
>                 if (len > sizeof(can_err_mask_t))
>                         len = sizeof(can_err_mask_t);
> @@ -822,7 +823,9 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>                 val = &ro->xl_frames;
>                 break;
>
> -       case CAN_RAW_XL_VCID_OPTS:
> +       case CAN_RAW_XL_VCID_OPTS: {
> +               int err = 0;
> +
>                 /* user space buffer to small for VCID opts? */
>                 if (len < sizeof(ro->raw_vcid_opts)) {
>                         /* return -ERANGE and needed space in optlen */
> @@ -839,6 +842,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>                         err = put_user(len, optlen);
>                 return err;
>
> +       }

Nitpick: to be aligned with the above, also remove the newline here
(same as above):

-
+       }

>         case CAN_RAW_JOIN_FILTERS:
>                 if (len > sizeof(int))
>                         len = sizeof(int);
Oliver Hartkopp Feb. 20, 2024, 8:37 a.m. UTC | #3
On 2024-02-20 09:25, Eric Dumazet wrote:
> On Tue, Feb 20, 2024 at 9:16 AM Marc Kleine-Budde <mkl@pengutronix.de> wrote:
>>
>> Reduce the scope of the variable "err" to the individual cases. This
>> is to avoid the mistake of setting "err" in the mistaken belief that
>> it will be evaluated later.
>>
>> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
>> ---
>>   net/can/raw.c | 12 ++++++++----
>>   1 file changed, 8 insertions(+), 4 deletions(-)
>>
>> diff --git a/net/can/raw.c b/net/can/raw.c
>> index 897ffc17d850..2bb3eab98af0 100644
>> --- a/net/can/raw.c
>> +++ b/net/can/raw.c
>> @@ -756,7 +756,6 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>>          struct raw_sock *ro = raw_sk(sk);
>>          int len;
>>          void *val;
>> -       int err = 0;
>>
>>          if (level != SOL_CAN_RAW)
>>                  return -EINVAL;
>> @@ -766,7 +765,9 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>>                  return -EINVAL;
>>
>>          switch (optname) {
>> -       case CAN_RAW_FILTER:
>> +       case CAN_RAW_FILTER: {
>> +               int err = 0;
>> +
>>                  lock_sock(sk);
>>                  if (ro->count > 0) {
>>                          int fsize = ro->count * sizeof(struct can_filter);
>> @@ -791,7 +792,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>>                  if (!err)
>>                          err = put_user(len, optlen);
>>                  return err;
>> -
>> +       }
>>          case CAN_RAW_ERR_FILTER:
>>                  if (len > sizeof(can_err_mask_t))
>>                          len = sizeof(can_err_mask_t);
>> @@ -822,7 +823,9 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>>                  val = &ro->xl_frames;
>>                  break;
>>
>> -       case CAN_RAW_XL_VCID_OPTS:
>> +       case CAN_RAW_XL_VCID_OPTS: {
>> +               int err = 0;
>> +
>>                  /* user space buffer to small for VCID opts? */
>>                  if (len < sizeof(ro->raw_vcid_opts)) {
>>                          /* return -ERANGE and needed space in optlen */
>> @@ -839,6 +842,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>>                          err = put_user(len, optlen);
>>                  return err;
>>
>> +       }
>>          case CAN_RAW_JOIN_FILTERS:
>>                  if (len > sizeof(int))
>>                          len = sizeof(int);
>>
>> ---
>> base-commit: c8fba5d6df5e476aa791db4f1f014dad2bb5e904
>> change-id: 20240220-raw-setsockopt-f6e173cdbbbb
> 
> What is the target tree ?
> 
> In net-next tree, syzbot complained about a bug added in
> 
> commit c83c22ec1493c0b7cc77327bedbd387e295872b6
> Author: Oliver Hartkopp <socketcan@hartkopp.net>
> Date:   Mon Feb 12 22:35:50 2024 +0100
> 
>      can: canxl: add virtual CAN network identifier support
> 
> Patch to fix the issue has not been sent yet ?

The patch with the fix has been sent yesterday and Marc applied it to 
his linux-can-next tree for a pull request to net-next.

https://lore.kernel.org/linux-can/20240220-mobility-thigh-8ddfb02bfab9-mkl@pengutronix.de/T/#t

Best regards,
Oliver

> 
> diff --git a/net/can/raw.c b/net/can/raw.c
> index cb8e6f788af84ac65830399baac6d1cf3d093e08..897ffc17d850670003e5cf3402477e8fc201f61e
> 100644
> --- a/net/can/raw.c
> +++ b/net/can/raw.c
> @@ -835,7 +835,9 @@ static int raw_getsockopt(struct socket *sock, int
> level, int optname,
>                          if (copy_to_user(optval, &ro->raw_vcid_opts, len))
>                                  err = -EFAULT;
>                  }
> -               break;
> +               if (!err)
> +                       err = put_user(len, optlen);
> +               return err;
> 
>          case CAN_RAW_JOIN_FILTERS:
>                  if (len > sizeof(int))
>
Marc Kleine-Budde Feb. 20, 2024, 8:37 a.m. UTC | #4
On 20.02.2024 09:25:58, Eric Dumazet wrote:
> On Tue, Feb 20, 2024 at 9:16 AM Marc Kleine-Budde <mkl@pengutronix.de> wrote:
> >
> > Reduce the scope of the variable "err" to the individual cases. This
> > is to avoid the mistake of setting "err" in the mistaken belief that
> > it will be evaluated later.
> >
> > Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
> > ---
> >  net/can/raw.c | 12 ++++++++----
> >  1 file changed, 8 insertions(+), 4 deletions(-)
> >
> > diff --git a/net/can/raw.c b/net/can/raw.c
> > index 897ffc17d850..2bb3eab98af0 100644
> > --- a/net/can/raw.c
> > +++ b/net/can/raw.c
> > @@ -756,7 +756,6 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
> >         struct raw_sock *ro = raw_sk(sk);
> >         int len;
> >         void *val;
> > -       int err = 0;
> >
> >         if (level != SOL_CAN_RAW)
> >                 return -EINVAL;
> > @@ -766,7 +765,9 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
> >                 return -EINVAL;
> >
> >         switch (optname) {
> > -       case CAN_RAW_FILTER:
> > +       case CAN_RAW_FILTER: {
> > +               int err = 0;
> > +
> >                 lock_sock(sk);
> >                 if (ro->count > 0) {
> >                         int fsize = ro->count * sizeof(struct can_filter);
> > @@ -791,7 +792,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
> >                 if (!err)
> >                         err = put_user(len, optlen);
> >                 return err;
> > -
> > +       }
> >         case CAN_RAW_ERR_FILTER:
> >                 if (len > sizeof(can_err_mask_t))
> >                         len = sizeof(can_err_mask_t);
> > @@ -822,7 +823,9 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
> >                 val = &ro->xl_frames;
> >                 break;
> >
> > -       case CAN_RAW_XL_VCID_OPTS:
> > +       case CAN_RAW_XL_VCID_OPTS: {
> > +               int err = 0;
> > +
> >                 /* user space buffer to small for VCID opts? */
> >                 if (len < sizeof(ro->raw_vcid_opts)) {
> >                         /* return -ERANGE and needed space in optlen */
> > @@ -839,6 +842,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
> >                         err = put_user(len, optlen);
> >                 return err;
> >
> > +       }
> >         case CAN_RAW_JOIN_FILTERS:
> >                 if (len > sizeof(int))
> >                         len = sizeof(int);
> >
> > ---
> > base-commit: c8fba5d6df5e476aa791db4f1f014dad2bb5e904
> > change-id: 20240220-raw-setsockopt-f6e173cdbbbb
> 
> What is the target tree ?

Sorry I have to fine tune my b4 setup, this targets net-next. And
depends on https://lore.kernel.org/all/20240219200021.12113-1-socketcan@hartkopp.net/

> In net-next tree, syzbot complained about a bug added in
> 
> commit c83c22ec1493c0b7cc77327bedbd387e295872b6
> Author: Oliver Hartkopp <socketcan@hartkopp.net>
> Date:   Mon Feb 12 22:35:50 2024 +0100
> 
>     can: canxl: add virtual CAN network identifier support
> 
> Patch to fix the issue has not been sent yet ?
> 
> diff --git a/net/can/raw.c b/net/can/raw.c
> index cb8e6f788af84ac65830399baac6d1cf3d093e08..897ffc17d850670003e5cf3402477e8fc201f61e
> 100644
> --- a/net/can/raw.c
> +++ b/net/can/raw.c
> @@ -835,7 +835,9 @@ static int raw_getsockopt(struct socket *sock, int
> level, int optname,
>                         if (copy_to_user(optval, &ro->raw_vcid_opts, len))
>                                 err = -EFAULT;
>                 }
> -               break;
> +               if (!err)
> +                       err = put_user(len, optlen);
> +               return err;
> 
>         case CAN_RAW_JOIN_FILTERS:
>                 if (len > sizeof(int))
>

The above mentioned patch fixes the issue introduced in c83c22ec1493
("can: canxl: add virtual CAN network identifier support").

regards,
Marc
Marc Kleine-Budde Feb. 20, 2024, 8:42 a.m. UTC | #5
On 20.02.2024 17:27:05, Vincent Mailhol wrote:
> On Tue. 20 Feb. 2024 at 17:16, Marc Kleine-Budde <mkl@pengutronix.de> wrote:
> > Reduce the scope of the variable "err" to the individual cases. This
> > is to avoid the mistake of setting "err" in the mistaken belief that
> > it will be evaluated later.
> >
> > Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
> 
> One nitpick to remove a newline, but aside from that:
> 
> Reviewed-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
> 
> (as usual, I do not mind if the nitpick gets resolved while applying).

Thanks for the review, fixed that while applying.

regards,
Marc
Marc Kleine-Budde Feb. 20, 2024, 8:55 a.m. UTC | #6
On 20.02.2024 09:25:58, Eric Dumazet wrote:
> In net-next tree, syzbot complained about a bug added in
> 
> commit c83c22ec1493c0b7cc77327bedbd387e295872b6
> Author: Oliver Hartkopp <socketcan@hartkopp.net>
> Date:   Mon Feb 12 22:35:50 2024 +0100
> 
>     can: canxl: add virtual CAN network identifier support
> 
> Patch to fix the issue has not been sent yet ?

Done, it's part of this PR:

| https://lore.kernel.org/all/20240220085130.2936533-9-mkl@pengutronix.de/

regards,
Marc
David Laight Feb. 21, 2024, 10:44 p.m. UTC | #7
From: Marc Kleine-Budde
> Sent: 20 February 2024 08:16
> 
> Reduce the scope of the variable "err" to the individual cases. This
> is to avoid the mistake of setting "err" in the mistaken belief that
> it will be evaluated later.
> 
> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
> ---
>  net/can/raw.c | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/net/can/raw.c b/net/can/raw.c
> index 897ffc17d850..2bb3eab98af0 100644
> --- a/net/can/raw.c
> +++ b/net/can/raw.c
> @@ -756,7 +756,6 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>  	struct raw_sock *ro = raw_sk(sk);
>  	int len;
>  	void *val;
> -	int err = 0;
> 
>  	if (level != SOL_CAN_RAW)
>  		return -EINVAL;
> @@ -766,7 +765,9 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>  		return -EINVAL;
> 
>  	switch (optname) {
> -	case CAN_RAW_FILTER:
> +	case CAN_RAW_FILTER: {
> +		int err = 0;
> +
>  		lock_sock(sk);
>  		if (ro->count > 0) {
>  			int fsize = ro->count * sizeof(struct can_filter);
> @@ -791,7 +792,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>  		if (!err)
>  			err = put_user(len, optlen);
>  		return err;
> -
> +	}
>  	case CAN_RAW_ERR_FILTER:
>  		if (len > sizeof(can_err_mask_t))
>  			len = sizeof(can_err_mask_t);
> @@ -822,7 +823,9 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>  		val = &ro->xl_frames;
>  		break;
> 
> -	case CAN_RAW_XL_VCID_OPTS:
> +	case CAN_RAW_XL_VCID_OPTS: {
> +		int err = 0;
> +
>  		/* user space buffer to small for VCID opts? */
>  		if (len < sizeof(ro->raw_vcid_opts)) {
>  			/* return -ERANGE and needed space in optlen */
> @@ -839,6 +842,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
>  			err = put_user(len, optlen);
>  		return err;
> 
> +	}
>  	case CAN_RAW_JOIN_FILTERS:
>  		if (len > sizeof(int))
>  			len = sizeof(int);

I'd be very tempted to change the code so that there is only one
put_user(len, optlen) right at the bottom.

If the code is obeying the normal 'rules' for getsockopt() the function
can actually return the length or an error and the caller can sort
out what to do with the data.
That makes the changes required to implement kernel_getsockopt()
a lot simpler.
I did start writing a patchset to do that, but some of the code
is entirely brain-dead.
But it would certainly make sense for the 'optlen' parameter to
be a kernel address and have been validated to be 'sane' (eg positive)
and have the syscall wrapper do the user copies.
It can't be done for the buffer (in all cases) because the user
supplied length isn't always the buffer length.
But the length field is defined to be an integer.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
diff mbox series

Patch

diff --git a/net/can/raw.c b/net/can/raw.c
index 897ffc17d850..2bb3eab98af0 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -756,7 +756,6 @@  static int raw_getsockopt(struct socket *sock, int level, int optname,
 	struct raw_sock *ro = raw_sk(sk);
 	int len;
 	void *val;
-	int err = 0;
 
 	if (level != SOL_CAN_RAW)
 		return -EINVAL;
@@ -766,7 +765,9 @@  static int raw_getsockopt(struct socket *sock, int level, int optname,
 		return -EINVAL;
 
 	switch (optname) {
-	case CAN_RAW_FILTER:
+	case CAN_RAW_FILTER: {
+		int err = 0;
+
 		lock_sock(sk);
 		if (ro->count > 0) {
 			int fsize = ro->count * sizeof(struct can_filter);
@@ -791,7 +792,7 @@  static int raw_getsockopt(struct socket *sock, int level, int optname,
 		if (!err)
 			err = put_user(len, optlen);
 		return err;
-
+	}
 	case CAN_RAW_ERR_FILTER:
 		if (len > sizeof(can_err_mask_t))
 			len = sizeof(can_err_mask_t);
@@ -822,7 +823,9 @@  static int raw_getsockopt(struct socket *sock, int level, int optname,
 		val = &ro->xl_frames;
 		break;
 
-	case CAN_RAW_XL_VCID_OPTS:
+	case CAN_RAW_XL_VCID_OPTS: {
+		int err = 0;
+
 		/* user space buffer to small for VCID opts? */
 		if (len < sizeof(ro->raw_vcid_opts)) {
 			/* return -ERANGE and needed space in optlen */
@@ -839,6 +842,7 @@  static int raw_getsockopt(struct socket *sock, int level, int optname,
 			err = put_user(len, optlen);
 		return err;
 
+	}
 	case CAN_RAW_JOIN_FILTERS:
 		if (len > sizeof(int))
 			len = sizeof(int);