Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

HID: ft260: improve error handling of ft260_hid_feature_report_get()

The ft260_hid_feature_report_get() checks if the return size matches the
requested size. But the function can also fail with at least -ENOMEM. Add the
< 0 checks.

In ft260_hid_feature_report_get(), do not do the memcpy to the caller's buffer
if there is an error.

Fixes: 6a82582d9fa4 ("HID: ft260: add usb hid to i2c host bridge driver")
Signed-off-by: Tom Rix <trix@redhat.com>
Signed-off-by: Michael Zaidman <michael.zaidman@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>

authored by

Michael Zaidman and committed by
Jiri Kosina
82f09a63 4fb12519

+12 -12
+12 -12
drivers/hid/hid-ft260.c
··· 249 249 250 250 ret = hid_hw_raw_request(hdev, report_id, buf, len, HID_FEATURE_REPORT, 251 251 HID_REQ_GET_REPORT); 252 - memcpy(data, buf, len); 252 + if (likely(ret == len)) 253 + memcpy(data, buf, len); 254 + else if (ret >= 0) 255 + ret = -EIO; 253 256 kfree(buf); 254 257 return ret; 255 258 } ··· 301 298 302 299 ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS, 303 300 (u8 *)&report, sizeof(report)); 304 - if (ret < 0) { 301 + if (unlikely(ret < 0)) { 305 302 hid_err(hdev, "failed to retrieve status: %d\n", ret); 306 303 return ret; 307 304 } ··· 727 724 728 725 ret = ft260_hid_feature_report_get(hdev, FT260_SYSTEM_SETTINGS, 729 726 (u8 *)cfg, len); 730 - if (ret != len) { 727 + if (ret < 0) { 731 728 hid_err(hdev, "failed to retrieve system status\n"); 732 - if (ret >= 0) 733 - return -EIO; 729 + return ret; 734 730 } 735 731 return 0; 736 732 } ··· 782 780 int ret; 783 781 784 782 ret = ft260_hid_feature_report_get(hdev, id, cfg, len); 785 - if (ret != len && ret >= 0) 786 - return -EIO; 783 + if (ret < 0) 784 + return ret; 787 785 788 786 return scnprintf(buf, PAGE_SIZE, "%hi\n", *field); 789 787 } ··· 794 792 int ret; 795 793 796 794 ret = ft260_hid_feature_report_get(hdev, id, cfg, len); 797 - if (ret != len && ret >= 0) 798 - return -EIO; 795 + if (ret < 0) 796 + return ret; 799 797 800 798 return scnprintf(buf, PAGE_SIZE, "%hi\n", le16_to_cpu(*field)); 801 799 } ··· 946 944 947 945 ret = ft260_hid_feature_report_get(hdev, FT260_CHIP_VERSION, 948 946 (u8 *)&version, sizeof(version)); 949 - if (ret != sizeof(version)) { 947 + if (ret < 0) { 950 948 hid_err(hdev, "failed to retrieve chip version\n"); 951 - if (ret >= 0) 952 - ret = -EIO; 953 949 goto err_hid_close; 954 950 } 955 951