libc - Fix numerous fork/exec*() leaks, also add mkostemp() and mkostemps().
authorMatthew Dillon <dillon@apollo.backplane.com>
Wed, 27 Jul 2016 18:13:44 +0000 (11:13 -0700)
committerMatthew Dillon <dillon@apollo.backplane.com>
Wed, 27 Jul 2016 18:13:44 +0000 (11:13 -0700)
commit5270936ced7738caef7b7302e4b02466f8e913bc
tree6927152d29ba2e039f76faa89cbeffdf3b84175d
parentb4758707dd5c7257ed376de88139920e95589c78
libc - Fix numerous fork/exec*() leaks, also add mkostemp() and mkostemps().

* Use O_CLOEXEC in many places to prevent temporary descriptors from leaking
  into fork/exec'd code (e.g. in multi-threaded situations).

* Note that the popen code will close any other popen()'d descriptors in
  the child process that it forks just prior to exec.  However, there was
  a descriptor leak where another thread issuing popen() at the same time
  could leak the descriptors into their exec.

  Use O_CLOEXEC to close this hole.

* popen() now accepts the 'e' flag (i.e. "re") to retain O_CLOEXEC in the
  returned descriptor.  Normal "r" (etc) will clear O_CLOEXEC in the returned
  descriptor.

  Note that normal "r" modes are still fine for most use cases since popen
  properly closes other popen()d descriptors in the fork().  BUT!! If the
  threaded program calls exec*() in other ways, such descriptors may
  unintentionally be passed onto sub-processes.  So consider using "re".

* Add mkostemp() and mkostemps() to allow O_CLOEXEC to be passed in,
  closing a thread race that would otherwise leak the temporary descriptor
  into other fork/exec()s.

Taken-from: Mostly taken from FreeBSD
20 files changed:
include/stdlib.h
lib/libc/db/btree/bt_open.c
lib/libc/db/db/db.c
lib/libc/db/hash/hash.c
lib/libc/db/hash/hash_page.c
lib/libc/db/recno/rec_open.c
lib/libc/gen/arc4random.c
lib/libc/gen/getcap.c
lib/libc/gen/nlist.c
lib/libc/gen/popen.c
lib/libc/gen/readpassphrase.c
lib/libc/gen/syslog.c
lib/libc/gen/wordexp.c
lib/libc/gmon/gmon.c
lib/libc/stdio/Symbol.map
lib/libc/stdio/fdopen.c
lib/libc/stdio/freopen.c
lib/libc/stdio/mktemp.3
lib/libc/stdio/mktemp.c
lib/libc/yp/yplib.c