Input: rmi4 - refactor register descriptor parsing
Subsystems (3)
Dmitry Torokhov
maintainerPATCH 1/2 Input: rmi4 - refactor register descriptor parsing
Factor out parsing a register descriptor item from rmi_read_register_desc() and ensure there are no out-of-bounds accesses.
Use get_unaligned_le16() and get_unaligned_le32() for reading multi-byte values.
--- drivers/input/rmi4/rmi_driver.c | 119 ++++++++++++++++++++------------ 1 file changed, 73 insertions(+), 46 deletions(-)
drivers/input/rmi4/rmi_driver.c +73 -46
@@ -22,6 +22,7 @@#include <uapi/linux/input.h> #include <linux/rmi.h> #include <linux/export.h> +#include <linux/unaligned.h> #include "rmi_bus.h" #include "rmi_driver.h"
@@ -558,30 +559,74 @@ int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,return retval <0 ? retval :0 ; } +static int rmi_parse_register_desc_item (struct rmi_register_desc_item *item, +const u8 *buf,size_t size) +{ +int offset =0 ; +int map_offset =0 ; +int b; + +if (offset >= size) +return -EIO ; + + item->reg_size = buf[offset++]; +if (item->reg_size ==0 ) { +if (size - offset <2 ) +return -EIO ; + item->reg_size =get_unaligned_le16 (&buf[offset]); + offset +=2 ; + } + +if (item->reg_size ==0 ) { +if (size - offset <4 ) +return -EIO ; + item->reg_size =get_unaligned_le32 (&buf[offset]); + offset +=4 ; + } + +do { +if (offset >= size) +return -EIO ; + +for (b =0 ; b <7 ; b++) { +if (buf[offset] &BIT (b)) { +if (map_offset >=RMI_REG_DESC_SUBPACKET_BITS ) +return -EIO ; +bitmap_set (item->subpacket_map, map_offset,1 ); + } + ++map_offset; + } + }while (buf[offset++] &0x80 ); + + item->num_subpackets =bitmap_weight (item->subpacket_map, +RMI_REG_DESC_SUBPACKET_BITS ); + +return offset; +} +int rmi_read_register_desc (struct rmi_device *d,u16 addr, -struct rmi_register_descriptor *rdesc) +struct rmi_register_descriptor *rdesc) {int ret;u8 size_presence_reg;u8 buf[35 ]; -int presense_offset =1 ; -u8 *struct_buf; +int presence_offset;int reg;int offset =0 ; -int map_offset =0 ; +int map_offset;int i;int b;/* * The first register of the register descriptor is the size of -* the register descriptor's presense register. +* the register descriptor's presence register. */ ret =rmi_read (d, addr, &size_presence_reg);if (ret)return ret; ++addr; -if (size_presence_reg <0 || size_presence_reg >35 ) +if (size_presence_reg <1 || size_presence_reg >35 )return -EIO ;memset (buf,0 ,sizeof (buf));
@@ -597,16 +642,23 @@ int rmi_read_register_desc(struct rmi_device *d, u16 addr, ++addr;if (buf[0 ] ==0 ) { - presense_offset =3 ; - rdesc->struct_size = buf[1 ] | (buf[2 ] <<8 ); +if (size_presence_reg <3 ) +return -EIO ; + presence_offset =3 ; + rdesc->struct_size =get_unaligned_le16 (&buf[1 ]); }else { + presence_offset =1 ; rdesc->struct_size = buf[0 ]; } -for (i = presense_offset; i < size_presence_reg; i++) { + map_offset =0 ; +for (i = presence_offset; i < size_presence_reg; i++) {for (b =0 ; b <8 ; b++) { -if (buf[i] & (0x1 << b)) +if (buf[i] &BIT (b)) { +if (map_offset >=RMI_REG_DESC_PRESENSE_BITS ) +return -EIO ;bitmap_set (rdesc->presense_map, map_offset,1 ); + } ++map_offset; } }
@@ -626,7 +678,7 @@ int rmi_read_register_desc(struct rmi_device *d, u16 addr,* I'm not using devm_kzalloc here since it will not be retained * after exiting this function */ - struct_buf =kzalloc (rdesc->struct_size,GFP_KERNEL ); + u8 *struct_buf __free(kfree) = kzalloc(rdesc->struct_size, GFP_KERNEL);if (!struct_buf)return -ENOMEM ;
@@ -638,56 +690,31 @@ int rmi_read_register_desc(struct rmi_device *d, u16 addr,*/ ret =rmi_read_block (d, addr, struct_buf, rdesc->struct_size);if (ret) - goto free_struct_buff; +return ret; reg =find_first_bit (rdesc->presense_map,RMI_REG_DESC_PRESENSE_BITS );for (i =0 ; i < rdesc->num_registers; i++) {struct rmi_register_desc_item *item = &rdesc->registers[i]; -int reg_size = struct_buf[offset]; +int item_size; - ++offset; -if (reg_size ==0 ) { - reg_size = struct_buf[offset] | - (struct_buf[offset +1 ] <<8 ); - offset +=2 ; - } - -if (reg_size ==0 ) { - reg_size = struct_buf[offset] | - (struct_buf[offset +1 ] <<8 ) | - (struct_buf[offset +2 ] <<16 ) | - (struct_buf[offset +3 ] <<24 ); - offset +=4 ; - } + item_size =rmi_parse_register_desc_item (item, + &struct_buf[offset], + rdesc->struct_size - offset); +if (item_size <0 ) +return item_size; item->reg = reg; - item->reg_size = reg_size; - - map_offset =0 ; - -do { -for (b =0 ; b <7 ; b++) { -if (struct_buf[offset] & (0x1 << b)) -bitmap_set (item->subpacket_map, - map_offset,1 ); - ++map_offset; - } - }while (struct_buf[offset++] &0x80 ); - - item->num_subpackets =bitmap_weight (item->subpacket_map, -RMI_REG_DESC_SUBPACKET_BITS ); + offset += item_size;rmi_dbg (RMI_DEBUG_CORE , &d->dev,"%s: reg: %d reg size: %ld subpackets: %d\n" , __func__, item->reg, item->reg_size, item->num_subpackets); reg =find_next_bit (rdesc->presense_map, -RMI_REG_DESC_PRESENSE_BITS , reg +1 ); +RMI_REG_DESC_PRESENSE_BITS , reg +1 ); } -free_struct_buff: -kfree (struct_buf); -return ret; +return 0 ; } const struct rmi_register_desc_item *rmi_get_register_desc_item(
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Dmitry Torokhov
maintainerPATCH 2/2 Input: rmi4 - fix num_subpackets overflow in register descriptor
RMI_REG_DESC_SUBPACKET_BITS is defined as 296 (37 * BITS_PER_BYTE). This may overflow num_subpackets in struct rmi_register_desc_item which is defined as a u8.
Fix this by changing the type of num_subpackets to u16.
Pack the structure by rearranging the members to avoid holes, change reg_size from unsigned long to u32 to save space and ensure consistent size across 32-bit and 64-bit architectures, and use DECLARE_BITMAP() for subpacket_map.
--- drivers/input/rmi4/rmi_driver.h | 8 ++++---- drivers/input/rmi4/rmi_f12.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/input/rmi4/rmi_driver.h +4 -4
@@ -11,6 +11,7 @@#include <linux/hrtimer.h> #include <linux/ktime.h> #include <linux/input.h> +#include <linux/types.h> #include "rmi_bus.h" #define SYNAPTICS_INPUT_DEVICE_NAME "Synaptics RMI4 Touch Sensor"
@@ -52,10 +53,9 @@ struct pdt_entry {/* describes a single packet register */ struct rmi_register_desc_item {u16 reg; -unsigned long reg_size; -u8 num_subpackets; -unsigned long subpacket_map[BITS_TO_LONGS ( -RMI_REG_DESC_SUBPACKET_BITS )]; +u16 num_subpackets; +u32 reg_size; + DECLARE_BITMAP(subpacket_map, RMI_REG_DESC_SUBPACKET_BITS); };/*
drivers/input/rmi4/rmi_f12.c +1 -1
@@ -88,7 +88,7 @@ static int rmi_f12_read_sensor_tuning(struct f12_data *f12)if (item->reg_size >sizeof (buf)) {dev_err (&fn->dev, -"F12 control8 should be no bigger than %zd bytes, not: %ld\n" , +"F12 control8 should be no bigger than %zd bytes, not: %d\n" ,sizeof (buf), item->reg_size);return -ENODEV ; }
Signature · 1 line
2.54.0.545.g6539524ca2-goog