-
Notifications
You must be signed in to change notification settings - Fork 165
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
base: main
Are you sure you want to change the base?
helper: partially follow link in path_lookup #218
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>
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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. |
There was a problem hiding this comment.
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:
drgn/drgn/helpers/linux/sched.py
Lines 59 to 67 in 220c1c7
# 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: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 😄
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