lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20210715022652.GA2054080@pc>
Date:   Thu, 15 Jul 2021 03:26:52 +0100
From:   Salah Triki <salah.triki@...il.com>
To:     abbotti@....co.uk, hsweeten@...ionengravers.com,
        gregkh@...uxfoundation.org
Cc:     linux-kernel@...r.kernel.org
Subject: [PATCH] comedi: usbdux: free allocated memory/urbs on failure

Free allocated memory/urbs on failure in order to prevent memory leaks.

Signed-off-by: Salah Triki <salah.triki@...il.com>
---
 drivers/comedi/drivers/usbdux.c | 42 ++++++++++++++++++++++++++++-----
 1 file changed, 36 insertions(+), 6 deletions(-)

diff --git a/drivers/comedi/drivers/usbdux.c b/drivers/comedi/drivers/usbdux.c
index 0350f303d557..fe388a9a588a 100644
--- a/drivers/comedi/drivers/usbdux.c
+++ b/drivers/comedi/drivers/usbdux.c
@@ -1456,7 +1456,7 @@ static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
 		/* one frame: 1ms */
 		urb = usb_alloc_urb(1, GFP_KERNEL);
 		if (!urb)
-			return -ENOMEM;
+			goto free_ai_urbs1;
 		devpriv->ai_urbs[i] = urb;
 
 		urb->dev = usb;
@@ -1465,7 +1465,7 @@ static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
 		urb->transfer_flags = URB_ISO_ASAP;
 		urb->transfer_buffer = kzalloc(SIZEINBUF, GFP_KERNEL);
 		if (!urb->transfer_buffer)
-			return -ENOMEM;
+			goto free_ai_urbs2;
 
 		urb->complete = usbduxsub_ai_isoc_irq;
 		urb->number_of_packets = 1;
@@ -1478,7 +1478,7 @@ static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
 		/* one frame: 1ms */
 		urb = usb_alloc_urb(1, GFP_KERNEL);
 		if (!urb)
-			return -ENOMEM;
+			goto free_ao_urbs1;
 		devpriv->ao_urbs[i] = urb;
 
 		urb->dev = usb;
@@ -1487,7 +1487,7 @@ static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
 		urb->transfer_flags = URB_ISO_ASAP;
 		urb->transfer_buffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
 		if (!urb->transfer_buffer)
-			return -ENOMEM;
+			goto free_ao_urbs2;
 
 		urb->complete = usbduxsub_ao_isoc_irq;
 		urb->number_of_packets = 1;
@@ -1504,17 +1504,47 @@ static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
 	if (devpriv->pwm_buf_sz) {
 		urb = usb_alloc_urb(0, GFP_KERNEL);
 		if (!urb)
-			return -ENOMEM;
+			goto free_ao_urbs0;
 		devpriv->pwm_urb = urb;
 
 		/* max bulk ep size in high speed */
 		urb->transfer_buffer = kzalloc(devpriv->pwm_buf_sz,
 					       GFP_KERNEL);
 		if (!urb->transfer_buffer)
-			return -ENOMEM;
+			goto free_urb;
 	}
 
 	return 0;
+
+free_urb:
+	usb_free_urb(urb);
+
+free_ao_urbs0:
+	i = devpriv->n_ao_urbs;
+
+free_ao_urbs1:
+	i--;
+
+free_ao_urbs2:
+	while (i >= 0) {
+		kfree(devpriv->ao_urbs[i]->transfer_buffer);
+		usb_free_urb(devpriv->ao_urbs[i]);
+		i--;
+	}
+	i = devpriv->n_ai_urbs;
+
+free_ai_urbs1:
+	i--;
+
+free_ai_urbs2:
+	while (i >= 0) {
+		kfree(devpriv->ai_urbs[i]->transfer_buffer);
+		usb_free_urb(devpriv->ai_urbs[i]);
+		i--;
+	}
+
+	return -ENOMEM;
+
 }
 
 static void usbdux_free_usb_buffers(struct comedi_device *dev)
-- 
2.25.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