Skip to content

Commit fe6c9b4

Browse files
Jiri Kosinagregkh
authored andcommitted
HID: fix data access in implement()
commit 27ce405 upstream. implement() is setting bytes in LE data stream. In case the data is not aligned to 64bits, it reads past the allocated buffer. It doesn't really change any value there (it's properly bitmasked), but in case that this read past the boundary hits a page boundary, pagefault happens when accessing 64bits of 'x' in implement(), and kernel oopses. This happens much more often when numbered reports are in use, as the initial 8bit skip in the buffer makes the whole process work on values which are not aligned to 64bits. This problem dates back to attempts in 2005 and 2006 to make implement() and extract() as generic as possible, and even back then the problem was realized by Adam Kroperlin, but falsely assumed to be impossible to cause any harm: http://www.mail-archive.com/linux-usb-devel@lists.sourceforge.net/msg47690.html I have made several attempts at fixing it "on the spot" directly in implement(), but the results were horrible; the special casing for processing last 64bit chunk and switching to different math makes it unreadable mess. I therefore took a path to allocate a few bytes more which will never make it into final report, but are there as a cushion for all the 64bit math operations happening in implement() and extract(). All callers of hid_output_report() are converted at the same time to allocate the buffer by newly introduced hid_alloc_report_buf() helper. Bruno noticed that the whole raw_size test can be dropped as well, as hid_alloc_report_buf() makes sure that the buffer is always of a proper size. Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> Acked-by: Gustavo Padovan <gustavo.padovan@collabora.co.uk> Signed-off-by: Jiri Kosina <jkosina@suse.cz> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 3cd1fa8 commit fe6c9b4

6 files changed

Lines changed: 52 additions & 22 deletions

File tree

‎drivers/hid/hid-core.c‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,8 @@ static void hid_output_field(const struct hid_device *hid,
11881188
}
11891189

11901190
/*
1191-
* Create a report.
1191+
* Create a report. 'data' has to be allocated using
1192+
* hid_alloc_report_buf() so that it has proper size.
11921193
*/
11931194

11941195
void hid_output_report(struct hid_report *report, __u8 *data)
@@ -1204,6 +1205,22 @@ void hid_output_report(struct hid_report *report, __u8 *data)
12041205
}
12051206
EXPORT_SYMBOL_GPL(hid_output_report);
12061207

