2022-04-06 15:36:29

by Charlie Sands

[permalink] [raw]
Subject: [PATCH V3] staging: r8188eu: Fix unsafe memory access by memcmp.

This patch fixes sparse warnings about the memcmp function unsafely accessing
userspace memory without first copying it to kernel space.

Signed-off-by: Charlie Sands <[email protected]>
---

V2: Fixed checkpatch.pl warning and changed variable name as suggested by Greg
K. H. and improved error checking on the "copy_from_user" function as suggested
by Pavel Skripkin.

V3: return -EFAULT from the function when there is a memory error while moving
it from user space to kernel space and fix incorrect subject from V2

drivers/staging/r8188eu/os_dep/ioctl_linux.c | 21 ++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/drivers/staging/r8188eu/os_dep/ioctl_linux.c b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
index 7df213856d66..9f2182b8e451 100644
--- a/drivers/staging/r8188eu/os_dep/ioctl_linux.c
+++ b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
@@ -3233,23 +3233,28 @@ static int rtw_p2p_get(struct net_device *dev,
struct iw_request_info *info,
union iwreq_data *wrqu, char *extra)
{
- if (!memcmp(wrqu->data.pointer, "status", 6)) {
+ char wrqu_data[9];
+
+ if (copy_from_user(wrqu_data, wrqu->data.pointer, 9) != 0)
+ return -EFAULT;
+
+ if (!memcmp(wrqu_data, "status", 6)) {
rtw_p2p_get_status(dev, info, wrqu, extra);
- } else if (!memcmp(wrqu->data.pointer, "role", 4)) {
+ } else if (!memcmp(wrqu_data, "role", 4)) {
rtw_p2p_get_role(dev, info, wrqu, extra);
- } else if (!memcmp(wrqu->data.pointer, "peer_ifa", 8)) {
+ } else if (!memcmp(wrqu_data, "peer_ifa", 8)) {
rtw_p2p_get_peer_ifaddr(dev, info, wrqu, extra);
- } else if (!memcmp(wrqu->data.pointer, "req_cm", 6)) {
+ } else if (!memcmp(wrqu_data, "req_cm", 6)) {
rtw_p2p_get_req_cm(dev, info, wrqu, extra);
- } else if (!memcmp(wrqu->data.pointer, "peer_deva", 9)) {
+ } else if (!memcmp(wrqu_data, "peer_deva", 9)) {
/* Get the P2P device address when receiving the provision discovery request frame. */
rtw_p2p_get_peer_devaddr(dev, info, wrqu, extra);
- } else if (!memcmp(wrqu->data.pointer, "group_id", 8)) {
+ } else if (!memcmp(wrqu_data, "group_id", 8)) {
rtw_p2p_get_groupid(dev, info, wrqu, extra);
- } else if (!memcmp(wrqu->data.pointer, "peer_deva_inv", 9)) {
+ } else if (!memcmp(wrqu_data, "peer_deva_inv", 9)) {
/* Get the P2P device address when receiving the P2P Invitation request frame. */
rtw_p2p_get_peer_devaddr_by_invitation(dev, info, wrqu, extra);
- } else if (!memcmp(wrqu->data.pointer, "op_ch", 5)) {
+ } else if (!memcmp(wrqu_data, "op_ch", 5)) {
rtw_p2p_get_op_ch(dev, info, wrqu, extra);
}
return 0;
--
2.35.1


2022-04-06 16:41:53

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH V3] staging: r8188eu: Fix unsafe memory access by memcmp.

On Wed, Apr 06, 2022 at 07:25:42AM -0400, Charlie Sands wrote:
> This patch fixes sparse warnings about the memcmp function unsafely accessing
> userspace memory without first copying it to kernel space.
>
> Signed-off-by: Charlie Sands <[email protected]>
> ---
>
> V2: Fixed checkpatch.pl warning and changed variable name as suggested by Greg
> K. H. and improved error checking on the "copy_from_user" function as suggested
> by Pavel Skripkin.
>
> V3: return -EFAULT from the function when there is a memory error while moving
> it from user space to kernel space and fix incorrect subject from V2
>
> drivers/staging/r8188eu/os_dep/ioctl_linux.c | 21 ++++++++++++--------
> 1 file changed, 13 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/staging/r8188eu/os_dep/ioctl_linux.c b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
> index 7df213856d66..9f2182b8e451 100644
> --- a/drivers/staging/r8188eu/os_dep/ioctl_linux.c
> +++ b/drivers/staging/r8188eu/os_dep/ioctl_linux.c
> @@ -3233,23 +3233,28 @@ static int rtw_p2p_get(struct net_device *dev,
> struct iw_request_info *info,
> union iwreq_data *wrqu, char *extra)
> {
> - if (!memcmp(wrqu->data.pointer, "status", 6)) {
> + char wrqu_data[9];
> +
> + if (copy_from_user(wrqu_data, wrqu->data.pointer, 9) != 0)

How do you know that the user buffer is passing 9 characters long?

You're fixing a real bug, sure, but you've inventing a new API and it's
a rubbish API. Just delete rtw_p2p_get() and the associated code. It
has never worked so no one will miss it.

regards,
dan carpenter