libdasd: Use util_path_sysfs() to construct sysfs paths

Don't rely on rather arbitrary values for the string buffer size of
different sysfs paths. Instead use util_path_sysfs(), which allocates
the exact amount of memory necessary for a certain path string.

As these path strings are dynamically constructed, potential buffer
overflows can hereby be avoided and make the library functions more
robust.

Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Jan Höppner
2019-04-03 13:00:34 +02:00
parent 0efacac5b8
commit 8c06dea4e5
+24 -12
View File
@@ -13,6 +13,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "lib/dasd_sys.h" #include "lib/dasd_sys.h"
#include "lib/util_path.h"
/** /**
* Get raw-track access mode status * Get raw-track access mode status
@@ -32,21 +33,23 @@
int dasd_sys_raw_track_access(char *devnode) int dasd_sys_raw_track_access(char *devnode)
{ {
char busid[9]; char busid[9];
char path[47]; char *path;
FILE *fp; FILE *fp;
int rc; int rc;
if (u2s_getbusid(devnode, busid)) if (u2s_getbusid(devnode, busid))
return 0; return 0;
sprintf(path, "/sys/bus/ccw/devices/%s/raw_track_access", busid); path = util_path_sysfs("bus/ccw/devices/%s/raw_track_access", busid);
fp = fopen(path, "r"); fp = fopen(path, "r");
if (!fp) if (!fp) {
free(path);
return 0; return 0;
}
rc = fgetc(fp) - '0'; rc = fgetc(fp) - '0';
fclose(fp); fclose(fp);
free(path);
return (rc == 1) ? 1 : 0; return (rc == 1) ? 1 : 0;
} }
@@ -55,15 +58,17 @@ int dasd_sys_raw_track_access(char *devnode)
int dasd_get_pm_from_chpid(char *busid, unsigned int chpid, int *mask) int dasd_get_pm_from_chpid(char *busid, unsigned int chpid, int *mask)
{ {
unsigned int val; unsigned int val;
char path[40];
int count, i; int count, i;
char *path;
FILE *fp; FILE *fp;
sprintf(path, "/sys/bus/ccw/devices/%s/../chpids", busid); path = util_path_sysfs("bus/ccw/devices/%s/../chpids", busid);
*mask = 0; *mask = 0;
fp = fopen(path, "r"); fp = fopen(path, "r");
if (!fp) if (!fp) {
free(path);
return ENODEV; return ENODEV;
}
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
count = fscanf(fp, " %x", &val); count = fscanf(fp, " %x", &val);
@@ -75,6 +80,7 @@ int dasd_get_pm_from_chpid(char *busid, unsigned int chpid, int *mask)
*mask = 0x80 >> i; *mask = 0x80 >> i;
} }
fclose(fp); fclose(fp);
free(path);
return 0; return 0;
} }
@@ -102,22 +108,25 @@ int dasd_get_pm_from_chpid(char *busid, unsigned int chpid, int *mask)
int dasd_reset_chpid(char *devnode, char *chpid_char) int dasd_reset_chpid(char *devnode, char *chpid_char)
{ {
unsigned int chpid; unsigned int chpid;
char path[41];
char busid[9]; char busid[9];
int mask, rc; int mask, rc;
char *endptr; char *endptr;
char *path;
FILE *fp; FILE *fp;
if (u2s_getbusid(devnode, busid)) if (u2s_getbusid(devnode, busid))
return ENODEV; return ENODEV;
if (!chpid_char) { if (!chpid_char) {
sprintf(path, "/sys/bus/ccw/devices/%s/path_reset", busid); path = util_path_sysfs("bus/ccw/devices/%s/path_reset", busid);
fp = fopen(path, "w"); fp = fopen(path, "w");
if (!fp) if (!fp) {
free(path);
return ENODEV; return ENODEV;
}
fprintf(fp, "%s", "all\n"); fprintf(fp, "%s", "all\n");
fclose(fp); fclose(fp);
free(path);
return 0; return 0;
} }
@@ -132,12 +141,15 @@ int dasd_reset_chpid(char *devnode, char *chpid_char)
if (!mask) if (!mask)
return ENOENT; return ENOENT;
sprintf(path, "/sys/bus/ccw/devices/%s/path_reset", busid); path = util_path_sysfs("bus/ccw/devices/%s/path_reset", busid);
fp = fopen(path, "w"); fp = fopen(path, "w");
if (!fp) if (!fp) {
free(path);
return ENODEV; return ENODEV;
}
fprintf(fp, "%02x", mask); fprintf(fp, "%02x", mask);
fclose(fp); fclose(fp);
free(path);
return 0; return 0;
} }