HID: battery: don't do DMA from stack
[firefly-linux-kernel-4.4.55.git] / drivers / hid / hid-input.c
index 945b8158ec4c7f556d6c9db8753762ac958276c1..3fc4034a43678e0282b8e9c4bba42bd3648eeee6 100644 (file)
@@ -340,7 +340,7 @@ static int hidinput_get_battery_property(struct power_supply *psy,
 {
        struct hid_device *dev = container_of(psy, struct hid_device, battery);
        int ret = 0;
-       __u8 buf[2] = {};
+       __u8 *buf;
 
        switch (prop) {
        case POWER_SUPPLY_PROP_PRESENT:
@@ -349,21 +349,29 @@ static int hidinput_get_battery_property(struct power_supply *psy,
                break;
 
        case POWER_SUPPLY_PROP_CAPACITY:
+
+               buf = kmalloc(2 * sizeof(__u8), GFP_KERNEL);
+               if (!buf) {
+                       ret = -ENOMEM;
+                       break;
+               }
                ret = dev->hid_get_raw_report(dev, dev->battery_report_id,
-                                             buf, sizeof(buf),
+                                             buf, 2,
                                              dev->battery_report_type);
 
                if (ret != 2) {
-                       if (ret >= 0)
-                               ret = -EINVAL;
+                       ret = -ENODATA;
+                       kfree(buf);
                        break;
                }
+               ret = 0;
 
                if (dev->battery_min < dev->battery_max &&
                    buf[1] >= dev->battery_min &&
                    buf[1] <= dev->battery_max)
                        val->intval = (100 * (buf[1] - dev->battery_min)) /
                                (dev->battery_max - dev->battery_min);
+               kfree(buf);
                break;
 
        case POWER_SUPPLY_PROP_MODEL_NAME:
@@ -1042,9 +1050,14 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
 
        /*
         * Ignore out-of-range values as per HID specification,
-        * section 5.10 and 6.2.25
+        * section 5.10 and 6.2.25.
+        *
+        * The logical_minimum < logical_maximum check is done so that we
+        * don't unintentionally discard values sent by devices which
+        * don't specify logical min and max.
         */
        if ((field->flags & HID_MAIN_ITEM_VARIABLE) &&
+           (field->logical_minimum < field->logical_maximum) &&
            (value < field->logical_minimum ||
             value > field->logical_maximum)) {
                dbg_hid("Ignoring out-of-range value %x\n", value);