Skip to contentSkip to conversation

max17042_battery: fix a couple buffer overflows

PATCH Applied Dan Carpenter 3 participants 4 messages Mar 15, 2012 – May 5, 2012 Kernel janitors

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 max17042_battery: fix a couple buffer overflows

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.

---
I don't have this hardware. The original code is clearly buggy, but I can't test that my fix is correct. Please review carefully.

drivers/power/max17042_battery.c +4 -4
index e36763a..f8cd48c 100644
@@ -328,11 +328,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;
 
@@ -357,12 +356,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;
 
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Dan Carpenter

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().

Quoted 5 lines

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.

Quoted 35 lines

---
I don't have this hardware. The original code is clearly buggy, but I can't test that my fix is correct. Please review carefully.

drivers/power/max17042_battery.c +4 -4
index e36763a..f8cd48c 100644
@@ -328,11 +328,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;
 
@@ -357,12 +356,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;
 
Acked-by on v1 1/1
#2 lore raw
In reply to Dan Carpenter
Quoted 9 lines · Dan Carpenter

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().

Quoted 5 lines

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.

Typo in first ack :-(

Quoted 35 lines

---
I don't have this hardware. The original code is clearly buggy, but I can't test that my fix is correct. Please review carefully.

drivers/power/max17042_battery.c +4 -4
index e36763a..f8cd48c 100644
@@ -328,11 +328,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;
 
@@ -357,12 +356,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;
 
#3 lore raw
Quoted 10 lines · Dirk Brandewie
Quoted 9 lines · Dan Carpenter

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().

Quoted 8 lines
Quoted 5 lines

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.

Typo in first ack :-(

Applied, thanks! :-)

Signature · 2 lines
Anton Vorontsov
Email: [email protected]