Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp2640726ybg; Fri, 5 Jun 2020 20:13:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFKs/4fagzyetDTP8VOtivrElUpnG6m3gh5kVwEpYuWXaak0cvr9gl2Mz2Z/gfEva1H+BP X-Received: by 2002:a17:906:69d1:: with SMTP id g17mr11188088ejs.521.1591413223402; Fri, 05 Jun 2020 20:13:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591413223; cv=none; d=google.com; s=arc-20160816; b=v8JbNe51yZfQeqXLeCnyf0utqRtV1FoJhkt/KwDqprChWn15WQSuOskXCeH2Vtq0v+ z8veEjtQ9Wm3tDgZ+tPEnPAPcuNLP32HqwPisQlt/PpPYIjqA+okRxYhDhLg43/yuPMg hJyeWD180Zvo9gQmvbS7naGWmDLKhDslnJd/BHeHpdJwLxtPiPO7oDohHANsVF557zxB 6cD4ZiM+Y9x4t3A3RTiksATRS39MOfVsldP70iNBOuThliwnb+ilUIc/khMmYSPxtaD2 2w0felgkSQfVkyQycSu+ZcIQTUscwhd5dFZPOqGFiDQhXN+4L5OD8DfTg4AJO4qmnrGf Bwpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:cc:subject:message-id:date:from :in-reply-to:references:mime-version; bh=/CPYHoPQ+4MBa/JKhDtYSo55iVRRoATubwfoUkhgeTE=; b=OmxkUTPqA1PCJ1Qn1lGgdjkTfHyfwfcjWPksl2rSMt/9t5y/47+Pzb331m/3W2v5Ty Yrkarh6sVpkFY1cp71FFSQHt4g6EKHBOv9UtftU6VafT4WWV/1QE4ftJkkAgD9hAU56d 2IX4EZ9WDzJwoQ18qJv0rotSssgAX6how58erYK2gd0XBDTpflauQ/f9CCI+9iuBYQOo R9RjsXgZbmf2b/SUKW/RpoeGKjPPr8WssMjTOEqGxHmE0VK4FWZzFNGWcRV2tKm22C32 +gmBCG1Uxc7rTypOCWZxfuXKWaya9kpi7LqBcoG9yJBL0WySHMV9WsgfPPLdv5kcemoq ItVw== 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 z2si4872381ejo.329.2020.06.05.20.13.20; Fri, 05 Jun 2020 20:13:43 -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 S1728554AbgFFDLc (ORCPT + 99 others); Fri, 5 Jun 2020 23:11:32 -0400 Received: from mx2.freebsd.org ([96.47.72.81]:24233 "EHLO mx2.freebsd.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728506AbgFFDLb (ORCPT ); Fri, 5 Jun 2020 23:11:31 -0400 Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits)) (Client CN "mx1.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx2.freebsd.org (Postfix) with ESMTPS id A092A892E6 for ; Sat, 6 Jun 2020 03:11:26 +0000 (UTC) (envelope-from kevans@freebsd.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 49f4Kp36tLz4cKc for ; Sat, 6 Jun 2020 03:11:26 +0000 (UTC) (envelope-from kevans@freebsd.org) Received: from mail-qk1-f181.google.com (mail-qk1-f181.google.com [209.85.222.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) (Authenticated sender: kevans) by smtp.freebsd.org (Postfix) with ESMTPSA id 3F5B92188C for ; Sat, 6 Jun 2020 03:11:26 +0000 (UTC) (envelope-from kevans@freebsd.org) Received: by mail-qk1-f181.google.com with SMTP id b27so11879747qka.4 for ; Fri, 05 Jun 2020 20:11:26 -0700 (PDT) X-Gm-Message-State: AOAM531pXJJFKTFOTNhKiVkmj1BmohBoH7pr6fWzTip5+8iy3djWusnS yYBj2OBpDDCCnxkEZes7NMwJ0ow8UO3f4ANNhBY= X-Received: by 2002:a37:a3ce:: with SMTP id m197mt14010554qke.493.1591413085829; Fri, 05 Jun 2020 20:11:25 -0700 (PDT) MIME-Version: 1.0 References: <20200602204219.186620-1-christian.brauner@ubuntu.com> <20200602204219.186620-2-christian.brauner@ubuntu.com> <20200605145549.GC673948@port70.net> In-Reply-To: From: Kyle Evans Date: Fri, 5 Jun 2020 22:11:14 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v5 1/3] open: add close_range() Cc: Szabolcs Nagy , Christian Brauner , torvalds@linux-foundation.org, linux-kernel , Victor Stinner , viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, fweimer@redhat.com, jannh@google.com, oleg@redhat.com, arnd@arndb.de, shuah@kernel.org, dhowells@redhat.com, ldv@altlinux.org Content-Type: text/plain; charset="UTF-8" To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 5, 2020 at 9:54 PM Kyle Evans wrote: > > On Fri, Jun 5, 2020 at 9:55 AM Szabolcs Nagy wrote: > > > > * Christian Brauner [2020-06-02 22:42:17 +0200]: > > > [... snip ...] > > > > > > First, it helps to close all file descriptors of an exec()ing task. This > > > can be done safely via (quoting Al's example from [1] verbatim): > > > > > > /* that exec is sensitive */ > > > unshare(CLONE_FILES); > > > /* we don't want anything past stderr here */ > > > close_range(3, ~0U); > > > execve(....); > > > > this api needs a documentation patch if there isn't yet. > > > > currently there is no libc interface contract in place that > > says which calls may use libc internal fds e.g. i've seen > > > > openlog(...) // opens libc internal syslog fd > > ... > > fork() > > closefrom(...) // close syslog fd > > open(...) // something that reuses the closed fd > > syslog(...) // unsafe: uses the wrong fd > > execve(...) > > > > syslog uses a libc internal fd that the user trampled on and > > this can go bad in many ways depending on what libc apis are > > used between closefrom (or equivalent) and exec. > > > > Documentation is good. :-) I think you'll find that while this example > seems to be innocuous on FreeBSD (and likely other *BSD), this is an > atypical scenario and generally not advised. You would usually not > start closing until you're actually ready to exec/fail. > Minor correction: not innocuous here, either; O_CLOFORK is not yet a thing. :-)