Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper: partially follow link in path_lookup #218

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Wengang-oracle
Copy link

Current path_lookup() doesn't follow symbol links. The symbol link following is FS specific, it's hard and not neccesary to support all file systems. This patch supports:

  1. EXT4 simple symbol link (target path stored in inode.i_link).
  2. XFS inlined symbol link.

Signed-off-by: Wengang Wang [email protected]
Reviewed-by: Stephen Brennan [email protected]

Current path_lookup() doesn't follow symbol links.
The symbol link following is FS specific, it's hard and not neccesary to
support all file systems. This patch supports:
1) EXT4 simple symbol link (target path stored in inode.i_link).
2) XFS inlined symbol link.

Signed-off-by: Wengang Wang <[email protected]>
Reviewed-by: Stephen Brennan <[email protected]>
osandov added a commit that referenced this pull request Dec 2, 2022
For some upcoming helpers (#218), we're going to want to test against a
few common filesystems.

Signed-off-by: Omar Sandoval <[email protected]>
Copy link
Owner

@osandov osandov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very cool but very tricky feature. I left a few comments.

Repeating what I mentioned inline, I'd like to have an inode_readlink() helper first so we can test that independently, then add this.

def get_link(
inode: Object) -> bytes:
if inode.i_link:
return inode.i_link
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be:

Suggested change
return inode.i_link
return inode.i_link.string_()

return b""

# caller makes sure this is a symbol link inode
def get_link(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This deserves to be a public helper. Could you please name this inode_readlink(), document it, and add it to __all__? We should do that before adding symlink lookups to path_lookup(). That can be in a separate PR or a separate commit before this one in this PR, it's up to you.

Then, we can figure out how to write some tests for it.

inode: Object) -> bytes:
xfs_inode = container_of(inode, "struct xfs_inode", "i_vnode")
ifork = xfs_inode.i_df
# xfs_ifork_t has different members in different versions.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document which commit changed this. See drgn.helpers.linux.sched.task_state_to_char() for an example:

# Since Linux kernel commit 2f064a59a11f ("sched: Change
# task_struct::state") (in v5.14), the task state is named "__state".
# Before that, it is named "state".
try:
task_state = task.__state
task_state_name = "__state"
except AttributeError:
task_state = task.state
task_state_name = "state"

xfs_inode = container_of(inode, "struct xfs_inode", "i_vnode")
ifork = xfs_inode.i_df
# xfs_ifork_t has different members in different versions.
if hasattr(ifork, "if_format") and ifork.if_format == XFS_DINODE_FMT_LOCAL:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a try/except AttributeError instead of hasattr() ("easier to ask for forgiveness than permission").

link_target = s
else:
solved_path = b"/" + b"/".join(components[0:i+1])
link_target = solved_path + "/" + s
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will redo the entire lookup up to the symlink, right? That sounds pretty inefficient (and racy on live systems), especially if there are multiple symlinks in the path traversal.

There are probably multiple ways to avoid this, but the one that comes to mind is to keep a stack of names being resolved and avoid recursion. Something like:

class PathLookupName:
    def __init__(self, name: str) -> None:
        self.components = name.split(b"/")
        self.index = 0

name_stack = [PathLookupName(os.fsencode(path))]
while name_stack:
    name = name_stack[-1]
    if name.index >= len(name.components):
        name_stack.pop()
        continue

    component = name_stack.components[name.index]
    name.index += 1

    ... handle component ...

    if is_symlink(...):
        link_target = ...
        name_stack.push(PathLookupName(link_target))
        if link_target.startswith(b"/"):
            # Restart at the root.
            mnt = root_mnt
            dentry = root_dentry

I think that general idea will work. We also need to be careful about cycles (the kernel handles this by returning ELOOP if it encounters too many symlinks.)

This is going to need lots of test cases 😄

@osandov osandov linked an issue Dec 3, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle symlinks in path_lookup()
2 participants