1208+
/*
1209+
* Allocator for buffer that is going to be passed to hid_output_report()
1210+
*/
1211+
u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
1212+
{
1213+
/*
1214+
* 7 extra bytes are necessary to achieve proper functionality
1215+
* of implement() working on 8 byte chunks
1216+
*/
1217+
1218+
int len = ((report->size - 1) >> 3) + 1 + (report->id > 0) + 7;
1219+
1220+
return kmalloc(len, flags);
1221+
}
1222+
EXPORT_SYMBOL_GPL(hid_alloc_report_buf);
1223+
12071224
/*
12081225
* Set a field value. The report this field belongs to has to be
12091226
* created and transferred to the device, to set this value in the

‎drivers/hid/hid-logitech-dj.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ static int logi_dj_ll_input_event(struct input_dev *dev, unsigned int type,
619619

620620
struct hid_field *field;
621621
struct hid_report *report;
622-
unsigned char data[8];
622+
unsigned char *data;
623623
int offset;
624624

625625
dbg_hid("%s: %s, type:%d | code:%d | value:%d\n",
@@ -635,6 +635,13 @@ static int logi_dj_ll_input_event(struct input_dev *dev, unsigned int type,
635635
return -1;
636636
}
637637
hid_set_field(field, offset, value);
638+
639+
data = hid_alloc_report_buf(field->report, GFP_KERNEL);
640+
if (!data) {
641+
dev_warn(&dev->dev, "failed to allocate report buf memory\n");
642+
return -1;
643+
}
644+
638645
hid_output_report(field->report, &data[0]);
639646

640647
output_report_enum = &dj_rcv_hiddev->report_enum[HID_OUTPUT_REPORT];
@@ -645,8 +652,9 @@ static int logi_dj_ll_input_event(struct input_dev *dev, unsigned int type,
645652

646653
hid_hw_request(dj_rcv_hiddev, report, HID_REQ_SET_REPORT);
647654

648-
return 0;
655+
kfree(data);
649656

657+
return 0;
650658
}
651659

652660
static int logi_dj_ll_start(struct hid_device *hid)

‎drivers/hid/hid-picolcd_debugfs.c‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ static void dump_buff_as_hex(char *dst, size_t dst_sz, const u8 *data,
394394
void picolcd_debug_out_report(struct picolcd_data *data,
395395
struct hid_device *hdev, struct hid_report *report)
396396
{
397-
u8 raw_data[70];
397+
u8 *raw_data;
398398
int raw_size = (report->size >> 3) + 1;
399399
char *buff;
400400
#define BUFF_SZ 256
@@ -407,20 +407,20 @@ void picolcd_debug_out_report(struct picolcd_data *data,
407407
if (!buff)
408408
return;
409409

410-
snprintf(buff, BUFF_SZ, "\nout report %d (size %d) = ",
411-
report->id, raw_size);
412-
hid_debug_event(hdev, buff);
413-
if (raw_size + 5 > sizeof(raw_data)) {
410+
raw_data = hid_alloc_report_buf(report, GFP_ATOMIC);
411+
if (!raw_data) {
414412
kfree(buff);
415-
hid_debug_event(hdev, " TOO BIG\n");
416413
return;
417-
} else {
418-
raw_data[0] = report->id;
419-
hid_output_report(report, raw_data);
420-
dump_buff_as_hex(buff, BUFF_SZ, raw_data, raw_size);
421-
hid_debug_event(hdev, buff);
422414
}
423415

416+
snprintf(buff, BUFF_SZ, "\nout report %d (size %d) = ",
417+
report->id, raw_size);
418+
hid_debug_event(hdev, buff);
419+
raw_data[0] = report->id;
420+
hid_output_report(report, raw_data);
421+
dump_buff_as_hex(buff, BUFF_SZ, raw_data, raw_size);
422+
hid_debug_event(hdev, buff);
423+
424424
switch (report->id) {
425425
case REPORT_LED_STATE:
426426
/* 1 data byte with GPO state */
@@ -644,6 +644,7 @@ void picolcd_debug_out_report(struct picolcd_data *data,
644644
break;
645645
}
646646
wake_up_interruptible(&hdev->debug_wait);
647+
kfree(raw_data);
647648
kfree(buff);
648649
}
649650

‎drivers/hid/usbhid/hid-core.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,6 @@ static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *re
535535
{
536536
int head;
537537
struct usbhid_device *usbhid = hid->driver_data;
538-
int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
539538

540539
if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
541540
return;
@@ -546,7 +545,7 @@ static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *re
546545
return;
547546
}
548547

549-
usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
548+
usbhid->out[usbhid->outhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
550549
if (!usbhid->out[usbhid->outhead].raw_report) {
551550
hid_warn(hid, "output queueing failed\n");
552551
return;
@@ -595,7 +594,7 @@ static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *re
595594
}
596595

597596
if (dir == USB_DIR_OUT) {
598-
usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
597+
usbhid->ctrl[usbhid->ctrlhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
599598
if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
600599
hid_warn(hid, "control queueing failed\n");
601600
return;

‎include/linux/hid.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ struct hid_field *hidinput_get_led_field(struct hid_device *hid);
746746
unsigned int hidinput_count_leds(struct hid_device *hid);
747747
__s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code);
748748
void hid_output_report(struct hid_report *report, __u8 *data);
749+
u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags);
749750
struct hid_device *hid_allocate_device(void);
750751
struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id);
751752
int hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size);

‎net/bluetooth/hidp/core.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,21 @@ static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb)
225225

226226
static int hidp_send_report(struct hidp_session *session, struct hid_report *report)
227227
{
228-
unsigned char buf[32], hdr;
229-
int rsize;
228+
unsigned char hdr;
229+
u8 *buf;
230+
int rsize, ret;
230231

231-
rsize = ((report->size - 1) >> 3) + 1 + (report->id > 0);
232-
if (rsize > sizeof(buf))
232+
buf = hid_alloc_report_buf(report, GFP_ATOMIC);
233+
if (!buf)
233234
return -EIO;
234235

235236
hid_output_report(report, buf);
236237
hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUPUT;
237238

238-
return hidp_send_intr_message(session, hdr, buf, rsize);
239+
ret = hidp_send_intr_message(session, hdr, buf, rsize);
240+
241+
kfree(buf);
242+
return ret;
239243
}
240244

241245
static int hidp_get_raw_report(struct hid_device *hid,

0 commit comments

Comments
 (0)