helper: partially follow link in path_lookup#218
helper: partially follow link in path_lookup#218Wengang-oracle wants to merge 1 commit intoosandov:mainfrom
Conversation
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 <wen.gang.wang@oracle.com> Reviewed-by: Stephen Brennan <stephen.s.brennan@oracle.com>
For some upcoming helpers (#218), we're going to want to test against a few common filesystems. Signed-off-by: Omar Sandoval <osandov@osandov.com>
osandov
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
This should probably be:
| return inode.i_link | |
| return inode.i_link.string_() |
| return b"" | ||
|
|
||
| # caller makes sure this is a symbol link inode | ||
| def get_link( |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Please document which commit changed this. See drgn.helpers.linux.sched.task_state_to_char() for an example:
drgn/drgn/helpers/linux/sched.py
Lines 59 to 67 in 220c1c7
| 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: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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_dentryI 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 😄
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:
Signed-off-by: Wengang Wang wen.gang.wang@oracle.com
Reviewed-by: Stephen Brennan stephen.s.brennan@oracle.com