Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1444429ybz; Wed, 22 Apr 2020 21:53:26 -0700 (PDT) X-Google-Smtp-Source: APiQypInhoaTqnmE1yqYnexAm7DPzRsV3wpJ7CYD/flyuZEc61yyxQmOiVsPENdLW0tzjUz24WxW X-Received: by 2002:a50:c004:: with SMTP id r4mr1249014edb.110.1587617605916; Wed, 22 Apr 2020 21:53:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587617605; cv=none; d=google.com; s=arc-20160816; b=M25ucSWb0q1nIjR3lgwQxywGo4B9/haZvAsqwDhg3/xbv+lXFiDuesf75e2MA11Rvf 67Prwi3qOUBT0Z5G/8zMmHMCwFK/phH3aCxA86sjgdgu5C6hWKu8jdny4OX3PRhMeLQM qlbaiajbEnR0/9b/3uz3uYbSbvQ4f4pJDpaIGzWxLQvpmAV5aX+8Cc5A+xZhjen6ThmR TG88gRLSP6hOKa21injNmBzDlomyhxs6q4RnNCE5707N5SL6UUez/M4pwV93if9PwO44 YyDzvXNdAo6QsI/IhYwEhViO1YZ+kpg1tNDlauMi2FqWmmn57W/wtXvkVM4rxGO0VBQl S1Ww== 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; bh=iwO3Og7SE1S4hrS1xX1vzmYj6pl52Z2DP2RcvTvu4nQ=; b=PZilH6aZNBPOuR6He2tKEQbG8kIhkmT50zbZg7PtMYVSw+mE52JFd41RTXXiv99PQy 2UFvrq2cMtOKtG654Gis3m9oUScuPww4+fbV8fhwZguGK2bZShvC96+p24EOr22HS7UO YqfQcBUfYOLvyJCoRZOLoh+F5Kq0peFy6xOOmfzrsb2XOI0HOot2YKp6NpGicfLdNjOD J4h4IzVySXxWaObwPyAaMMe16Ns9FSWAc8ZtlS89xwYpSSCbdVJ5I7bUjpdn/knWLLY0 NizCkxpF7g30IGTedBmDcMTbTS2ECb+LJ1XRcGXmjc9Td5cKE5W6aj3TTaOlVJ7n5XkE IhUQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g26si591687edu.55.2020.04.22.21.53.03; Wed, 22 Apr 2020 21:53:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726179AbgDWEvW (ORCPT + 99 others); Thu, 23 Apr 2020 00:51:22 -0400 Received: from relay6-d.mail.gandi.net ([217.70.183.198]:43895 "EHLO relay6-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725867AbgDWEvW (ORCPT ); Thu, 23 Apr 2020 00:51:22 -0400 X-Originating-IP: 50.39.163.217 Received: from localhost (50-39-163-217.bvtn.or.frontiernet.net [50.39.163.217]) (Authenticated sender: josh@joshtriplett.org) by relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 36D75C0005; Thu, 23 Apr 2020 04:51:14 +0000 (UTC) Date: Wed, 22 Apr 2020 21:51:12 -0700 From: Josh Triplett To: "Dmitry V. Levin" Cc: io-uring@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, mtk.manpages@gmail.com, Alexander Viro , Arnd Bergmann , Jens Axboe , Aleksa Sarai , linux-man@vger.kernel.org, Linux API Subject: Re: [PATCH v5 1/3] fs: Support setting a minimum fd for "lowest available fd" allocation Message-ID: <20200423045112.GI161058@localhost> References: <05c9a6725490c5a5c4ee71be73326c2fedf35ba5.1587531463.git.josh@joshtriplett.org> <20200423011253.GA18957@altlinux.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200423011253.GA18957@altlinux.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 23, 2020 at 04:12:53AM +0300, Dmitry V. Levin wrote: > On Tue, Apr 21, 2020 at 10:19:49PM -0700, Josh Triplett wrote: > > Some applications want to prevent the usual "lowest available fd" > > allocation from allocating certain file descriptors. For instance, they > > may want to prevent allocation of a closed fd 0, 1, or 2 other than via > > dup2/dup3, or reserve some low file descriptors for other purposes. > > > > Add a prctl to increase the minimum fd and return the previous minimum. > > > > System calls that allocate a specific file descriptor, such as > > dup2/dup3, ignore this minimum. > > > > exec resets the minimum fd, to prevent one program from interfering with > > another program's expectations about fd allocation. > > Please make this aspect properly documented in "Effect on process > attributes" section of execve(2) manual page. Done. I'll include updated manpage patches in v6. > > +unsigned int increase_min_fd(unsigned int num) > > +{ > > + struct files_struct *files = current->files; > > + unsigned int old_min_fd; > > + > > + spin_lock(&files->file_lock); > > + old_min_fd = files->min_fd; > > + files->min_fd += num; > > + spin_unlock(&files->file_lock); > > + return old_min_fd; > > +} > > If it's "increase", there should be an overflow check. > Otherwise it's "assign" rather than "increase". I'll add a check in v6, to make sure that the value cannot overflow into the errno range. (Note that this is not security-sensitive, it's just providing a footgun-resistant interface. It should absolutely check, though.) - Josh Triplett