From nobody Fri Dec 19 07:31:42 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 52D381A76BC; Fri, 6 Jun 2025 21:30:29 +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=1749245430; cv=none; b=phOIM4sgvFYClzqYOk1ogOpeDmp08Gj9S4QSdXOnzMMsjSRXLpcmEgbdX3GRleSUoagm4W4HJspsjy2lBJ2G3sOTgaW8Re9LzlBlhAY439q8LSGp6DRLFb3UJQbPipo5cXC2XOY+BkCucxYIh2eD3yzpbyhZY6zZKg+qjCkQGk8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749245430; c=relaxed/simple; bh=/615fZRRWGLCiRUGOTiVn04Y0AU0vnVl6B2PImYgEn0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dYGey/1h6QqHX1qjs3QI9mNm8nRbksKvBR7AbQrXkGwEUj+KjWRFs8NuEApy2cIK6zqk4/o7eTtSVExdSqSIxeV6sTKKMZFZ0MfZKUihUJNuiP90Za2bYnLvlAGWUOnzLYfXaL2AOJ2MZknCGfi4oxZAzkScsNW7Ubr5oghgZXo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lwCBZRmN; 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="lwCBZRmN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 052ABC4CEEB; Fri, 6 Jun 2025 21:30:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1749245429; bh=/615fZRRWGLCiRUGOTiVn04Y0AU0vnVl6B2PImYgEn0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lwCBZRmN3UWQL2YwbltywLQmOZF1AC99bHKIYtoHcOzZkdimuTxuRK7SOoqXjHc8E V84kNq4s4U7u5bePuvWUCjWzIbx3yBk9+qGYy2UKsuVN4EErzONuP7RPwPVxtJxyOf pZzmbdV8Y3fFE5iC7r4HOQc9CPgY1KYeQiSr5O7LilyEby6Ldpd3PkcgmX0Ird5DzL kZg6snEY3yXSUf8dZrkPyAyi2aGOY01dMzfYXz/DCOr4Asc2GqqrCPwc0oZUYtyVAW DSAUeTy9UPBNi6BQl7uuUjDDYW1fbkbb8wPcu4e6gubUJOp73Rnq13u9xU/UokBHQS ROKkktd/SxdOw== 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, amir73il@gmail.com, repnop@google.com, jlayton@kernel.org, josef@toxicpanda.com, mic@digikod.net, gnoack@google.com, m@maowtm.org, Song Liu Subject: [PATCH v3 bpf-next 1/5] namei: Introduce new helper function path_walk_parent() Date: Fri, 6 Jun 2025 14:30:11 -0700 Message-ID: <20250606213015.255134-2-song@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250606213015.255134-1-song@kernel.org> References: <20250606213015.255134-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. Signed-off-by: Song Liu --- fs/namei.c | 51 +++++++++++++++++++++++++++++++++++++++++++ include/linux/namei.h | 2 ++ 2 files changed, 53 insertions(+) diff --git a/fs/namei.c b/fs/namei.c index 4bb889fc980b..f02183e9c073 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -1424,6 +1424,57 @@ static bool choose_mountpoint(struct mount *m, const= struct path *root, return found; } =20 +/** + * 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. + * + * The logic of path_walk_parent() is similar to follow_dotdot(), except + * that path_walk_parent() will continue walking for !path_connected case. + * This effectively means we are walking from disconnected bind mount to + * the original mount. If this behavior is not desired, the caller can add + * a check like: + * + * if (path_walk_parent(&path) && !path_connected(path.mnt, path.dentry) + * // continue walking + * else + * // stop walking + * + * Returns: + * true - if @path is updated to its parent. + * false - if @path is already the root (real root or @root). + */ +bool path_walk_parent(struct path *path, const struct path *root) +{ + struct dentry *parent; + + if (path_equal(path, root)) + return false; + + if (unlikely(path->dentry =3D=3D path->mnt->mnt_root)) { + struct path p; + + if (!choose_mountpoint(real_mount(path->mnt), root, &p)) + return false; + path_put(path); + *path =3D p; + } + + if (unlikely(IS_ROOT(path->dentry))) + return false; + + parent =3D dget_parent(path->dentry); + dput(path->dentry); + path->dentry =3D parent; + return true; +} +EXPORT_SYMBOL_GPL(path_walk_parent); + /* * Perform an automount * - return -EISDIR to tell follow_managed() to stop and return the path we diff --git a/include/linux/namei.h b/include/linux/namei.h index 5d085428e471..cba5373ecf86 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 +bool 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 Fri Dec 19 07:31:42 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 D534021B9E4; Fri, 6 Jun 2025 21:30:35 +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=1749245436; cv=none; b=OXVO9SckI4d/pQmOYsKq4MjAiqq2aJcRPJMk90Xlp9QBmLMBWt3akJFYH6Z0mp9zeviojrG0dPitMaEpcxvyTN0wcwCkxjDMBCXJ0QGDVU3FDd6HZ3AUhCvK2atpMpmJs2ulet0GJgyvWw3BTf6Xkz08cg4Rl8Sm1XJst8EufSc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749245436; c=relaxed/simple; bh=r6SeHhJh7cN3udLApUOE4uIsJst1l3j9wVKO2KszBrc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=S4vOw52m1DY84GgQUN/8RxCWJVCq0z8biflXVZFy8sZHCNnLfdOsKpJTWJoe3H0nnsRel+Pdy5CkOigCpRnzBmPYvwfGze6V88nbiJ78/8u6m+4+zb3W+S1GUJATIjPwtZdmByCmEgREnriWWoZkQFqwaU4lbbRSPt8I9cDvZIA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NT3xtTsu; 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="NT3xtTsu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A70A4C4CEEB; Fri, 6 Jun 2025 21:30:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1749245435; bh=r6SeHhJh7cN3udLApUOE4uIsJst1l3j9wVKO2KszBrc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NT3xtTsuEDnByf8wiTbYH9aJ1CMbbeibHuFjtuc+QGeyLHUNx1htAiy9AZ1mWdfzn PhqJ4kx/TrZ9ufpcJ9qAyzsEKoVVDac+QfW9C0ROzqdaDSQBgk0uG4sJBPqKZq1MG5 fiAhp1Ev2JLMdFet8yjesKmr3kvUpBOumn0oi5MJygVHmlKz5/JWjTpjJM0O+L6QmN vdocnkD0nJFlYI7JhWFxmYTWF6mUv8QkZJ/24xjEzT6KckJDTJYtiM34q5XHo6a67W hhFLVnYPBjsPsS6T1K8DWCXdwWrAt74xMBVBKb+U1R1fmNZX0cnEsH8v6ol57p1A8Q QUDa+FdX+L8Nw== 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, amir73il@gmail.com, repnop@google.com, jlayton@kernel.org, josef@toxicpanda.com, mic@digikod.net, gnoack@google.com, m@maowtm.org, Song Liu Subject: [PATCH v3 bpf-next 2/5] landlock: Use path_walk_parent() Date: Fri, 6 Jun 2025 14:30:12 -0700 Message-ID: <20250606213015.255134-3-song@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250606213015.255134-1-song@kernel.org> References: <20250606213015.255134-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 --- security/landlock/fs.c | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/security/landlock/fs.c b/security/landlock/fs.c index 6fee7c20f64d..3adac544dc9e 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,23 @@ 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 (path_walk_parent(&walker_path, &root)) + continue; + if (unlikely(IS_ROOT(walker_path.dentry))) { /* - * Stops at disconnected root directories. Only allows - * access to internal filesystems (e.g. nsfs, which is - * reachable through /proc//ns/). + * Stops at disconnected or real 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; } - break; } - parent_dentry =3D dget_parent(walker_path.dentry); - dput(walker_path.dentry); - walker_path.dentry =3D parent_dentry; + break; } path_put(&walker_path); =20 --=20 2.47.1 From nobody Fri Dec 19 07:31:42 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 7C471238C1E; Fri, 6 Jun 2025 21:30:41 +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=1749245441; cv=none; b=kCA+uPkcWtEGBTi3U4E+DLdb5uNw0cZ++6lzPEUzCP+JLCOF57mnIHRghFp07jBZFM8WegURf4+fi8AfQbHPYuDI9Z68sUwHuZyFBPfSv1U26/15bbAF7X2k0P2GIJwazQV5+QVtEP9jijVy599tmLx2y30DR3rMi9Ho1nRSi5M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749245441; c=relaxed/simple; bh=OnL2EfwsdgVdb5c1ai8U7ZmDoCqhqPrRkXhX1QYpDSQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jyDOXn3VZubKDosoV/NXCP0P9aKhzKbdK2xXBrKPm634/R8kykEhnkqqBjyL6HQkBYUbXfmgqLPO1AgsydF3ZcZGcYYvBrOFAefLxLePiwx4XQyhtednVAbDAiuqUd4fiqkJwjiLo/F2wmdwP+mrk8eCrRB3ZXJdUcbZbC3SYDg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j7OPb0wZ; 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="j7OPb0wZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F62AC4CEEB; Fri, 6 Jun 2025 21:30:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1749245441; bh=OnL2EfwsdgVdb5c1ai8U7ZmDoCqhqPrRkXhX1QYpDSQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=j7OPb0wZ/rRJW16/XiQile83oAX++PfmsP6B625RkpSWMjqdq5vp77jQo9B1FABcB ESooZg02+7m1jTgj6eL0e8g2fDF7cSncBsMpLhnjatKmLFhJzfk88+8sJySrpkVmEX x6GIzpGhZy8bV3qb10SIcszVVEubUjynKhBU2rHHPUW3qiO5LfVjL73sr9jjVbHhuw d+kS6o5nL/LTa1XCDSkqfX6nhzbfZdleiHic+5eOL7iMR61AYj8pD4euqw/nYCMFnG m4dLkoHANLij5TR+2KwukoIwTQdv+q+DvjwyaaqjnV0uy7WrgxY2gVoiQwJPXZoDFW SlAuEJpcSIvLw== 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, amir73il@gmail.com, repnop@google.com, jlayton@kernel.org, josef@toxicpanda.com, mic@digikod.net, gnoack@google.com, m@maowtm.org, Song Liu Subject: [PATCH v3 bpf-next 3/5] bpf: Introduce path iterator Date: Fri, 6 Jun 2025 14:30:13 -0700 Message-ID: <20250606213015.255134-4-song@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250606213015.255134-1-song@kernel.org> References: <20250606213015.255134-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 --- fs/bpf_fs_kfuncs.c | 73 +++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 5 +++ 2 files changed, 78 insertions(+) diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c index 08412532db1b..8c618154df0a 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,75 @@ __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, but keep valid kit->path. _destroy() will + * always path_put(&kit->path). + */ + 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; + + 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 +403,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 e31f6b0ccb30..2c187c05c06f 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -7036,6 +7036,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) @@ -7076,6 +7080,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 Fri Dec 19 07:31:42 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 21CEB23F404; Fri, 6 Jun 2025 21:30:46 +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=1749245447; cv=none; b=EmlwMMxTmBitZWpWjUhK0MDv+hHQA1bS5GlaBZOVyyC6FXPkgW7rXRky2O8ZLx+mXK3zjDcehpPBodi24B8fk7LehDUEYtoaWahEO6R7QB5GgW09BmRi+xTkyUI09YEtaMid4YnoyuwNyKulx0BdpldB3THDDY7SxUE504GtWZI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749245447; c=relaxed/simple; bh=9F10qO9Msdgz89bnpZRuwO8YiDXQDw7GfTMJBW3OmZE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tD48nbOrRgK1t1uZprrIUJJ+CNzDJSShmIIj0ToR6+M7VY0wy3kKHbvOdf8htzoleEvhV5LRvPYohxYlpLiL1/F+EoGc6Uk3RkwIHJzQFdly4veL/CWmqf209JqvHjLPcdnmhAnnUbr6oknNUZw3bvrmpb0S9IbhtjhY+g4rHA8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YNg4ExAe; 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="YNg4ExAe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DCC53C4CEEB; Fri, 6 Jun 2025 21:30:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1749245446; bh=9F10qO9Msdgz89bnpZRuwO8YiDXQDw7GfTMJBW3OmZE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YNg4ExAeCXGc+bn0OHnPisPTpjL2XzeFSHdHU4GlYFORVNucTzqCXTySyPvwnigzX U/XIhyISmy5HUUzN2pNqKuiQFC1dT+RIv7MFDCS1SnKyPMBCPP+Lj1FhxRBHJCwjZY QVRSDg9QGqdAf+lI4nt4BrNKzL7KTRn0XpiTS+ZZPH7gUilUTG0FfC6QkW6yg855hC OyxK09LkWQCreJDHgmvLZRlJc6ihXv7CPls1XvYLNhyTOxFFH1gRdp/IjPSfUYn5QJ VaZJI/qAuxQnQdp3pwF945oi8tLInH7EgKY1Jg5VGILiue5VxYUrNUNr7JO9s9knzV X1WIrsdeemmPQ== 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, amir73il@gmail.com, repnop@google.com, jlayton@kernel.org, josef@toxicpanda.com, mic@digikod.net, gnoack@google.com, m@maowtm.org, Song Liu Subject: [PATCH v3 bpf-next 4/5] selftests/bpf: Add tests for bpf path iterator Date: Fri, 6 Jun 2025 14:30:14 -0700 Message-ID: <20250606213015.255134-5-song@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250606213015.255134-1-song@kernel.org> References: <20250606213015.255134-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 Fri Dec 19 07:31:42 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 B999B241136; Fri, 6 Jun 2025 21:30:52 +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=1749245452; cv=none; b=d8PsHORPLQtBF3S3Nslqb9FVQIQVHzosfki11DPx2QRn813u/jF6UA0PjAfPaLvjj7C5l7EQ23wUsn4nBhcw6Jj3qlEQdBXMoxEOGeD5l2qKAYe+uRcPFtWZLBWpVtvdlZwY9f9T6BiBWe+9mVbcmqv2/nWQKe0k77aR0HxsHCE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749245452; c=relaxed/simple; bh=vl/pVqr2RXH2Xn1E+8wtiXLeu/mTXlC3sdSyiyVMmIA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Lv/FXN5cUDTeYSj4sBgdUgv8rY4f1lBn++oBAWS+XdAgtJT1xtmmJwQdJayWie2XHw0MgsVnKkvzcMhDnZNlETQgNmC9O0CztTMR3fMTUXkdCZ3H9+uHd4QMG88JNJiauMFMk2liIpdCvg/akHpNDzQzWmW4xYWX1Rky7ck04VA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EEzWkcOy; 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="EEzWkcOy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 70B3CC4CEEF; Fri, 6 Jun 2025 21:30:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1749245452; bh=vl/pVqr2RXH2Xn1E+8wtiXLeu/mTXlC3sdSyiyVMmIA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EEzWkcOyrqoaWqtG1eRTxvfwoQcINpoRi36EuVXRcKvYvhxhbTytJeZmTjE7Pg2rd DjYGM45FK5kDqJeFQXxdxMjw/AfNsxhPHmtd3RDnODTMSn6YovlMmS1Z3HLfZ8kpQ3 Cv4IxEdLaFgrQAg3vCkm2Uo0n3oCLRE32G/48XIQMP/a9VhEK5LNUfAWKmJoPzpYDN znpJOwgECxmY4qlSGPndywZOl8qzk73sAp+Ynu0/y9koJ9ddF7QRYCSNobzrLZE16o ftLkNL2gB6pWxkzNaNYSDc4olFCIvOIfBGzBDgAnwyJEmREBTRfh10rWtxsfEUvBwe FMgFRgRmdKwsg== 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, amir73il@gmail.com, repnop@google.com, jlayton@kernel.org, josef@toxicpanda.com, mic@digikod.net, gnoack@google.com, m@maowtm.org, Song Liu Subject: [PATCH v3 bpf-next 5/5] selftests/bpf: Path walk test Date: Fri, 6 Jun 2025 14:30:15 -0700 Message-ID: <20250606213015.255134-6-song@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250606213015.255134-1-song@kernel.org> References: <20250606213015.255134-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