thermal/tmon: Fix crashes and compiler warnings
PATCH 1/1 thermal/tmon: Fix crashes and compiler warnings
The TMON tool has several issues that affect stability and build quality:
1. In sysfs.c: find_tzone_cdev() uses readlink(), which does not append a null terminator to the buffer. This leads to improper string termination (CWE-170). Fixed by manually adding '\0'.
2. In sysfs.c: get_instance_id() had a logic error where it could access an invalid pointer or pass NULL to atol() if strtok() returned NULL unexpectedly. Added checks and used a string copy to avoid modifying the original data during scanning.
3. In tmon.c: Fixed -Wstringop-truncation warnings by ensuring control device names are properly null-terminated after strncpy().
4. In tui.c: Fixed format string mismatches where 'unsigned long' variables were being printed as 'int' (%d), causing warnings.
--- tools/thermal/tmon/sysfs.c | 92 ++++++++++++++++++++++++-------------- tools/thermal/tmon/tmon.c | 4 +- tools/thermal/tmon/tui.c | 4 +- 3 files changed, 62 insertions(+), 38 deletions(-)
tools/thermal/tmon/sysfs.c +58 -34
@@ -150,13 +150,18 @@ static int get_instance_id(char *name, int pos, int skip)char *ch;int i =0 ; +/* strtok modifies the 'name' string by injecting NUL bytes */ ch =strtok (name,"_" );while (ch !=NULL ) { ++i; -syslog (LOG_INFO ,"%s:%s:%s:%d" , __func__, name, ch, i); +syslog (LOG_INFO ,"%s: token %d: %s" , __func__, i, ch); +if (pos == i) { +/* verify length before performing pointer arithmetic */ +if (strlen (ch) > (size_t )skip) +return atol (ch + skip); +return -1 ; + } ch =strtok (NULL ,"_" ); -if (pos == i) -return atol (ch + skip); }return -1 ;
@@ -168,6 +173,7 @@ static int find_tzone_tp(char *tz_name, char *d_name, struct tz_info *tzi, {int tp_id;unsigned long temp_ulong; +char name_copy[256 ];if (strstr (d_name,"trip_point" ) &&strstr (d_name,"temp" )) {
@@ -178,7 +184,12 @@ static int find_tzone_tp(char *tz_name, char *d_name, struct tz_info *tzi,if (temp_ulong <MAX_TEMP_KC ) { tzi->nr_trip_pts++;/* found a valid trip point */ - tp_id =get_instance_id (d_name,2 ,0 ); + +/* get_instance_id modifies string, use a copy */ +strncpy (name_copy, d_name,sizeof (name_copy) -1 ); + name_copy[sizeof (name_copy) -1 ] ='\0' ; + + tp_id =get_instance_id (name_copy,2 ,0 );syslog (LOG_DEBUG ,"tzone %s trip %d temp %lu tpnode %s" , tz_name, tp_id, temp_ulong, d_name);if (tp_id <0 || tp_id >=MAX_NR_TRIP ) {
@@ -203,6 +214,7 @@ static int find_tzone_cdev(struct dirent *nl, char *tz_name,char cdev_name[PATH_MAX ];char cdev_trip_name[PATH_MAX ];int cdev_id; +ssize_t len;if (nl->d_type ==DT_LNK ) { syslog(LOG_DEBUG, "TZ%d: cdev: %s cid %d\n", tz_id, nl->d_name,
@@ -217,33 +229,43 @@ static int find_tzone_cdev(struct dirent *nl, char *tz_name,snprintf (cdev_name,sizeof (cdev_name) -2 ,"%s/%s" , tz_name, nl->d_name);memset (cdev_name_linked,0 ,sizeof (cdev_name_linked)); -if (readlink (cdev_name, cdev_name_linked, -sizeof (cdev_name_linked) -1 ) !=-1 ) { - cdev_id =get_instance_id (cdev_name_linked,1 , + len =readlink (cdev_name, cdev_name_linked, +sizeof (cdev_name_linked) -1 ); +if (len !=-1 ) { +/* readlink does not append NUL, handle it manually (CWE-170) */ + cdev_name_linked[len] ='\0' ; + +/* get_instance_id modifies string, use a copy */ +char link_copy[256 ]; + +strncpy (link_copy, cdev_name_linked,sizeof (link_copy) -1 ); + link_copy[sizeof (link_copy) -1 ] ='\0' ; + + cdev_id =get_instance_id (link_copy,1 ,sizeof ("device" ) -1 );syslog (LOG_DEBUG ,"cdev %s linked to %s : %d\n" , cdev_name, cdev_name_linked, cdev_id); - tzi->cdev_binding |= (1 << cdev_id); - -/* find the trip point in which the cdev is binded to -* in this tzone -*/ -snprintf (cdev_trip_name,sizeof (cdev_trip_name) -1 , -"%s%s" , nl->d_name,"_trip_point" ); -sysfs_get_ulong (tz_name, cdev_trip_name, - &trip_instance); -/* validate trip point range, e.g. trip could return -1 -* when passive is enabled -*/ -if (trip_instance >MAX_NR_TRIP ) - trip_instance =0 ; - tzi->trip_binding[cdev_id] |=1 << trip_instance; -syslog (LOG_DEBUG ,"cdev %s -> trip:%lu: 0x%lx %d\n" , - cdev_name, trip_instance, - tzi->trip_binding[cdev_id], - cdev_id); - +if (cdev_id >=0 ) { + tzi->cdev_binding |= (1 << cdev_id); +/* find the trip point in which the cdev is binded to +* in this tzone +*/ +snprintf (cdev_trip_name,sizeof (cdev_trip_name) -1 , +"%s%s" , nl->d_name,"_trip_point" ); +sysfs_get_ulong (tz_name, cdev_trip_name, + &trip_instance); +/* validate trip point range, e.g. trip could return -1 +* when passive is enabled +*/ +if (trip_instance >MAX_NR_TRIP ) + trip_instance =0 ; + tzi->trip_binding[cdev_id] |=1 << trip_instance; +syslog (LOG_DEBUG ,"cdev %s -> trip:%lu: 0x%lx %d\n" , + cdev_name, trip_instance, + tzi->trip_binding[cdev_id], + cdev_id); + } }return 0 ; }
@@ -398,9 +420,14 @@ int probe_thermal_sysfs(void)/* detect number of thermal zones and cooling devices */ while (n--) {int inst; +char name_copy[256 ]; + +/* Use copy to prevent strtok corruption in get_instance_id */ +strncpy (name_copy, namelist[n]->d_name,sizeof (name_copy) -1 ); + name_copy[sizeof (name_copy) -1 ] ='\0' ;if (strstr (namelist[n]->d_name,CDEV )) { - inst =get_instance_id (namelist[n]->d_name,1 , + inst =get_instance_id (name_copy,1 ,sizeof ("device" ) -1 );/* keep track of the max cooling device since * there may be gaps.
@@ -414,7 +441,7 @@ int probe_thermal_sysfs(void) ptdata.max_cdev_instance); ptdata.nr_cooling_dev++; }else if (strstr (namelist[n]->d_name,TZONE )) { - inst =get_instance_id (namelist[n]->d_name,1 , + inst =get_instance_id (name_copy,1 ,sizeof ("zone" ) -1 );if (inst > ptdata.max_tz_instance) ptdata.max_tz_instance = inst;
@@ -476,10 +503,10 @@ int zone_instance_to_index(int zone_inst) }/* read temperature of all thermal zones */ -int update_thermal_data () +int update_thermal_data (void ) {int i; -int next_thermal_record = cur_thermal_record +1 ; +int next_thermal_record = (cur_thermal_record +1 ) %NR_THERMAL_RECORDS ;char tz_name[256 ];static unsigned long samples;
@@ -488,9 +515,6 @@ int update_thermal_data()return -1 ; } -/* circular buffer for keeping historic data */ -if (next_thermal_record >=NR_THERMAL_RECORDS ) - next_thermal_record =0 ;gettimeofday (&trec[next_thermal_record].tv,NULL );if (tmon_log) {fprintf (tmon_log,"%lu " , ++samples);
tools/thermal/tmon/tmon.c +2 -2
@@ -229,8 +229,8 @@ int main(int argc, char **argv)while ((c =getopt_long (argc, argv,"c:dlht:T:vgz:" , opts, &id2)) !=-1 ) {switch (c) {case 'c' : - no_control =0 ; -strncpy (ctrl_cdev, optarg,CDEV_NAME_SIZE ); +strncpy (ctrl_cdev, optarg,CDEV_NAME_SIZE -1 ); + ctrl_cdev[CDEV_NAME_SIZE -1 ] ='\0' ;break ;case 'd' :start_daemon_mode ();
tools/thermal/tmon/tui.c +2 -2
@@ -208,12 +208,12 @@ void show_cooling_device(void)mvwprintw (cooling_device_window,1 ,1 ,"ID Cooling Dev Cur Max Thermal Zone Binding" );wattroff (cooling_device_window,A_BOLD ); -for (j =0 ; j < ptdata.nr_cooling_dev; j++) { +for (j =0 ; j < ptdata.nr_cooling_dev; j++) {/* draw cooling device list on the left in the order of * cooling device instances. skip unused idr. */ mvwprintw(cooling_device_window, j + 2, 1, - "%02d %12.12s%6d %6d", + "%02d %12.12s%6lu %6lu", /* FIXED: %d -> %lu */ ptdata.cdi[j].instance, ptdata.cdi[j].type, ptdata.cdi[j].cur_state,
Signature · 1 line
2.52.0