Skip to contentSkip to conversation

libsensors: Avoid insane memory allocations

RFC PATCH In review Jean Delvare 2 participants 3 messages Jan 29, 2014 Library code

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 libsensors: Avoid insane memory allocations

While there is no longer a hard limit to the number of sensor of a given type per chip, I feel a little uncomfortable having no limit at all on the amount of memory we may try to allocate. Add an arbitrary safety limit so that a design error or a bug in a hwmon driver can't result into an insane memory allocation.
---
Does anyone have an opinion on this? No limit at all makes me feel bad, but I don't like arbitrary limits either. So I'm really not sure what is the best option here.

 lib/sysfs.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
lib/sysfs.c +4 -3
@@ -1,7 +1,7 @@
 /*
     sysfs.c - Part of libsensors, a library for reading Linux sensor data
     Copyright (c) 2005 Mark M. Hoffman <[email protected]>
-    Copyright (C) 2007-2010 Jean Delvare <[email protected]>
+    Copyright (C) 2007-2014 Jean Delvare <[email protected]>
 
     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public
@@ -468,8 +468,9 @@ static int sensors_read_dynamic_chip(sen
 			break;
 		}
 
-		/* Skip invalid entries */
-		if (nr < 0) {
+		/* Skip invalid entries. The high limit is arbitrary, we just
+		   don't want to allocate an insane amount of memory. */
+		if (nr < 0 || nr >= 8192) {
 #ifdef DEBUG
 			sensors_fatal_error(__func__,
 					    "Invalid channel number!");
Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#1 lore raw
Quoted 25 lines · Jean Delvare

While there is no longer a hard limit to the number of sensor of a given type per chip, I feel a little uncomfortable having no limit at all on the amount of memory we may try to allocate. Add an arbitrary safety limit so that a design error or a bug in a hwmon driver can't result into an insane memory allocation.
---
Does anyone have an opinion on this? No limit at all makes me feel bad, but I don't like arbitrary limits either. So I'm really not sure what is the best option here.

 lib/sysfs.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
lib/sysfs.c +1 -1
@@ -1,7 +1,7 @@
 /*
     sysfs.c - Part of libsensors, a library for reading Linux sensor data
     Copyright (c) 2005 Mark M. Hoffman <[email protected]>
-    Copyright (C) 2007-2010 Jean Delvare <[email protected]>
+    Copyright (C) 2007-2014 Jean Delvare <[email protected]>
 
     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public

lib/sysfs.c static int sensors_read_dynamic_chip(sen · line 473

Quoted 8 lines
lib/sysfs.c +3 -2
@@ -468,8 +468,9 @@ static int sensors_read_dynamic_chip(sen
 			break;
 		}
 
-		/* Skip invalid entries */
-		if (nr < 0) {
+		/* Skip invalid entries. The high limit is arbitrary, we just
+		   don't want to allocate an insane amount of memory. */
+		if (nr < 0 || nr >= 8192) {

Hi Jean,

makes sense, and good idea.

Does that mean the limit is 8191 sensors per type ? May be a bit high. I'd be more restrictive and go with something like 1024.

Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#2 lore raw
In reply to Jean Delvare
Quoted 37 lines · Guenter Roeck
Quoted 25 lines · Jean Delvare

While there is no longer a hard limit to the number of sensor of a given type per chip, I feel a little uncomfortable having no limit at all on the amount of memory we may try to allocate. Add an arbitrary safety limit so that a design error or a bug in a hwmon driver can't result into an insane memory allocation.
---
Does anyone have an opinion on this? No limit at all makes me feel bad, but I don't like arbitrary limits either. So I'm really not sure what is the best option here.

 lib/sysfs.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
lib/sysfs.c +1 -1
@@ -1,7 +1,7 @@
 /*
     sysfs.c - Part of libsensors, a library for reading Linux sensor data
     Copyright (c) 2005 Mark M. Hoffman <[email protected]>
-    Copyright (C) 2007-2010 Jean Delvare <[email protected]>
+    Copyright (C) 2007-2014 Jean Delvare <[email protected]>
 
     This library is free software; you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public

lib/sysfs.c static int sensors_read_dynamic_chip(sen · line 473

Quoted 8 lines
lib/sysfs.c +3 -2
@@ -468,8 +468,9 @@ static int sensors_read_dynamic_chip(sen
 			break;
 		}
 
-		/* Skip invalid entries */
-		if (nr < 0) {
+		/* Skip invalid entries. The high limit is arbitrary, we just
+		   don't want to allocate an insane amount of memory. */
+		if (nr < 0 || nr >= 8192) {

Hi Jean,

Quoted 4 lines

makes sense, and good idea.

Does that mean the limit is 8191 sensors per type ? May be a bit high. I'd be more restrictive and go with something like 1024.

Fine with me. I don't really expect anyone to go anywhere close to that so it should be OK. And if we ever spot really huge values, we can always raise the limit then.

Thanks,

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors