From nobody Thu Oct 9 16:33:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E994A1E521D; Tue, 17 Jun 2025 06:11:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750140687; cv=none; b=mgwNS/jvPRwGrk9hv07zFLEmaY+HKrgOuSzGCou/iSupu+bbuWehmWTPRxJcQhRCZH1o4wcCR1t6aSi+LoOsy2Oa9Ndx2R7NriYtuYYT5olEraUOiNK48tVgxQL7Bw1mKhlMTgAHNSYh5SGA2UJKLfi0/xqlQ2l/DDbGnCkLYa8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750140687; c=relaxed/simple; bh=RgJWHD65/lLqLRRj/mZf66iDcTZ9KMomSVgLP6/ZWFs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pOW2pc/3riDTILLEoiug0tX9QpPDXB/1xMCGmtuQR333gzVk/ZExEzR5q7X1DPmqfg4GgR9yYqr0mcHQ6EPvxxud+sOr6CjJWuNuQyRwoQkE+ggv9yOjip8FVIP+y52bDnR+zAJ2FQo5ZkNDT2xN1czhXqGa0Jqd4yc9MT++t9w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Q3xJYa2D; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Q3xJYa2D" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A9011C4CEE3; Tue, 17 Jun 2025 06:11:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750140686; bh=RgJWHD65/lLqLRRj/mZf66iDcTZ9KMomSVgLP6/ZWFs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q3xJYa2Dx37asskvhJcEb2wXZaSzAH1DPhKkXRyClSTWR+He5ZalWkkP7ONdMuPEK T7PvSxP+V+/Z/yV2GSoS7eIQVWGD3FUNbzcLXDV9r/rhPoAXdzRjzvRK/3kCDyfnGS ITi6x7ck1nOFgauZtRPytsFUxynAz04qzpfogCKWvLhbr5aRU0B0CobchHRRDLqwvO 8u47+EzNNuDHekc57FWyRrBlIMDV62qJNpObWQsWcusYvY3qf6ikHPxX18ktFNLYZs ehEhqK4gzNDPbPG7Y5w336pJ1Vu+XbXeNMDtSFjqYRiCgthqniAdWtXkJUw6Xd5KkO kEMRkfVMLBUNg== From: Song Liu To: bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Cc: kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com, ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, kpsingh@kernel.org, mattbobrowski@google.com, m@maowtm.org, neil@brown.name, Song Liu Subject: [PATCH v5 bpf-next 1/5] namei: Introduce new helper function path_walk_parent() Date: Mon, 16 Jun 2025 23:11:12 -0700 Message-ID: <20250617061116.3681325-2-song@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250617061116.3681325-1-song@kernel.org> References: <20250617061116.3681325-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This helper walks an input path to its parent. Logic are added to handle walking across mount tree. This will be used by landlock, and BPF LSM. Suggested-by: Neil Brown Signed-off-by: Song Liu Reviewed-by: Jan Kara --- fs/namei.c | 95 +++++++++++++++++++++++++++++++++++-------- include/linux/namei.h | 2 + 2 files changed, 79 insertions(+), 18 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 4bb889fc980b..d0557c0b5cc8 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -2048,36 +2048,95 @@ static struct dentry *follow_dotdot_rcu(struct name= idata *nd) return nd->path.dentry; } =20 -static struct dentry *follow_dotdot(struct nameidata *nd) +/** + * __path_walk_parent - Find the parent of the given struct path + * @path - The struct path to start from + * @root - A struct path which serves as a boundary not to be crosses. + * - If @root is zero'ed, walk all the way to global root. + * @flags - Some LOOKUP_ flags. + * + * Find and return the dentry for the parent of the given path + * (mount/dentry). If the given path is the root of a mounted tree, it + * is first updated to the mount point on which that tree is mounted. + * + * If %LOOKUP_NO_XDEV is given, then *after* the path is updated to a new + * mount, the error EXDEV is returned. + * + * If no parent can be found, either because the tree is not mounted or + * because the @path matches the @root, then @path->dentry is returned + * unless @flags contains %LOOKUP_BENEATH, in which case -EXDEV is returne= d. + * + * Returns: either an ERR_PTR() or the chosen parent which will have had + * the refcount incremented. + */ +static struct dentry *__path_walk_parent(struct path *path, const struct p= ath *root, int flags) { - struct dentry *parent; - - if (path_equal(&nd->path, &nd->root)) + if (path_equal(path, root)) goto in_root; - if (unlikely(nd->path.dentry =3D=3D nd->path.mnt->mnt_root)) { - struct path path; + if (unlikely(path->dentry =3D=3D path->mnt->mnt_root)) { + struct path new_path; =20 - if (!choose_mountpoint(real_mount(nd->path.mnt), - &nd->root, &path)) + if (!choose_mountpoint(real_mount(path->mnt), + root, &new_path)) goto in_root; - path_put(&nd->path); - nd->path =3D path; - nd->inode =3D path.dentry->d_inode; - if (unlikely(nd->flags & LOOKUP_NO_XDEV)) + path_put(path); + *path =3D new_path; + if (unlikely(flags & LOOKUP_NO_XDEV)) return ERR_PTR(-EXDEV); } /* rare case of legitimate dget_parent()... */ - parent =3D dget_parent(nd->path.dentry); + return dget_parent(path->dentry); + +in_root: + if (unlikely(flags & LOOKUP_BENEATH)) + return ERR_PTR(-EXDEV); + return dget(path->dentry); +} + +/** + * path_walk_parent - Walk to the parent of path + * @path: input and output path. + * @root: root of the path walk, do not go beyond this root. If @root is + * zero'ed, walk all the way to real root. + * + * Given a path, find the parent path. Replace @path with the parent path. + * If we were already at the real root or a disconnected root, @path is + * not changed. + * + * Returns: + * 0 - if @path is updated to its parent. + * <0 - if @path is already the root (real root or @root). + */ +int path_walk_parent(struct path *path, const struct path *root) +{ + struct dentry *parent; + + parent =3D __path_walk_parent(path, root, LOOKUP_BENEATH); + + if (IS_ERR(parent)) + return PTR_ERR(parent); + + if (parent =3D=3D path->dentry) { + dput(parent); + return -ENOENT; + } + dput(path->dentry); + path->dentry =3D parent; + return 0; +} + +static struct dentry *follow_dotdot(struct nameidata *nd) +{ + struct dentry *parent =3D __path_walk_parent(&nd->path, &nd->root, nd->fl= ags); + + if (IS_ERR(parent)) + return parent; if (unlikely(!path_connected(nd->path.mnt, parent))) { dput(parent); return ERR_PTR(-ENOENT); } + nd->inode =3D nd->path.dentry->d_inode; return parent; - -in_root: - if (unlikely(nd->flags & LOOKUP_BENEATH)) - return ERR_PTR(-EXDEV); - return dget(nd->path.dentry); } =20 static const char *handle_dots(struct nameidata *nd, int type) diff --git a/include/linux/namei.h b/include/linux/namei.h index 5d085428e471..ca68fa4089e0 100644 --- a/include/linux/namei.h +++ b/include/linux/namei.h @@ -85,6 +85,8 @@ extern int follow_down_one(struct path *); extern int follow_down(struct path *path, unsigned int flags); extern int follow_up(struct path *); =20 +int path_walk_parent(struct path *path, const struct path *root); + extern struct dentry *lock_rename(struct dentry *, struct dentry *); extern struct dentry *lock_rename_child(struct dentry *, struct dentry *); extern void unlock_rename(struct dentry *, struct dentry *); --=20 2.47.1 From nobody Thu Oct 9 16:33:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BA54B28B7C8; Tue, 17 Jun 2025 06:11:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750140692; cv=none; b=g8MuOga/qDCIVivY+0IFH6vFmiSwEbKVpKE5iYg4waYt5G+aL8uSuyCn+Dc1vVhBHTbPh0l6DnNBJ6DHqTSbyXUbpN+VSnu18lQV6eZ6xZDS+0CKyU3cgd9phrAHGrC3GaKWlQenz2zx3XkIpKwUoIhJ2aUkyC+/ZVF/EHBo5h4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750140692; c=relaxed/simple; bh=WGXBCdIhrRQgnhx+v3v9M3OHAqARRicRAUBwy8RZfxc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GHOr8QeiMZP9S1POczrDrnBTZRdHuN76jhzOPczQHvd6xwjbdb+gGFj6iyeXzyRis5VMbanCGNnvzyKyv4JiigjkZJP8ZFlab+b4OUGFxUjpPLqDQZuwWwvIlI5+yk3FZani1V6wN1MDUKEp+5dayeZS+YOFJ7+z2/4m7V6rhyY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=h0SrVpkq; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="h0SrVpkq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 93D03C4CEE3; Tue, 17 Jun 2025 06:11:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750140692; bh=WGXBCdIhrRQgnhx+v3v9M3OHAqARRicRAUBwy8RZfxc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h0SrVpkqKMUohXQBrausf95FECjL9Jm8y/Tv44i/VhBTMs+8S+NyWCGjgVBn+dzo9 GdcbMqrgp2ExOMvFXY1imnfLrYH0AAw1IJoUWP0fqVK+pckO/wnybMLEqdr6wftKzr wkinVaQVhpDO9X+d2b+yT19RGYTIhyIVx6xygR/B7c+FyjUiNaX/HxxezbQZFO9Dw4 hiBPS+5JI6oNsfrymbp0P7C0FhRLhzZAxauiLPup7K5SkuzCg4J7wyGKBfldqcMaYA J2ES88cZgXv7HIVgaay9BokCF6D1HesYuJqDS9Wp5S14DOjtxKz/bQJbplipojj4Ym Ni0DFYUgF2lXg== From: Song Liu To: bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Cc: kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com, ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, kpsingh@kernel.org, mattbobrowski@google.com, m@maowtm.org, neil@brown.name, Song Liu Subject: [PATCH v5 bpf-next 2/5] landlock: Use path_walk_parent() Date: Mon, 16 Jun 2025 23:11:13 -0700 Message-ID: <20250617061116.3681325-3-song@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250617061116.3681325-1-song@kernel.org> References: <20250617061116.3681325-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use path_walk_parent() to walk a path up to its parent. No functional changes intended. Signed-off-by: Song Liu Reported-by: Al Viro --- security/landlock/fs.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/security/landlock/fs.c b/security/landlock/fs.c index 6fee7c20f64d..e26ab8c34dd4 100644 --- a/security/landlock/fs.c +++ b/security/landlock/fs.c @@ -837,8 +837,8 @@ static bool is_access_to_paths_allowed( * restriction. */ while (true) { - struct dentry *parent_dentry; const struct landlock_rule *rule; + struct path root =3D {}; =20 /* * If at least all accesses allowed on the destination are @@ -895,34 +895,20 @@ static bool is_access_to_paths_allowed( /* Stops when a rule from each layer grants access. */ if (allowed_parent1 && allowed_parent2) break; -jump_up: - if (walker_path.dentry =3D=3D walker_path.mnt->mnt_root) { - if (follow_up(&walker_path)) { - /* Ignores hidden mount points. */ - goto jump_up; - } else { - /* - * Stops at the real root. Denies access - * because not all layers have granted access. - */ - break; - } - } - if (unlikely(IS_ROOT(walker_path.dentry))) { + + if (unlikely(IS_ROOT(walker_path.dentry)) && + (walker_path.mnt->mnt_flags & MNT_INTERNAL)) { /* * Stops at disconnected root directories. Only allows * access to internal filesystems (e.g. nsfs, which is * reachable through /proc//ns/). */ - if (walker_path.mnt->mnt_flags & MNT_INTERNAL) { - allowed_parent1 =3D true; - allowed_parent2 =3D true; - } + allowed_parent1 =3D true; + allowed_parent2 =3D true; break; } - parent_dentry =3D dget_parent(walker_path.dentry); - dput(walker_path.dentry); - walker_path.dentry =3D parent_dentry; + if (path_walk_parent(&walker_path, &root)) + break; } path_put(&walker_path); =20 --=20 2.47.1 From nobody Thu Oct 9 16:33:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29A4528BABB; Tue, 17 Jun 2025 06:11:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750140698; cv=none; b=SkZaFS3TJoXnqjZDp07TTcyMZVXCUvAJPQAXA4cqA5+zXaCwLbj9+7yzRp47NzYQuIN+BJadkxxmSYBZsZJV7STXQ36JsiUTdkYTgNiBwQNN/ipfAzDajL7wopx9EH16TXsodGorK2wE/x7vEewkMhM/B9ZlhtsEaC8k9VItTkU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750140698; c=relaxed/simple; bh=xWQ8ZTj8fLqEhGkBO7EXf6izR+8Hq06Q6V8sGdcTNBY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WK59c6VJ4n6GtT2+Y6+nlAqN/yzRbZEulxRkLvNv60PzoPKwr6/tG+ve2aGRss/E1apXaVp21qHyJKzh2LLcixmVNykvvt+QDAcSpEChUzGMw74/bti564XOCHzKfJEk2IzQlOdA0LcDqNVuPj3Dn7ho475qARRJLVc3z3YTU18= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=enTK2ZDr; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="enTK2ZDr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 462CAC4CEE3; Tue, 17 Jun 2025 06:11:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750140698; bh=xWQ8ZTj8fLqEhGkBO7EXf6izR+8Hq06Q6V8sGdcTNBY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=enTK2ZDrFrzgkISgpLM/4mmBgB4+eZhatgYpJzX8b33DrffY5GPHtXEOGLoX41L/k SFs9NW9sQxVw3JO5B681zT4fiRwa8ICAFve36IIWJu6/YrH5A8HMNhKuInc3UAloyl UHnxuLvKwOiAO7V6/ipyxSRyKeOTw5JcfYwlSxuvCV5aIpQSprrU3kbNuUxO3TG6iW gKuFXqPcuWrr6P0uAm4CHvWflKqGb6C7yyI+yDYr3A71h8p1NI7REASG8OfK5FqTeQ cAYWoX0UKsR872nbbayHE3fnVsDJ8bz0l98Vrc+QsG0F7mLSBGFFyyLbQsbRQl5Hby Erfp8KEuWKcxA== From: Song Liu To: bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Cc: kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com, ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, kpsingh@kernel.org, mattbobrowski@google.com, m@maowtm.org, neil@brown.name, Song Liu Subject: [PATCH v5 bpf-next 3/5] bpf: Introduce path iterator Date: Mon, 16 Jun 2025 23:11:14 -0700 Message-ID: <20250617061116.3681325-4-song@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250617061116.3681325-1-song@kernel.org> References: <20250617061116.3681325-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a path iterator, which walks a struct path toward the root. This path iterator is based on path_walk_parent. A fixed zero'ed root is passed to path_walk_parent(). Therefore, unless the user terminates it earlier, the iterator will terminate at the real root. Signed-off-by: Song Liu Acked-by: Andrii Nakryiko --- fs/bpf_fs_kfuncs.c | 72 +++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 5 +++ 2 files changed, 77 insertions(+) diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c index 08412532db1b..888867678981 100644 --- a/fs/bpf_fs_kfuncs.c +++ b/fs/bpf_fs_kfuncs.c @@ -10,6 +10,7 @@ #include #include #include +#include #include =20 __bpf_kfunc_start_defs(); @@ -324,6 +325,74 @@ __bpf_kfunc int bpf_remove_dentry_xattr(struct dentry = *dentry, const char *name_ =20 __bpf_kfunc_end_defs(); =20 +/* open-coded path iterator */ +struct bpf_iter_path { + __u64 __opaque[2]; +} __aligned(8); + +struct bpf_iter_path_kern { + struct path path; +} __aligned(8); + +__bpf_kfunc_start_defs(); + +__bpf_kfunc int bpf_iter_path_new(struct bpf_iter_path *it, + struct path *start, + __u64 flags) +{ + struct bpf_iter_path_kern *kit =3D (void *)it; + + BUILD_BUG_ON(sizeof(*kit) > sizeof(*it)); + BUILD_BUG_ON(__alignof__(*kit) !=3D __alignof__(*it)); + + if (flags) { + /* + * _destroy() is still called when _new() fails. Zero + * kit->path so that it be passed to path_put() safely. + * Note: path_put() is no-op for zero'ed path. + */ + memset(&kit->path, 0, sizeof(struct path)); + return -EINVAL; + } + + kit->path =3D *start; + path_get(&kit->path); + + return 0; +} + +__bpf_kfunc struct path *bpf_iter_path_next(struct bpf_iter_path *it) +{ + struct bpf_iter_path_kern *kit =3D (void *)it; + struct path root =3D {}; + + /* + * "root" is zero'ed. Therefore, unless the loop is explicitly + * terminated, bpf_iter_path_next() will continue looping until + * we've reached the global root of the VFS. + * + * If a root of walk is needed, the user can check "path" against + * that root on each iteration. + */ + if (path_walk_parent(&kit->path, &root)) + return NULL; + + return &kit->path; +} + +__bpf_kfunc void bpf_iter_path_destroy(struct bpf_iter_path *it) +{ + struct bpf_iter_path_kern *kit =3D (void *)it; + + /* + * kit->path might be zero'ed, but this is OK because path_put() + * is no-op for zero'ed struct path + */ + path_put(&kit->path); +} + +__bpf_kfunc_end_defs(); + BTF_KFUNCS_START(bpf_fs_kfunc_set_ids) BTF_ID_FLAGS(func, bpf_get_task_exe_file, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) @@ -333,6 +402,9 @@ BTF_ID_FLAGS(func, bpf_get_dentry_xattr, KF_SLEEPABLE |= KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_get_file_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_set_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_remove_dentry_xattr, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_iter_path_new, KF_ITER_NEW | KF_TRUSTED_ARGS | KF_S= LEEPABLE) +BTF_ID_FLAGS(func, bpf_iter_path_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLE= EPABLE) +BTF_ID_FLAGS(func, bpf_iter_path_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) BTF_KFUNCS_END(bpf_fs_kfunc_set_ids) =20 static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 279a64933262..b495c3cc4095 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -7101,6 +7101,10 @@ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket) { struct sock *sk; }; =20 +BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path) { + struct dentry *dentry; +}; + static bool type_is_rcu(struct bpf_verifier_env *env, struct bpf_reg_state *reg, const char *field_name, u32 btf_id) @@ -7141,6 +7145,7 @@ static bool type_is_trusted_or_null(struct bpf_verifi= er_env *env, const char *field_name, u32 btf_id) { BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct socket)); + BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct path)); =20 return btf_nested_type_is_trusted(&env->log, reg, field_name, btf_id, "__safe_trusted_or_null"); --=20 2.47.1 From nobody Thu Oct 9 16:33:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC10828BAB3; Tue, 17 Jun 2025 06:11:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750140704; cv=none; b=Qlzia7KEsULgz6UE5jAJpOEq5GQBfqjf1Xp4xSDpHKdKz6KljrjLli9DZqZEIQxVgj07Pfu3HfQAosRFLUK4E/0rABXU5G0Vwd90+DwrT0pJpizF73wYfowk9xcWQUqBeOjcBJFy+Rg+dGmsnRSUIz3TzdyTQQ1RLqBA0g75Wlw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750140704; c=relaxed/simple; bh=9F10qO9Msdgz89bnpZRuwO8YiDXQDw7GfTMJBW3OmZE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PSF2sjB2qCuhQcIcWKGms4vd1RLAO8Pe5zZLwonkKbSHdc586PQYgVjd2VDpHN9oyUJAFVoPnDfc9z7SKGR0A5Pe3Wifu6tzuSeDBuO6D7BtwtDI3LxhrTTyRc7p0SJM/0rKEuWCVRAXoEq+Of+P1p5qF0NBUZrm3DK4MLgklAo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Mn6udA3y; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Mn6udA3y" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1571DC4CEE3; Tue, 17 Jun 2025 06:11:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750140703; bh=9F10qO9Msdgz89bnpZRuwO8YiDXQDw7GfTMJBW3OmZE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mn6udA3yB79z8yC8ygoRrxMRO8mWkizhYEKHHaL8t+nI1J7ZKscNDnnAB93/9mN+b OZxD6CUDlPTnQy5Unjl9+5Jyx0RQU4AilFBBleS3Nn4mT0kYKpZXyCWfAIz4GhK4Rq 3i1JVJei50XA/OwfM6UxXcG2sZKuKn1E2ImoWduSZxXXaK03vIhZDx7kK8v38yoqUs fXD0RdbH4LVWSpVxBRC2BmAol5Ab+Toj5eZxnJyvn77/IXayMj1L34QxKjniJn/zdw 51w/DnVPBrC/nwJwzjmhyR2IxuUPBciW/KGs5GGMnQKindt4bJiGE/sRhAO9SvL32y Vrep7EVsxfDMg== From: Song Liu To: bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Cc: kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com, ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, kpsingh@kernel.org, mattbobrowski@google.com, m@maowtm.org, neil@brown.name, Song Liu Subject: [PATCH v5 bpf-next 4/5] selftests/bpf: Add tests for bpf path iterator Date: Mon, 16 Jun 2025 23:11:15 -0700 Message-ID: <20250617061116.3681325-5-song@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250617061116.3681325-1-song@kernel.org> References: <20250617061116.3681325-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add tests for bpf path iterator, including test cases similar to real workload (call bpf_path_d_path and bpf_get_dentry_xattr), and test cases where the verifier rejects invalid use of the iterator. Signed-off-by: Song Liu --- .../testing/selftests/bpf/bpf_experimental.h | 6 + .../selftests/bpf/prog_tests/path_iter.c | 12 ++ tools/testing/selftests/bpf/progs/path_iter.c | 145 ++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/path_iter.c create mode 100644 tools/testing/selftests/bpf/progs/path_iter.c diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing= /selftests/bpf/bpf_experimental.h index 5e512a1d09d1..cbb759b473df 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -596,4 +596,10 @@ extern int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf = *it) __weak __ksym; extern struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it) __= weak __ksym; extern void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it) __weak __k= sym; =20 +struct bpf_iter_path; +extern int bpf_iter_path_new(struct bpf_iter_path *it, struct path *start, + __u64 flags) __weak __ksym; +extern struct path *bpf_iter_path_next(struct bpf_iter_path *it) __weak __= ksym; +extern void bpf_iter_path_destroy(struct bpf_iter_path *it) __weak __ksym; + #endif diff --git a/tools/testing/selftests/bpf/prog_tests/path_iter.c b/tools/tes= ting/selftests/bpf/prog_tests/path_iter.c new file mode 100644 index 000000000000..3c99c24fbd96 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/path_iter.c @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include +#include "path_iter.skel.h" + +void test_path_iter(void) +{ + RUN_TESTS(path_iter); +} diff --git a/tools/testing/selftests/bpf/progs/path_iter.c b/tools/testing/= selftests/bpf/progs/path_iter.c new file mode 100644 index 000000000000..74d0f4e19ffa --- /dev/null +++ b/tools/testing/selftests/bpf/progs/path_iter.c @@ -0,0 +1,145 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include "vmlinux.h" +#include +#include +#include "bpf_misc.h" +#include "bpf_experimental.h" + +char _license[] SEC("license") =3D "GPL"; + +char path_name[256]; +char xattr_val[64]; + +static __always_inline void access_path_dentry(struct path *p) +{ + struct bpf_dynptr ptr; + struct dentry *dentry; + + if (!p) + return; + + bpf_dynptr_from_mem(xattr_val, sizeof(xattr_val), 0, &ptr); + bpf_path_d_path(p, path_name, sizeof(path_name)); + + dentry =3D p->dentry; + if (dentry) + bpf_get_dentry_xattr(dentry, "user.xattr", &ptr); +} + +SEC("lsm.s/file_open") +__success +int BPF_PROG(open_code, struct file *f) +{ + struct bpf_iter_path path_it; + struct path *p; + int ret; + + ret =3D bpf_iter_path_new(&path_it, &f->f_path, 0); + if (ret) { + bpf_iter_path_destroy(&path_it); + return 0; + } + + p =3D bpf_iter_path_next(&path_it); + access_path_dentry(p); + bpf_iter_path_destroy(&path_it); + + return 0; +} + +SEC("lsm.s/file_open") +__success +int BPF_PROG(for_each, struct file *f) +{ + struct path *p; + + bpf_for_each(path, p, &f->f_path, 0) + access_path_dentry(p); + + return 0; +} + +SEC("lsm.s/file_open") +__failure __msg("Unreleased reference") +int BPF_PROG(missing_destroy, struct file *f) +{ + struct bpf_iter_path path_it; + + bpf_iter_path_new(&path_it, &f->f_path, 0); + + return 0; +} + +SEC("lsm.s/file_open") +__failure __msg("expected an initialized iter_path") +int BPF_PROG(missing_new, struct file *f) +{ + struct bpf_iter_path path_it; + + bpf_iter_path_destroy(&path_it); + return 0; +} + +SEC("lsm.s/file_open") +__failure __msg("expected uninitialized iter_path") +int BPF_PROG(new_twice, struct file *f) +{ + struct bpf_iter_path path_it; + + bpf_iter_path_new(&path_it, &f->f_path, 0); + bpf_iter_path_new(&path_it, &f->f_path, 0); + bpf_iter_path_destroy(&path_it); + return 0; +} + +SEC("lsm.s/file_open") +__failure __msg("expected an initialized iter_path") +int BPF_PROG(destroy_twice, struct file *f) +{ + struct bpf_iter_path path_it; + + bpf_iter_path_new(&path_it, &f->f_path, 0); + bpf_iter_path_destroy(&path_it); + bpf_iter_path_destroy(&path_it); + return 0; +} + +SEC("lsm.s/file_open") +__success +int BPF_PROG(reuse_path_iter, struct file *f) +{ + struct bpf_iter_path path_it; + + bpf_iter_path_new(&path_it, &f->f_path, 0); + bpf_iter_path_destroy(&path_it); + bpf_iter_path_new(&path_it, &f->f_path, 0); + bpf_iter_path_destroy(&path_it); + return 0; +} + +SEC("lsm.s/file_open") +__failure __msg("invalid read from stack off") +int BPF_PROG(invalid_read_path_iter, struct file *f) +{ + struct bpf_iter_path path_it; + struct bpf_iter_path path_it_2; + + + bpf_iter_path_new(&path_it, &f->f_path, 0); + path_it_2 =3D path_it; + bpf_iter_path_destroy(&path_it_2); + return 0; +} + +SEC("lsm.s/sb_alloc_security") +__failure __msg("must be referenced or trusted") +int BPF_PROG(untrusted_path, struct super_block *sb) +{ + struct bpf_iter_path path_it; + + bpf_iter_path_new(&path_it, &sb->s_bdev_file->f_path, 0); + bpf_iter_path_destroy(&path_it); + return 0; +} --=20 2.47.1 From nobody Thu Oct 9 16:33:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EDDD428BAB3; Tue, 17 Jun 2025 06:11:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750140710; cv=none; b=l1HD9b4paqoKUmg8HD+5Es2TYbT+4afN2TC6arLwSgcYTvT1bSgr4N4El6x7TgnpUQX9f4vyo5QjHHjJCgaeS9p8hWfnKBlqPa1d2Jyo/X4uE9YiCJ5cGciK23yDnfK5+la8WacpZJIfekF2ocMNg5S6+iNZFbCJfIq7mlAyxWs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750140710; c=relaxed/simple; bh=vl/pVqr2RXH2Xn1E+8wtiXLeu/mTXlC3sdSyiyVMmIA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CaLiLa63//cgFKCdTwppunwEr1R5UDxWTXenj0YYAhEsD+8qWNO8liAjoXaHItXF6lY/2ADGmrMYyDB0sm/5S9vDXZAbzjT6GVCz9Fr6tQ2r6m3OClYg/wBook2MCCSC2tSdIKvrykJTLUEcEpVC7oc7G3p8ugB8Q9Bc9bHcCL8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lJaDTvWB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lJaDTvWB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2633C4CEE3; Tue, 17 Jun 2025 06:11:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750140709; bh=vl/pVqr2RXH2Xn1E+8wtiXLeu/mTXlC3sdSyiyVMmIA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lJaDTvWBuUu7uzNHqpaHCABJAyUUcT/hgqiGsloEdR4krrPfARRCesxl1qmnuzR7+ djpFLuIP/ceReVkyVwNAHIolySR4K201J4WqAXJ5wtoa0+F76Na87eAu9XKs1CFHbN 6tyeTOr07rHfCwG22bBldkcnVxDUFro7W0DVPjXplnVI9KGfFnLG+e/ll+7DWzBUpP Ym6dZBkOmRbFFn3+cfA11gN7leD3JECshsNL29M9VBGfaElzHNuWlckb5ViRv2JsBJ aOamur7zHgX3+EUkhUWAjM+9zHfz4bjAVEtGU/KvSJkahGAEXYVghOXo0cArtJe+7j Avhh2rqn+0lXA== From: Song Liu To: bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Cc: kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com, ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, kpsingh@kernel.org, mattbobrowski@google.com, m@maowtm.org, neil@brown.name, Song Liu Subject: [PATCH v5 bpf-next 5/5] selftests/bpf: Path walk test Date: Mon, 16 Jun 2025 23:11:16 -0700 Message-ID: <20250617061116.3681325-6-song@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250617061116.3681325-1-song@kernel.org> References: <20250617061116.3681325-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an end-to-end test with path_iter on security hook file_open. A test file is created in folder /tmp/test_progs_path_iter/folder. On file_open, walk file->f_path up to its parent and grand parent, and test bpf_get_dentry_xattr and bpf_path_d_path on the folders. Signed-off-by: Song Liu --- .../selftests/bpf/prog_tests/path_iter.c | 99 +++++++++++++++++++ tools/testing/selftests/bpf/progs/path_walk.c | 59 +++++++++++ 2 files changed, 158 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/path_walk.c diff --git a/tools/testing/selftests/bpf/prog_tests/path_iter.c b/tools/tes= ting/selftests/bpf/prog_tests/path_iter.c index 3c99c24fbd96..b9772026fbf7 100644 --- a/tools/testing/selftests/bpf/prog_tests/path_iter.c +++ b/tools/testing/selftests/bpf/prog_tests/path_iter.c @@ -2,11 +2,110 @@ /* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ =20 #include +#include #include #include +#include +#include + #include "path_iter.skel.h" +#include "path_walk.skel.h" + +static const char grand_parent_path[] =3D "/tmp/test_progs_path_iter"; +static const char parent_path[] =3D "/tmp/test_progs_path_iter/folder"; +static const char file_path[] =3D "/tmp/test_progs_path_iter/folder/file"; +static const char xattr_name[] =3D "user.bpf.selftests"; +static const char xattr_value[] =3D "selftest_path_iter"; + +static void cleanup_files(void) +{ + remove(file_path); + rmdir(parent_path); + rmdir(grand_parent_path); +} + +static int setup_files_and_xattrs(void) +{ + int ret =3D -1; + + /* create test folders */ + if (mkdir(grand_parent_path, 0755)) + goto error; + if (mkdir(parent_path, 0755)) + goto error; + + /* setxattr for test folders */ + ret =3D setxattr(grand_parent_path, xattr_name, + xattr_value, sizeof(xattr_value), 0); + if (ret < 0) { + /* return errno, so that we can handle EOPNOTSUPP in the caller */ + ret =3D errno; + goto error; + } + ret =3D setxattr(parent_path, xattr_name, + xattr_value, sizeof(xattr_value), 0); + if (ret < 0) { + /* return errno, so that we can handle EOPNOTSUPP in the caller */ + ret =3D errno; + goto error; + } + + return 0; +error: + cleanup_files(); + return ret; +} + +static void test_path_walk(void) +{ + struct path_walk *skel =3D NULL; + int file_fd; + int err; + + err =3D setup_files_and_xattrs(); + if (err =3D=3D EOPNOTSUPP) { + printf("%s:SKIP:local fs doesn't support xattr (%d)\n" + "To run this test, make sure /tmp filesystem supports xattr.\n", + __func__, errno); + test__skip(); + return; + } + + if (!ASSERT_OK(err, "setup_file")) + return; + + skel =3D path_walk__open_and_load(); + if (!ASSERT_OK_PTR(skel, "path_walk__open_and_load")) + goto cleanup; + + skel->bss->monitored_pid =3D getpid(); + if (!ASSERT_OK(path_walk__attach(skel), "path_walk__attach")) + goto cleanup; + + file_fd =3D open(file_path, O_CREAT); + if (!ASSERT_OK_FD(file_fd, "open_file")) + goto cleanup; + close(file_fd); + + ASSERT_OK(strncmp(skel->bss->parent_xattr_buf, xattr_value, strlen(xattr_= value)), + "parent_xattr"); + ASSERT_OK(strncmp(skel->bss->grand_parent_xattr_buf, xattr_value, strlen(= xattr_value)), + "grand_parent_xattr"); + + ASSERT_OK(strncmp(skel->bss->parent_path_buf, parent_path, strlen(parent_= path)), + "parent_d_path"); + ASSERT_OK(strncmp(skel->bss->grand_parent_path_buf, grand_parent_path, + strlen(grand_parent_path)), + "grand_parent_d_path"); + +cleanup: + path_walk__destroy(skel); + cleanup_files(); +} =20 void test_path_iter(void) { RUN_TESTS(path_iter); + if (test__start_subtest("path_walk_example")) + test_path_walk(); } diff --git a/tools/testing/selftests/bpf/progs/path_walk.c b/tools/testing/= selftests/bpf/progs/path_walk.c new file mode 100644 index 000000000000..1e1ae82b47a2 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/path_walk.c @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include "vmlinux.h" +#include +#include +#include +#include +#include "bpf_kfuncs.h" +#include "bpf_misc.h" + +char _license[] SEC("license") =3D "GPL"; + +__u32 monitored_pid; + +#define BUF_SIZE 1024 +char parent_path_buf[BUF_SIZE] =3D {}; +char parent_xattr_buf[BUF_SIZE] =3D {}; +char grand_parent_path_buf[BUF_SIZE] =3D {}; +char grand_parent_xattr_buf[BUF_SIZE] =3D {}; + +static __always_inline void d_path_and_read_xattr(struct path *p, char *pa= th, char *xattr) +{ + struct bpf_dynptr ptr; + struct dentry *dentry; + + if (!p) + return; + bpf_path_d_path(p, path, BUF_SIZE); + bpf_dynptr_from_mem(xattr, BUF_SIZE, 0, &ptr); + dentry =3D p->dentry; + if (dentry) + bpf_get_dentry_xattr(dentry, "user.bpf.selftests", &ptr); +} + +SEC("lsm.s/file_open") +int BPF_PROG(test_file_open, struct file *f) +{ + __u32 pid =3D bpf_get_current_pid_tgid() >> 32; + struct bpf_iter_path path_it; + struct path *p; + + if (pid !=3D monitored_pid) + return 0; + + bpf_iter_path_new(&path_it, &f->f_path, 0); + + /* Get d_path and xattr for the parent directory */ + p =3D bpf_iter_path_next(&path_it); + d_path_and_read_xattr(p, parent_path_buf, parent_xattr_buf); + + /* Get d_path and xattr for the grand parent directory */ + p =3D bpf_iter_path_next(&path_it); + d_path_and_read_xattr(p, grand_parent_path_buf, grand_parent_xattr_buf); + + bpf_iter_path_destroy(&path_it); + + return 0; +} --=20 2.47.1