[PATCH v6] staging: rtl8723bs: Add error handling for sd_read()

Wentao Liang posted 1 patch 8 months, 2 weeks ago
There is a newer version of this series
drivers/staging/rtl8723bs/hal/sdio_ops.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
[PATCH v6] staging: rtl8723bs: Add error handling for sd_read()
Posted by Wentao Liang 8 months, 2 weeks ago
The sdio_read32() calls sd_read(), but does not handle the error if
sd_read() fails. This could lead to subsequent operations processing
invalid data. A proper implementation can be found in sdio_readN().

Add error handling for the sd_read() to free tmpbuf and return error
code if sd_read() fails. This ensure that the memcpy() is only performed
when the read operation is successful.

Fixes: 554c0a3abf21 ("staging: Add rtl8723bs sdio wifi driver")
Cc: stable@vger.kernel.org # v4.12+
Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
---
v6: Fix improper code to propagate error code
v5: Fix error code
v4: Add change log and fix error code
v3: Add Cc flag
v2: Change code to initialize val

 drivers/staging/rtl8723bs/hal/sdio_ops.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/staging/rtl8723bs/hal/sdio_ops.c b/drivers/staging/rtl8723bs/hal/sdio_ops.c
index 21e9f1858745..eb21c7e55949 100644
--- a/drivers/staging/rtl8723bs/hal/sdio_ops.c
+++ b/drivers/staging/rtl8723bs/hal/sdio_ops.c
@@ -185,7 +185,12 @@ static u32 sdio_read32(struct intf_hdl *intfhdl, u32 addr)
 			return SDIO_ERR_VAL32;
 
 		ftaddr &= ~(u16)0x3;
-		sd_read(intfhdl, ftaddr, 8, tmpbuf);
+		err = sd_read(intfhdl, ftaddr, 8, tmpbuf);
+		if (err) {
+			kfree(tmpbuf);
+			return (u32)err;
+		}
+
 		memcpy(&le_tmp, tmpbuf + shift, 4);
 		val = le32_to_cpu(le_tmp);
 
