staging:wlan-ng: use memdup_user instead of kmalloc/copy_from_user
PATCH 1/1 staging:wlan-ng: use memdup_user instead of kmalloc/copy_from_user
memdup_user() is shorter and safer equivalent
of kmalloc/copy_from_user pair.
--- drivers/staging/wlan-ng/p80211netdev.c | 28 ++++++++++++-------------- 1 file changed, 13 insertions(+), 15 deletions(-)
drivers/staging/wlan-ng/p80211netdev.c +13 -15
@@ -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;
Signature · 1 line
2.26.2
Quoted 14 lines · Ivan Safonov
memdup_user()is shorter and safer equivalent
of kmalloc/copy_from_user pair.Signed-off-by Ivan Safonov <[email protected]>--- drivers/staging/wlan-ng/p80211netdev.c | 28 ++++++++++++-------------- 1 file changed, 13 insertions(+), 15 deletions(-)drivers/staging/wlan-ng/p80211netdev.c +0
-0@@ -569,24 +569,22 @@ static int p80211knetdev_do_ioctl(struct net_device *dev, goto bail;
drivers/staging/wlan-ng/p80211netdev.c static int p80211knetdev_do_ioctl(struct net_device *dev, · line 572
Quoted 10 lines
drivers/staging/wlan-ng/p80211netdev.c +1
-7} -/* 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.
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 17 lines · Michal Hocko
Quoted 16 lines · Ivan Safonov
memdup_user()is shorter and safer equivalent
of kmalloc/copy_from_user pair.Signed-off-by Ivan Safonov <[email protected]>--- drivers/staging/wlan-ng/p80211netdev.c | 28 ++++++++++++-------------- 1 file changed, 13 insertions(+), 15 deletions(-)drivers/staging/wlan-ng/p80211netdev.c +0
-0@@ -569,24 +569,22 @@ static int p80211knetdev_do_ioctl(struct net_device *dev, goto bail; }
Quoted 12 lines
drivers/staging/wlan-ng/p80211netdev.c static int p80211knetdev_do_ioctl(struct net_device *dev, · line 572
Quoted 8 lines
drivers/staging/wlan-ng/p80211netdev.c +1
-7-/* 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_useris definitely a right step. What is the range ofreq->lenthough? If this can be larger thanPAGE_SIZEthenvmemdup_userwould 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