Skip to contentSkip to conversation

linux-user: keep the name-ending parenthesis in /proc/self/stat

PATCH Brice Goglin 3 participants 5 messages Mar 30, 2020 – Apr 8, 2020

lore
Share & tools
lore
#0 lore raw

When the program name is very long, qemu-user may truncate it in /proc/self/stat. However the truncation must keep the ending ") " to conform to the proc manpage which says:
(2) comm %s
The filename of the executable, in parentheses. This is visible whether or not the executable is swapped out.

To reproduce:

$ ln -s /bin/cat <filenamewithmorethan128chars>
$ qemu-x86_64 ./<filenamewithmorethan128chars> /proc/self/stat

Before the patch, you get:
1134631 (<filenametruncated>0 0 0 0 0 0 0 0 ...
After the patch:
1134631 (<filenametruncat>) 0 0 0 0 0 0 0 0 ...

This fixes an issue with hwloc failing to parse /proc/self/stat when Ludovic Courtes was testing it in guix over qemu-aarch64.

linux-user/syscall.c +4 -1
index 5af55fca78..a1126dcf5b 100644
@@ -7305,7 +7305,10 @@ static int open_self_stat(void *cpu_env, int fd)
         snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
       } else if (i == 1) {
         /* app name */
-        snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        len = snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        if (len >= sizeof(buf))
+          /* bring back the ending ") " that was truncated */
+          strcpy(buf+sizeof(buf)-3, ") ");
       } else if (i == 27) {
         /* stack bottom */
         val = start_stack;
#1 lore raw
Quoted 27 lines · Brice Goglin

When the program name is very long, qemu-user may truncate it in /proc/self/stat. However the truncation must keep the ending ") " to conform to the proc manpage which says:
(2) comm %s
The filename of the executable, in parentheses. This is visible whether or not the executable is swapped out.

To reproduce:

$ ln -s /bin/cat <filenamewithmorethan128chars>
$ qemu-x86_64 ./<filenamewithmorethan128chars> /proc/self/stat

Before the patch, you get:
1134631 (<filenametruncated>0 0 0 0 0 0 0 0 ...
After the patch:
1134631 (<filenametruncat>) 0 0 0 0 0 0 0 0 ...

This fixes an issue with hwloc failing to parse /proc/self/stat when Ludovic Courtes was testing it in guix over qemu-aarch64.

linux-user/syscall.c +0 -0
index 5af55fca78..a1126dcf5b 100644

linux-user/syscall.c static int open_self_stat(void *cpu_env, int fd) · line 7311

Quoted 8 lines
linux-user/syscall.c +4 -1
@@ -7305,7 +7305,10 @@ static int open_self_stat(void *cpu_env, int fd)
          snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
        } else if (i == 1) {
          /* app name */
-        snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        len = snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        if (len >= sizeof(buf))
+          /* bring back the ending ") " that was truncated */
+          strcpy(buf+sizeof(buf)-3, ") ");

Maybe we can avoid the sprintf() call:

-- >8 --

linux-user/syscall.c +5 -1
@@ -7305,7 +7305,11 @@ static int open_self_stat(void *cpu_env, int fd)
         snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
       } else if (i == 1) {
         /* app name */
-        snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        char *ptr = buf;
+
+        *ptr++ = '(';
+        ptr = stpncpy(ptr, ts->bprm->argv[0], sizeof(buf) - 3);
+        strcpy(ptr, ") ");
       } else if (i == 27) {
         /* stack bottom */
         val = start_stack;

---

Quoted 4 lines
linux-user/syscall.c +0 -0
        } else if (i == 27) {
          /* stack bottom */
          val = start_stack;
#2 lore raw

Le 31/03/2020 à 00:05, Philippe Mathieu-Daudé a écrit :

Quoted 47 lines · Brice Goglin
Quoted 27 lines

When the program name is very long, qemu-user may truncate it in /proc/self/stat. However the truncation must keep the ending ") " to conform to the proc manpage which says:
     (2) comm  %s
            The  filename of the executable, in parentheses.  This is visible whether or not the  executable  is  swapped out.

To reproduce:

$ ln -s /bin/cat <filenamewithmorethan128chars>
$ qemu-x86_64 ./<filenamewithmorethan128chars> /proc/self/stat

Before the patch, you get:
1134631 (<filenametruncated>0 0 0 0 0 0 0 0 ...
After the patch:
1134631 (<filenametruncat>) 0 0 0 0 0 0 0 0 ...

This fixes an issue with hwloc failing to parse /proc/self/stat when Ludovic Courtes was testing it in guix over qemu-aarch64.

linux-user/syscall.c +0 -0
index 5af55fca78..a1126dcf5b 100644

linux-user/syscall.c static int open_self_stat(void *cpu_env, int fd) · line 7311

Quoted 8 lines
linux-user/syscall.c +4 -1
@@ -7305,7 +7305,10 @@ static int open_self_stat(void *cpu_env, int fd)
           snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
         } else if (i == 1) {
           /* app name */
-        snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        len = snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        if (len >= sizeof(buf))
+          /* bring back the ending ") " that was truncated */
+          strcpy(buf+sizeof(buf)-3, ") ");

Maybe we can avoid the sprintf() call:

-- >8 --

linux-user/syscall.c +0 -0
@@ -7305,7 +7305,11 @@ static int open_self_stat(void *cpu_env, int fd)
          snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
        } else if (i == 1) {

linux-user/syscall.c static int open_self_stat(void *cpu_env, int fd) · line 7315

Quoted 10 lines
linux-user/syscall.c +5 -1
          /* app name */
-        snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        char *ptr = buf;
+
+        *ptr++ = '(';
+        ptr = stpncpy(ptr, ts->bprm->argv[0], sizeof(buf) - 3);
+        strcpy(ptr, ") ");
        } else if (i == 27) {
          /* stack bottom */
          val = start_stack;

This works too.

Brice

#3 lore raw

Le 31/03/2020 à 00:29, Brice Goglin a écrit :

Quoted 50 lines

Le 31/03/2020 à 00:05, Philippe Mathieu-Daudé a écrit :

Quoted 49 lines · Brice Goglin
Quoted 27 lines

When the program name is very long, qemu-user may truncate it in /proc/self/stat. However the truncation must keep the ending ") " to conform to the proc manpage which says:
     (2) comm  %s
            The  filename of the executable, in parentheses.  This is visible whether or not the  executable  is  swapped out.

To reproduce:

$ ln -s /bin/cat <filenamewithmorethan128chars>
$ qemu-x86_64 ./<filenamewithmorethan128chars> /proc/self/stat

Before the patch, you get:
1134631 (<filenametruncated>0 0 0 0 0 0 0 0 ...
After the patch:
1134631 (<filenametruncat>) 0 0 0 0 0 0 0 0 ...

This fixes an issue with hwloc failing to parse /proc/self/stat when Ludovic Courtes was testing it in guix over qemu-aarch64.

linux-user/syscall.c +0 -0
index 5af55fca78..a1126dcf5b 100644

linux-user/syscall.c static int open_self_stat(void *cpu_env, int fd) · line 7311

Quoted 8 lines
linux-user/syscall.c +4 -1
@@ -7305,7 +7305,10 @@ static int open_self_stat(void *cpu_env, int fd)
           snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
         } else if (i == 1) {
           /* app name */
-        snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        len = snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        if (len >= sizeof(buf))
+          /* bring back the ending ") " that was truncated */
+          strcpy(buf+sizeof(buf)-3, ") ");

Maybe we can avoid the sprintf() call:

-- >8 --

linux-user/syscall.c +2 -1
@@ -7305,7 +7305,11 @@ static int open_self_stat(void *cpu_env, int fd)
          snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
        } else if (i == 1) {
          /* app name */
-        snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        char *ptr = buf;
+
Quoted 8 lines

linux-user/syscall.c static int open_self_stat(void *cpu_env, int fd) · line 7315

Quoted 7 lines
linux-user/syscall.c +3 -0
+        *ptr++ = '(';
+        ptr = stpncpy(ptr, ts->bprm->argv[0], sizeof(buf) - 3);
+        strcpy(ptr, ") ");
        } else if (i == 27) {
          /* stack bottom */
          val = start_stack;

This works too.

Hello

Is anybody going to fix this anyhow for the next release?

Thank you

Brice

#4 lore raw

Le 08/04/2020 à 08:40, Brice Goglin a écrit :

Quoted 61 lines

Le 31/03/2020 à 00:29, Brice Goglin a écrit :

Quoted 50 lines

Le 31/03/2020 à 00:05, Philippe Mathieu-Daudé a écrit :

Quoted 49 lines · Brice Goglin
Quoted 27 lines

When the program name is very long, qemu-user may truncate it in /proc/self/stat. However the truncation must keep the ending ") " to conform to the proc manpage which says:
     (2) comm  %s
            The  filename of the executable, in parentheses.  This is visible whether or not the  executable  is  swapped out.

To reproduce:

$ ln -s /bin/cat <filenamewithmorethan128chars>
$ qemu-x86_64 ./<filenamewithmorethan128chars> /proc/self/stat

Before the patch, you get:
1134631 (<filenametruncated>0 0 0 0 0 0 0 0 ...
After the patch:
1134631 (<filenametruncat>) 0 0 0 0 0 0 0 0 ...

This fixes an issue with hwloc failing to parse /proc/self/stat when Ludovic Courtes was testing it in guix over qemu-aarch64.

linux-user/syscall.c +0 -0
index 5af55fca78..a1126dcf5b 100644

linux-user/syscall.c static int open_self_stat(void *cpu_env, int fd) · line 7311

Quoted 8 lines
linux-user/syscall.c +4 -1
@@ -7305,7 +7305,10 @@ static int open_self_stat(void *cpu_env, int fd)
           snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
         } else if (i == 1) {
           /* app name */
-        snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        len = snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        if (len >= sizeof(buf))
+          /* bring back the ending ") " that was truncated */
+          strcpy(buf+sizeof(buf)-3, ") ");

Maybe we can avoid the sprintf() call:

-- >8 --

linux-user/syscall.c +2 -1
@@ -7305,7 +7305,11 @@ static int open_self_stat(void *cpu_env, int fd)
          snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
        } else if (i == 1) {
          /* app name */
-        snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
+        char *ptr = buf;
+
Quoted 8 lines

linux-user/syscall.c static int open_self_stat(void *cpu_env, int fd) · line 7315

Quoted 7 lines
linux-user/syscall.c +3 -0
+        *ptr++ = '(';
+        ptr = stpncpy(ptr, ts->bprm->argv[0], sizeof(buf) - 3);
+        strcpy(ptr, ") ");
        } else if (i == 27) {
          /* stack bottom */
          val = start_stack;

This works too.

Quoted 3 lines

Hello

Is anybody going to fix this anyhow for the next release?

Hi,

we are expecting you fix the patch according to Philippe's comment and re-send it or explain why you don't want.

Thanks,
Laurent