-- 
2.42.0.windows.2
Re: [PATCH v6] staging: rtl8723bs: Add error handling for sd_read()
Posted by Dan Carpenter 8 months, 2 weeks ago
On Mon, Apr 07, 2025 at 06:03:18PM +0800, Wentao Liang wrote:
> The sdio_read32() calls sd_read(), but does not handle the error if
> sd_read() fails. This could lead to subsequent operations processing
> invalid data. A proper implementation can be found in sdio_readN().
> 
> Add error handling for the sd_read() to free tmpbuf and return error
> code if sd_read() fails. This ensure that the memcpy() is only performed
> when the read operation is successful.
> 
> Fixes: 554c0a3abf21 ("staging: Add rtl8723bs sdio wifi driver")
> Cc: stable@vger.kernel.org # v4.12+
> Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
> ---
> v6: Fix improper code to propagate error code
> v5: Fix error code
> v4: Add change log and fix error code
> v3: Add Cc flag
> v2: Change code to initialize val
> 
>  drivers/staging/rtl8723bs/hal/sdio_ops.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/staging/rtl8723bs/hal/sdio_ops.c b/drivers/staging/rtl8723bs/hal/sdio_ops.c
> index 21e9f1858745..eb21c7e55949 100644
> --- a/drivers/staging/rtl8723bs/hal/sdio_ops.c
> +++ b/drivers/staging/rtl8723bs/hal/sdio_ops.c
> @@ -185,7 +185,12 @@ static u32 sdio_read32(struct intf_hdl *intfhdl, u32 addr)
>  			return SDIO_ERR_VAL32;
>  
>  		ftaddr &= ~(u16)0x3;
> -		sd_read(intfhdl, ftaddr, 8, tmpbuf);
> +		err = sd_read(intfhdl, ftaddr, 8, tmpbuf);
> +		if (err) {
> +			kfree(tmpbuf);
> +			return (u32)err;

Heh.

So the fundamental problem is that non of the callers check for errors.

To be honest, I had expected you to just return zero, but I don't like to
give out the answers to students.  I hadn't even known that SDIO_ERR_VAL32
was an option.  It's still a garbage value but it's kind of a predictable
garbage value and, whatever, it seemed fine to me.  It wasn't fine to Greg
so, yeah, you have to re-write it.  But now this is again not fine to me
(or Greg when he gets around to checking his email).

The bug here is that if you pull out the hardware while doing a read
then it returns whatever was in the kmalloc().  In other words it's an
information leak.

I think you could make an argument that returnnig zero is a good solution.
It fixes the information leak.  It's not a a horrible random value like
"(u32)-EINVAL".

The other option would be to go through all the callers and add error
handling.  So for this this function you would have to pass a pointer to
u32 *val and return zero on success or negative on failure.

regards,
dan carpenter
Re: [PATCH v6] staging: rtl8723bs: Add error handling for sd_read()
Posted by Greg KH 8 months, 2 weeks ago
On Mon, Apr 07, 2025 at 01:36:44PM +0300, Dan Carpenter wrote:
> On Mon, Apr 07, 2025 at 06:03:18PM +0800, Wentao Liang wrote:
> > The sdio_read32() calls sd_read(), but does not handle the error if
> > sd_read() fails. This could lead to subsequent operations processing
> > invalid data. A proper implementation can be found in sdio_readN().
> > 
> > Add error handling for the sd_read() to free tmpbuf and return error
> > code if sd_read() fails. This ensure that the memcpy() is only performed
> > when the read operation is successful.
> > 
> > Fixes: 554c0a3abf21 ("staging: Add rtl8723bs sdio wifi driver")
> > Cc: stable@vger.kernel.org # v4.12+
> > Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
> > ---
> > v6: Fix improper code to propagate error code
> > v5: Fix error code
> > v4: Add change log and fix error code
> > v3: Add Cc flag
> > v2: Change code to initialize val
> > 
> >  drivers/staging/rtl8723bs/hal/sdio_ops.c | 7 ++++++-
> >  1 file changed, 6 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/staging/rtl8723bs/hal/sdio_ops.c b/drivers/staging/rtl8723bs/hal/sdio_ops.c
> > index 21e9f1858745..eb21c7e55949 100644
> > --- a/drivers/staging/rtl8723bs/hal/sdio_ops.c
> > +++ b/drivers/staging/rtl8723bs/hal/sdio_ops.c
> > @@ -185,7 +185,12 @@ static u32 sdio_read32(struct intf_hdl *intfhdl, u32 addr)
> >  			return SDIO_ERR_VAL32;
> >  
> >  		ftaddr &= ~(u16)0x3;
> > -		sd_read(intfhdl, ftaddr, 8, tmpbuf);
> > +		err = sd_read(intfhdl, ftaddr, 8, tmpbuf);
> > +		if (err) {
> > +			kfree(tmpbuf);
> > +			return (u32)err;
> 
> Heh.
> 
> So the fundamental problem is that non of the callers check for errors.
> 
> To be honest, I had expected you to just return zero, but I don't like to
> give out the answers to students.  I hadn't even known that SDIO_ERR_VAL32
> was an option.  It's still a garbage value but it's kind of a predictable
> garbage value and, whatever, it seemed fine to me.  It wasn't fine to Greg
> so, yeah, you have to re-write it.  But now this is again not fine to me
> (or Greg when he gets around to checking his email).
> 
> The bug here is that if you pull out the hardware while doing a read
> then it returns whatever was in the kmalloc().  In other words it's an
> information leak.
> 
> I think you could make an argument that returnnig zero is a good solution.
> It fixes the information leak.  It's not a a horrible random value like
> "(u32)-EINVAL".
> 
> The other option would be to go through all the callers and add error
> handling.  So for this this function you would have to pass a pointer to
> u32 *val and return zero on success or negative on failure.

The other option is to unwind the "HAL" layer here and just call the
correct sd card read function like all other drivers of this type do :)

thanks,

greg k-h
Re: [PATCH v6] staging: rtl8723bs: Add error handling for sd_read()
Posted by Greg KH 8 months, 2 weeks ago
On Mon, Apr 07, 2025 at 06:03:18PM +0800, Wentao Liang wrote:
> The sdio_read32() calls sd_read(), but does not handle the error if
> sd_read() fails. This could lead to subsequent operations processing
> invalid data. A proper implementation can be found in sdio_readN().
> 
> Add error handling for the sd_read() to free tmpbuf and return error
> code if sd_read() fails. This ensure that the memcpy() is only performed
> when the read operation is successful.
> 
> Fixes: 554c0a3abf21 ("staging: Add rtl8723bs sdio wifi driver")
> Cc: stable@vger.kernel.org # v4.12+
> Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
> ---
> v6: Fix improper code to propagate error code
> v5: Fix error code
> v4: Add change log and fix error code
> v3: Add Cc flag
> v2: Change code to initialize val
> 
>  drivers/staging/rtl8723bs/hal/sdio_ops.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/staging/rtl8723bs/hal/sdio_ops.c b/drivers/staging/rtl8723bs/hal/sdio_ops.c
> index 21e9f1858745..eb21c7e55949 100644
> --- a/drivers/staging/rtl8723bs/hal/sdio_ops.c
> +++ b/drivers/staging/rtl8723bs/hal/sdio_ops.c
> @@ -185,7 +185,12 @@ static u32 sdio_read32(struct intf_hdl *intfhdl, u32 addr)
>  			return SDIO_ERR_VAL32;
>  
>  		ftaddr &= ~(u16)0x3;
> -		sd_read(intfhdl, ftaddr, 8, tmpbuf);
> +		err = sd_read(intfhdl, ftaddr, 8, tmpbuf);
> +		if (err) {
> +			kfree(tmpbuf);
> +			return (u32)err;

You just casted a negative number to a positive type?

Step back and think exactly of what you should be doing here.  Walk the
callchain properly and think about how this all needed to be fixed up
properly, and then take some time to do that work and test it and submit
it properly.

Take your time, there's no rush here.

thanks,

greg k-h