Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1360114pxm; Thu, 3 Mar 2022 16:10:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJwLVOqxfYXg3eWU/vGuipt7pwM6pRar5OqmWUlDbMKY1xpejIYoBAL8QzNJ86JobOa1w0cH X-Received: by 2002:a17:902:a3c1:b0:14f:dc65:ff6c with SMTP id q1-20020a170902a3c100b0014fdc65ff6cmr38801477plb.13.1646352647704; Thu, 03 Mar 2022 16:10:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646352647; cv=none; d=google.com; s=arc-20160816; b=xQtIFXIrgVKyHScqRwEo5N8aIZTJr7oITLxvzK7ewObHpsWsqd7YSVS0U8G7hJhnbg KmOdfIcD+bOFU7voGsfqlmsW8XNT7qhzOYAFzkw2aAE588LyFvn9afsQ6PqqgQ8Eld5R 1o1Oo0u4k8JTBFPrBBZgbd5ZnpqrmugJdtEwlJYVBjz0TBu8O8UqUY1EZ44O84kH8/G9 OmQ+tGhwpn4XxMlYaOfRzRbApvp5ocwxQ7tMCt5oK5qZsdkThjmapRq3qzBY/+iQCOZp mwwewPN7Zz/VUuweYR3lmXBP2vUFtNNK8vpVDiY82TRCC99kiJiaRKoz3sLTfl2XnfGH 005A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=yS7kazZpf1WctKIduUm7SU3KoWdFaMISwpfhZK76qk4=; b=0cP8uegP4Gv1XeBlAd0c8YFTolkTqalqC4BufwpTMlvCg9GaUpexBg3gvYfbzcAREt 5fBQ2+r0GHCXaa0Lo3A5BuN8n6AgHydNpH3cGm1TuaQedarjT53VRleBrNdwcE7V7iAY 0ZCHeHYWpVULWtJeRC9BKMD+EO5iyI91/VgJI/AiIva8ekANOXEHXwUPQcT8MjIQf55U 2TR4fOI6g3SwCl4e0OD3m+0oCyE1sS9GA4HBcNwUeX3UGSfsW14hK2wRSMNj2RgIZ0j1 GK7OPwMPV8dhOgP2rh2ZxQipPKsir+SlP4+o0GdUBsh+jkwOzE/EgtLTMNjxhwfB7A+6 Spmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Nc+Q0v+P; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x70-20020a638649000000b0037c951c4823si397479pgd.614.2022.03.03.16.10.30; Thu, 03 Mar 2022 16:10:47 -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; dkim=pass header.i=@google.com header.s=20210112 header.b=Nc+Q0v+P; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232862AbiCCS55 (ORCPT + 99 others); Thu, 3 Mar 2022 13:57:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234299AbiCCS5v (ORCPT ); Thu, 3 Mar 2022 13:57:51 -0500 Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53EAC57488 for ; Thu, 3 Mar 2022 10:57:05 -0800 (PST) Received: by mail-qt1-x82b.google.com with SMTP id 11so5424995qtt.9 for ; Thu, 03 Mar 2022 10:57:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=yS7kazZpf1WctKIduUm7SU3KoWdFaMISwpfhZK76qk4=; b=Nc+Q0v+PIGcbgNSKGbAhoiP4zv0ZLw9EYz+X+vcZ7eAvYAkptoELg10H+l8UiNtoQd zTdX1x+Dejrwn4ZSU240LOeUObpgjLNFjn6VhA87Bwir4Zd3RnU4IrPqjJPyTMqfnnbW TRjwEalgqsarcOi9ia2xb267aKpZApOuvMurAkWuYqQ0Wd7WIeYIAsrlvb6jxVyVX9sc 7WNuDEZytTuZxDL7mlDkxob3dH1Y0V11qSnKIefDDmk1Xein6gXCNLxJzzqFMMynOROe Z2y+e7G4bGHlc4zthKwbDP8O5oo7rYFxSEazLVK2bVW1XBoMsZlxJveTaRdgTStHcJW2 g5LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=yS7kazZpf1WctKIduUm7SU3KoWdFaMISwpfhZK76qk4=; b=BNp6vNfUUo4H6kSn/OLL+sk6mxUAtiH+SQZ3ElEGNMZ4/HBgBXWBW/wHtuJnnO99oD gV7DdK3FGJE8/9/nmNjc2gZpcDQ/H1doZG15q5mlpFqGHcjixkOwlI/Z5MH/TcYsVS2s Ga2Equ5Dei2tqyEufVPdi81YCH2t8tMSZZF3jrcIADSahexhn40mH/YXUCmcHiqlSq6Z cfUeBfJAuWBTMIJq63Q7raLAmYuu2cUNtAtaSqFkGQyZIqeifSx1HqQ8l4ak7b1gLJGr NgEaVBAOyeIDoCfufp2v+171gRwXERp9T48zYC2+tEk7Ff8mVMehzh0c998neNybYThI lBvA== X-Gm-Message-State: AOAM532+mT3H9qw06r7/eTx2xxnOFXaAZWuzw0JZqocXYdmtyamdRJ63 CMfd7VzSQkAKJmeEN73x3YKxQ7ZN/cqk+uIAMhjM5A== X-Received: by 2002:ac8:58d5:0:b0:2de:2dfc:77d3 with SMTP id u21-20020ac858d5000000b002de2dfc77d3mr28109007qta.168.1646333824282; Thu, 03 Mar 2022 10:57:04 -0800 (PST) MIME-Version: 1.0 References: <20220225234339.2386398-1-haoluo@google.com> <20220225234339.2386398-2-haoluo@google.com> <7e862b1c-7818-6759-caf1-962598d2c8b3@fb.com> In-Reply-To: <7e862b1c-7818-6759-caf1-962598d2c8b3@fb.com> From: Hao Luo Date: Thu, 3 Mar 2022 10:56:53 -0800 Message-ID: Subject: Re: [PATCH bpf-next v1 1/9] bpf: Add mkdir, rmdir, unlink syscalls for prog_bpf_syscall To: Yonghong Song Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Song Liu , KP Singh , Shakeel Butt , Joe Burton , Tejun Heo , joshdon@google.com, sdf@google.com, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-18.1 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 Wed, Mar 2, 2022 at 12:55 PM Yonghong Song wrote: > > > > On 2/25/22 3:43 PM, 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. > > > > Signed-off-by: Hao Luo > > --- > > include/linux/bpf.h | 1 + > > include/uapi/linux/bpf.h | 26 +++++ > > kernel/bpf/inode.c | 9 +- > > kernel/bpf/syscall.c | 177 +++++++++++++++++++++++++++++++++ > > tools/include/uapi/linux/bpf.h | 26 +++++ > > 5 files changed, 236 insertions(+), 3 deletions(-) > > > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > > index f19abc59b6cd..fce5e26179f5 100644 > > --- a/include/linux/bpf.h > > +++ b/include/linux/bpf.h > > @@ -1584,6 +1584,7 @@ int bpf_link_new_fd(struct bpf_link *link); > > struct file *bpf_link_new_file(struct bpf_link *link, int *reserved_fd); > > struct bpf_link *bpf_link_get_from_fd(u32 ufd); > > > > +bool bpf_path_is_bpf_dir(const struct path *path); > > int bpf_obj_pin_user(u32 ufd, const char __user *pathname); > > int bpf_obj_get_user(const char __user *pathname, int flags); > > > > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h > > index afe3d0d7f5f2..a5dbc794403d 100644 > > --- a/include/uapi/linux/bpf.h > > +++ b/include/uapi/linux/bpf.h > > @@ -5086,6 +5086,29 @@ union bpf_attr { > > * Return > > * 0 on success, or a negative error in case of failure. On error > > * *dst* buffer is zeroed out. > > + * > > + * long bpf_mkdir(const char *pathname, int pathname_sz, u32 mode) > > Can we make pathname_sz to be u32 instead of int? pathname_sz should > never be negative any way. > > Also, I think it is a good idea to add 'u64 flags' parameter for all > three helpers, so we have room in the future to tune for new use cases. > SG. Will make this change. Actually, I think I need to cap patthname_sz from above, to ensure pathname_sz isn't too big. Is that necessary? I see there are other helpers that don't have this type of check. > > + * Description > > + * Attempts to create a directory name *pathname*. The argument > > + * *pathname_sz* specifies the length of the string *pathname*. > > + * The argument *mode* specifies the mode for the new directory. It > > + * is modified by the process's umask. It has the same semantic as > > + * the syscall mkdir(2). > > + * Return > > + * 0 on success, or a negative error in case of failure. > > + * > > + * long bpf_rmdir(const char *pathname, int pathname_sz) > > + * Description > > + * Deletes a directory, which must be empty. > > + * Return > > + * 0 on sucess, or a negative error in case of failure. > > + * > > + * long bpf_unlink(const char *pathname, int pathname_sz) > > + * Description > > + * Deletes a name and possibly the file it refers to. It has the > > + * same semantic as the syscall unlink(2). > > + * Return > > + * 0 on success, or a negative error in case of failure. > > */ > > #define __BPF_FUNC_MAPPER(FN) \ > > FN(unspec), \ > > @@ -5280,6 +5303,9 @@ union bpf_attr { > > FN(xdp_load_bytes), \ > > FN(xdp_store_bytes), \ > > FN(copy_from_user_task), \ > > + FN(mkdir), \ > > + FN(rmdir), \ > > + FN(unlink), \ > > /* */ > > > > /* integer value in 'imm' field of BPF_CALL instruction selects which helper > [...]