netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: "David S. Miller" <davem@davemloft.net>, Johan Hovold <johan@kernel.org>
Cc: Jakub Kicinski <kuba@kernel.org>,
	Oliver Neukum <oneukum@suse.com>,
	Anirudh Rayabharam <mail@anirudhrb.com>,
	Johan Hovold <johan@kernel.org>,
	Zheng Yongjun <zhengyongjun3@huawei.com>,
	Geert Uytterhoeven <geert@linux-m68k.org>,
	Emil Renner Berthing <kernel@esmil.dk>,
	Rustam Kovhaev <rkovhaev@gmail.com>,
	linux-usb@vger.kernel.org, netdev@vger.kernel.org,
	kernel-janitors@vger.kernel.org
Subject: [PATCH v2 net] net: hso: check for allocation failure in hso_create_bulk_serial_device()
Date: Fri, 14 May 2021 17:24:48 +0300	[thread overview]
Message-ID: <YJ6IMH7jI9QFdGIX@mwanda> (raw)
In-Reply-To: <YJurlxqQ9L+zzIAS@hovoldconsulting.com>

In current kernels, small allocations never actually fail so this
patch shouldn't affect runtime.

Originally this error handling code written with the idea that if
the "serial->tiocmget" allocation failed, then we would continue
operating instead of bailing out early.  But in later years we added
an unchecked dereference on the next line.

	serial->tiocmget->serial_state_notification = kzalloc();
        ^^^^^^^^^^^^^^^^^^

Since these allocations are never going fail in real life, this is
mostly a philosophical debate, but I think bailing out early is the
correct behavior that the user would want.  And generally it's safer to
bail as soon an error happens.

Fixes: af0de1303c4e ("usb: hso: obey DMA rules in tiocmget")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
v2: Do more extensive clean up.  As Johan pointed out the comments and
later NULL checks can be removed.

 drivers/net/usb/hso.c | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c
index 3ef4b2841402..260f850d69eb 100644
--- a/drivers/net/usb/hso.c
+++ b/drivers/net/usb/hso.c
@@ -2618,29 +2618,28 @@ static struct hso_device *hso_create_bulk_serial_device(
 		num_urbs = 2;
 		serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
 					   GFP_KERNEL);
+		if (!serial->tiocmget)
+			goto exit;
 		serial->tiocmget->serial_state_notification
 			= kzalloc(sizeof(struct hso_serial_state_notification),
 					   GFP_KERNEL);
-		/* it isn't going to break our heart if serial->tiocmget
-		 *  allocation fails don't bother checking this.
-		 */
-		if (serial->tiocmget && serial->tiocmget->serial_state_notification) {
-			tiocmget = serial->tiocmget;
-			tiocmget->endp = hso_get_ep(interface,
-						    USB_ENDPOINT_XFER_INT,
-						    USB_DIR_IN);
-			if (!tiocmget->endp) {
-				dev_err(&interface->dev, "Failed to find INT IN ep\n");
-				goto exit;
-			}
-
-			tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
-			if (tiocmget->urb) {
-				mutex_init(&tiocmget->mutex);
-				init_waitqueue_head(&tiocmget->waitq);
-			} else
-				hso_free_tiomget(serial);
+		if (!serial->tiocmget->serial_state_notification)
+			goto exit;
+		tiocmget = serial->tiocmget;
+		tiocmget->endp = hso_get_ep(interface,
+					    USB_ENDPOINT_XFER_INT,
+					    USB_DIR_IN);
+		if (!tiocmget->endp) {
+			dev_err(&interface->dev, "Failed to find INT IN ep\n");
+			goto exit;
 		}
+
+		tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
+		if (tiocmget->urb) {
+			mutex_init(&tiocmget->mutex);
+			init_waitqueue_head(&tiocmget->waitq);
+		} else
+			hso_free_tiomget(serial);
 	}
 	else
 		num_urbs = 1;
-- 
2.30.2


  parent reply	other threads:[~2021-05-14 14:26 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-12 10:09 [PATCH net] net: hso: check for allocation failure in hso_create_bulk_serial_device() Dan Carpenter
2021-05-12 10:19 ` Johan Hovold
2021-05-12 13:12   ` Dan Carpenter
2021-05-14 14:24   ` Dan Carpenter [this message]
2021-05-17  8:07     ` [PATCH v2 " Johan Hovold
2021-05-17 21:00     ` patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YJ6IMH7jI9QFdGIX@mwanda \
    --to=dan.carpenter@oracle.com \
    --cc=davem@davemloft.net \
    --cc=geert@linux-m68k.org \
    --cc=johan@kernel.org \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=kernel@esmil.dk \
    --cc=kuba@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mail@anirudhrb.com \
    --cc=netdev@vger.kernel.org \
    --cc=oneukum@suse.com \
    --cc=rkovhaev@gmail.com \
    --cc=zhengyongjun3@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).