From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF386ECE58D for ; Thu, 10 Oct 2019 17:14:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BE8B421835 for ; Thu, 10 Oct 2019 17:14:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570727644; bh=rVBQfiP/YB1yYBxqZtiGACMzxcOAKtX1Jx40mSLBOVM=; h=References:In-Reply-To:From:Date:Subject:To:Cc:List-ID:From; b=I3E8HMMUqbxijZShY5jZgFvS6T86f/a+3B27SDHLPwv0q7XknpSKlCBhHia0Yl38m BmGClWLf0zI4PqfEUkCg2AILFPeAM33+E0doIGn5Enxu0NyxjOURnRMnqlYj+ihDmH v3JCYQKdw2y7MpZ+BJucQU7PDFB1esnPiIRLsV1I= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726131AbfJJROE (ORCPT ); Thu, 10 Oct 2019 13:14:04 -0400 Received: from mail-lf1-f66.google.com ([209.85.167.66]:45621 "EHLO mail-lf1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726397AbfJJROE (ORCPT ); Thu, 10 Oct 2019 13:14:04 -0400 Received: by mail-lf1-f66.google.com with SMTP id r134so4964757lff.12 for ; Thu, 10 Oct 2019 10:14:01 -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=gnDWinQmWw+cGtzlWucJ39UhLuuQ7yJ5AkF+LJffNnLRisa6sPwF3Y/Xi8mRMOQIal FavSCOQYk34aFYgkiGekAzRgo6DsKcLZgQH/jQo0Yyxzl1+F4VqPhG4yI8JL2KU+YCOZ uGR07+jRd2fu0waU7VIRI/Hg/AxtKe73Cs3quHLpAPL9FYQnHRNaLnRsIZ22CQy7cPQF zJh4bATqmrjcg/9SCvjvRQEUSLSJrW7oLzXVSjWNs2ZMAXrGArssQCLySHb9cU+SPLQT rfn34T+EV/dVCq0z8Yud/W6dMXfrBISPrVXUimvGGs1HGZBq41Il5LLv7U/Uktzxgq6L U5PA== X-Gm-Message-State: APjAAAWfuKvqDcdrHp6IfRQjCJ7K6hWnhPEGSvNpLvvceT8rjLo+zMlu OUjdRy+h/6NaKPlMhpSORz8rtnQIc+8= X-Google-Smtp-Source: APXvYqx+n0G9rl/RyAgskmFk09y7OWh/UxZYXADAQgqtINSzi0rsAhDaQ7/YCExQTSoy4/d3Pk2wVg== X-Received: by 2002:ac2:4946:: with SMTP id o6mr6312172lfi.86.1570727640790; Thu, 10 Oct 2019 10:14:00 -0700 (PDT) Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com. [209.85.167.48]) by smtp.gmail.com with ESMTPSA id b6sm1637145lfi.72.2019.10.10.10.14.00 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 10 Oct 2019 10:14:00 -0700 (PDT) Received: by mail-lf1-f48.google.com with SMTP id x80so4987776lff.3 for ; Thu, 10 Oct 2019 10:14:00 -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-parisc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-parisc@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