procps/proc
Qualys Security Advisory 36c350f07c proc/readproc.c: Fix bugs and overflows in file2strvec().
Note: this is by far the most important and complex patch of the whole
series, please review it carefully; thank you very much!

For this patch, we decided to keep the original function's design and
skeleton, to avoid regressions and behavior changes, while fixing the
various bugs and overflows. And like the "Harden file2str()" patch, this
patch does not fail when about to overflow, but truncates instead: there
is information available about this process, so return it to the caller;
also, we used INT_MAX as a limit, but a lower limit could be used.

The easy changes:

- Replace sprintf() with snprintf() (and check for truncation).

- Replace "if (n == 0 && rbuf == 0)" with "if (n <= 0 && tot <= 0)" and
  do break instead of return: it simplifies the code (only one place to
  handle errors), and also guarantees that in the while loop either n or
  tot is > 0 (or both), even if n is reset to 0 when about to overflow.

- Remove the "if (n < 0)" block in the while loop: it is (and was) dead
  code, since we enter the while loop only if n >= 0.

- Rewrite the missing-null-terminator detection: in the original
  function, if the size of the file is a multiple of 2047, a null-
  terminator is appended even if the file is already null-terminated.

- Replace "if (n <= 0 && !end_of_file)" with "if (n < 0 || tot <= 0)":
  originally, it was equivalent to "if (n < 0)", but we added "tot <= 0"
  to handle the first break of the while loop, and to guarantee that in
  the rest of the function tot is > 0.

- Double-force ("belt and suspenders") the null-termination of rbuf:
  this is (and was) essential to the correctness of the function.

- Replace the final "while" loop with a "for" loop that behaves just
  like the preceding "for" loop: in the original function, this would
  lead to unexpected results (for example, if rbuf is |\0|A|\0|, this
  would return the array {"",NULL} but should return {"","A",NULL}; and
  if rbuf is |A|\0|B| (should never happen because rbuf should be null-
  terminated), this would make room for two pointers in ret, but would
  write three pointers to ret).

The hard changes:

- Prevent the integer overflow of tot in the while loop, but unlike
  file2str(), file2strvec() cannot let tot grow until it almost reaches
  INT_MAX, because it needs more space for the pointers: this is why we
  introduced ARG_LEN, which also guarantees that we can add "align" and
  a few sizeof(char*)s to tot without overflowing.

- Prevent the integer overflow of "tot + c + align": when INT_MAX is
  (almost) reached, we write the maximal safe amount of pointers to ret
  (ARG_LEN guarantees that there is always space for *ret = rbuf and the
  NULL terminator).
2018-05-19 07:32:22 +10:00
..
alloc.c proc/alloc.*: Use size_t, not unsigned int. 2018-05-19 07:32:21 +10:00
alloc.h proc/alloc.*: Use size_t, not unsigned int. 2018-05-19 07:32:21 +10:00
COPYING miscellaneous: clean up trailing whitespace once again 2013-04-07 18:05:01 +10:00
devname.c proc/devname.c: Never write more than "chop" (part 2). 2018-05-19 07:32:21 +10:00
devname.h Change restrict to __restrict in public includes 2012-01-09 21:44:51 +11:00
escape.c proc/escape.c: Handle negative snprintf() return value. 2018-05-19 07:32:21 +10:00
escape.h Change restrict to __restrict in public includes 2012-01-09 21:44:51 +11:00
fprocopen.3 docs: Change name of fprocopen man page 2018-02-28 21:24:03 +11:00
libprocps.pc.in Renaming libprocfs to libprocps 2011-12-23 09:18:43 +11:00
libprocps.sym sysctl: Bring procio functions out of library 2018-03-01 21:25:04 +11:00
numa.c library: set stage for NUMA node field display support 2017-05-22 21:34:32 +10:00
numa.h library: build on non-glibc systems 2018-04-10 21:28:11 +10:00
openproc.3 documentation: fix man pages due to refactor for wchan 2015-06-19 19:09:21 +10:00
procps-private.h library: Change linux version 2015-06-19 21:00:46 +10:00
procps.h library: relocate the typedef used in alloc.h override 2017-12-23 17:41:37 +11:00
pwcache.c library: rename clashing pwcache functions 2016-07-06 22:26:02 +10:00
pwcache.h library: rename clashing pwcache functions 2016-07-06 22:26:02 +10:00
readproc.3 misc: result after checking all files for misspellings 2014-08-08 22:14:21 +02:00
readproc.c proc/readproc.c: Fix bugs and overflows in file2strvec(). 2018-05-19 07:32:22 +10:00
readproc.h library: avoid QUICK_THREADS, swat Redhat bug #1284091 2016-07-09 13:23:27 +10:00
readproctab.3 misc: result after checking all files for misspellings 2014-08-08 22:14:21 +02:00
sig.c proc/sig.c: Harden print_given_signals(). 2018-05-19 07:32:21 +10:00
sig.h Change restrict to __restrict in public includes 2012-01-09 21:44:51 +11:00
slab.c proc/slab.c: Initialize struct slab_info in get_slabnode(). 2018-05-19 07:32:21 +10:00
slab.h proc/slab.h: Fix off-by-one overflow in sscanf(). 2018-05-19 07:32:21 +10:00
sysinfo.c proc/sysinfo.c: Ensure null-termination in getstat(). 2018-05-19 07:32:22 +10:00
sysinfo.h - Fixing sysinfo - devices with length exceeding 15 chars are not displayed in vmstat -d 2016-06-30 18:29:50 +02:00
version.c miscellaneous: remove some trailing whitespace buildup 2015-06-20 07:46:23 +10:00
version.h proc/version.h: Protect parameter in LINUX_VERSION() macro. 2018-05-19 07:32:21 +10:00
wchan.c misc: remove some trailing whitespace newly introduced 2016-12-07 21:50:59 +11:00
wchan.h library: refactor and rely on modern kernels for wchan 2015-06-19 19:09:20 +10:00
whattime.c proc/whattime.c: Always initialize buf. 2018-05-19 07:32:21 +10:00
whattime.h Add -p|--pretty option to uptime(1) for pretty output. 2012-02-22 10:35:12 -05:00