Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp2638834ybp; Thu, 10 Oct 2019 10:15:26 -0700 (PDT) X-Google-Smtp-Source: APXvYqyGYkth5Epgtu6lbtlPUnk/YiBm+f5oOlWAQNFY45zIJOe6TtYw0JOarY5KTq1hzHwN0DkU X-Received: by 2002:a17:906:3946:: with SMTP id g6mr9149109eje.49.1570727726224; Thu, 10 Oct 2019 10:15:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570727726; cv=none; d=google.com; s=arc-20160816; b=jdmYfx0QvgahUKjC5NmOH34NAhBa1TvkZk7MiYYNvz/UsMWGCWY+AeAP8wN658qVNB J0cih6537DMao2uv1USJA29rlM93KasH6PVq/zKjYz3itlJ8H4fLgRTf+t5jVi+rBE2m E41VjPQQqhCVixyHQI11/WIOXjNCKh7WF9+F3v8+KlqNZiKX9OMkkohp9aZ3r84jQ0WH MpStODOoW8nqBiSdOf2CCr/7oI1lvXHt5O5Q5ZU2a0qLsTkXba4Vlv2gY2RIM8bXIvBT bSLwrxMNh8XmR4xPmscoESgqiTqelwJ74GduYcLEZsK0O4NlG9t4fq0hXHj0WTYseaqz Bo4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=bqvz+afB1Rg3ieCrlwoBKML7ipcOdiXJ/q/XXxTUovI=; b=g05+9II+w4v1M2yfHni6jqup7NIj4LFQFjK3hY+lFkXemmY2roACg2HxarcpJ1JTLC 4QmyGB+mn75NU5LKW3IPAgpwis5vGP2BZOCk5JEuGzjUxGVB8jhYRl5RoYagrSZOTO4b YGaoALe6aYBX5yUVZjK8X5R7gRJHpWnpaLbuR0Hx0UpEF9hHWWnlk/YPekVdjQ7+W/8z qEA+G8eCcdMsLOB7T0gjVWMNe4LzwUQghZYObaigD1Dopj5lCPDEJRmt0autRLdRKvH3 BH+e2zDux0K6Vw/e48nwtS7U3EJzmVgfSm7C0mw+pJ6DDUa8Ep5dKZ2ZitDBW+vN9D08 CACw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b="Xol/3a0t"; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v14si4518521ede.424.2019.10.10.10.15.01; Thu, 10 Oct 2019 10:15:26 -0700 (PDT) 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=@linux-foundation.org header.s=google header.b="Xol/3a0t"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726752AbfJJROp (ORCPT + 99 others); Thu, 10 Oct 2019 13:14:45 -0400 Received: from mail-lf1-f68.google.com ([209.85.167.68]:43950 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726453AbfJJROo (ORCPT ); Thu, 10 Oct 2019 13:14:44 -0400 Received: by mail-lf1-f68.google.com with SMTP id u3so4963030lfl.10 for ; Thu, 10 Oct 2019 10:14:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=bqvz+afB1Rg3ieCrlwoBKML7ipcOdiXJ/q/XXxTUovI=; b=Xol/3a0t0AtCulvKJeuAKS1uo5PWCjeyeDeHYLSA1FXKSnOkgSTF276gwVNHa4nPiN l58RJ3zV8SDjz5HcQUbk/k9PwAg2ytkBSxmhuzf7L/EFmiieyqeQcJuKFBVQbWUjfXE1 U4t1RhqK0BT10C1Atodrl5m0eWGZPM7fTB2Dk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=bqvz+afB1Rg3ieCrlwoBKML7ipcOdiXJ/q/XXxTUovI=; b=glodWuALEM8cYbt/zcs/CojeJ1zS2ukvV26k4/OJpiCo+7eHNXvn2iQEqw0cnr5NUr IjjZAFQNGMw1fP013bbzZzEXCPVUBHhSaP7yLolpXB3UEp0ZXRnlrxTTj2ZjjEhiD+gW IbJtzE/UpomgpP2psuQHlV6hg8HEmCbZVJQa8H7pyUalee+n4yT72dexFwa/0Jbq7vL+ zfdWKDCLNiiqQQiOqG5UdffngQVlUNXZyXumYXYS3IRdK4NB7TNYgkqgpzzPyVH3JZJV a43nP0CA9AVgShVuYPesU87LQ4g/QckFjvq92o/I+5ePEuBrDNAvx9dn5W4bVLYORcMg /LBw== X-Gm-Message-State: APjAAAUbxynaiBWYbMl91nZtUMV48NnuyqI/4heUfIuXgQI+HS5YqGir 7Mt0vQWp32Vq4uT0klyRKWJpc4Rhb2s= X-Received: by 2002:ac2:4946:: with SMTP id o6mr6313540lfi.86.1570727681525; Thu, 10 Oct 2019 10:14:41 -0700 (PDT) Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com. [209.85.167.50]) by smtp.gmail.com with ESMTPSA id q14sm1381586ljc.7.2019.10.10.10.14.41 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 10 Oct 2019 10:14:41 -0700 (PDT) Received: by mail-lf1-f50.google.com with SMTP id w6so4987921lfl.2 for ; Thu, 10 Oct 2019 10:14:41 -0700 (PDT) X-Received: by 2002:a19:f709:: with SMTP id z9mr6611392lfe.170.1570727268603; Thu, 10 Oct 2019 10:07:48 -0700 (PDT) MIME-Version: 1.0 References: <20191010054140.8483-1-cyphar@cyphar.com> <20191010054140.8483-3-cyphar@cyphar.com> In-Reply-To: <20191010054140.8483-3-cyphar@cyphar.com> From: Linus Torvalds Date: Thu, 10 Oct 2019 10:07:32 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v14 2/6] namei: LOOKUP_IN_ROOT: chroot-like path resolution To: Aleksa Sarai Cc: Al Viro , Jeff Layton , "J. Bruce Fields" , Arnd Bergmann , David Howells , Shuah Khan , Shuah Khan , Ingo Molnar , Peter Zijlstra , Eric Biederman , Andy Lutomirski , Andrew Morton , Alexei Starovoitov , Kees Cook , Jann Horn , Tycho Andersen , David Drysdale , Chanho Min , Oleg Nesterov , Rasmus Villemoes , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Christian Brauner , Aleksa Sarai , Linux Containers , alpha , Linux API , GNU C Library , linux-arch , Linux ARM , linux-fsdevel , linux-ia64@vger.kernel.org, Linux Kernel Mailing List , "open list:KERNEL SELFTEST FRAMEWORK" , linux-m68k , linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390 , Linux-sh list , linux-xtensa@linux-xtensa.org, sparclinux@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Oct 9, 2019 at 10:42 PM Aleksa Sarai wrote: > > --- a/fs/namei.c > +++ b/fs/namei.c > @@ -2277,6 +2277,11 @@ static const char *path_init(struct nameidata *nd, unsigned flags) > > nd->m_seq = read_seqbegin(&mount_lock); > > + /* LOOKUP_IN_ROOT treats absolute paths as being relative-to-dirfd. */ > + if (flags & LOOKUP_IN_ROOT) > + while (*s == '/') > + s++; > + > /* Figure out the starting path and root (if needed). */ > if (*s == '/') { > error = nd_jump_root(nd); Hmm. Wouldn't this make more sense all inside the if (*s =- '/') test? That way if would be where we check for "should we start at the root", which seems to make more sense conceptually. That test for '/' currently has a "} else if (..)", but that's pointless since it ends with a "return" anyway. So the "else" logic is just noise. And if you get rid of the unnecessary else, moving the LOOKUP_IN_ROOT inside the if-statement works fine. So this could be something like --- a/fs/namei.c +++ b/fs/namei.c @@ -2194,11 +2196,19 @@ static const char *path_init(struct nameidata *nd, unsigned flags) nd->m_seq = read_seqbegin(&mount_lock); if (*s == '/') { - set_root(nd); - if (likely(!nd_jump_root(nd))) - return s; - return ERR_PTR(-ECHILD); - } else if (nd->dfd == AT_FDCWD) { + /* LOOKUP_IN_ROOT treats absolute paths as being relative-to-dirfd. */ + if (!(flags & LOOKUP_IN_ROOT)) { + set_root(nd); + if (likely(!nd_jump_root(nd))) + return s; + return ERR_PTR(-ECHILD); + } + + /* Skip initial '/' for LOOKUP_IN_ROOT */ + do { s++; } while (*s == '/'); + } + + if (nd->dfd == AT_FDCWD) { if (flags & LOOKUP_RCU) { struct fs_struct *fs = current->fs; unsigned seq; instead. The patch ends up slightly bigger (due to the re-indentation) but now it handles all the "start at root" in the same place. Doesn't that make sense? Linus