Skip to content

Commit 6c58cf4

Browse files
ffainellirafaeljw
authored andcommitted
tools/thermal: Fix possible path truncations
A build with -D_FORTIFY_SOURCE=2 enabled will produce the following warnings: sysfs.c:63:30: warning: '%s' directive output may be truncated writing up to 255 bytes into a region of size between 0 and 255 [-Wformat-truncation=] snprintf(filepath, 256, "%s/%s", path, filename); ^~ Bump up the buffer to PATH_MAX which is the limit and account for all of the possible NUL and separators that could lead to exceeding the allocated buffer sizes. Fixes: 94f6996 ("tools/thermal: Introduce tmon, a tool for thermal subsystem") Signed-off-by: Florian Fainelli <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent c1dbe9a commit 6c58cf4

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

tools/thermal/tmon/sysfs.c

+13-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <stdint.h>
1414
#include <dirent.h>
1515
#include <libintl.h>
16+
#include <limits.h>
1617
#include <ctype.h>
1718
#include <time.h>
1819
#include <syslog.h>
@@ -33,9 +34,9 @@ int sysfs_set_ulong(char *path, char *filename, unsigned long val)
3334
{
3435
FILE *fd;
3536
int ret = -1;
36-
char filepath[256];
37+
char filepath[PATH_MAX + 2]; /* NUL and '/' */
3738

38-
snprintf(filepath, 256, "%s/%s", path, filename);
39+
snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
3940

4041
fd = fopen(filepath, "w");
4142
if (!fd) {
@@ -57,9 +58,9 @@ static int sysfs_get_ulong(char *path, char *filename, unsigned long *p_ulong)
5758
{
5859
FILE *fd;
5960
int ret = -1;
60-
char filepath[256];
61+
char filepath[PATH_MAX + 2]; /* NUL and '/' */
6162

62-
snprintf(filepath, 256, "%s/%s", path, filename);
63+
snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
6364

6465
fd = fopen(filepath, "r");
6566
if (!fd) {
@@ -76,9 +77,9 @@ static int sysfs_get_string(char *path, char *filename, char *str)
7677
{
7778
FILE *fd;
7879
int ret = -1;
79-
char filepath[256];
80+
char filepath[PATH_MAX + 2]; /* NUL and '/' */
8081

81-
snprintf(filepath, 256, "%s/%s", path, filename);
82+
snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
8283

8384
fd = fopen(filepath, "r");
8485
if (!fd) {
@@ -199,8 +200,8 @@ static int find_tzone_cdev(struct dirent *nl, char *tz_name,
199200
{
200201
unsigned long trip_instance = 0;
201202
char cdev_name_linked[256];
202-
char cdev_name[256];
203-
char cdev_trip_name[256];
203+
char cdev_name[PATH_MAX];
204+
char cdev_trip_name[PATH_MAX];
204205
int cdev_id;
205206

206207
if (nl->d_type == DT_LNK) {
@@ -213,7 +214,8 @@ static int find_tzone_cdev(struct dirent *nl, char *tz_name,
213214
return -EINVAL;
214215
}
215216
/* find the link to real cooling device record binding */
216-
snprintf(cdev_name, 256, "%s/%s", tz_name, nl->d_name);
217+
snprintf(cdev_name, sizeof(cdev_name) - 2, "%s/%s",
218+
tz_name, nl->d_name);
217219
memset(cdev_name_linked, 0, sizeof(cdev_name_linked));
218220
if (readlink(cdev_name, cdev_name_linked,
219221
sizeof(cdev_name_linked) - 1) != -1) {
@@ -226,8 +228,8 @@ static int find_tzone_cdev(struct dirent *nl, char *tz_name,
226228
/* find the trip point in which the cdev is binded to
227229
* in this tzone
228230
*/
229-
snprintf(cdev_trip_name, 256, "%s%s", nl->d_name,
230-
"_trip_point");
231+
snprintf(cdev_trip_name, sizeof(cdev_trip_name) - 1,
232+
"%s%s", nl->d_name, "_trip_point");
231233
sysfs_get_ulong(tz_name, cdev_trip_name,
232234
&trip_instance);
233235
/* validate trip point range, e.g. trip could return -1

0 commit comments

Comments
 (0)