docs, parallelism: Do not leak blocking mode to other readers

Setting non-blocking via a local copy of the jobserver file descriptor
is safer than just assuming other reader processes with the same fd open
are prepared for it to be non-blocking.

Suggested-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Link: https://lore.kernel.org/lkml/44c01043-ab24-b4de-6544-e8efd153e27a@rasmusvillemoes.dk
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20191121205929.40371-3-keescook@chromium.org
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
This commit is contained in:
Kees Cook 2019-11-21 12:59:28 -08:00 коммит произвёл Jonathan Corbet
Родитель 4920323cff
Коммит dffd011480
1 изменённых файлов: 6 добавлений и 8 удалений

Просмотреть файл

@ -12,12 +12,6 @@ default="1"
if len(sys.argv) > 1: if len(sys.argv) > 1:
default=sys.argv[1] default=sys.argv[1]
# Set non-blocking for a given file descriptor.
def nonblock(fd):
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
return fd
# Extract and prepare jobserver file descriptors from envirnoment. # Extract and prepare jobserver file descriptors from envirnoment.
try: try:
# Fetch the make environment options. # Fetch the make environment options.
@ -31,8 +25,12 @@ try:
# Parse out R,W file descriptor numbers and set them nonblocking. # Parse out R,W file descriptor numbers and set them nonblocking.
fds = opts[0].split("=", 1)[1] fds = opts[0].split("=", 1)[1]
reader, writer = [int(x) for x in fds.split(",", 1)] reader, writer = [int(x) for x in fds.split(",", 1)]
reader = nonblock(reader) # Open a private copy of reader to avoid setting nonblocking
except (KeyError, IndexError, ValueError, IOError): # on an unexpecting process with the same reader fd.
reader = os.open("/proc/self/fd/%d" % (reader),
os.O_RDONLY | os.O_NONBLOCK)
except (KeyError, IndexError, ValueError, IOError, OSError) as e:
print(e, file=sys.stderr)
# Any missing environment strings or bad fds should result in just # Any missing environment strings or bad fds should result in just
# using the default specified parallelism. # using the default specified parallelism.
print(default) print(default)