Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3421128imm; Mon, 8 Oct 2018 03:52:32 -0700 (PDT) X-Google-Smtp-Source: ACcGV63cDumQUAKYiyBjKCAsNDslPfHPa4WPFy638vqE+7HKUuki/4PPRUxGGEqRSYRMdyuiFctx X-Received: by 2002:a63:7d2:: with SMTP id 201-v6mr21010247pgh.129.1538995952373; Mon, 08 Oct 2018 03:52:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538995952; cv=none; d=google.com; s=arc-20160816; b=EZbtcx+wgH75ctElAUPCgBqPjEHnutWV5a3fdOd/jWVJ+dyBku7b/yLNq0CbR7DFBK T140961KjE0aoPY7vBhdtF8o5nSIMNoNsEU2IcWTV6JmGHHBY0wLr/7/lnWwdzGLiGae qtf9FCbgQmsttMiOjJ/z/8eIjfdIBTlEXrccVrt5qWHBYbajGbP70OHeOXizQczBf/9b oO4npe70umdW8XdACUxgyY+3R9fWBvXm6Be8y0DqbjACJynyljkOXr6WWJqNdImnGeH1 fVezjM9WbcuwFu384eHNOHEmsGFYsiA/UuYCEMWrdcpV0qWoWUPyZR5c+5XfcIgCIVQN X22Q== 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=Jnx21yYhkaDAgnAPDUHthI2Lxm2pq2fKPQuUILKjyno=; b=Vp7M9/6Zrt2k37vgXQB23OwfsHcxT3WbeXZ9/5FE/d8WYtc0UaQpeZ6tC2bE8pWg9n +jzmFq9NrW+FcZvVUfPrY+s9zLSkYqa0646CqmvLR5B/I6PxUfJhc5w1wai5YICPxLn6 k44v040X+B1DU/DxqD9TS/zcSX8FcY7t7Kffb3zdJcBeBsxqhhJVrVkjlWp0lmaRSXVy abf0rG0GKZNKdxq7wpnEtVsWoicK6hWdgO3PJXt5UTyywEa8OuERp8vQ0gkh35nMTYz1 cTHsBXv4IjabYF6aVpsOedk8Dh/Tl4gPa5UeH8u9U7bNOwUNqq67LuDbcJoTlI9+pNtG 6f5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tcqAtd4L; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e34-v6si19438515plb.2.2018.10.08.03.52.17; Mon, 08 Oct 2018 03:52:32 -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=@google.com header.s=20161025 header.b=tcqAtd4L; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727754AbeJHSCL (ORCPT + 99 others); Mon, 8 Oct 2018 14:02:11 -0400 Received: from mail-ot1-f65.google.com ([209.85.210.65]:36569 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727728AbeJHSCJ (ORCPT ); Mon, 8 Oct 2018 14:02:09 -0400 Received: by mail-ot1-f65.google.com with SMTP id k5so6356620ote.3 for ; Mon, 08 Oct 2018 03:51:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Jnx21yYhkaDAgnAPDUHthI2Lxm2pq2fKPQuUILKjyno=; b=tcqAtd4L+AIEsFo1daXRekm7DgJfKFNUMezobs3mu5wqCgij+WP7pP9hKeDZMrKb5D mev6ypSErX2li7D1URVBFLRCMsMiJZNw5Hq+250+dMduyMiT7FZeHAyZw8xVSwu9bRf2 4qRb00sCDSizNwlXG6pgjDT7ZmIIVBakqa0P8LfVJeoelV4JBPkGzPtZy1OmoV00gQBT e+EdOozWtyPwsRZgUB4Jdc6syBjBxg03feqFBbkoYcFliFmdBlgWBpRDC8Id2GYJP/98 LEYEHYHrY/lNWJeYQmkPHrfGu11LJxMBmCOOj/wSVaZeRz2Ev52Nv99K20wemtGPVfmQ D8Ow== 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=Jnx21yYhkaDAgnAPDUHthI2Lxm2pq2fKPQuUILKjyno=; b=pEdwO1D1zwtyZ5fomn6VW98LwOR7J/xjvawe1EcaZRz3dyRD5Kdeirv56mz5AZprTt ckSmdrpfvwJezstAig+NJKBxBlEANBT48Rzx68soAundDrQ3ld8zbKYjdqlS+4L6vw0P htmdw8Qmm9UxoAi6uGWAnUDGxnFHpINeh2wvCq39BrUb2/+8WyuEioiR+3uh6psHK8Be By477LdveO6TfB0Lsck0aroucBA0GvEAsJlci17ny0EoZN4vVSMGKBWrmnmEObI3W9g0 I+H8IVTwiW+40dkzrJKPCRPP2IfTsTx+rSDzdyNt+JsWBji027UPNX30zJfUbT8qqPN/ /yZg== X-Gm-Message-State: ABuFfohcskpd6MMSpkaSTHTYWyS2TMV6tfkvFTnThNPRNybhd5MUAmYx fPh/bdB9ilcmhW3WOuuiKOQId+N413sf0Xfblu+8jg== X-Received: by 2002:a9d:4c15:: with SMTP id l21mr13617334otf.242.1538995861373; Mon, 08 Oct 2018 03:51:01 -0700 (PDT) MIME-Version: 1.0 References: <20180929103453.12025-1-cyphar@cyphar.com> <20180929131534.24472-1-cyphar@cyphar.com> <20181004162611.vdlujbdguvagalpt@ryuk> <20181005150728.mgqnpkbukpeu3bsm@ryuk> <20181006021002.6vzsdwd3klddbmji@ryuk> In-Reply-To: <20181006021002.6vzsdwd3klddbmji@ryuk> From: Jann Horn Date: Mon, 8 Oct 2018 12:50:34 +0200 Message-ID: Subject: Re: [PATCH 2/3] namei: implement AT_THIS_ROOT chroot-like path resolution To: cyphar@cyphar.com Cc: "Eric W. Biederman" , Al Viro , jlayton@kernel.org, Bruce Fields , Arnd Bergmann , shuah@kernel.org, David Howells , Andy Lutomirski , christian@brauner.io, Tycho Andersen , kernel list , linux-fsdevel@vger.kernel.org, linux-arch , linux-kselftest@vger.kernel.org, dev@opencontainers.org, containers@lists.linux-foundation.org, Linux API 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 Sat, Oct 6, 2018 at 4:10 AM Aleksa Sarai wrote: > On 2018-10-05, Jann Horn wrote: > > > What if we took rename_lock (call it nd->r_seq) at the start of the > > > resolution, and then only tried the __d_path-style check > > > > > > if (read_seqretry(&rename_lock, nd->r_seq) || > > > read_seqretry(&mount_lock, nd->m_seq)) > > > /* do the __d_path lookup. */ > > > > > > That way you would only hit the slow path if there were concurrent > > > renames or mounts *and* you are doing a path resolution with > > > AT_THIS_ROOT or AT_BENEATH. I've attached a modified patch that does > > > this (and after some testing it also appears to work). > > > > Yeah, I think that might do the job. > > *phew* I was all out of other ideas. :P > > > > --- > > > fs/namei.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- > > > 1 file changed, 46 insertions(+), 3 deletions(-) > > > > > > diff --git a/fs/namei.c b/fs/namei.c > > > index 6f995e6de6b1..12c9be175cb4 100644 > > > --- a/fs/namei.c > > > +++ b/fs/namei.c > > > @@ -493,7 +493,7 @@ struct nameidata { > > > struct path root; > > > struct inode *inode; /* path.dentry.d_inode */ > > > unsigned int flags; > > > - unsigned seq, m_seq; > > > + unsigned seq, m_seq, r_seq; > > > int last_type; > > > unsigned depth; > > > int total_link_count; > > > @@ -1375,6 +1375,27 @@ static int follow_dotdot_rcu(struct nameidata *nd) > > > return -EXDEV; > > > break; > > > } > > > + if (unlikely((nd->flags & (LOOKUP_BENEATH | LOOKUP_CHROOT)) && > > > + (read_seqretry(&rename_lock, nd->r_seq) || > > > + read_seqretry(&mount_lock, nd->m_seq)))) { > > > + char *pathbuf, *pathptr; > > > + > > > + nd->r_seq = read_seqbegin(&rename_lock); > > > + /* Cannot take m_seq here. */ > > > + > > > + pathbuf = kmalloc(PATH_MAX, GFP_ATOMIC); > > > + if (!pathbuf) > > > + return -ECHILD; > > > + pathptr = __d_path(&nd->path, &nd->root, pathbuf, PATH_MAX); > > > + kfree(pathbuf); > > > > You're doing this check before actually looking up the parent, right? > > So as long as I don't trigger the "path_equal(&nd->path, &nd->root)" > > check that you do for O_BENEATH, escaping up by one level is possible, > > right? You should probably move this check so that it happens after > > following "..". > > Yup, you're right. I'll do that. > > > (Also: I assume that you're going to get rid of that memory allocation > > in a future version.) > > Sure. Would you prefer adding some scratch space in nameidata, or that I > change __d_path so it accepts NULL as the buffer (and thus it doesn't > actually do any string operations)? Well, I think accepting a NULL buffer would be much cleaner; but keep in mind that I'm just someone making suggestions, Al Viro is the one who has to like your code. :P > > > if (nd->path.dentry != nd->path.mnt->mnt_root) { > > > int ret = path_parent_directory(&nd->path); > > > if (ret) > > > @@ -2269,6 +2311,9 @@ static const char *path_init(struct nameidata *nd, unsigned flags) > > > nd->last_type = LAST_ROOT; /* if there are only slashes... */ > > > nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT; > > > nd->depth = 0; > > > + nd->m_seq = read_seqbegin(&mount_lock); > > > + nd->r_seq = read_seqbegin(&rename_lock); > > > > This means that now, attempting to perform a lookup while something is > > holding the rename_lock will spin on the lock. I don't know whether > > that's a problem in practice though. Does anyone on this thread know > > whether this is problematic? > > I could make it so that we only take &rename_lock > if (nd->flags & (FOLLOW_BENEATH | FOLLOW_CHROOT)), > since it's not used outside of that path. I think that might be a sensible change; but as I said, I don't actually know whether it's necessary, and it would be very helpful if someone who actually knows commented on this.