Skip to content

Commit 5afeb10

Browse files
Oliver Neukumgregkh
authored andcommitted
USB: usblcd doesn't limit memory consumption during write
usblcd currently has no way to limit memory consumption by fast writers. This is a security problem, as it allows users with write access to this device to drive the system into oom despite resource limits. Here's the fix taken from the modern skeleton driver. Signed-off-by: Oliver Neukum <oneukum@suse.de> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent fc0f8fc commit 5afeb10

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

‎drivers/usb/misc/usblcd.c‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ struct usb_lcd {
4242
size_t bulk_in_size; /* the size of the receive buffer */
4343
__u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
4444
__u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
45-
struct kref kref;
45+
struct kref kref;
46+
struct semaphore limit_sem; /* to stop writes at full throttle from
47+
* using up all RAM */
4648
};
4749
#define to_lcd_dev(d) container_of(d, struct usb_lcd, kref)
4850

51+
#define USB_LCD_CONCURRENT_WRITES 5
52+
4953
static struct usb_driver lcd_driver;
5054
static DEFINE_MUTEX(usb_lcd_open_mutex);
5155

@@ -186,12 +190,13 @@ static void lcd_write_bulk_callback(struct urb *urb)
186190
/* free up our allocated buffer */
187191
usb_buffer_free(urb->dev, urb->transfer_buffer_length,
188192
urb->transfer_buffer, urb->transfer_dma);
193+
up(&dev->limit_sem);
189194
}
190195

191196
static ssize_t lcd_write(struct file *file, const char __user * user_buffer, size_t count, loff_t *ppos)
192197
{
193198
struct usb_lcd *dev;
194-
int retval = 0;
199+
int retval = 0, r;
195200
struct urb *urb = NULL;
196201
char *buf = NULL;
197202

@@ -201,10 +206,16 @@ static ssize_t lcd_write(struct file *file, const char __user * user_buffer, siz
201206
if (count == 0)
202207
goto exit;
203208

209+
r = down_interruptible(&dev->limit_sem);
210+
if (r < 0)
211+
return -EINTR;
212+
204213
/* create a urb, and a buffer for it, and copy the data to the urb */
205214
urb = usb_alloc_urb(0, GFP_KERNEL);
206-
if (!urb)
207-
return -ENOMEM;
215+
if (!urb) {
216+
retval = -ENOMEM;
217+
goto err_no_buf;
218+
}
208219

209220
buf = usb_buffer_alloc(dev->udev, count, GFP_KERNEL, &urb->transfer_dma);
210221
if (!buf) {
@@ -239,6 +250,8 @@ static ssize_t lcd_write(struct file *file, const char __user * user_buffer, siz
239250
error:
240251
usb_buffer_free(dev->udev, count, buf, urb->transfer_dma);
241252
usb_free_urb(urb);
253+
err_no_buf:
254+
up(&dev->limit_sem);
242255
return retval;
243256
}
244257

@@ -277,6 +290,7 @@ static int lcd_probe(struct usb_interface *interface, const struct usb_device_id
277290
goto error;
278291
}
279292
kref_init(&dev->kref);
293+
sema_init(&dev->limit_sem, USB_LCD_CONCURRENT_WRITES);
280294

281295
dev->udev = usb_get_dev(interface_to_usbdev(interface));
282296
dev->interface = interface;

0 commit comments

Comments
 (0)