Bug 11826 - ps pid/ppid/pgid fields truncated when kernel.pid_max > 99999
Summary: ps pid/ppid/pgid fields truncated when kernel.pid_max > 99999
Status: RESOLVED FIXED
Alias: None
Product: Busybox
Classification: Unclassified
Component: Other (show other bugs)
Version: unspecified
Hardware: All Linux
: P5 critical
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-05-02 19:25 UTC by josh
Modified: 2019-05-03 07:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description josh 2019-05-02 19:25:20 UTC
`ps` from all versions checked and the current `master` uses a hard-coded width per field for output formatting.

From `procps/ps.c`

```
static const ps_out_t out_spec[] = {
  /* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.ht  ml: */
      { 8                  , "user"  ,"USER"   ,func_user  ,PSSCAN_UIDGID  },
      { 8                  , "group" ,"GROUP"  ,func_group ,PSSCAN_UIDGID  },
      { 16                 , "comm"  ,"COMMAND",func_comm  ,PSSCAN_COMM    },
      { MAX_WIDTH          , "args"  ,"COMMAND",func_args  ,PSSCAN_COMM    },
      { 5                  , "pid"   ,"PID"    ,func_pid   ,PSSCAN_PID     },
      { 5                  , "ppid"  ,"PPID"   ,func_ppid  ,PSSCAN_PPID    },
      { 5                  , "pgid"  ,"PGID"   ,func_pgid  ,PSSCAN_PGID    },
```

This works when the linux sysctl `kernel.pid_max` is at its default of 32768.  However, if the value set to > 99999, it creates the potential for the `pid`, `ppid`, and `pgid` fields to be not only truncated upon output but with no delimiting space between it and the right hand side adjacent field. Eg.,

```
/ # ps -efo pid,comm
PID   COMMAND
    1 dockerd
   31 containerd
408249containerd-shim
408266cat
408786containerd-shim
408803cat
413819sh
414865ps
```

This is a bit of a disaster for programs that try to parse the output of `ps` and I've recently run into this with `docker`. Eg.,

https://github.com/moby/moby/issues/34282
Comment 1 Denys Vlasenko 2019-05-03 07:49:51 UTC
Fixing in git:

--- a/procps/ps.c
+++ b/procps/ps.c
@@ -533,7 +533,7 @@ static void format_process(const procps_status_t *ps)
                len = out[i].width - len + 1;
                if (++i == out_cnt) /* do not pad last field */
                        break;
-               p += sprintf(p, "%*s", len, "");
+               p += sprintf(p, "%*s", len, " "); /* " ", not "", to ensure separation of fields */
        }
        printf("%.*s\n", terminal_width, buffer);
 }