cdc-acm: remove unused element of struct acm
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v2 1/4 cdc-acm: reassemble fragmented notifications
USB devices may have very limitited endpoint packet sizes, so that notifications can not be transferred within one single usb packet. Reassembling of multiple packages may be necessary.
--- drivers/usb/class/cdc-acm.c | 102 +++++++++++++++++++++++++++++++------------- drivers/usb/class/cdc-acm.h | 2 + 2 files changed, 75 insertions(+), 29 deletions(-)
drivers/usb/class/cdc-acm.c +73 -29
@@ -282,39 +282,13 @@ static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);* Interrupt handlers for various ACM device responses */ -/* control interface reports status changes with "interrupt" transfers */ -static void acm_ctrl_irq (struct urb *urb) +static void acm_process_notification (struct acm *acm,unsigned char *buf) { -struct acm *acm = urb->context; -struct usb_cdc_notification *dr = urb->transfer_buffer; -unsigned char *data;int newctrl;int difference; -int retval; -int status = urb->status; +struct usb_cdc_notification *dr = (struct usb_cdc_notification *)buf; +unsigned char *data = (unsigned char *)(dr +1 ); -switch (status) { -case 0 : -/* success */ -break ; -case -ECONNRESET : -case -ENOENT : -case -ESHUTDOWN : -/* this urb is terminated, clean up */ -dev_dbg (&acm->control->dev, -"%s - urb shutting down with status: %d\n" , - __func__, status); -return ; -default : -dev_dbg (&acm->control->dev, -"%s - nonzero urb status received: %d\n" , - __func__, status); - goto exit; - } - -usb_mark_last_busy (acm->dev); - - data = (unsigned char *)(dr +1 );switch (dr->bNotificationType) {case USB_CDC_NOTIFY_NETWORK_CONNECTION : dev_dbg(&acm->control->dev,
@@ -363,8 +337,74 @@ static void acm_ctrl_irq(struct urb *urb) __func__, dr->bNotificationType, dr->wIndex, dr->wLength, data[0], data[1]); + } +} + +/* control interface reports status changes with "interrupt" transfers */ +static void acm_ctrl_irq (struct urb *urb) +{ +struct acm *acm = urb->context; +struct usb_cdc_notification *dr = urb->transfer_buffer; +unsigned int current_size = urb->actual_length; +unsigned int expected_size, copy_size; +int retval; +int status = urb->status; + +switch (status) { +case 0 : +/* success */ break ; +case -ECONNRESET : +case -ENOENT : +case -ESHUTDOWN : +/* this urb is terminated, clean up */ +kfree (acm->notification_buffer); + acm->notification_buffer =NULL ; +dev_dbg (&acm->control->dev, +"%s - urb shutting down with status: %d\n" , + __func__, status); +return ; +default : +dev_dbg (&acm->control->dev, +"%s - nonzero urb status received: %d\n" , + __func__, status); + goto exit; } + +usb_mark_last_busy (acm->dev); + +if (acm->notification_buffer) + dr = (struct usb_cdc_notification *)acm->notification_buffer; + +/* assume the first package contains at least two bytes */ + expected_size = dr->wLength +8 ; + +if (current_size < expected_size) { +/* notification is transmitted framented, reassemble */ +if (!acm->notification_buffer) { + acm->notification_buffer = +kmalloc (expected_size,GFP_ATOMIC ); + acm->nb_index =0 ; + } + + copy_size =min (current_size, + expected_size - acm->nb_index); + +memcpy (&acm->notification_buffer[acm->nb_index], + urb->transfer_buffer, copy_size); + acm->nb_index += copy_size; + current_size = acm->nb_index; + } + +if (current_size < expected_size) + goto exit; + +/* notification complete */ +acm_process_notification (acm, (unsigned char *)dr); + +kfree (acm->notification_buffer); + acm->notification_buffer =NULL ; + exit: retval =usb_submit_urb (urb,GFP_ATOMIC );if (retval && retval != -EPERM )
@@ -1488,6 +1528,8 @@ static int acm_probe(struct usb_interface *intf, epctrl->bInterval ? epctrl->bInterval : 16); acm->ctrlurb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; acm->ctrlurb->transfer_dma = acm->ctrl_dma; + acm->notification_buffer =NULL ; + acm->nb_index =0 ;dev_info (&intf->dev,"ttyACM%d: USB ACM device\n" , minor);
@@ -1580,6 +1622,8 @@ static void acm_disconnect(struct usb_interface *intf)usb_free_coherent (acm->dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);acm_read_buffers_free (acm); +kfree (acm->notification_buffer); +if (!acm->combined_interfaces)usb_driver_release_interface (&acm_driver, intf == acm->control ? acm->data : acm->control);
drivers/usb/class/cdc-acm.h +2 -0
@@ -98,6 +98,8 @@ struct acm {struct acm_wb *putbuffer;/* for acm_tty_put_char() */ int rx_buflimit;spinlock_t read_lock; +u8 *notification_buffer;/* to reassemble fragmented notifications */ +unsigned int nb_index;int write_used;/* number of non-empty write buffers */ int transmitting;spinlock_t write_lock;
Signature · 1 line
2.1.4
PATCH v2 2/4 cdc-acm: fix possible invalid access when processing notification
Notifications may only be 8 bytes so long. Accessing the 9th and 10th byte of unimplemented/unknown notifications may be insecure. Also check the length of known notifications before accessing anything behind the 8th byte.
--- drivers/usb/class/cdc-acm.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/usb/class/cdc-acm.c +8 -3
@@ -296,6 +296,12 @@ static void acm_process_notification(struct acm *acm, unsigned char *buf)break ;case USB_CDC_NOTIFY_SERIAL_STATE : +if (dr->wLength !=2 ) { +dev_dbg (&acm->control->dev, +"%s - malformed serial state\n" , __func__); +break ; + } + newctrl =get_unaligned_le16 (data);if (!acm->clocal && (acm->ctrlin & ~newctrl &ACM_CTRL_DCD )) {
@@ -332,11 +338,10 @@ static void acm_process_notification(struct acm *acm, unsigned char *buf)default :dev_dbg (&acm->control->dev, -"%s - unknown notification %d received: index %d " -"len %d data0 %d data1 %d\n" , +"%s - unknown notification %d received: index %d len %d\n" , __func__, dr->bNotificationType, dr->wIndex, - dr->wLength, data[0 ], data[1 ]); + dr->wLength); } }
Signature · 1 line
2.1.4
Oliver Neukum
maintainerRe: [PATCH 2/4] cdc-acm: fix possible invalid access when processing notification
Am Dienstag, den 14.03.2017, 21:14 +0100 schrieb Tobias Herzog:
Quoted 15 lines
Notifications may only be 8 bytes so long. Accessing the 9th and 10th byte of unimplemented/unknown notifications may be insecure. Also check the length of known notifications before accessing anything behind the 8th byte.
Signed-off-by Tobias Herzog <[email protected]>--- drivers/usb/class/cdc-acm.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)drivers/usb/class/cdc-acm.c +0
-0
drivers/usb/class/cdc-acm.c static void acm_process_notification(struct acm *acm, unsigned char *buf) · line 299
Quoted 4 lines
drivers/usb/class/cdc-acm.c +1
-0@@ -296,6 +296,12 @@ static void acm_process_notification(struct acm *acm, unsigned char *buf)break ;case USB_CDC_NOTIFY_SERIAL_STATE : +if (dr->wLength !=2 ) {
Endianness
Quoted 22 lines
drivers/usb/class/cdc-acm.c +7
-3+dev_dbg (&acm->control->dev, +"%s - malformed serial state\n" , __func__); +break ; + } + newctrl =get_unaligned_le16 (data);if (!acm->clocal && (acm->ctrlin & ~newctrl &ACM_CTRL_DCD )) {@@ -332,11 +338,10 @@ static void acm_process_notification(struct acm *acm, unsigned char *buf)default :dev_dbg (&acm->control->dev, -"%s - unknown notification %d received: index %d " -"len %d data0 %d data1 %d\n" , +"%s - unknown notification %d received: index %d len %d\n" , __func__, dr->bNotificationType, dr->wIndex, - dr->wLength, data[0 ], data[1 ]); + dr->wLength); } }
PATCH v2 3/4 cdc-acm: log message for serial state notification
In reply to Tobias HerzogAdds a similar log message to USB_CDC_NOTIFY_SERIAL_STATE as it is already done with USB_CDC_NOTIFY_NETWORK_CONNECTION.
--- drivers/usb/class/cdc-acm.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/usb/class/cdc-acm.c +2 -0
@@ -303,6 +303,8 @@ static void acm_process_notification(struct acm *acm, unsigned char *buf) } newctrl =get_unaligned_le16 (data); +dev_dbg (&acm->control->dev, +"%s - serial state: 0x%x\n" , __func__, newctrl);if (!acm->clocal && (acm->ctrlin & ~newctrl &ACM_CTRL_DCD )) { dev_dbg(&acm->control->dev,
Signature · 1 line
2.1.4
PATCH v2 4/4 cdc-acm: remove unused element of struct acm
In reply to Tobias Herzogwrite_used was introduced with commit 884b600f63dc ("[PATCH] USB: fix acm trouble with terminals") but never used since.
--- drivers/usb/class/cdc-acm.h | 1 - 1 file changed, 1 deletion(-)
drivers/usb/class/cdc-acm.h +0 -1
@@ -100,7 +100,6 @@ struct acm {spinlock_t read_lock;u8 *notification_buffer;/* to reassemble fragmented notifications */ unsigned int nb_index; -int write_used;/* number of non-empty write buffers */ int transmitting;spinlock_t write_lock;struct mutex mutex;
Signature · 1 line
2.1.4
Oliver Neukum
maintainerRe: [PATCH 1/4] cdc-acm: reassemble fragmented notifications
In reply to Tobias HerzogAm Dienstag, den 14.03.2017, 21:14 +0100 schrieb Tobias Herzog:
Quoted 3 lines
USB devices may have very limitited endpoint packet sizes, so that notifications can not be transferred within one single usb packet. Reassembling of multiple packages may be necessary.
Hi,
thank you for the patch. Unfortunately it has some issue. Please see the comments inside.
Regards
Oliver
Quoted 71 lines
Signed-off-by Tobias Herzog <[email protected]>--- drivers/usb/class/cdc-acm.c | 102 +++++++++++++++++++++++++++++++------------- drivers/usb/class/cdc-acm.h | 2 + 2 files changed, 75 insertions(+), 29 deletions(-)drivers/usb/class/cdc-acm.c +16
-29@@ -282,39 +282,13 @@ static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);* Interrupt handlers for various ACM device responses */ -/* control interface reports status changes with "interrupt" transfers */ -static void acm_ctrl_irq (struct urb *urb) +static void acm_process_notification (struct acm *acm,unsigned char *buf) { -struct acm *acm = urb->context; -struct usb_cdc_notification *dr = urb->transfer_buffer; -unsigned char *data;int newctrl;int difference; -int retval; -int status = urb->status; +struct usb_cdc_notification *dr = (struct usb_cdc_notification *)buf; +unsigned char *data = (unsigned char *)(dr +1 ); -switch (status) { -case 0 : -/* success */ -break ; -case -ECONNRESET : -case -ENOENT : -case -ESHUTDOWN : -/* this urb is terminated, clean up */ -dev_dbg (&acm->control->dev, -"%s - urb shutting down with status: %d\n" , - __func__, status); -return ; -default : -dev_dbg (&acm->control->dev, -"%s - nonzero urb status received: %d\n" , - __func__, status); - goto exit; - } - -usb_mark_last_busy (acm->dev); - - data = (unsigned char *)(dr +1 );switch (dr->bNotificationType) {case USB_CDC_NOTIFY_NETWORK_CONNECTION : dev_dbg(&acm->control->dev,@@ -363,8 +337,74 @@ static void acm_ctrl_irq(struct urb *urb) __func__, dr->bNotificationType, dr->wIndex, dr->wLength, data[0], data[1]); + } +} + +/* control interface reports status changes with "interrupt" transfers */ +static void acm_ctrl_irq (struct urb *urb) +{ +struct acm *acm = urb->context; +struct usb_cdc_notification *dr = urb->transfer_buffer; +unsigned int current_size = urb->actual_length; +unsigned int expected_size, copy_size; +int retval; +int status = urb->status; +
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 362
Quoted 10 lines
drivers/usb/class/cdc-acm.c +9
-0+switch (status) { +case 0 : +/* success */ break ; +case -ECONNRESET : +case -ENOENT : +case -ESHUTDOWN : +/* this urb is terminated, clean up */ +kfree (acm->notification_buffer); + acm->notification_buffer =NULL ;
Why? Disconnect() will free it anyway. It should be enough to discard the content.
Quoted 8 lines
drivers/usb/class/cdc-acm.c +8
-0+dev_dbg (&acm->control->dev, +"%s - urb shutting down with status: %d\n" , + __func__, status); +return ; +default : +dev_dbg (&acm->control->dev, +"%s - nonzero urb status received: %d\n" , + __func__, status);
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 380
Quoted 10 lines
drivers/usb/class/cdc-acm.c +9
-0+ goto exit; } + +usb_mark_last_busy (acm->dev); + +if (acm->notification_buffer) + dr = (struct usb_cdc_notification *)acm->notification_buffer; + +/* assume the first package contains at least two bytes */ + expected_size = dr->wLength +8 ;
You need the explain where you got the 8 from. In fact a define would be best.
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 383
Quoted 3 lines
drivers/usb/class/cdc-acm.c +3
-0+ +if (current_size < expected_size) { +/* notification is transmitted framented, reassemble */
Please fix the typo.
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 386
Quoted 3 lines
drivers/usb/class/cdc-acm.c +3
-0+if (!acm->notification_buffer) { + acm->notification_buffer = +kmalloc (expected_size,GFP_ATOMIC );
This can fail. You _must_ check for that.
Quoted 4 lines
drivers/usb/class/cdc-acm.c +4
-0+ acm->nb_index =0 ; + } + + copy_size = min(current_size,
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 400
Quoted 10 lines
drivers/usb/class/cdc-acm.c +10
-0+ expected_size - acm->nb_index); + +memcpy (&acm->notification_buffer[acm->nb_index], + urb->transfer_buffer, copy_size); + acm->nb_index += copy_size; + current_size = acm->nb_index; + } + +if (current_size < expected_size) + goto exit;
This is an unneeded goto.
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 405
Quoted 4 lines
drivers/usb/class/cdc-acm.c +4
-0+/* notification complete */ +acm_process_notification (acm, (unsigned char *)dr); + +kfree (acm->notification_buffer);
Why? If one message was fragmented, the next one will also likely be fragmented. Why release the buffer before you know whether it can be reused?
Quoted 36 lines
drivers/usb/class/cdc-acm.c +6
-0+ acm->notification_buffer =NULL ; + exit: retval =usb_submit_urb (urb,GFP_ATOMIC );if (retval && retval != -EPERM )@@ -1488,6 +1528,8 @@ static int acm_probe(struct usb_interface *intf, epctrl->bInterval ? epctrl->bInterval : 16); acm->ctrlurb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; acm->ctrlurb->transfer_dma = acm->ctrl_dma; + acm->notification_buffer =NULL ; + acm->nb_index =0 ;dev_info (&intf->dev,"ttyACM%d: USB ACM device\n" , minor);@@ -1580,6 +1622,8 @@ static void acm_disconnect(struct usb_interface *intf)usb_free_coherent (acm->dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);acm_read_buffers_free (acm); +kfree (acm->notification_buffer); +if (!acm->combined_interfaces)usb_driver_release_interface (&acm_driver, intf == acm->control ? acm->data : acm->control);drivers/usb/class/cdc-acm.h +2
-0@@ -98,6 +98,8 @@ struct acm {struct acm_wb *putbuffer;/* for acm_tty_put_char() */ int rx_buflimit;spinlock_t read_lock; +u8 *notification_buffer;/* to reassemble fragmented notifications */ +unsigned int nb_index;int write_used;/* number of non-empty write buffers */ int transmitting;spinlock_t write_lock;
Re: [PATCH 1/4] cdc-acm: reassemble fragmented notifications
Hi,
thank you for your feedback. I tried to fix all issues with v2. I was just unsure with one point (see comment).
/tobias
Quoted 123 lines
Am Dienstag, den 14.03.2017, 21:14 +0100 schrieb Tobias Herzog:
Quoted 4 lines
USB devices may have very limitited endpoint packet sizes, so that notifications can not be transferred within one single usb packet. Reassembling of multiple packages may be necessary.
Hi,
thank you for the patch. Unfortunately it has some issue. Please see the comments inside.
Regards
OliverQuoted 84 lines
Signed-off-by Tobias Herzog <[email protected]>---
drivers/usb/class/cdc-acm.c | 102 +++++++++++++++++++++++++++++++-
------------
drivers/usb/class/cdc-acm.h | 2 +2 files changed, 75 insertions(+), 29 deletions(-)drivers/usb/class/cdc- renamed from drivers/usb/class/cdc-acm.c +0
-0
acm.c
index e35b150..40714fe 100644drivers/usb/class/cdc-acm.c +0
-0@@ -282,39 +282,13 @@ static DEVICE_ATTR(iCountryCodeRelDate,S_IRUGO, show_country_rel_date, NULL); * Interrupt handlers for various ACM device responses */
-/* control interface reports status changes with "interrupt" transfers */drivers/usb/class/cdc-acm.c +1
-1-static void acm_ctrl_irq (struct urb *urb) +static void acm_process_notification(struct acm *acm, unsignedchar *buf)
{drivers/usb/class/cdc-acm.c +0
-3-struct acm *acm = urb->context; -struct usb_cdc_notification *dr = urb->transfer_buffer; -unsigned char *data;int newctrl;
int difference;drivers/usb/class/cdc-acm.c +1
-2-int retval; -int status = urb->status; + struct usb_cdc_notification *dr = (struct
usb_cdc_notification*)buf;drivers/usb/class/cdc-acm.c +1
-0+unsigned char *data = (unsigned char *)(dr +1 );drivers/usb/class/cdc-acm.c +0
-10-switch (status) { -case 0 : -/* success */ -break ; -case -ECONNRESET : -case -ENOENT : -case -ESHUTDOWN : -/* this urb is terminated, clean up */ - dev_dbg(&acm->control->dev, - "%s - urb shutting down with status:%d\n",
drivers/usb/class/cdc-acm.c +0
-12- __func__, status); -return ; -default : -dev_dbg (&acm->control->dev, -"%s - nonzero urb status received: %d\n" , - __func__, status); - goto exit; - } - -usb_mark_last_busy (acm->dev); - - data = (unsigned char *)(dr +1 );switch (
dr->bNotificationType) {
caseUSB_CDC_NOTIFY_NETWORK_CONNECTION:
dev_dbg(&acm->control->dev,drivers/usb/class/cdc-acm.c +4
-0@@ -363,8 +337,74 @@ static void acm_ctrl_irq(struct urb *urb) __func__, dr->bNotificationType, dr->wIndex, dr->wLength, data[0], data[1]); + } +} + +/* control interface reports status changes with "interrupt" transfers */
drivers/usb/class/cdc-acm.c +12
-0+static void acm_ctrl_irq (struct urb *urb) +{ +struct acm *acm = urb->context; +struct usb_cdc_notification *dr = urb->transfer_buffer; +unsigned int current_size = urb->actual_length; +unsigned int expected_size, copy_size; +int retval; +int status = urb->status; + +switch (status) { +case 0 : +/* success */ break;
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 362
Quoted 6 lines
drivers/usb/class/cdc-acm.c +6
-0+case -ECONNRESET : +case -ENOENT : +case -ESHUTDOWN : +/* this urb is terminated, clean up */ +kfree (acm->notification_buffer); + acm->notification_buffer =NULL ;Why?
Disconnect()will free it anyway. It should be enough to discard the content.Quoted 17 lines
drivers/usb/class/cdc-acm.c +2
-0+ dev_dbg(&acm->control->dev, + "%s - urb shutting down with status:%d\n",
drivers/usb/class/cdc-acm.c +7
-0+ __func__, status); +return ; +default : +dev_dbg (&acm->control->dev, +"%s - nonzero urb status received: %d\n" , + __func__, status); + goto exit;}
drivers/usb/class/cdc-acm.c +5
-0+ +usb_mark_last_busy (acm->dev); + +if (acm->notification_buffer) + dr = (struct usb_cdc_notification *)acm-
Quoted 6 lines
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 380
Quoted 4 lines
Quoted 1 line
notification_buffer;drivers/usb/class/cdc-acm.c +3
-0+ +/* assume the first package contains at least two bytes */ + expected_size = dr->wLength +8 ;You need the explain where you got the 8 from. In fact a define would be best.
I feel better to use 'sizeof(struct usb_cdc_notification)' here, than using an separate, hard-coded define. In fact this is really what we want here, as the buffer needs to cover the notification header (i.e. struct usb_cdc_notification) + the additional (optional) data.
Quoted 87 lines
Quoted 6 lines
drivers/usb/class/cdc-acm.c +3
-0+ +if (current_size < expected_size) { +/* notification is transmitted framented, reassemble */
Please fix the typo.
Quoted 5 lines
drivers/usb/class/cdc-acm.c +3
-0+if (!acm->notification_buffer) { + acm->notification_buffer = + kmalloc(expected_size,
GFP_ATOMIC);This can fail. You _must_ check for that.
Quoted 5 lines
drivers/usb/class/cdc-acm.c +4
-0+ acm->nb_index =0 ; + } + + copy_size = min(current_size,drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 400
Quoted 10 lines
drivers/usb/class/cdc-acm.c +10
-0+ expected_size - acm->nb_index); + +memcpy (&acm->notification_buffer[acm->nb_index], + urb->transfer_buffer, copy_size); + acm->nb_index += copy_size; + current_size = acm->nb_index; + } + +if (current_size < expected_size) + goto exit;This is an unneeded goto.
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 405
Quoted 5 lines
drivers/usb/class/cdc-acm.c +4
-0+/* notification complete */ +acm_process_notification (acm, (unsigned char *)dr); + +kfree (acm->notification_buffer);Why? If one message was fragmented, the next one will also likely be fragmented. Why release the buffer before you know whether it can be reused?
Quoted 46 lines
drivers/usb/class/cdc-acm.c +2
-0+ acm->notification_buffer =NULL ; +exit:
retval =usb_submit_urb(urb, GFP_ATOMIC);
if (retval && retval !=-EPERM)drivers/usb/class/cdc-acm.c +0
-0@@ -1488,6 +1528,8 @@ static int acm_probe(struct usb_interface *intf,epctrl->bInterval ?
epctrl->bInterval:
16);
acm->ctrlurb->transfer_flags|=URB_NO_TRANSFER_DMA_MAP;acm->ctrlurb->transfer_dma=acm->ctrl_dma;drivers/usb/class/cdc-acm.c +2
-0+ acm->notification_buffer =NULL ; + acm->nb_index =0 ;
dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);drivers/usb/class/cdc-acm.c +0
-0@@ -1580,6 +1622,8 @@ static void acm_disconnect(struct usb_interface *intf)usb_free_coherent(acm->dev,
acm->ctrlsize, acm-Quoted 1 line
ctrl_buffer,acm->ctrl_dma);
acm_read_buffers_free(acm);drivers/usb/class/cdc-acm.c +2
-0+kfree (acm->notification_buffer); +if (!acm->combined_interfaces) usb_driver_release_interface(&acm_driver, intf ==
acm->control?
acm->data:acm->control);drivers/usb/class/cdc- renamed from drivers/usb/class/cdc-acm.h +0
-0
acm.h
index c980f11..bc07fb2 100644drivers/usb/class/cdc-acm.h +0
-0@@ -98,6 +98,8 @@ struct acm {struct
acm_wb*putbuffer; /* foracm_tty_put_char()*/
intrx_buflimit;
spinlock_tread_lock;drivers/usb/class/cdc-acm.h +1
-0+u8 *notification_buffer;/* to reassemble fragmented notifications */
drivers/usb/class/cdc-acm.h +1
-0+unsigned int nb_index;int
write_used; /*
number of non-empty write buffers */
int transmitting;
spinlock_twrite_lock;
v2 cover cdc-acm: reassemble fragmented notifications
In reply to a message not in the archiveUSB devices may have very limitited endpoint packet sizes, so that notifications can not be transferred within one single usb packet. This patchset adds the ability to reassemble notifications that are transmitted fragmented.
v2:
* reuse an allocated buffer for further notifications
* fixed issues with endianess
* check buffer allocation (kmalloc)
* don't use hard coded size of notification-header
* fixed typo + code structure (unneeded goto)
Tobias Herzog (4):
cdc-acm: reassemble fragmented notifications
cdc-acm: fix possible invalid access when processing notification
cdc-acm: log message for serial state notification
cdc-acm: remove unused element of struct acm
drivers/usb/class/cdc-acm.c | 119 ++++++++++++++++++++++++++++++++------------ drivers/usb/class/cdc-acm.h | 4 +- 2 files changed, 90 insertions(+), 33 deletions(-)
Signature · 1 line
2.1.4
PATCH v2 1/4 cdc-acm: reassemble fragmented notifications
USB devices may have very limitited endpoint packet sizes, so that notifications can not be transferred within one single usb packet. Reassembling of multiple packages may be necessary.
--- drivers/usb/class/cdc-acm.c | 106 ++++++++++++++++++++++++++++++++------------ drivers/usb/class/cdc-acm.h | 3 ++ 2 files changed, 80 insertions(+), 29 deletions(-)
drivers/usb/class/cdc-acm.c +77 -29
@@ -282,39 +282,13 @@ static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);* Interrupt handlers for various ACM device responses */ -/* control interface reports status changes with "interrupt" transfers */ -static void acm_ctrl_irq (struct urb *urb) +static void acm_process_notification (struct acm *acm,unsigned char *buf) { -struct acm *acm = urb->context; -struct usb_cdc_notification *dr = urb->transfer_buffer; -unsigned char *data;int newctrl;int difference; -int retval; -int status = urb->status; - -switch (status) { -case 0 : -/* success */ -break ; -case -ECONNRESET : -case -ENOENT : -case -ESHUTDOWN : -/* this urb is terminated, clean up */ -dev_dbg (&acm->control->dev, -"%s - urb shutting down with status: %d\n" , - __func__, status); -return ; -default : -dev_dbg (&acm->control->dev, -"%s - nonzero urb status received: %d\n" , - __func__, status); - goto exit; - } +struct usb_cdc_notification *dr = (struct usb_cdc_notification *)buf; +unsigned char *data = (unsigned char *)(dr +1 ); -usb_mark_last_busy (acm->dev); - - data = (unsigned char *)(dr +1 );switch (dr->bNotificationType) {case USB_CDC_NOTIFY_NETWORK_CONNECTION : dev_dbg(&acm->control->dev,
@@ -363,8 +337,77 @@ static void acm_ctrl_irq(struct urb *urb) __func__, dr->bNotificationType, dr->wIndex, dr->wLength, data[0], data[1]); + } +} + +/* control interface reports status changes with "interrupt" transfers */ +static void acm_ctrl_irq (struct urb *urb) +{ +struct acm *acm = urb->context; +struct usb_cdc_notification *dr = urb->transfer_buffer; +unsigned int current_size = urb->actual_length; +unsigned int expected_size, copy_size; +int retval; +int status = urb->status; + +switch (status) { +case 0 : +/* success */ break ; +case -ECONNRESET : +case -ENOENT : +case -ESHUTDOWN : +/* this urb is terminated, clean up */ + acm->nb_index =0 ; +dev_dbg (&acm->control->dev, +"%s - urb shutting down with status: %d\n" , + __func__, status); +return ; +default : +dev_dbg (&acm->control->dev, +"%s - nonzero urb status received: %d\n" , + __func__, status); + goto exit; } + +usb_mark_last_busy (acm->dev); + +if (acm->nb_index) + dr = (struct usb_cdc_notification *)acm->notification_buffer; + +/* size = notification-header + (optional) data */ + expected_size =sizeof (struct usb_cdc_notification ) + +le16_to_cpu (dr->wLength); + +if (current_size < expected_size) { +/* notification is transmitted fragmented, reassemble */ +if (acm->nb_size < expected_size) { +if (acm->nb_size) { +kfree (acm->notification_buffer); + acm->nb_size =0 ; + } + acm->notification_buffer = +kmalloc (expected_size,GFP_ATOMIC ); +if (!acm->notification_buffer) + goto exit; + acm->nb_size = expected_size; + } + + copy_size =min (current_size, + expected_size - acm->nb_index); + +memcpy (&acm->notification_buffer[acm->nb_index], + urb->transfer_buffer, copy_size); + acm->nb_index += copy_size; + current_size = acm->nb_index; + } + +if (current_size >= expected_size) { +/* notification complete */ +acm_process_notification (acm, (unsigned char *)dr); + acm->nb_index =0 ; + } + exit: retval =usb_submit_urb (urb,GFP_ATOMIC );if (retval && retval != -EPERM )
@@ -1488,6 +1531,9 @@ static int acm_probe(struct usb_interface *intf, epctrl->bInterval ? epctrl->bInterval : 16); acm->ctrlurb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; acm->ctrlurb->transfer_dma = acm->ctrl_dma; + acm->notification_buffer =NULL ; + acm->nb_index =0 ; + acm->nb_size =0 ;dev_info (&intf->dev,"ttyACM%d: USB ACM device\n" , minor);
@@ -1580,6 +1626,8 @@ static void acm_disconnect(struct usb_interface *intf)usb_free_coherent (acm->dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);acm_read_buffers_free (acm); +kfree (acm->notification_buffer); +if (!acm->combined_interfaces)usb_driver_release_interface (&acm_driver, intf == acm->control ? acm->data : acm->control);
drivers/usb/class/cdc-acm.h +3 -0
@@ -98,6 +98,9 @@ struct acm {struct acm_wb *putbuffer;/* for acm_tty_put_char() */ int rx_buflimit;spinlock_t read_lock; +u8 *notification_buffer;/* to reassemble fragmented notifications */ +unsigned int nb_index; +unsigned int nb_size;int write_used;/* number of non-empty write buffers */ int transmitting;spinlock_t write_lock;
Signature · 1 line
2.1.4
Oliver Neukum
maintainerRe: [PATCH v2 1/4] cdc-acm: reassemble fragmented notifications
Am Samstag, den 18.03.2017, 19:52 +0100 schrieb Tobias Herzog:
Quoted 3 lines
USB devices may have very limitited endpoint packet sizes, so that notifications can not be transferred within one single usb packet. Reassembling of multiple packages may be necessary.
Hi,
almost perfect.
A few new issue. Comments inline.
drivers/usb/class/cdc-acm.c static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL); · line 290
Quoted 3 lines
drivers/usb/class/cdc-acm.c +2
-0+struct usb_cdc_notification *dr = (struct usb_cdc_notification *)buf; +unsigned char *data = (unsigned char *)(dr +1 );
This is border line incorrect. It depends on the compiler not adding padding. Please make it
buf + sizeof(struct usb_cdc_notification)
Quoted 51 lines
drivers/usb/class/cdc-acm.c +39
-3-usb_mark_last_busy (acm->dev); - - data = (unsigned char *)(dr +1 );switch (dr->bNotificationType) {case USB_CDC_NOTIFY_NETWORK_CONNECTION : dev_dbg(&acm->control->dev,@@ -363,8 +337,77 @@ static void acm_ctrl_irq(struct urb *urb) __func__, dr->bNotificationType, dr->wIndex, dr->wLength, data[0], data[1]); + } +} + +/* control interface reports status changes with "interrupt" transfers */ +static void acm_ctrl_irq (struct urb *urb) +{ +struct acm *acm = urb->context; +struct usb_cdc_notification *dr = urb->transfer_buffer; +unsigned int current_size = urb->actual_length; +unsigned int expected_size, copy_size; +int retval; +int status = urb->status; + +switch (status) { +case 0 : +/* success */ break ; +case -ECONNRESET : +case -ENOENT : +case -ESHUTDOWN : +/* this urb is terminated, clean up */ + acm->nb_index =0 ; +dev_dbg (&acm->control->dev, +"%s - urb shutting down with status: %d\n" , + __func__, status); +return ; +default : +dev_dbg (&acm->control->dev, +"%s - nonzero urb status received: %d\n" , + __func__, status); + goto exit; } + +usb_mark_last_busy (acm->dev); + +if (acm->nb_index) + dr = (struct usb_cdc_notification *)acm->notification_buffer; + +/* size = notification-header + (optional) data */ + expected_size =sizeof (struct usb_cdc_notification ) + +le16_to_cpu (dr->wLength);
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 390
Quoted 10 lines
drivers/usb/class/cdc-acm.c +10
-0+ +if (current_size < expected_size) { +/* notification is transmitted fragmented, reassemble */ +if (acm->nb_size < expected_size) { +if (acm->nb_size) { +kfree (acm->notification_buffer); + acm->nb_size =0 ; + } + acm->notification_buffer = +kmalloc (expected_size,GFP_ATOMIC );
Given how kmalloc works you'd better round this up to a power of two.
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 392
Quoted 2 lines
drivers/usb/class/cdc-acm.c +2
-0+if (!acm->notification_buffer) + goto exit;
This is most subtle. Please add a comment that this prevents a double free if we get a disconnect()
Review static void acm_ctrl_irq(struct urb *urb) · line 394
Quoted 2 lines
(quoted diff) +2
-0+ acm->nb_size = expected_size; + }
Regards
Oliver
Re: [PATCH v2 1/4] cdc-acm: reassemble fragmented notifications
Hi Oliver,
thank you for your patience... :) I have a question to one of your comments (see below).
Best regards,
Tobias
Quoted 90 lines
Am Samstag, den 18.03.2017, 19:52 +0100 schrieb Tobias Herzog:
Quoted 4 lines
USB devices may have very limitited endpoint packet sizes, so that notifications can not be transferred within one single usb packet. Reassembling of multiple packages may be necessary.
Hi,
almost perfect.
A few new issue. Comments inline.drivers/usb/class/cdc-acm.c static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL); · line 290
Quoted 5 lines
(quoted diff) +1
-0+ struct usb_cdc_notification *dr = (struct
usb_cdc_notification*)buf;drivers/usb/class/cdc-acm.c +1
-0+unsigned char *data = (unsigned char *)(dr +1 );This is border line incorrect. It depends on the compiler not adding padding. Please make it
buf +
sizeof(struct usb_cdc_notification)Quoted 66 lines
drivers/usb/class/cdc-acm.c +0
-3-usb_mark_last_busy (acm->dev); - - data = (unsigned char *)(dr +1 );switch (
dr->bNotificationType) {
caseUSB_CDC_NOTIFY_NETWORK_CONNECTION:
dev_dbg(&acm->control->dev,drivers/usb/class/cdc-acm.c +4
-0@@ -363,8 +337,77 @@ static void acm_ctrl_irq(struct urb *urb) __func__, dr->bNotificationType, dr->wIndex, dr->wLength, data[0], data[1]); + } +} + +/* control interface reports status changes with "interrupt" transfers */
drivers/usb/class/cdc-acm.c +12
-0+static void acm_ctrl_irq (struct urb *urb) +{ +struct acm *acm = urb->context; +struct usb_cdc_notification *dr = urb->transfer_buffer; +unsigned int current_size = urb->actual_length; +unsigned int expected_size, copy_size; +int retval; +int status = urb->status; + +switch (status) { +case 0 : +/* success */ break;
drivers/usb/class/cdc-acm.c +7
-0+case -ECONNRESET : +case -ENOENT : +case -ESHUTDOWN : +/* this urb is terminated, clean up */ + acm->nb_index =0 ; + dev_dbg(&acm->control->dev, + "%s - urb shutting down with status:%d\n",
drivers/usb/class/cdc-acm.c +7
-0+ __func__, status); +return ; +default : +dev_dbg (&acm->control->dev, +"%s - nonzero urb status received: %d\n" , + __func__, status); + goto exit;}
drivers/usb/class/cdc-acm.c +5
-0+ +usb_mark_last_busy (acm->dev); + +if (acm->nb_index) + dr = (struct usb_cdc_notification *)acm-Quoted 1 line
notification_buffer;drivers/usb/class/cdc-acm.c +7
-0+ +/* size = notification-header + (optional) data */ + expected_size =sizeof (struct usb_cdc_notification ) + +le16_to_cpu (dr->wLength); + +if (current_size < expected_size) { +/* notification is transmitted fragmented, reassemble */
drivers/usb/class/cdc-acm.c +7
-0+if (acm->nb_size < expected_size) { +if (acm->nb_size) { +kfree (acm->notification_buffer); + acm->nb_size =0 ; + } + acm->notification_buffer = + kmalloc(expected_size,Quoted 1 line
GFP_ATOMIC);Given how kmalloc works you'd better round this up to a power of two.
Quoted 1 line
Quoted 4 lines
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 392
Quoted 2 lines
drivers/usb/class/cdc-acm.c +2
-0+if (!acm->notification_buffer) + goto exit;This is most subtle. Please add a comment that this prevents a double free if we get a
disconnect()
I'm unsure if I got this right: Are you talking about the fact, that the use of 'kmalloc' will make 'acm->notification_buffer' valid again (i.e. NULL or pointing to a correctly allocated block), after it was "invalidated" by 'kfree' (in case the previously allocated buffer was too small)?
The 'goto exit'-thing for me is just not to use the buffer if allocation fails. Or am I missing anything here?
Quoted 7 lines
Review
Quoted 4 lines
(quoted diff) +2
-0+ acm->nb_size = expected_size; + }Regards
Oliver
Oliver Neukum
maintainerRe: [PATCH v2 1/4] cdc-acm: reassemble fragmented notifications
Am Freitag, den 24.03.2017, 22:50 +0100 schrieb Tobias Herzog:
Quoted 104 lines
Hi Oliver,
thank you for your patience... :) I have a question to one of your comments (see below).
Best regards,
TobiasQuoted 96 lines
Am Samstag, den 18.03.2017, 19:52 +0100 schrieb Tobias Herzog:
Quoted 5 lines
USB devices may have very limitited endpoint packet sizes, so that notifications can not be transferred within one single usb packet. Reassembling of multiple packages may be necessary.
Hi,
almost perfect.
A few new issue. Comments inline.drivers/usb/class/cdc-acm.c static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL); · line 290
Quoted 6 lines
(quoted diff) +1
-0+ struct usb_cdc_notification *dr = (struct
usb_cdc_notification*)buf;drivers/usb/class/cdc-acm.c +1
-0+unsigned char *data = (unsigned char *)(dr +1 );This is border line incorrect. It depends on the compiler not adding padding. Please make it
buf +
sizeof(struct usb_cdc_notification)Quoted 68 lines
drivers/usb/class/cdc-acm.c +0
-3-usb_mark_last_busy (acm->dev); - - data = (unsigned char *)(dr +1 );switch (
dr->bNotificationType) {
caseUSB_CDC_NOTIFY_NETWORK_CONNECTION:
dev_dbg(&acm->control->dev,drivers/usb/class/cdc-acm.c +4
-0@@ -363,8 +337,77 @@ static void acm_ctrl_irq(struct urb *urb) __func__, dr->bNotificationType, dr->wIndex, dr->wLength, data[0], data[1]); + } +} + +/* control interface reports status changes with "interrupt" transfers */
drivers/usb/class/cdc-acm.c +12
-0+static void acm_ctrl_irq (struct urb *urb) +{ +struct acm *acm = urb->context; +struct usb_cdc_notification *dr = urb->transfer_buffer; +unsigned int current_size = urb->actual_length; +unsigned int expected_size, copy_size; +int retval; +int status = urb->status; + +switch (status) { +case 0 : +/* success */ break;
drivers/usb/class/cdc-acm.c +7
-0+case -ECONNRESET : +case -ENOENT : +case -ESHUTDOWN : +/* this urb is terminated, clean up */ + acm->nb_index =0 ; + dev_dbg(&acm->control->dev, + "%s - urb shutting down with status:%d\n",
drivers/usb/class/cdc-acm.c +7
-0+ __func__, status); +return ; +default : +dev_dbg (&acm->control->dev, +"%s - nonzero urb status received: %d\n" , + __func__, status); + goto exit;}
drivers/usb/class/cdc-acm.c +5
-0+ +usb_mark_last_busy (acm->dev); + +if (acm->nb_index) + dr = (struct usb_cdc_notification *)acm-Quoted 2 lines
notification_buffer;drivers/usb/class/cdc-acm.c +7
-0+ +/* size = notification-header + (optional) data */ + expected_size =sizeof (struct usb_cdc_notification ) + +le16_to_cpu (dr->wLength); + +if (current_size < expected_size) { +/* notification is transmitted fragmented, reassemble */
drivers/usb/class/cdc-acm.c +7
-0+if (acm->nb_size < expected_size) { +if (acm->nb_size) { +kfree (acm->notification_buffer); + acm->nb_size =0 ; + } + acm->notification_buffer = + kmalloc(expected_size,Quoted 1 line
GFP_ATOMIC);Given how kmalloc works you'd better round this up to a power of two.
Quoted 2 lines
Quoted 7 lines
Quoted 4 lines
drivers/usb/class/cdc-acm.c static void acm_ctrl_irq(struct urb *urb) · line 392
Quoted 2 lines
drivers/usb/class/cdc-acm.c +2
-0+if (!acm->notification_buffer) + goto exit;This is most subtle. Please add a comment that this prevents a double free if we get a
disconnect()I'm unsure if I got this right: Are you talking about the fact, that the use of 'kmalloc' will make '
acm->notification_buffer' valid again (i.e. NULL or pointing to a correctly allocated block), after it was
yes
Quoted 4 lines
"invalidated" by 'kfree' (in case the previously allocated buffer was too small)?
The 'goto exit'-thing for me is just not to use the buffer if allocation fails. Or am I missing anything here?
You need to consider the hot unplug case.
Regards
Oliver
PATCH v2 2/4 cdc-acm: fix possible invalid access when processing notification
In reply to Tobias HerzogNotifications may only be 8 bytes so long. Accessing the 9th and 10th byte of unimplemented/unknown notifications may be insecure. Also check the length of known notifications before accessing anything behind the 8th byte.
--- drivers/usb/class/cdc-acm.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/usb/class/cdc-acm.c +8 -3
@@ -296,6 +296,12 @@ static void acm_process_notification(struct acm *acm, unsigned char *buf)break ;case USB_CDC_NOTIFY_SERIAL_STATE : +if (le16_to_cpu (dr->wLength) !=2 ) { +dev_dbg (&acm->control->dev, +"%s - malformed serial state\n" , __func__); +break ; + } + newctrl =get_unaligned_le16 (data);if (!acm->clocal && (acm->ctrlin & ~newctrl &ACM_CTRL_DCD )) {
@@ -332,11 +338,10 @@ static void acm_process_notification(struct acm *acm, unsigned char *buf)default :dev_dbg (&acm->control->dev, -"%s - unknown notification %d received: index %d " -"len %d data0 %d data1 %d\n" , +"%s - unknown notification %d received: index %d len %d\n" , __func__, dr->bNotificationType, dr->wIndex, - dr->wLength, data[0 ], data[1 ]); + dr->wLength); } }
Signature · 1 line
2.1.4
Re: [PATCH v2 2/4] cdc-acm: fix possible invalid access when processing notification
Quoted 1 line · Tobias Herzog
Notifications may only be 8 bytes so long. Accessing the 9th and
s/so//.
"So long and thanks for all the fish!" :-)
Quoted 5 lines
10th byte of unimplemented/unknown notifications may be insecure. Also check the length of known notifications before accessing anything behind the 8th byte.
Signed-off-by Tobias Herzog <[email protected]>
[...]
MBR, Sergei
Oliver Neukum
maintainerRe: [PATCH v2 2/4] cdc-acm: fix possible invalid access when processing notification
In reply to Tobias HerzogAm Samstag, den 18.03.2017, 19:52 +0100 schrieb Tobias Herzog:
Quoted 5 lines
Notifications may only be 8 bytes so long. Accessing the 9th and 10th byte of unimplemented/unknown notifications may be insecure. Also check the length of known notifications before accessing anything behind the 8th byte.
This is fixing a potential security issue. Please make it first in the series and CC it to [email protected]
Regards
Oliver
PATCH v2 3/4 cdc-acm: log message for serial state notification
In reply to Tobias HerzogAdds a similar log message to USB_CDC_NOTIFY_SERIAL_STATE as it is already done with USB_CDC_NOTIFY_NETWORK_CONNECTION.
--- drivers/usb/class/cdc-acm.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/usb/class/cdc-acm.c +2 -0
@@ -303,6 +303,8 @@ static void acm_process_notification(struct acm *acm, unsigned char *buf) } newctrl =get_unaligned_le16 (data); +dev_dbg (&acm->control->dev, +"%s - serial state: 0x%x\n" , __func__, newctrl);if (!acm->clocal && (acm->ctrlin & ~newctrl &ACM_CTRL_DCD )) { dev_dbg(&acm->control->dev,
Signature · 1 line
2.1.4
PATCH v2 4/4 cdc-acm: remove unused element of struct acm
In reply to Tobias Herzogwrite_used was introduced with commit 884b600f63dc ("[PATCH] USB: fix acm trouble with terminals") but never used since.
--- drivers/usb/class/cdc-acm.h | 1 - 1 file changed, 1 deletion(-)
drivers/usb/class/cdc-acm.h +0 -1
@@ -101,7 +101,6 @@ struct acm {u8 *notification_buffer;/* to reassemble fragmented notifications */ unsigned int nb_index;unsigned int nb_size; -int write_used;/* number of non-empty write buffers */ int transmitting;spinlock_t write_lock;struct mutex mutex;
Signature · 1 line
2.1.4
v3 cover cdc-acm: reassemble fragmented notifications
In reply to a message not in the archiveUSB devices may have very limitited endpoint packet sizes, so that notifications can not be transferred within one single usb packet. This patchset adds the ability to reassemble notifications that are transmitted fragmented.
v3:
* reordering patches (security issues first)
* fixed possible alignment bug
* allocate buffer with size=2^x
* additional code comments + fixed typos in commit messages
v2:
* reuse an allocated buffer for further notifications
* fixed issues with endianess
* check buffer allocation (kmalloc)
* don't use hard coded size of notification-header
* fixed typo + code structure (unneeded goto)
Tobias Herzog (4):
cdc-acm: fix possible invalid access when processing notification
cdc-acm: reassemble fragmented notifications
cdc-acm: log message for serial state notification
cdc-acm: remove unused element of struct acm
drivers/usb/class/cdc-acm.c | 127 ++++++++++++++++++++++++++++++++------------ drivers/usb/class/cdc-acm.h | 4 +- 2 files changed, 97 insertions(+), 34 deletions(-)
Signature · 1 line
2.1.4
PATCH v3 1/4 cdc-acm: fix possible invalid access when processing notification
Notifications may only be 8 bytes long. Accessing the 9th and 10th byte of unimplemented/unknown notifications may be insecure. Also check the length of known notifications before accessing anything behind the 8th byte.
--- drivers/usb/class/cdc-acm.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/usb/class/cdc-acm.c +9 -4
@@ -322,6 +322,12 @@ static void acm_ctrl_irq(struct urb *urb)break ;case USB_CDC_NOTIFY_SERIAL_STATE : +if (le16_to_cpu (dr->wLength) !=2 ) { +dev_dbg (&acm->control->dev, +"%s - malformed serial state\n" , __func__); +break ; + } + newctrl =get_unaligned_le16 (data);if (!acm->clocal && (acm->ctrlin & ~newctrl &ACM_CTRL_DCD )) {
@@ -358,11 +364,10 @@ static void acm_ctrl_irq(struct urb *urb)default :dev_dbg (&acm->control->dev, -"%s - unknown notification %d received: index %d " -"len %d data0 %d data1 %d\n" , +"%s - unknown notification %d received: index %d len %d\n" , __func__, - dr->bNotificationType, dr->wIndex, - dr->wLength, data[0 ], data[1 ]); + dr->bNotificationType, dr->wIndex, dr->wLength); +break ; } exit:
Signature · 1 line
2.1.4
Re: [PATCH v3 1/4] cdc-acm: fix possible invalid access when processing notification
Am Donnerstag, den 30.03.2017, 22:15 +0200 schrieb Tobias Herzog:
Quoted 6 lines
Notifications may only be 8 bytes long. Accessing the 9th and 10th byte of unimplemented/unknown notifications may be insecure. Also check the length of known notifications before accessing anything behind the 8th byte.
Signed-off-by Tobias Herzog <[email protected]>
PATCH v3 2/4 cdc-acm: reassemble fragmented notifications
In reply to Tobias HerzogUSB devices may have very limited endpoint packet sizes, so that notifications can not be transferred within one single usb packet. Reassembling of multiple packages may be necessary.
--- drivers/usb/class/cdc-acm.c | 112 ++++++++++++++++++++++++++++++++------------ drivers/usb/class/cdc-acm.h | 3 ++ 2 files changed, 86 insertions(+), 29 deletions(-)
drivers/usb/class/cdc-acm.c +83 -29
@@ -35,6 +35,7 @@#include <linux/errno.h> #include <linux/init.h> #include <linux/slab.h> +#include <linux/log2.h> #include <linux/tty.h> #include <linux/serial.h> #include <linux/tty_driver.h>
@@ -282,39 +283,13 @@ static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);* Interrupt handlers for various ACM device responses */ -/* control interface reports status changes with "interrupt" transfers */ -static void acm_ctrl_irq (struct urb *urb) +static void acm_process_notification (struct acm *acm,unsigned char *buf) { -struct acm *acm = urb->context; -struct usb_cdc_notification *dr = urb->transfer_buffer; -unsigned char *data;int newctrl;int difference; -int retval; -int status = urb->status; - -switch (status) { -case 0 : -/* success */ -break ; -case -ECONNRESET : -case -ENOENT : -case -ESHUTDOWN : -/* this urb is terminated, clean up */ -dev_dbg (&acm->control->dev, -"%s - urb shutting down with status: %d\n" , - __func__, status); -return ; -default : -dev_dbg (&acm->control->dev, -"%s - nonzero urb status received: %d\n" , - __func__, status); - goto exit; - } +struct usb_cdc_notification *dr = (struct usb_cdc_notification *)buf; +unsigned char *data = buf +sizeof (struct usb_cdc_notification ); -usb_mark_last_busy (acm->dev); - - data = (unsigned char *)(dr +1 );switch (dr->bNotificationType) {case USB_CDC_NOTIFY_NETWORK_CONNECTION : dev_dbg(&acm->control->dev,
@@ -367,9 +342,83 @@ static void acm_ctrl_irq(struct urb *urb)"%s - unknown notification %d received: index %d len %d\n" , __func__, dr->bNotificationType, dr->wIndex, dr->wLength); + } +} +/* control interface reports status changes with "interrupt" transfers */ +static void acm_ctrl_irq (struct urb *urb) +{ +struct acm *acm = urb->context; +struct usb_cdc_notification *dr = urb->transfer_buffer; +unsigned int current_size = urb->actual_length; +unsigned int expected_size, copy_size, alloc_size; +int retval; +int status = urb->status; + +switch (status) { +case 0 : +/* success */ break ; +case -ECONNRESET : +case -ENOENT : +case -ESHUTDOWN : +/* this urb is terminated, clean up */ + acm->nb_index =0 ; +dev_dbg (&acm->control->dev, +"%s - urb shutting down with status: %d\n" , + __func__, status); +return ; +default : +dev_dbg (&acm->control->dev, +"%s - nonzero urb status received: %d\n" , + __func__, status); + goto exit; + } + +usb_mark_last_busy (acm->dev); + +if (acm->nb_index) + dr = (struct usb_cdc_notification *)acm->notification_buffer; + +/* size = notification-header + (optional) data */ + expected_size =sizeof (struct usb_cdc_notification ) + +le16_to_cpu (dr->wLength); + +if (current_size < expected_size) { +/* notification is transmitted fragmented, reassemble */ +if (acm->nb_size < expected_size) { +if (acm->nb_size) { +kfree (acm->notification_buffer); + acm->nb_size =0 ; + } + alloc_size =roundup_pow_of_two (expected_size); +/* +* kmalloc ensures a valid notification_buffer after a +* use of kfree in case the previous allocation was too +* small. Final freeing is done on disconnect. +*/ + acm->notification_buffer = +kmalloc (alloc_size,GFP_ATOMIC ); +if (!acm->notification_buffer) + goto exit; + acm->nb_size = alloc_size; + } + + copy_size =min (current_size, + expected_size - acm->nb_index); + +memcpy (&acm->notification_buffer[acm->nb_index], + urb->transfer_buffer, copy_size); + acm->nb_index += copy_size; + current_size = acm->nb_index; } + +if (current_size >= expected_size) { +/* notification complete */ +acm_process_notification (acm, (unsigned char *)dr); + acm->nb_index =0 ; + } + exit: retval =usb_submit_urb (urb,GFP_ATOMIC );if (retval && retval != -EPERM )
@@ -1493,6 +1542,9 @@ static int acm_probe(struct usb_interface *intf, epctrl->bInterval ? epctrl->bInterval : 16); acm->ctrlurb->transfer_flags |=URB_NO_TRANSFER_DMA_MAP ; acm->ctrlurb->transfer_dma = acm->ctrl_dma; + acm->notification_buffer =NULL ; + acm->nb_index =0 ; + acm->nb_size =0 ;dev_info (&intf->dev,"ttyACM%d: USB ACM device\n" , minor);
@@ -1585,6 +1637,8 @@ static void acm_disconnect(struct usb_interface *intf)usb_free_coherent (acm->dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);acm_read_buffers_free (acm); +kfree (acm->notification_buffer); +if (!acm->combined_interfaces)usb_driver_release_interface (&acm_driver, intf == acm->control ? acm->data : acm->control);
drivers/usb/class/cdc-acm.h +3 -0
@@ -98,6 +98,9 @@ struct acm {struct acm_wb *putbuffer;/* for acm_tty_put_char() */ int rx_buflimit;spinlock_t read_lock; +u8 *notification_buffer;/* to reassemble fragmented notifications */ +unsigned int nb_index; +unsigned int nb_size;int write_used;/* number of non-empty write buffers */ int transmitting;spinlock_t write_lock;
Signature · 1 line
2.1.4
Re: [PATCH v3 2/4] cdc-acm: reassemble fragmented notifications
Am Donnerstag, den 30.03.2017, 22:15 +0200 schrieb Tobias Herzog:
Quoted 5 lines
USB devices may have very limited endpoint packet sizes, so that notifications can not be transferred within one single usb packet. Reassembling of multiple packages may be necessary.
Signed-off-by Tobias Herzog <[email protected]>
PATCH v3 3/4 cdc-acm: log message for serial state notification
In reply to Tobias HerzogAdds a similar log message to USB_CDC_NOTIFY_SERIAL_STATE as it is already done with USB_CDC_NOTIFY_NETWORK_CONNECTION.
--- drivers/usb/class/cdc-acm.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/usb/class/cdc-acm.c +2 -0
@@ -304,6 +304,8 @@ static void acm_process_notification(struct acm *acm, unsigned char *buf) } newctrl =get_unaligned_le16 (data); +dev_dbg (&acm->control->dev, +"%s - serial state: 0x%x\n" , __func__, newctrl);if (!acm->clocal && (acm->ctrlin & ~newctrl &ACM_CTRL_DCD )) { dev_dbg(&acm->control->dev,
Signature · 1 line
2.1.4
Re: [PATCH v3 3/4] cdc-acm: log message for serial state notification
Am Donnerstag, den 30.03.2017, 22:15 +0200 schrieb Tobias Herzog:
Quoted 4 lines
Adds a similar log message to
USB_CDC_NOTIFY_SERIAL_STATEas it is already done withUSB_CDC_NOTIFY_NETWORK_CONNECTION.Signed-off-by Tobias Herzog <[email protected]>
PATCH v3 4/4 cdc-acm: remove unused element of struct acm
In reply to Tobias Herzogwrite_used was introduced with commit 884b600f63dc ("[PATCH] USB: fix acm trouble with terminals") but never used since.
--- drivers/usb/class/cdc-acm.h | 1 - 1 file changed, 1 deletion(-)
drivers/usb/class/cdc-acm.h +0 -1
@@ -101,7 +101,6 @@ struct acm {u8 *notification_buffer;/* to reassemble fragmented notifications */ unsigned int nb_index;unsigned int nb_size; -int write_used;/* number of non-empty write buffers */ int transmitting;spinlock_t write_lock;struct mutex mutex;
Signature · 1 line
2.1.4
Am Donnerstag, den 30.03.2017, 22:15 +0200 schrieb Tobias Herzog:
Quoted 4 lines
write_usedwas introduced with commit884b600f63dc("[PATCH] USB: fix acm trouble with terminals") but never used since.Signed-off-by Tobias Herzog <[email protected]>