diff mbox series

wifi: ath6kl: Check that the read operation returns a data length of 0

Message ID tencent_8D19734F828DA6A5938DF1122F5DDC5DBC07@qq.com (mailing list archive)
State Awaiting Upstream
Delegated to: Netdev Maintainers
Headers show
Series wifi: ath6kl: Check that the read operation returns a data length of 0 | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Edward Adam Davis Aug. 25, 2024, 7:10 a.m. UTC
If the data length returned by the device is 0, the read operation
should be considered a failure.

Reported-and-tested-by: syzbot+92c6dd14aaa230be6855@syzkaller.appspotmail.com
Signed-off-by: Edward Adam Davis <eadavis@qq.com>
---
 drivers/net/wireless/ath/ath6kl/usb.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Greg Kroah-Hartman Aug. 25, 2024, 7:25 a.m. UTC | #1
On Sun, Aug 25, 2024 at 03:10:03PM +0800, Edward Adam Davis wrote:
> If the data length returned by the device is 0, the read operation
> should be considered a failure.
> 
> Reported-and-tested-by: syzbot+92c6dd14aaa230be6855@syzkaller.appspotmail.com
> Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> ---
>  drivers/net/wireless/ath/ath6kl/usb.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
> index 5220809841a6..2a89bab81b24 100644
> --- a/drivers/net/wireless/ath/ath6kl/usb.c
> +++ b/drivers/net/wireless/ath/ath6kl/usb.c
> @@ -1034,6 +1034,9 @@ static int ath6kl_usb_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
>  		ath6kl_err("Unable to read the bmi data from the device: %d\n",
>  			   ret);
>  		return ret;
> +	} else {
> +		ath6kl_err("Actual read the bmi data length is 0 from the device\n");
> +		return -EIO;

Close, but not quite there.  ath6kl_usb_submit_ctrl_in() needs to verify
that the actual amount of data was read that was asked for.  If a short
read happens (or a long one), then an error needs to propagate out, not
just 0.  See the "note:" line in that function for what needs to be
properly checked.

hope this helps,

greg k-h
Edward Adam Davis Aug. 25, 2024, 8:14 a.m. UTC | #2
On Sun, 25 Aug 2024 09:25:37 +0200, Greg KH wrote:
> > If the data length returned by the device is 0, the read operation
> > should be considered a failure.
> >
> > Reported-and-tested-by: syzbot+92c6dd14aaa230be6855@syzkaller.appspotmail.com
> > Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> > ---
> >  drivers/net/wireless/ath/ath6kl/usb.c | 3 +++
> >  1 file changed, 3 insertions(+)
> >
> > diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
> > index 5220809841a6..2a89bab81b24 100644
> > --- a/drivers/net/wireless/ath/ath6kl/usb.c
> > +++ b/drivers/net/wireless/ath/ath6kl/usb.c
> > @@ -1034,6 +1034,9 @@ static int ath6kl_usb_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
> >  		ath6kl_err("Unable to read the bmi data from the device: %d\n",
> >  			   ret);
> >  		return ret;
> > +	} else {
> > +		ath6kl_err("Actual read the bmi data length is 0 from the device\n");
> > +		return -EIO;
> 
> Close, but not quite there.  ath6kl_usb_submit_ctrl_in() needs to verify
> that the actual amount of data was read that was asked for.  If a short
> read happens (or a long one), then an error needs to propagate out, not
> just 0.  See the "note:" line in that function for what needs to be
> properly checked.
> 
> hope this helps,
Thanks for your analysis.
I have carefully read your analysis and I am not sure if the following
understanding is appropriate:
diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
index 2a89bab81b24..35884316a8c8 100644
--- a/drivers/net/wireless/ath/ath6kl/usb.c
+++ b/drivers/net/wireless/ath/ath6kl/usb.c
@@ -932,6 +932,15 @@ static int ath6kl_usb_submit_ctrl_in(struct ath6kl_usb *ar_usb,

        kfree(buf);

+       /* There are two types of read failure situations that need to be captured:
+        * 1. short read: ret < size && ret >= 0
+        * 2. long read: ret > size
+        * */
+       if (req == ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP && ret != size) {
+               ath6kl_warn("Actual read the data length is: %d, but input size is %d\n", ret, size);
+               return -EIO;
+       }
+
        return 0;
 }

BR,
Edward
Greg Kroah-Hartman Aug. 25, 2024, 8:34 a.m. UTC | #3
On Sun, Aug 25, 2024 at 04:14:17PM +0800, Edward Adam Davis wrote:
> On Sun, 25 Aug 2024 09:25:37 +0200, Greg KH wrote:
> > > If the data length returned by the device is 0, the read operation
> > > should be considered a failure.
> > >
> > > Reported-and-tested-by: syzbot+92c6dd14aaa230be6855@syzkaller.appspotmail.com
> > > Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> > > ---
> > >  drivers/net/wireless/ath/ath6kl/usb.c | 3 +++
> > >  1 file changed, 3 insertions(+)
> > >
> > > diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
> > > index 5220809841a6..2a89bab81b24 100644
> > > --- a/drivers/net/wireless/ath/ath6kl/usb.c
> > > +++ b/drivers/net/wireless/ath/ath6kl/usb.c
> > > @@ -1034,6 +1034,9 @@ static int ath6kl_usb_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
> > >  		ath6kl_err("Unable to read the bmi data from the device: %d\n",
> > >  			   ret);
> > >  		return ret;
> > > +	} else {
> > > +		ath6kl_err("Actual read the bmi data length is 0 from the device\n");
> > > +		return -EIO;
> > 
> > Close, but not quite there.  ath6kl_usb_submit_ctrl_in() needs to verify
> > that the actual amount of data was read that was asked for.  If a short
> > read happens (or a long one), then an error needs to propagate out, not
> > just 0.  See the "note:" line in that function for what needs to be
> > properly checked.
> > 
> > hope this helps,
> Thanks for your analysis.
> I have carefully read your analysis and I am not sure if the following
> understanding is appropriate:
> diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
> index 2a89bab81b24..35884316a8c8 100644
> --- a/drivers/net/wireless/ath/ath6kl/usb.c
> +++ b/drivers/net/wireless/ath/ath6kl/usb.c
> @@ -932,6 +932,15 @@ static int ath6kl_usb_submit_ctrl_in(struct ath6kl_usb *ar_usb,
> 
>         kfree(buf);

First off, this should be using usb_control_msg_send() instead of having
to roll their own buffer handling, right?

> +       /* There are two types of read failure situations that need to be captured:
> +        * 1. short read: ret < size && ret >= 0
> +        * 2. long read: ret > size
> +        * */
> +       if (req == ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP && ret != size) {
> +               ath6kl_warn("Actual read the data length is: %d, but input size is %d\n", ret, size);
> +               return -EIO;
> +       }

If you switch to usb_control_msg_send() this logic gets a lot simpler.
Perhaps do that instead?

If not, then you need to check for "short writes" or zero writes, see
the documentation for usb_control_msg() for what it returns.  Your
comment is not correct here, there are 3 different return "states" that
you need to handle.

And why are you caring about what the req type is?

thanks,

greg k-h
Edward Adam Davis Aug. 25, 2024, 10:09 a.m. UTC | #4
On Sun, 25 Aug 2024 10:34:00 +0200, Greg KH wrote:
> On Sun, Aug 25, 2024 at 04:14:17PM +0800, Edward Adam Davis wrote:
> > On Sun, 25 Aug 2024 09:25:37 +0200, Greg KH wrote:
> > > > If the data length returned by the device is 0, the read operation
> > > > should be considered a failure.
> > > >
> > > > Reported-and-tested-by: syzbot+92c6dd14aaa230be6855@syzkaller.appspotmail.com
> > > > Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> > > > ---
> > > >  drivers/net/wireless/ath/ath6kl/usb.c | 3 +++
> > > >  1 file changed, 3 insertions(+)
> > > >
> > > > diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
> > > > index 5220809841a6..2a89bab81b24 100644
> > > > --- a/drivers/net/wireless/ath/ath6kl/usb.c
> > > > +++ b/drivers/net/wireless/ath/ath6kl/usb.c
> > > > @@ -1034,6 +1034,9 @@ static int ath6kl_usb_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
> > > >  		ath6kl_err("Unable to read the bmi data from the device: %d\n",
> > > >  			   ret);
> > > >  		return ret;
> > > > +	} else {
> > > > +		ath6kl_err("Actual read the bmi data length is 0 from the device\n");
> > > > +		return -EIO;
> > >
> > > Close, but not quite there.  ath6kl_usb_submit_ctrl_in() needs to verify
> > > that the actual amount of data was read that was asked for.  If a short
> > > read happens (or a long one), then an error needs to propagate out, not
> > > just 0.  See the "note:" line in that function for what needs to be
> > > properly checked.
> > >
> > > hope this helps,
> > Thanks for your analysis.
> > I have carefully read your analysis and I am not sure if the following
> > understanding is appropriate:
> > diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
> > index 2a89bab81b24..35884316a8c8 100644
> > --- a/drivers/net/wireless/ath/ath6kl/usb.c
> > +++ b/drivers/net/wireless/ath/ath6kl/usb.c
> > @@ -932,6 +932,15 @@ static int ath6kl_usb_submit_ctrl_in(struct ath6kl_usb *ar_usb,
> >
> >         kfree(buf);
> 
> First off, this should be using usb_control_msg_send() instead of having
> to roll their own buffer handling, right?
I couldn't figure it out with what you said. 

ath6kl_usb_submit_ctrl_in() is similar to usb_control_msg_send(),
both calling usb_control_msg() to communicate with USB devices.

In the current issue, when executing an ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP
read request, the length of the data returned from the device is 0, which
is different from the expected length of the data to be read, resulting in
a warning.

ath6kl_usb_submit_ctrl_in()--->
	usb_control_msg()--->
		usb_internal_control_msg()

usb_internal_control_msg() will return the length of the data returned from
the device, usb_control_msg() return the length too, so in ath6kl_usb_submit_ctrl_in(),
we can filter out incorrect data lengths by judging the value of ret, such
as ret != Size situation.
> 
> > +       /* There are two types of read failure situations that need to be captured:
> > +        * 1. short read: ret < size && ret >= 0
> > +        * 2. long read: ret > size
> > +        * */
> > +       if (req == ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP && ret != size) {
> > +               ath6kl_warn("Actual read the data length is: %d, but input size is %d\n", ret, size);
> > +               return -EIO;
> > +       }
> 
> If you switch to usb_control_msg_send() this logic gets a lot simpler.
> Perhaps do that instead?
> 
> If not, then you need to check for "short writes" or zero writes, see
> the documentation for usb_control_msg() for what it returns.  Your
> comment is not correct here, there are 3 different return "states" that
> you need to handle.
> 
> And why are you caring about what the req type is?

BR,
Edward
Greg Kroah-Hartman Aug. 25, 2024, 11:25 a.m. UTC | #5
On Sun, Aug 25, 2024 at 06:09:45PM +0800, Edward Adam Davis wrote:
> On Sun, 25 Aug 2024 10:34:00 +0200, Greg KH wrote:
> > On Sun, Aug 25, 2024 at 04:14:17PM +0800, Edward Adam Davis wrote:
> > > On Sun, 25 Aug 2024 09:25:37 +0200, Greg KH wrote:
> > > > > If the data length returned by the device is 0, the read operation
> > > > > should be considered a failure.
> > > > >
> > > > > Reported-and-tested-by: syzbot+92c6dd14aaa230be6855@syzkaller.appspotmail.com
> > > > > Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> > > > > ---
> > > > >  drivers/net/wireless/ath/ath6kl/usb.c | 3 +++
> > > > >  1 file changed, 3 insertions(+)
> > > > >
> > > > > diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
> > > > > index 5220809841a6..2a89bab81b24 100644
> > > > > --- a/drivers/net/wireless/ath/ath6kl/usb.c
> > > > > +++ b/drivers/net/wireless/ath/ath6kl/usb.c
> > > > > @@ -1034,6 +1034,9 @@ static int ath6kl_usb_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
> > > > >  		ath6kl_err("Unable to read the bmi data from the device: %d\n",
> > > > >  			   ret);
> > > > >  		return ret;
> > > > > +	} else {
> > > > > +		ath6kl_err("Actual read the bmi data length is 0 from the device\n");
> > > > > +		return -EIO;
> > > >
> > > > Close, but not quite there.  ath6kl_usb_submit_ctrl_in() needs to verify
> > > > that the actual amount of data was read that was asked for.  If a short
> > > > read happens (or a long one), then an error needs to propagate out, not
> > > > just 0.  See the "note:" line in that function for what needs to be
> > > > properly checked.
> > > >
> > > > hope this helps,
> > > Thanks for your analysis.
> > > I have carefully read your analysis and I am not sure if the following
> > > understanding is appropriate:
> > > diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
> > > index 2a89bab81b24..35884316a8c8 100644
> > > --- a/drivers/net/wireless/ath/ath6kl/usb.c
> > > +++ b/drivers/net/wireless/ath/ath6kl/usb.c
> > > @@ -932,6 +932,15 @@ static int ath6kl_usb_submit_ctrl_in(struct ath6kl_usb *ar_usb,
> > >
> > >         kfree(buf);
> > 
> > First off, this should be using usb_control_msg_send() instead of having
> > to roll their own buffer handling, right?
> I couldn't figure it out with what you said. 

Meaning this kfree() should not be needed if you use
usb_control_msg_send() (nor the allocation above it.)

> ath6kl_usb_submit_ctrl_in() is similar to usb_control_msg_send(),
> both calling usb_control_msg() to communicate with USB devices.

Yes, it's close, but not quite the same.

> In the current issue, when executing an ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP
> read request, the length of the data returned from the device is 0, which
> is different from the expected length of the data to be read, resulting in
> a warning.
> 
> ath6kl_usb_submit_ctrl_in()--->
> 	usb_control_msg()--->
> 		usb_internal_control_msg()
> 
> usb_internal_control_msg() will return the length of the data returned from
> the device, usb_control_msg() return the length too, so in ath6kl_usb_submit_ctrl_in(),
> we can filter out incorrect data lengths by judging the value of ret, such
> as ret != Size situation.

Then just do that type of check for that type of read request in the
code that does that call, not 2-3 layers deeper, no need for making this
more complex than needed.

Try removing both of these functions and just call usb functions
directly.

thanks,

greg k-h
Edward Adam Davis Aug. 25, 2024, 2:03 p.m. UTC | #6
On Sun, 25 Aug 2024 13:25:56 +0200, Greg KH wrote:
> On Sun, Aug 25, 2024 at 06:09:45PM +0800, Edward Adam Davis wrote:
> > On Sun, 25 Aug 2024 10:34:00 +0200, Greg KH wrote:
> > > On Sun, Aug 25, 2024 at 04:14:17PM +0800, Edward Adam Davis wrote:
> > > > On Sun, 25 Aug 2024 09:25:37 +0200, Greg KH wrote:
> > > > > > If the data length returned by the device is 0, the read operation
> > > > > > should be considered a failure.
> > > > > >
> > > > > > Reported-and-tested-by: syzbot+92c6dd14aaa230be6855@syzkaller.appspotmail.com
> > > > > > Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> > > > > > ---
> > > > > >  drivers/net/wireless/ath/ath6kl/usb.c | 3 +++
> > > > > >  1 file changed, 3 insertions(+)
> > > > > >
> > > > > > diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
> > > > > > index 5220809841a6..2a89bab81b24 100644
> > > > > > --- a/drivers/net/wireless/ath/ath6kl/usb.c
> > > > > > +++ b/drivers/net/wireless/ath/ath6kl/usb.c
> > > > > > @@ -1034,6 +1034,9 @@ static int ath6kl_usb_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
> > > > > >  		ath6kl_err("Unable to read the bmi data from the device: %d\n",
> > > > > >  			   ret);
> > > > > >  		return ret;
> > > > > > +	} else {
> > > > > > +		ath6kl_err("Actual read the bmi data length is 0 from the device\n");
> > > > > > +		return -EIO;
> > > > >
> > > > > Close, but not quite there.  ath6kl_usb_submit_ctrl_in() needs to verify
> > > > > that the actual amount of data was read that was asked for.  If a short
> > > > > read happens (or a long one), then an error needs to propagate out, not
> > > > > just 0.  See the "note:" line in that function for what needs to be
> > > > > properly checked.
> > > > >
> > > > > hope this helps,
> > > > Thanks for your analysis.
> > > > I have carefully read your analysis and I am not sure if the following
> > > > understanding is appropriate:
> > > > diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
> > > > index 2a89bab81b24..35884316a8c8 100644
> > > > --- a/drivers/net/wireless/ath/ath6kl/usb.c
> > > > +++ b/drivers/net/wireless/ath/ath6kl/usb.c
> > > > @@ -932,6 +932,15 @@ static int ath6kl_usb_submit_ctrl_in(struct ath6kl_usb *ar_usb,
> > > >
> > > >         kfree(buf);
> > > 
> > > First off, this should be using usb_control_msg_send() instead of having
> > > to roll their own buffer handling, right?
> > I couldn't figure it out with what you said. 
> 
> Meaning this kfree() should not be needed if you use
> usb_control_msg_send() (nor the allocation above it.)
> 
> > ath6kl_usb_submit_ctrl_in() is similar to usb_control_msg_send(),
> > both calling usb_control_msg() to communicate with USB devices.
> 
> Yes, it's close, but not quite the same.
> 
> > In the current issue, when executing an ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP
> > read request, the length of the data returned from the device is 0, which
> > is different from the expected length of the data to be read, resulting in
> > a warning.
> > 
> > ath6kl_usb_submit_ctrl_in()--->
> > 	usb_control_msg()--->
> > 		usb_internal_control_msg()
> > 
> > usb_internal_control_msg() will return the length of the data returned from
> > the device, usb_control_msg() return the length too, so in ath6kl_usb_submit_ctrl_in(),
> > we can filter out incorrect data lengths by judging the value of ret, such
> > as ret != Size situation.
> 
> Then just do that type of check for that type of read request in the
> code that does that call, not 2-3 layers deeper, no need for making this
> more complex than needed.
> 
> Try removing both of these functions and just call usb functions
> directly.
I have tried following fix:
diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
index 5220809841a6..dc1f89ebb740 100644
--- a/drivers/net/wireless/ath/ath6kl/usb.c
+++ b/drivers/net/wireless/ath/ath6kl/usb.c
@@ -1027,9 +1027,9 @@ static int ath6kl_usb_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
        int ret;
 
        /* get response */
-       ret = ath6kl_usb_submit_ctrl_in(ar_usb,
-                                       ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP,
-                                       0, 0, buf, len);
+       ret = usb_control_msg_recv(ar_usb->udev, 0, ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP,
+                                USB_DIR_IN | USB_TYPE_VENDOR |
+                                USB_RECIP_DEVICE, 0, 0, buf, len, 2000, GFP_KERNEL);
        if (ret) {
                ath6kl_err("Unable to read the bmi data from the device: %d\n",
                           ret);

I researched usb_control_msg_recv(), It handles the abnormal length of the
returned data, so using it directly can fix this warning.

BR,
Edward
diff mbox series

Patch

diff --git a/drivers/net/wireless/ath/ath6kl/usb.c b/drivers/net/wireless/ath/ath6kl/usb.c
index 5220809841a6..2a89bab81b24 100644
--- a/drivers/net/wireless/ath/ath6kl/usb.c
+++ b/drivers/net/wireless/ath/ath6kl/usb.c
@@ -1034,6 +1034,9 @@  static int ath6kl_usb_bmi_read(struct ath6kl *ar, u8 *buf, u32 len)
 		ath6kl_err("Unable to read the bmi data from the device: %d\n",
 			   ret);
 		return ret;
+	} else {
+		ath6kl_err("Actual read the bmi data length is 0 from the device\n");
+		return -EIO;
 	}
 
 	return 0;