[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAPhsuW5G0Th+9dRSmxDjo5E7CxV1E9N8AiKjw3cKyEhOBVWJFw@mail.gmail.com>
Date: Tue, 10 Jun 2025 10:26:13 -0700
From: Song Liu <song@...nel.org>
To: Mickaël Salaün <mic@...ikod.net>
Cc: bpf@...r.kernel.org, linux-fsdevel@...r.kernel.org,
linux-kernel@...r.kernel.org, linux-security-module@...r.kernel.org,
kernel-team@...a.com, andrii@...nel.org, eddyz87@...il.com, ast@...nel.org,
daniel@...earbox.net, martin.lau@...ux.dev, viro@...iv.linux.org.uk,
brauner@...nel.org, jack@...e.cz, kpsingh@...nel.org,
mattbobrowski@...gle.com, amir73il@...il.com, repnop@...gle.com,
jlayton@...nel.org, josef@...icpanda.com, gnoack@...gle.com, m@...wtm.org
Subject: Re: [PATCH v3 bpf-next 1/5] namei: Introduce new helper function path_walk_parent()
On Tue, Jun 10, 2025 at 10:19 AM Mickaël Salaün <mic@...ikod.net> wrote:
>
> On Fri, Jun 06, 2025 at 02:30:11PM -0700, Song Liu wrote:
> > 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 <song@...nel.org>
> > ---
> > 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;
> > }
> >
> > +/**
> > + * 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 == path->mnt->mnt_root)) {
> > + struct path p;
> > +
> > + if (!choose_mountpoint(real_mount(path->mnt), root, &p))
> > + return false;
> > + path_put(path);
> > + *path = p;
> > + }
> > +
> > + if (unlikely(IS_ROOT(path->dentry)))
>
> path would be updated while false is returned, which is not correct.
Good catch.. How about the following:
bool path_walk_parent(struct path *path, const struct path *root)
{
struct dentry *parent;
bool ret = false;
if (path_equal(path, root))
return false;
if (unlikely(path->dentry == path->mnt->mnt_root)) {
struct path p;
if (!choose_mountpoint(real_mount(path->mnt), root, &p))
return false;
path_put(path);
*path = p;
ret = true;
}
if (unlikely(IS_ROOT(path->dentry)))
return ret;
parent = dget_parent(path->dentry);
dput(path->dentry);
path->dentry = parent;
return true;
}
Thanks,
Song
Powered by blists - more mailing lists