process-util: also filter non-printable characters in get_process_com()

We already do that in get_process_cmdline(), which is very similar in
behaviour otherwise. Hence, let's be safe and also filter them in
get_process_comm(). Let's try to retain as much information as we can
though and escape rather than suppress unprintable characters. Let's not
increase comm names beyond the kernel limit on such names however.

Also see discussion about this here:

https://marc.info/?l=linux-api&m=152649570404881&w=2
This commit is contained in:
Lennart Poettering
2018-06-01 21:53:13 +02:00
parent 61f6e27671
commit ce26882553
2 changed files with 48 additions and 4 deletions
+15 -4
View File
@@ -76,20 +76,31 @@ int get_process_state(pid_t pid) {
return (unsigned char) state;
}
int get_process_comm(pid_t pid, char **name) {
int get_process_comm(pid_t pid, char **ret) {
_cleanup_free_ char *escaped = NULL, *comm = NULL;
const char *p;
int r;
assert(name);
assert(ret);
assert(pid >= 0);
escaped = new(char, TASK_COMM_LEN);
if (!escaped)
return -ENOMEM;
p = procfs_file_alloca(pid, "comm");
r = read_one_line_file(p, name);
r = read_one_line_file(p, &comm);
if (r == -ENOENT)
return -ESRCH;
if (r < 0)
return r;
return r;
/* Escape unprintable characters, just in case, but don't grow the string beyond the underlying size */
cellescape(escaped, TASK_COMM_LEN, comm);
*ret = TAKE_PTR(escaped);
return 0;
}
int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {