Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp3630780ybc; Thu, 21 Nov 2019 11:11:09 -0800 (PST) X-Google-Smtp-Source: APXvYqwXn3x6U0te78t7UpPOx+52/Ktu50M5Vgy3uVc1xE1rkgiccg1NgbzVzWu6Ls43yZo7vgtJ X-Received: by 2002:ac2:5323:: with SMTP id f3mr5397086lfh.89.1574363468956; Thu, 21 Nov 2019 11:11:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574363468; cv=none; d=google.com; s=arc-20160816; b=Wnuwu/Ip68WWJZxEntPxy4ANqB8eil14VFwIVohfqAScQlsqCW5FqtXky3zjnv9e1X lhA2/yOpeKpYqSd20qjOLwQ6I6ZcAsCia3gBSaMyHRBC+1nZXmR4u91h7fknOQ2f/Vya AYdPGNNgOfmVke3mCzQLclAqbNvJMs0m7Br1s2YFyMLwrIxqBhL1bbL/DMFpcpMpRilk QVoqa46SiuBlMdjmcCX0hCksy+PIuoGmZ2zAZCZ2egky3XnPoSi2RFw50b1ksnQKV3bj uD8/VJc3KLGSlkpLfosGfu6lK264+u6QZhCOMWfR8B7hxGhqwqrsAt+alb1br1xUQs+d xhuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=512M0jNKnpjQCVGlztaNviSFeHJgudNPE/mVt3EA2jY=; b=NfnB6PL7Ae4nL/PUpApMd2LGuM5SMq9l5YNx3cv78fTQeAAlcFb3i54WpbCG6bbTgK HxcVYauBlohpsZymSXE+jDZkO4Ui9YakOD+xXn94J1ko8nVcTOV1aag1yy+oqjvOBQY/ zBCGIHWvJGN2KJ8J+24lMzaGjzKaWYtccfrpe4G46UYfas6i6JFoCfST315nz+qyh6vz KFPMaXeaiU80fOa2YrgBG7cJozhTg7WDV+6d4hiOAPPofG54bFk6v/hK7KQkN9o1O+PD EYUJlINmzUP8SR7LyFKFgbPZIB1qMqiM5y3Ffm7wTVWMvOshIhFo2te7Yo9elSIipxdp 9w2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="KvzhMiJ/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m20si2606562ejx.33.2019.11.21.11.10.43; Thu, 21 Nov 2019 11:11:08 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="KvzhMiJ/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726774AbfKUTJH (ORCPT + 99 others); Thu, 21 Nov 2019 14:09:07 -0500 Received: from mail-pl1-f195.google.com ([209.85.214.195]:33203 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726279AbfKUTJH (ORCPT ); Thu, 21 Nov 2019 14:09:07 -0500 Received: by mail-pl1-f195.google.com with SMTP id ay6so2023065plb.0 for ; Thu, 21 Nov 2019 11:09:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=512M0jNKnpjQCVGlztaNviSFeHJgudNPE/mVt3EA2jY=; b=KvzhMiJ/FqEem7WAqVSVWVE0BXFKNvDf6cbkhw+1rbjEdn85GzRjaVdbAOa+5YLaGn qP1zc0EpcTp7UUjx+bet/L7YmKvoFJIjDtCldnbT7U5K5IdtQEf8DVy+gfL3kb06Tg0L zBcP6s4y2zTGOFtqX/0Zi4Wp7bgmz6Vm6EP/M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=512M0jNKnpjQCVGlztaNviSFeHJgudNPE/mVt3EA2jY=; b=d25mkYAsCbuLW5WYgey+j/ytNeDQsz5zz+54yZ2rNWcU9iMr+CeMJkAAVze9rE9Lgm z3cxovpAjy0/YwMYsRSsBZPCm6llhBO5N02J3lXWgBqJ6ucyHfAQGXuvdMhsSkUl+PyP GgX4c0RURnT4D2u1+u2K8R4qo9FYV9uv7bulm/Ra+hm5H6BvEyqqtgl0WCoadFjQH56U MlbpUu/tQVR7WYHhG0U4gboen3qpKQwM+otzwVbO+HHAOUbBQySLUpIiD9Dqb2UyiQ9E q9q6HIFhVddEZOoR0N2M/X7vMEbriCDU2DVIVboCayLupR01P9zfFdyA1fOFLVbdiPuY n8Bg== X-Gm-Message-State: APjAAAXnegTwgkoEPP58wZGNtveT0FxIf8o8CzrllCPCYksu+kvPvAab eXwLfyCV6DuQX5KXBNYiaaRsSw== X-Received: by 2002:a17:902:ac84:: with SMTP id h4mr10557273plr.328.1574363346681; Thu, 21 Nov 2019 11:09:06 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 16sm4390942pfc.21.2019.11.21.11.09.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Nov 2019 11:09:05 -0800 (PST) Date: Thu, 21 Nov 2019 11:09:04 -0800 From: Kees Cook To: Rasmus Villemoes Cc: Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/3] docs, parallelism: Do not leak blocking mode to writer Message-ID: <201911211105.E11EEBAC4@keescook> References: <20191121000304.48829-1-keescook@chromium.org> <20191121000304.48829-3-keescook@chromium.org> <041953ef-0b6c-4ea8-8734-aa1e6703f9f8@rasmusvillemoes.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <041953ef-0b6c-4ea8-8734-aa1e6703f9f8@rasmusvillemoes.dk> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 21, 2019 at 08:41:01AM +0100, Rasmus Villemoes wrote: > On 21/11/2019 01.03, Kees Cook wrote: > > Setting non-blocking via a local copy of the jobserver file descriptor > > is safer than just assuming the writer on the original fd is prepared > > for it to be non-blocking. > > This is a bit inaccurate. The fd referring to the write side of the pipe > is always blocking - it has to be, due to the protocol requiring you to > write back the tokens you've read, so you can't just drop a token on the > floor. But it's also rather moot, since the pipe will never hold > anywhere near 4096 bytes, let alone a (linux) pipe's default capacity of > 64K. > > But what we cannot do is change the mode of the open file description to > non-blocking for the read side, in case the parent make (or some sibling > process that has also inherited the same "struct file") expects it to be > blocking. Ah! This explains my confusion over what you were trying to tell me before. I thought you meant the other end of the pipe, which seemed crazy. You mean the other jobserver readers (i.e. "make" itself) who have the same shared _reader_ fd. This is exactly what you said, but I was too dense. :) I'll fix this up! > > > Suggested-by: Rasmus Villemoes > > Link: https://lore.kernel.org/lkml/44c01043-ab24-b4de-6544-e8efd153e27a@rasmusvillemoes.dk > > Signed-off-by: Kees Cook > > --- > > scripts/jobserver-count | 15 +++++++-------- > > 1 file changed, 7 insertions(+), 8 deletions(-) > > > > diff --git a/scripts/jobserver-count b/scripts/jobserver-count > > index 6e15b38df3d0..a68a04ad304f 100755 > > --- a/scripts/jobserver-count > > +++ b/scripts/jobserver-count > > @@ -12,12 +12,6 @@ default="1" > > if len(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. > > try: > > # Fetch the make environment options. > > @@ -31,8 +25,13 @@ try: > > # Parse out R,W file descriptor numbers and set them nonblocking. > > fds = opts[0].split("=", 1)[1] > > reader, writer = [int(x) for x in fds.split(",", 1)] > > - reader = nonblock(reader) > > -except (KeyError, IndexError, ValueError, IOError): > > + # Open a private copy of reader to avoid setting nonblocking > > + # on an unexpecting writer. > > s/writer/reader/ > > > + reader = os.open("/proc/self/fd/%d" % (reader), os.O_RDONLY) > > + flags = fcntl.fcntl(reader, fcntl.F_GETFL) > > + fcntl.fcntl(reader, fcntl.F_SETFL, flags | os.O_NONBLOCK) > > I think you can just specify O_NONBLOCK in the open() call so you avoid > those two fcntls. Hah. Yes indeed. -- Kees Cook