seq_file_path() invocations and backslash escaping

From: Adam Osuchowski
Date: Sun Feb 25 2024 - 17:52:00 EST


Hi all,

seq_file_path() invocations in several places have not specified proper
characters set to escape. It leads to potentially ambiguous content in
/proc/<pid>/maps, /proc/<pid>/smaps /proc/<pid>/numa_maps, and also in
/proc/mdstat files. In corner cases contents of these files may be unparseable
in correct way and may give invalid results (non-existing paths).

The reason is that if any of sensitive characters (space, TAB, NL, etc.) are
escaped as octal using backslash character (ASCII 0x5C), the backslash itself
should be escaped as well.

Find attached a patch for this issue.

Additionally, paths in contents of /proc/<pid>/{,s}maps files may have
" (deleted)" string appended to them to indicate that path is non-existing.
Maybe one should consider escaping space in paths in this files to distinguish
non-existing path case from case when real path name ends with " (deleted)"
string. Of course, then space in this suffix must not be escaped, only path
part.

Regards,
Adam
--- a/fs/proc/task_mmu.c 2024-01-15 18:57:06.000000000 +0100
+++ b/fs/proc/task_mmu.c 2024-02-25 21:53:39.825517588 +0100
@@ -296,7 +296,7 @@
if (anon_name)
seq_printf(m, "[anon_shmem:%s]", anon_name->name);
else
- seq_file_path(m, file, "\n");
+ seq_file_path(m, file, "\n\\");
goto done;
}

@@ -1967,7 +1967,7 @@

if (file) {
seq_puts(m, " file=");
- seq_file_path(m, file, "\n\t= ");
+ seq_file_path(m, file, "\n\t= \\");
} else if (vma_is_initial_heap(vma)) {
seq_puts(m, " heap");
} else if (vma_is_initial_stack(vma)) {
--- a/drivers/md/md-bitmap.c 2024-01-15 18:57:06.000000000 +0100
+++ b/drivers/md/md-bitmap.c 2024-02-25 21:52:31.974675003 +0100
@@ -2115,7 +2115,7 @@
chunk_kb ? "KB" : "B");
if (bitmap->storage.file) {
seq_printf(seq, ", file: ");
- seq_file_path(seq, bitmap->storage.file, " \t\n");
+ seq_file_path(seq, bitmap->storage.file, " \t\n\\");
}

seq_printf(seq, "\n");