Skip to contentSkip to conversation

ss: escape characters in command name

PATCH In review Stephen Hemminger 2 participants 3 messages Feb 4, 2025 – Feb 8

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ss: escape characters in command name

Since the process name is under user control with prctl(PR_SET_NAME) it may contain escape characters to try and mess with screen output.

Reuse the existing string logic from procps (used by ps command).

---
 include/utils.h |   2 +
 lib/Makefile    |   2 +-
 lib/escape.c    | 111 ++++++++++++++++++++++++++++++++++++++++++++++++
 misc/ss.c       |  10 ++---
 4 files changed, 119 insertions(+), 6 deletions(-)
 create mode 100644 lib/escape.c
include/utils.h +2 -0
index 9a81494d..e91c5f0e 100644
@@ -372,6 +372,8 @@ const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val);
 unsigned int get_str_char_count(const char *str, int match);
 int str_split_by_char(char *str, char **before, char **after, int match);
 
+int escape_str (char *dst, const char *src, int bufsize);
+
 #define INDENT_STR_MAXLEN 32
 
 struct indent_mem {
lib/Makefile +1 -1
index aa7bbd2e..c259722f 100644
@@ -5,7 +5,7 @@ CFLAGS += -fPIC
 
 UTILOBJ = utils.o utils_math.o rt_names.o ll_map.o ll_types.o ll_proto.o ll_addr.o \
 	inet_proto.o namespace.o json_writer.o json_print.o json_print_math.o \
-	names.o color.o bpf_legacy.o bpf_glue.o exec.o fs.o cg_map.o ppp_proto.o
+	names.o color.o bpf_legacy.o bpf_glue.o exec.o fs.o cg_map.o ppp_proto.o escape.o
 
 ifeq ($(HAVE_ELF),y)
 ifeq ($(HAVE_LIBBPF),y)
lib/escape.c new file +111 -0

111 lines not on this page · raw message

misc/ss.c +5 -5
index aef1a714..1d70242e 100644
@@ -550,8 +550,7 @@ static void user_ent_add(unsigned int ino, char *task,
 static void user_ent_hash_build_task(char *path, int pid, int tid)
 {
 	const char *no_ctx = "unavailable";
-	char task[16] = {'\0', };
-	char stat[MAX_PATH_LEN];
+	char task[20] = { };
 	int pos_id, pos_fd;
 	char *task_context;
 	struct dirent *d;
@@ -599,6 +598,8 @@ static void user_ent_hash_build_task(char *path, int pid, int tid)
 			sock_context = strdup(no_ctx);
 
 		if (task[0] == '\0') {
+			char stat[MAX_PATH_LEN];
+			char name[16];
 			FILE *fp;
 
 			strlcpy(stat, path, pos_id + 1);
@@ -606,9 +607,8 @@ static void user_ent_hash_build_task(char *path, int pid, int tid)
 
 			fp = fopen(stat, "r");
 			if (fp) {
-				if (fscanf(fp, "%*d (%[^)])", task) < 1) {
-					; /* ignore */
-				}
+				if (fscanf(fp, "%*d (%[^)])", name) == 1)
+					escape_str(task, name, sizeof(task));
 				fclose(fp);
 			}
 		}
Signature · 1 line
2.47.2
#1 lore raw

Dne Tue, Feb 04, 2025 at 08:24:16AM GMT, Stephen Hemminger napsal:

Quoted 7 lines

Since the process name is under user control with prctl(PR_SET_NAME) it may contain escape characters to try and mess with screen output.

Reuse the existing string logic from procps (used by ps command).

Hello,

I found this patch while checking if the problem of sanitizing process names in ss output has been already discussed. As far as I can see, it was not applied to git and there wasn't any discussion in the list either. Was it just forgotten or was it rejected for some reason?

Michal

#2 lore raw
In reply to Stephen Hemminger
Quoted 7 lines · Stephen Hemminger

Since the process name is under user control with prctl(PR_SET_NAME) it may contain escape characters to try and mess with screen output.

Reuse the existing string logic from procps (used by ps command).

Hello,

I found this patch while checking if this problem was already discussed in the list. As far as I can see, the patch has not been applied and there was no discussion either. Was it just forgotten or was it rejected for some reason?

Thank you,
Michal