Skip to content

Commit ab79aa3

Browse files
committed
Sanitize process name for GUI notification helper
As it turns out, the process name field /proc/PID/stat can contain arbitrary characters. This is a problem, because we call a notification helper, usually notify-send, using system(). Aggressively strip all non-alphanumeric characters to fix a shell code injection vulnerability. Users who do not use GUI notifications (-n or -N) are not affected.
1 parent c91dc9e commit ab79aa3

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

kill.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define BADNESS_AVOID -300
2020

2121
extern int enable_debug;
22+
void sanitize(char* s);
2223

2324
struct procinfo {
2425
int oom_score;
@@ -219,6 +220,8 @@ static void userspace_kill(DIR* procdir, int sig, int ignore_oom_score_adj,
219220
// that there is enough memory to spawn the notification helper.
220221
if (sig != 0) {
221222
char notif_args[PATH_MAX + 1000];
223+
// maybe_notify() calls system(). We must sanitize the strings we pass.
224+
sanitize(victim_name);
222225
snprintf(notif_args, sizeof(notif_args), "-i dialog-warning 'earlyoom' 'Killing process %d %s'", victim_pid, victim_name);
223226
maybe_notify(notif_command, notif_args);
224227
}

sanitize.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
/* sanitize replaces everything in string "s" that is not [a-zA-Z0-9]
4+
* with an underscore. The resulting string is safe to pass to a shell.
5+
*/
6+
void sanitize(char* s)
7+
{
8+
char c;
9+
for (int i = 0; s[i] != 0; i++) {
10+
c = s[i];
11+
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
12+
continue;
13+
}
14+
s[i] = '_';
15+
}
16+
}

0 commit comments

Comments
 (0)