max17042_battery: Fix a couple buffer overflows
authorDan Carpenter <dan.carpenter@oracle.com>
Thu, 15 Mar 2012 11:37:32 +0000 (14:37 +0300)
committerAnton Vorontsov <anton.vorontsov@linaro.org>
Sat, 5 May 2012 02:24:48 +0000 (19:24 -0700)
There are a couple issues here caused by confusion between sizeof()
and ARRAY_SIZE().  "table_size" should be the number of elements, but we
should allocate it with kcalloc() so that we allocate the correct number
of bytes.

In max17042_init_model() we don't allocate enough space so we go past
the end of the array in max17042_read_model_data() and
max17042_model_data_compare().

In max17042_verify_model_lock() we allocate the right amount of space
but we call max17042_read_model_data() with the wrong number of elements
and also in the for loop we go past the end of the array.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Acked-by: Dirk Brandewie <dirk.brandewie@gmail.com>
Signed-off-by: Anton Vorontsov <anton.vorontsov@linaro.org>
drivers/power/max17042_battery.c

index 04620c2cb388f3f5f2411f6a2b8191c5b36b459f..39dd610994acea729a65e60fbc0ca866498e7d78 100644 (file)
@@ -325,11 +325,10 @@ static inline int max17042_model_data_compare(struct max17042_chip *chip,
 static int max17042_init_model(struct max17042_chip *chip)
 {
        int ret;
-       int table_size =
-               sizeof(chip->pdata->config_data->cell_char_tbl)/sizeof(u16);
+       int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
        u16 *temp_data;
 
-       temp_data = kzalloc(table_size, GFP_KERNEL);
+       temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
        if (!temp_data)
                return -ENOMEM;
 
@@ -354,12 +353,11 @@ static int max17042_init_model(struct max17042_chip *chip)
 static int max17042_verify_model_lock(struct max17042_chip *chip)
 {
        int i;
-       int table_size =
-               sizeof(chip->pdata->config_data->cell_char_tbl);
+       int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
        u16 *temp_data;
        int ret = 0;
 
-       temp_data = kzalloc(table_size, GFP_KERNEL);
+       temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
        if (!temp_data)
                return -ENOMEM;