Commit 09e241fb authored by Hisham Muhammad's avatar Hisham Muhammad
Browse files

Security review: check results of snprintf.

Calls marked with xSnprintf shouldn't fail.
Abort program cleanly if any of them does.
parent 3975e9ce
...@@ -56,7 +56,7 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short ...@@ -56,7 +56,7 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short
unsigned long int total = 0; unsigned long int total = 0;
for (unsigned int i = 0; i < nBatteries; i++) { for (unsigned int i = 0; i < nBatteries; i++) {
char infoPath[30]; char infoPath[30];
snprintf(infoPath, sizeof infoPath, "%s%s/%s", batteryPath, batteries[i], fileName); xSnprintf(infoPath, sizeof infoPath, "%s%s/%s", batteryPath, batteries[i], fileName);
FILE* file = fopen(infoPath, "r"); FILE* file = fopen(infoPath, "r");
if (!file) { if (!file) {
...@@ -106,7 +106,7 @@ static ACPresence procAcpiCheck() { ...@@ -106,7 +106,7 @@ static ACPresence procAcpiCheck() {
continue; continue;
char statePath[50]; char statePath[50];
snprintf((char *) statePath, sizeof statePath, "%s/%s/state", power_supplyPath, entryName); xSnprintf((char *) statePath, sizeof statePath, "%s/%s/state", power_supplyPath, entryName);
FILE* file = fopen(statePath, "r"); FILE* file = fopen(statePath, "r");
if (!file) { if (!file) {
...@@ -196,7 +196,7 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) { ...@@ -196,7 +196,7 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) {
if (entryName[0] == 'B' && entryName[1] == 'A' && entryName[2] == 'T') { if (entryName[0] == 'B' && entryName[1] == 'A' && entryName[2] == 'T') {
snprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/uevent", entryName); xSnprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/uevent", entryName);
int fd = open(filePath, O_RDONLY); int fd = open(filePath, O_RDONLY);
if (fd == -1) { if (fd == -1) {
closedir(dir); closedir(dir);
...@@ -249,7 +249,7 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) { ...@@ -249,7 +249,7 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) {
continue; continue;
} }
snprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/online", entryName); xSnprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/online", entryName);
int fd = open(filePath, O_RDONLY); int fd = open(filePath, O_RDONLY);
if (fd == -1) { if (fd == -1) {
closedir(dir); closedir(dir);
......
...@@ -27,7 +27,7 @@ Panel* IOPriorityPanel_new(IOPriority currPrio) { ...@@ -27,7 +27,7 @@ Panel* IOPriorityPanel_new(IOPriority currPrio) {
for (int c = 0; classes[c].name; c++) { for (int c = 0; classes[c].name; c++) {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
char name[50]; char name[50];
snprintf(name, sizeof(name)-1, "%s %d %s", classes[c].name, i, i == 0 ? "(High)" : (i == 7 ? "(Low)" : "")); xSnprintf(name, sizeof(name)-1, "%s %d %s", classes[c].name, i, i == 0 ? "(High)" : (i == 7 ? "(Low)" : ""));
IOPriority ioprio = IOPriority_tuple(classes[c].klass, i); IOPriority ioprio = IOPriority_tuple(classes[c].klass, i);
Panel_add(this, (Object*) ListItem_new(name, ioprio)); Panel_add(this, (Object*) ListItem_new(name, ioprio));
if (currPrio == ioprio) Panel_setSelected(this, Panel_size(this) - 1); if (currPrio == ioprio) Panel_setSelected(this, Panel_size(this) - 1);
......
...@@ -296,10 +296,10 @@ void LinuxProcess_writeField(Process* this, RichString* str, ProcessField field) ...@@ -296,10 +296,10 @@ void LinuxProcess_writeField(Process* this, RichString* str, ProcessField field)
switch ((int)field) { switch ((int)field) {
case TTY_NR: { case TTY_NR: {
if (lp->ttyDevice) { if (lp->ttyDevice) {
snprintf(buffer, n, "%-9s", lp->ttyDevice + 5 /* skip "/dev/" */); xSnprintf(buffer, n, "%-9s", lp->ttyDevice + 5 /* skip "/dev/" */);
} else { } else {
attr = CRT_colors[PROCESS_SHADOW]; attr = CRT_colors[PROCESS_SHADOW];
snprintf(buffer, n, "? "); xSnprintf(buffer, n, "? ");
} }
break; break;
} }
...@@ -332,31 +332,31 @@ void LinuxProcess_writeField(Process* this, RichString* str, ProcessField field) ...@@ -332,31 +332,31 @@ void LinuxProcess_writeField(Process* this, RichString* str, ProcessField field)
} }
#endif #endif
#ifdef HAVE_OPENVZ #ifdef HAVE_OPENVZ
case CTID: snprintf(buffer, n, "%7u ", lp->ctid); break; case CTID: xSnprintf(buffer, n, "%7u ", lp->ctid); break;
case VPID: snprintf(buffer, n, Process_pidFormat, lp->vpid); break; case VPID: xSnprintf(buffer, n, Process_pidFormat, lp->vpid); break;
#endif #endif
#ifdef HAVE_VSERVER #ifdef HAVE_VSERVER
case VXID: snprintf(buffer, n, "%5u ", lp->vxid); break; case VXID: xSnprintf(buffer, n, "%5u ", lp->vxid); break;
#endif #endif
#ifdef HAVE_CGROUP #ifdef HAVE_CGROUP
case CGROUP: snprintf(buffer, n, "%-10s ", lp->cgroup); break; case CGROUP: xSnprintf(buffer, n, "%-10s ", lp->cgroup); break;
#endif #endif
case OOM: snprintf(buffer, n, Process_pidFormat, lp->oom); break; case OOM: xSnprintf(buffer, n, Process_pidFormat, lp->oom); break;
case IO_PRIORITY: { case IO_PRIORITY: {
int klass = IOPriority_class(lp->ioPriority); int klass = IOPriority_class(lp->ioPriority);
if (klass == IOPRIO_CLASS_NONE) { if (klass == IOPRIO_CLASS_NONE) {
// see note [1] above // see note [1] above
snprintf(buffer, n, "B%1d ", (int) (this->nice + 20) / 5); xSnprintf(buffer, n, "B%1d ", (int) (this->nice + 20) / 5);
} else if (klass == IOPRIO_CLASS_BE) { } else if (klass == IOPRIO_CLASS_BE) {
snprintf(buffer, n, "B%1d ", IOPriority_data(lp->ioPriority)); xSnprintf(buffer, n, "B%1d ", IOPriority_data(lp->ioPriority));
} else if (klass == IOPRIO_CLASS_RT) { } else if (klass == IOPRIO_CLASS_RT) {
attr = CRT_colors[PROCESS_HIGH_PRIORITY]; attr = CRT_colors[PROCESS_HIGH_PRIORITY];
snprintf(buffer, n, "R%1d ", IOPriority_data(lp->ioPriority)); xSnprintf(buffer, n, "R%1d ", IOPriority_data(lp->ioPriority));
} else if (klass == IOPRIO_CLASS_IDLE) { } else if (klass == IOPRIO_CLASS_IDLE) {
attr = CRT_colors[PROCESS_LOW_PRIORITY]; attr = CRT_colors[PROCESS_LOW_PRIORITY];
snprintf(buffer, n, "id "); xSnprintf(buffer, n, "id ");
} else { } else {
snprintf(buffer, n, "?? "); xSnprintf(buffer, n, "?? ");
} }
break; break;
} }
......
...@@ -248,7 +248,7 @@ static inline unsigned long long LinuxProcess_adjustTime(unsigned long long t) { ...@@ -248,7 +248,7 @@ static inline unsigned long long LinuxProcess_adjustTime(unsigned long long t) {
static bool LinuxProcessList_readStatFile(Process *process, const char* dirname, const char* name, char* command, int* commLen) { static bool LinuxProcessList_readStatFile(Process *process, const char* dirname, const char* name, char* command, int* commLen) {
LinuxProcess* lp = (LinuxProcess*) process; LinuxProcess* lp = (LinuxProcess*) process;
char filename[MAX_NAME+1]; char filename[MAX_NAME+1];
snprintf(filename, MAX_NAME, "%s/%s/stat", dirname, name); xSnprintf(filename, MAX_NAME, "%s/%s/stat", dirname, name);
int fd = open(filename, O_RDONLY); int fd = open(filename, O_RDONLY);
if (fd == -1) if (fd == -1)
return false; return false;
...@@ -326,7 +326,7 @@ static bool LinuxProcessList_statProcessDir(Process* process, const char* dirnam ...@@ -326,7 +326,7 @@ static bool LinuxProcessList_statProcessDir(Process* process, const char* dirnam
char filename[MAX_NAME+1]; char filename[MAX_NAME+1];
filename[MAX_NAME] = '\0'; filename[MAX_NAME] = '\0';
snprintf(filename, MAX_NAME, "%s/%s", dirname, name); xSnprintf(filename, MAX_NAME, "%s/%s", dirname, name);
struct stat sstat; struct stat sstat;
int statok = stat(filename, &sstat); int statok = stat(filename, &sstat);
if (statok == -1) if (statok == -1)
...@@ -348,7 +348,7 @@ static void LinuxProcessList_readIoFile(LinuxProcess* process, const char* dirna ...@@ -348,7 +348,7 @@ static void LinuxProcessList_readIoFile(LinuxProcess* process, const char* dirna
char filename[MAX_NAME+1]; char filename[MAX_NAME+1];
filename[MAX_NAME] = '\0'; filename[MAX_NAME] = '\0';
snprintf(filename, MAX_NAME, "%s/%s/io", dirname, name); xSnprintf(filename, MAX_NAME, "%s/%s/io", dirname, name);
int fd = open(filename, O_RDONLY); int fd = open(filename, O_RDONLY);
if (fd == -1) { if (fd == -1) {
process->io_rate_read_bps = -1; process->io_rate_read_bps = -1;
...@@ -417,7 +417,7 @@ static void LinuxProcessList_readIoFile(LinuxProcess* process, const char* dirna ...@@ -417,7 +417,7 @@ static void LinuxProcessList_readIoFile(LinuxProcess* process, const char* dirna
static bool LinuxProcessList_readStatmFile(LinuxProcess* process, const char* dirname, const char* name) { static bool LinuxProcessList_readStatmFile(LinuxProcess* process, const char* dirname, const char* name) {
char filename[MAX_NAME+1]; char filename[MAX_NAME+1];
snprintf(filename, MAX_NAME, "%s/%s/statm", dirname, name); xSnprintf(filename, MAX_NAME, "%s/%s/statm", dirname, name);
int fd = open(filename, O_RDONLY); int fd = open(filename, O_RDONLY);
if (fd == -1) if (fd == -1)
return false; return false;
...@@ -447,7 +447,7 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, const char* d ...@@ -447,7 +447,7 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, const char* d
return; return;
} }
char filename[MAX_NAME+1]; char filename[MAX_NAME+1];
snprintf(filename, MAX_NAME, "%s/%s/stat", dirname, name); xSnprintf(filename, MAX_NAME, "%s/%s/stat", dirname, name);
FILE* file = fopen(filename, "r"); FILE* file = fopen(filename, "r");
if (!file) if (!file)
return; return;
...@@ -470,7 +470,7 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, const char* d ...@@ -470,7 +470,7 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, const char* d
static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* dirname, const char* name) { static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* dirname, const char* name) {
char filename[MAX_NAME+1]; char filename[MAX_NAME+1];
snprintf(filename, MAX_NAME, "%s/%s/cgroup", dirname, name); xSnprintf(filename, MAX_NAME, "%s/%s/cgroup", dirname, name);
FILE* file = fopen(filename, "r"); FILE* file = fopen(filename, "r");
if (!file) { if (!file) {
process->cgroup = xStrdup(""); process->cgroup = xStrdup("");
...@@ -491,7 +491,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d ...@@ -491,7 +491,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d
at++; at++;
left--; left--;
} }
int wrote = snprintf(at, left, "%s", group); int wrote = xSnprintf(at, left, "%s", group);
left -= wrote; left -= wrote;
} }
fclose(file); fclose(file);
...@@ -505,7 +505,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d ...@@ -505,7 +505,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d
static void LinuxProcessList_readVServerData(LinuxProcess* process, const char* dirname, const char* name) { static void LinuxProcessList_readVServerData(LinuxProcess* process, const char* dirname, const char* name) {
char filename[MAX_NAME+1]; char filename[MAX_NAME+1];
snprintf(filename, MAX_NAME, "%s/%s/status", dirname, name); xSnprintf(filename, MAX_NAME, "%s/%s/status", dirname, name);
FILE* file = fopen(filename, "r"); FILE* file = fopen(filename, "r");
if (!file) if (!file)
return; return;
...@@ -536,7 +536,7 @@ static void LinuxProcessList_readVServerData(LinuxProcess* process, const char* ...@@ -536,7 +536,7 @@ static void LinuxProcessList_readVServerData(LinuxProcess* process, const char*
static void LinuxProcessList_readOomData(LinuxProcess* process, const char* dirname, const char* name) { static void LinuxProcessList_readOomData(LinuxProcess* process, const char* dirname, const char* name) {
char filename[MAX_NAME+1]; char filename[MAX_NAME+1];
snprintf(filename, MAX_NAME, "%s/%s/oom_score", dirname, name); xSnprintf(filename, MAX_NAME, "%s/%s/oom_score", dirname, name);
FILE* file = fopen(filename, "r"); FILE* file = fopen(filename, "r");
if (!file) { if (!file) {
return; return;
...@@ -567,7 +567,7 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, const char* dirna ...@@ -567,7 +567,7 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, const char* dirna
return true; return true;
char filename[MAX_NAME+1]; char filename[MAX_NAME+1];
snprintf(filename, MAX_NAME, "%s/%s/cmdline", dirname, name); xSnprintf(filename, MAX_NAME, "%s/%s/cmdline", dirname, name);
int fd = open(filename, O_RDONLY); int fd = open(filename, O_RDONLY);
if (fd == -1) if (fd == -1)
return false; return false;
...@@ -688,7 +688,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char* ...@@ -688,7 +688,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char*
LinuxProcess* lp = (LinuxProcess*) proc; LinuxProcess* lp = (LinuxProcess*) proc;
char subdirname[MAX_NAME+1]; char subdirname[MAX_NAME+1];
snprintf(subdirname, MAX_NAME, "%s/%s/task", dirname, name); xSnprintf(subdirname, MAX_NAME, "%s/%s/task", dirname, name);
LinuxProcessList_recurseProcTree(this, subdirname, proc, period, tv); LinuxProcessList_recurseProcTree(this, subdirname, proc, period, tv);
#ifdef HAVE_TASKSTATS #ifdef HAVE_TASKSTATS
......
...@@ -215,7 +215,7 @@ void Platform_setSwapValues(Meter* this) { ...@@ -215,7 +215,7 @@ void Platform_setSwapValues(Meter* this) {
char* Platform_getProcessEnv(pid_t pid) { char* Platform_getProcessEnv(pid_t pid) {
char procname[32+1]; char procname[32+1];
snprintf(procname, 32, "/proc/%d/environ", pid); xSnprintf(procname, 32, "/proc/%d/environ", pid);
FILE* fd = fopen(procname, "r"); FILE* fd = fopen(procname, "r");
char *env = NULL; char *env = NULL;
if (fd) { if (fd) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment