From c29be769f180d6bca9e995cff9ab7bbeea433949 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Thu, 30 Jan 2014 12:45:23 +0000 Subject: [PATCH] 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. --- lib/sysfs.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/sysfs.c b/lib/sysfs.c index 89ad4001..74d58d6e 100644 --- a/lib/sysfs.c +++ b/lib/sysfs.c @@ -1,7 +1,7 @@ /* sysfs.c - Part of libsensors, a library for reading Linux sensor data Copyright (c) 2005 Mark M. Hoffman - Copyright (C) 2007-2010 Jean Delvare + Copyright (C) 2007-2014 Jean Delvare 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(sensors_chip_features *chip, 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 >= 1024) { #ifdef DEBUG sensors_fatal_error(__func__, "Invalid channel number!");