All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] staging:wlan-ng: use memdup_user instead of kmalloc/copy_from_user
@ 2021-02-13 12:05 ` Ivan Safonov
  0 siblings, 0 replies; 6+ messages in thread
From: Ivan Safonov @ 2021-02-13 12:05 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Andrew Morton, Allen Pais, Johannes Weiner, Michal Hocko,
	Waiman Long, Abheek Dhawan, devel, linux-kernel, Ivan Safonov

memdup_user() is shorter and safer equivalent
of kmalloc/copy_from_user pair.

Signed-off-by: Ivan Safonov <insafonov@gmail.com>
---
 drivers/staging/wlan-ng/p80211netdev.c | 28 ++++++++++++--------------
 1 file changed, 13 insertions(+), 15 deletions(-)

diff --git a/drivers/staging/wlan-ng/p80211netdev.c b/drivers/staging/wlan-ng/p80211netdev.c
index a15abb2c8f54..6f9666dc0277 100644
--- a/drivers/staging/wlan-ng/p80211netdev.c
+++ b/drivers/staging/wlan-ng/p80211netdev.c
@@ -569,24 +569,22 @@ static int p80211knetdev_do_ioctl(struct net_device *dev,
 		goto bail;
 	}
 
-	/* Allocate a buf of size req->len */
-	msgbuf = kmalloc(req->len, GFP_KERNEL);
-	if (msgbuf) {
-		if (copy_from_user(msgbuf, (void __user *)req->data, req->len))
-			result = -EFAULT;
-		else
-			result = p80211req_dorequest(wlandev, msgbuf);
+	msgbuf = memdup_user(req->data, req->len);
+	if (IS_ERR(msgbuf)) {
+		result = PTR_ERR(msgbuf);
+		goto bail;
+	}
 
-		if (result == 0) {
-			if (copy_to_user
-			    ((void __user *)req->data, msgbuf, req->len)) {
-				result = -EFAULT;
-			}
+	result = p80211req_dorequest(wlandev, msgbuf);
+
+	if (result == 0) {
+		if (copy_to_user
+		    ((void __user *)req->data, msgbuf, req->len)) {
+			result = -EFAULT;
 		}
-		kfree(msgbuf);
-	} else {
-		result = -ENOMEM;
 	}
+	kfree(msgbuf);
+
 bail:
 	/* If allocate,copyfrom or copyto fails, return errno */
 	return result;
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH] staging:wlan-ng: use memdup_user instead of kmalloc/copy_from_user
@ 2021-02-13 12:05 ` Ivan Safonov
  0 siblings, 0 replies; 6+ messages in thread
From: Ivan Safonov @ 2021-02-13 12:05 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: devel, Michal Hocko, Ivan Safonov, linux-kernel, Johannes Weiner,
	Waiman Long, Andrew Morton, Allen Pais, Abheek Dhawan

memdup_user() is shorter and safer equivalent
of kmalloc/copy_from_user pair.

Signed-off-by: Ivan Safonov <insafonov@gmail.com>
---
 drivers/staging/wlan-ng/p80211netdev.c | 28 ++++++++++++--------------
 1 file changed, 13 insertions(+), 15 deletions(-)

diff --git a/drivers/staging/wlan-ng/p80211netdev.c b/drivers/staging/wlan-ng/p80211netdev.c
index a15abb2c8f54..6f9666dc0277 100644
--- a/drivers/staging/wlan-ng/p80211netdev.c
+++ b/drivers/staging/wlan-ng/p80211netdev.c
@@ -569,24 +569,22 @@ static int p80211knetdev_do_ioctl(struct net_device *dev,
 		goto bail;
 	}
 
-	/* Allocate a buf of size req->len */
-	msgbuf = kmalloc(req->len, GFP_KERNEL);
-	if (msgbuf) {
-		if (copy_from_user(msgbuf, (void __user *)req->data, req->len))
-			result = -EFAULT;
-		else
-			result = p80211req_dorequest(wlandev, msgbuf);
+	msgbuf = memdup_user(req->data, req->len);
+	if (IS_ERR(msgbuf)) {
+		result = PTR_ERR(msgbuf);
+		goto bail;
+	}
 
-		if (result == 0) {
-			if (copy_to_user
-			    ((void __user *)req->data, msgbuf, req->len)) {
-				result = -EFAULT;
-			}
+	result = p80211req_dorequest(wlandev, msgbuf);
+
+	if (result == 0) {
+		if (copy_to_user
+		    ((void __user *)req->data, msgbuf, req->len)) {
+			result = -EFAULT;
 		}
-		kfree(msgbuf);
-	} else {
-		result = -ENOMEM;
 	}
+	kfree(msgbuf);
+
 bail:
 	/* If allocate,copyfrom or copyto fails, return errno */
 	return result;
-- 
2.26.2

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] staging:wlan-ng: use memdup_user instead of kmalloc/copy_from_user
  2021-02-13 12:05 ` Ivan Safonov
@ 2021-02-15  8:44   ` Michal Hocko
  -1 siblings, 0 replies; 6+ messages in thread
From: Michal Hocko @ 2021-02-15  8:44 UTC (permalink / raw)
  To: Ivan Safonov
  Cc: Greg Kroah-Hartman, Andrew Morton, Allen Pais, Johannes Weiner,
	Waiman Long, Abheek Dhawan, devel, linux-kernel

On Sat 13-02-21 15:05:28, Ivan Safonov wrote:
> memdup_user() is shorter and safer equivalent
> of kmalloc/copy_from_user pair.
> 
> Signed-off-by: Ivan Safonov <insafonov@gmail.com>
> ---
>  drivers/staging/wlan-ng/p80211netdev.c | 28 ++++++++++++--------------
>  1 file changed, 13 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/staging/wlan-ng/p80211netdev.c b/drivers/staging/wlan-ng/p80211netdev.c
> index a15abb2c8f54..6f9666dc0277 100644
> --- a/drivers/staging/wlan-ng/p80211netdev.c
> +++ b/drivers/staging/wlan-ng/p80211netdev.c
> @@ -569,24 +569,22 @@ static int p80211knetdev_do_ioctl(struct net_device *dev,
>  		goto bail;
>  	}
>  
> -	/* Allocate a buf of size req->len */
> -	msgbuf = kmalloc(req->len, GFP_KERNEL);
> -	if (msgbuf) {
> -		if (copy_from_user(msgbuf, (void __user *)req->data, req->len))
> -			result = -EFAULT;
> -		else
> -			result = p80211req_dorequest(wlandev, msgbuf);
> +	msgbuf = memdup_user(req->data, req->len);

Move to memdup_user is definitely a right step. What is the range of
req->len though? If this can be larger than PAGE_SIZE then vmemdup_user
would be a better alternative.

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] staging:wlan-ng: use memdup_user instead of kmalloc/copy_from_user
@ 2021-02-15  8:44   ` Michal Hocko
  0 siblings, 0 replies; 6+ messages in thread
From: Michal Hocko @ 2021-02-15  8:44 UTC (permalink / raw)
  To: Ivan Safonov
  Cc: devel, Greg Kroah-Hartman, linux-kernel, Johannes Weiner,
	Waiman Long, Andrew Morton, Allen Pais, Abheek Dhawan

On Sat 13-02-21 15:05:28, Ivan Safonov wrote:
> memdup_user() is shorter and safer equivalent
> of kmalloc/copy_from_user pair.
> 
> Signed-off-by: Ivan Safonov <insafonov@gmail.com>
> ---
>  drivers/staging/wlan-ng/p80211netdev.c | 28 ++++++++++++--------------
>  1 file changed, 13 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/staging/wlan-ng/p80211netdev.c b/drivers/staging/wlan-ng/p80211netdev.c
> index a15abb2c8f54..6f9666dc0277 100644
> --- a/drivers/staging/wlan-ng/p80211netdev.c
> +++ b/drivers/staging/wlan-ng/p80211netdev.c
> @@ -569,24 +569,22 @@ static int p80211knetdev_do_ioctl(struct net_device *dev,
>  		goto bail;
>  	}
>  
> -	/* Allocate a buf of size req->len */
> -	msgbuf = kmalloc(req->len, GFP_KERNEL);
> -	if (msgbuf) {
> -		if (copy_from_user(msgbuf, (void __user *)req->data, req->len))
> -			result = -EFAULT;
> -		else
> -			result = p80211req_dorequest(wlandev, msgbuf);
> +	msgbuf = memdup_user(req->data, req->len);

Move to memdup_user is definitely a right step. What is the range of
req->len though? If this can be larger than PAGE_SIZE then vmemdup_user
would be a better alternative.

-- 
Michal Hocko
SUSE Labs
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] staging:wlan-ng: use memdup_user instead of kmalloc/copy_from_user
  2021-02-15  8:44   ` Michal Hocko
@ 2021-02-15 13:26     ` Dan Carpenter
  -1 siblings, 0 replies; 6+ messages in thread
From: Dan Carpenter @ 2021-02-15 13:26 UTC (permalink / raw)
  To: Michal Hocko
  Cc: Ivan Safonov, devel, Greg Kroah-Hartman, linux-kernel,
	Johannes Weiner, Waiman Long, Andrew Morton, Allen Pais,
	Abheek Dhawan

On Mon, Feb 15, 2021 at 09:44:24AM +0100, Michal Hocko wrote:
> On Sat 13-02-21 15:05:28, Ivan Safonov wrote:
> > memdup_user() is shorter and safer equivalent
> > of kmalloc/copy_from_user pair.
> > 
> > Signed-off-by: Ivan Safonov <insafonov@gmail.com>
> > ---
> >  drivers/staging/wlan-ng/p80211netdev.c | 28 ++++++++++++--------------
> >  1 file changed, 13 insertions(+), 15 deletions(-)
> > 
> > diff --git a/drivers/staging/wlan-ng/p80211netdev.c b/drivers/staging/wlan-ng/p80211netdev.c
> > index a15abb2c8f54..6f9666dc0277 100644
> > --- a/drivers/staging/wlan-ng/p80211netdev.c
> > +++ b/drivers/staging/wlan-ng/p80211netdev.c
> > @@ -569,24 +569,22 @@ static int p80211knetdev_do_ioctl(struct net_device *dev,
> >  		goto bail;
> >  	}
> >  
> > -	/* Allocate a buf of size req->len */
> > -	msgbuf = kmalloc(req->len, GFP_KERNEL);
> > -	if (msgbuf) {
> > -		if (copy_from_user(msgbuf, (void __user *)req->data, req->len))
> > -			result = -EFAULT;
> > -		else
> > -			result = p80211req_dorequest(wlandev, msgbuf);
> > +	msgbuf = memdup_user(req->data, req->len);
> 
> Move to memdup_user is definitely a right step. What is the range of
> req->len though? If this can be larger than PAGE_SIZE then vmemdup_user
> would be a better alternative.

req->len shoudn't be anywhere close to PAGE_SIZE but it's actually
important to check req->len and this code does not do that which leads
to memory corruption:

drivers/staging/wlan-ng/p80211netdev.c
   566                  goto bail;
   567          } else if (cmd != P80211_IFREQ) {
   568                  result = -EINVAL;
   569                  goto bail;
   570          }
   571  
   572          msgbuf = memdup_user(req->data, req->len);
   573          if (IS_ERR(msgbuf)) {
   574                  result = PTR_ERR(msgbuf);
   575                  goto bail;
   576          }
   577  
   578          result = p80211req_dorequest(wlandev, msgbuf);

We don't know that "req->len" is >= sizeof(*msgbuf), and then we pass
msgbuf top80211req_dorequest() which calls p80211req_handlemsg().  In
p80211req_handlemsg() then "req->len" has to be larger than
sizeof(struct p80211msg_lnxreq_hostwep).

   579  
   580          if (result == 0) {
   581                  if (copy_to_user
   582                      ((void __user *)req->data, msgbuf, req->len)) {
   583                          result = -EFAULT;
   584                  }
   585          }
   586          kfree(msgbuf);
   587  
   588  bail:
   589          /* If allocate,copyfrom or copyto fails, return errno */
   590          return result;
   591  }

Smatch has a problem parsing this code because struct ifreq *ifr is a
union and Smatch gets confused.  :/

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] staging:wlan-ng: use memdup_user instead of kmalloc/copy_from_user
@ 2021-02-15 13:26     ` Dan Carpenter
  0 siblings, 0 replies; 6+ messages in thread
From: Dan Carpenter @ 2021-02-15 13:26 UTC (permalink / raw)
  To: Michal Hocko
  Cc: devel, Ivan Safonov, Greg Kroah-Hartman, linux-kernel,
	Johannes Weiner, Waiman Long, Andrew Morton, Allen Pais,
	Abheek Dhawan

On Mon, Feb 15, 2021 at 09:44:24AM +0100, Michal Hocko wrote:
> On Sat 13-02-21 15:05:28, Ivan Safonov wrote:
> > memdup_user() is shorter and safer equivalent
> > of kmalloc/copy_from_user pair.
> > 
> > Signed-off-by: Ivan Safonov <insafonov@gmail.com>
> > ---
> >  drivers/staging/wlan-ng/p80211netdev.c | 28 ++++++++++++--------------
> >  1 file changed, 13 insertions(+), 15 deletions(-)
> > 
> > diff --git a/drivers/staging/wlan-ng/p80211netdev.c b/drivers/staging/wlan-ng/p80211netdev.c
> > index a15abb2c8f54..6f9666dc0277 100644
> > --- a/drivers/staging/wlan-ng/p80211netdev.c
> > +++ b/drivers/staging/wlan-ng/p80211netdev.c
> > @@ -569,24 +569,22 @@ static int p80211knetdev_do_ioctl(struct net_device *dev,
> >  		goto bail;
> >  	}
> >  
> > -	/* Allocate a buf of size req->len */
> > -	msgbuf = kmalloc(req->len, GFP_KERNEL);
> > -	if (msgbuf) {
> > -		if (copy_from_user(msgbuf, (void __user *)req->data, req->len))
> > -			result = -EFAULT;
> > -		else
> > -			result = p80211req_dorequest(wlandev, msgbuf);
> > +	msgbuf = memdup_user(req->data, req->len);
> 
> Move to memdup_user is definitely a right step. What is the range of
> req->len though? If this can be larger than PAGE_SIZE then vmemdup_user
> would be a better alternative.

req->len shoudn't be anywhere close to PAGE_SIZE but it's actually
important to check req->len and this code does not do that which leads
to memory corruption:

drivers/staging/wlan-ng/p80211netdev.c
   566                  goto bail;
   567          } else if (cmd != P80211_IFREQ) {
   568                  result = -EINVAL;
   569                  goto bail;
   570          }
   571  
   572          msgbuf = memdup_user(req->data, req->len);
   573          if (IS_ERR(msgbuf)) {
   574                  result = PTR_ERR(msgbuf);
   575                  goto bail;
   576          }
   577  
   578          result = p80211req_dorequest(wlandev, msgbuf);

We don't know that "req->len" is >= sizeof(*msgbuf), and then we pass
msgbuf top80211req_dorequest() which calls p80211req_handlemsg().  In
p80211req_handlemsg() then "req->len" has to be larger than
sizeof(struct p80211msg_lnxreq_hostwep).

   579  
   580          if (result == 0) {
   581                  if (copy_to_user
   582                      ((void __user *)req->data, msgbuf, req->len)) {
   583                          result = -EFAULT;
   584                  }
   585          }
   586          kfree(msgbuf);
   587  
   588  bail:
   589          /* If allocate,copyfrom or copyto fails, return errno */
   590          return result;
   591  }

Smatch has a problem parsing this code because struct ifreq *ifr is a
union and Smatch gets confused.  :/

regards,
dan carpenter
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-02-15 13:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-13 12:05 [PATCH] staging:wlan-ng: use memdup_user instead of kmalloc/copy_from_user Ivan Safonov
2021-02-13 12:05 ` Ivan Safonov
2021-02-15  8:44 ` Michal Hocko
2021-02-15  8:44   ` Michal Hocko
2021-02-15 13:26   ` Dan Carpenter
2021-02-15 13:26     ` Dan Carpenter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.