Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp988598pxp; Fri, 11 Mar 2022 21:49:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJzIscxXL8Q1FWx2qLPa1Xq1NahWktGGUh9jMZoShcGvmaS15hCDdg4REDq/y0bwfGhyNM+u X-Received: by 2002:aa7:d648:0:b0:416:4b85:da68 with SMTP id v8-20020aa7d648000000b004164b85da68mr11439809edr.253.1647064152573; Fri, 11 Mar 2022 21:49:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1647064152; cv=none; d=google.com; s=arc-20160816; b=qinnPScoNW2ugYOAmCDLn7jEj2FROMAzPLQawv5wno5odWYpqjiZel8iOqJNkqbyQa J1ZwebzDf54otyCB8TqE+LuRsdCz4KY4koxjOdOwKrhiR1DyCoGBYpYHno90aiA+EQiV mviPaP+m8LTLu6HUFeREPggR6+DdSRom4Okv2iX6tEBRA/bHKTUhZsxE8WFnzGFMySR+ rS7SjY8Ip4EE/NHE2JnauM7BmtZIAJPuvmWz9DQMI3q0R2jNtOSelnDPzkRrZF7TGR+c yTTDACG/cKV4a5JPRY9VbzKN+iAR9H/cI+ruSMdp9hZ2hiH1esdQ03MErRx+ijP1Q9AV fOJg== 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=yUxN6n0AT8/UeHSQ7idiriqXrlvV8KmtEt7D/r8T0r4=; b=LTg2pn3jW6ScrwIdgpjVdDF4AjC197ywB5muF11aSDDSOiExSxoKitn9Cf0PZKDDMs vxg9748ZJw0pVqFWsdPwdgK2B12mwOOtBlfEn17YBH0xEmfQALadAyFv+r9/PvMh4z6H 7xhdJRuqYGPgfJhMVJCJfe/+7ywfQPTQYqZGfmstXw6xINhsRHW6q11NNFIhqgWZoaiw wwBSSv+cJWx2oMe895YUDjkDKVe5QaAjvlIOlMCwsw6vW05Xa8S66eDcI7bf7ezTHNys tDAVmfHQVOFW+/bl5t+sabsZyeSClbLJ/zCtdSBstxALkj6IvjKCkphjKcd8h+/xA+Mu zGHw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b9-20020a056402084900b0041850767609si1754411edz.48.2022.03.11.21.48.42; Fri, 11 Mar 2022 21:49:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230284AbiCLDry (ORCPT + 99 others); Fri, 11 Mar 2022 22:47:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229679AbiCLDrx (ORCPT ); Fri, 11 Mar 2022 22:47:53 -0500 Received: from zeniv-ca.linux.org.uk (zeniv-ca.linux.org.uk [IPv6:2607:5300:60:148a::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F11052396BD; Fri, 11 Mar 2022 19:46:48 -0800 (PST) Received: from viro by zeniv-ca.linux.org.uk with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1nSsiD-00A6aq-T5; Sat, 12 Mar 2022 03:46:37 +0000 Date: Sat, 12 Mar 2022 03:46:37 +0000 From: Al Viro To: Hao Luo Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Shakeel Butt , Joe Burton , Tejun Heo , joshdon@google.com, sdf@google.com, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall Message-ID: References: <20220225234339.2386398-1-haoluo@google.com> <20220225234339.2386398-2-haoluo@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220225234339.2386398-2-haoluo@google.com> Sender: Al Viro X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Feb 25, 2022 at 03:43:31PM -0800, Hao Luo wrote: > This patch allows bpf_syscall prog to perform some basic filesystem > operations: create, remove directories and unlink files. Three bpf > helpers are added for this purpose. When combined with the following > patches that allow pinning and getting bpf objects from bpf prog, > this feature can be used to create directory hierarchy in bpffs that > help manage bpf objects purely using bpf progs. > > The added helpers subject to the same permission checks as their syscall > version. For example, one can not write to a read-only file system; > The identity of the current process is checked to see whether it has > sufficient permission to perform the operations. > > Only directories and files in bpffs can be created or removed by these > helpers. But it won't be too hard to allow these helpers to operate > on files in other filesystems, if we want. In which contexts can those be called? > +BPF_CALL_2(bpf_rmdir, const char *, pathname, int, pathname_sz) > +{ > + struct user_namespace *mnt_userns; > + struct path parent; > + struct dentry *dentry; > + int err; > + > + if (pathname_sz <= 1 || pathname[pathname_sz - 1]) > + return -EINVAL; > + > + err = kern_path(pathname, 0, &parent); > + if (err) > + return err; > + > + if (!bpf_path_is_bpf_dir(&parent)) { > + err = -EPERM; > + goto exit1; > + } > + > + err = mnt_want_write(parent.mnt); > + if (err) > + goto exit1; > + > + dentry = kern_path_locked(pathname, &parent); This can't be right. Ever. There is no promise whatsoever that these two lookups will resolve to the same place. > +BPF_CALL_2(bpf_unlink, const char *, pathname, int, pathname_sz) > +{ > + struct user_namespace *mnt_userns; > + struct path parent; > + struct dentry *dentry; > + struct inode *inode = NULL; > + int err; > + > + if (pathname_sz <= 1 || pathname[pathname_sz - 1]) > + return -EINVAL; > + > + err = kern_path(pathname, 0, &parent); > + if (err) > + return err; > + > + err = mnt_want_write(parent.mnt); > + if (err) > + goto exit1; > + > + dentry = kern_path_locked(pathname, &parent); > + if (IS_ERR(dentry)) { > + err = PTR_ERR(dentry); > + goto exit2; > + } Ditto. NAK; if you want to poke into fs/namei.c guts, do it right. Or at least discuss that on fsdevel. As it is, it's completely broken. It's racy *and* it blatantly leaks both vfsmount and dentry references. NAKed-by: Al Viro