| 1 |
#!/usr/bin/env drgn |
| 2 |
|
| 3 |
import argparse |
| 4 |
import os |
| 5 |
import sys |
| 6 |
import typing |
| 7 |
from typing import Any, Callable, Iterator, Optional, Sequence, Union |
| 8 |
|
| 9 |
from drgn import FaultError, Object, Program, TypeKind, cast, container_of |
| 10 |
from drgn.helpers.linux.cpumask import for_each_possible_cpu |
| 11 |
from drgn.helpers.linux.fs import ( |
| 12 |
d_path, |
| 13 |
fget, |
| 14 |
for_each_file, |
| 15 |
for_each_mount, |
| 16 |
inode_path, |
| 17 |
mount_dst, |
| 18 |
) |
| 19 |
from drgn.helpers.linux.idr import idr_for_each_entry |
| 20 |
from drgn.helpers.linux.list import ( |
| 21 |
hlist_for_each_entry, |
| 22 |
list_empty, |
| 23 |
list_for_each_entry, |
| 24 |
) |
| 25 |
from drgn.helpers.linux.mm import for_each_vma |
| 26 |
from drgn.helpers.linux.percpu import per_cpu_ptr |
| 27 |
from drgn.helpers.linux.pid import find_task, for_each_task |
| 28 |
from drgn.helpers.linux.plist import plist_for_each_entry |
| 29 |
from drgn.helpers.linux.rbtree import rbtree_inorder_for_each_entry |
| 30 |
|
| 31 |
|
| 32 |
class warn_on_fault: |
| 33 |
def __init__(self, message: Union[str, Callable[[], str]]) -> None: |
| 34 |
self._message = message |
| 35 |
|
| 36 |
def __enter__(self) -> None: |
| 37 |
pass |
| 38 |
|
| 39 |
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> bool: |
| 40 |
if exc_type is not None and issubclass(exc_type, FaultError): |
| 41 |
message = ( |
| 42 |
self._message if isinstance(self._message, str) else self._message() |
| 43 |
) |
| 44 |
if message: |
| 45 |
print( |
| 46 |
f"warning: fault while {message}, possibly due to race; results may be incomplete", |
| 47 |
file=sys.stderr, |
| 48 |
) |
| 49 |
return True |
| 50 |
return False |
| 51 |
|
| 52 |
|
| 53 |
ignore_fault = warn_on_fault("") |
| 54 |
|
| 55 |
|
| 56 |
format_args = { |
| 57 |
"dereference": False, |
| 58 |
"symbolize": False, |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
if typing.TYPE_CHECKING: |
| 63 |
|
| 64 |
class Visitor(typing.Protocol): # novermin |
| 65 |
def visit_file(self, file: Object) -> Optional[str]: ... |
| 66 |
|
| 67 |
def visit_inode(self, inode: Object) -> Optional[str]: ... |
| 68 |
|
| 69 |
def visit_path(self, path: Object) -> Optional[str]: ... |
| 70 |
|
| 71 |
|
| 72 |
class InodeVisitor: |
| 73 |
def __init__(self, inode: Object) -> None: |
| 74 |
self._inode = inode.read_() |
| 75 |
|
| 76 |
def visit_file(self, file: Object) -> Optional[str]: |
| 77 |
if file.f_inode != self._inode: |
| 78 |
return None |
| 79 |
return file.format_(**format_args) |
| 80 |
|
| 81 |
def visit_inode(self, inode: Object) -> Optional[str]: |
| 82 |
if inode != self._inode: |
| 83 |
return None |
| 84 |
return inode.format_(**format_args) |
| 85 |
|
| 86 |
def visit_path(self, path: Object) -> Optional[str]: |
| 87 |
if path.dentry.d_inode != self._inode: |
| 88 |
return None |
| 89 |
return path.format_(**format_args) |
| 90 |
|
| 91 |
|
| 92 |
def _format_file_with_path(file: Object) -> str: |
| 93 |
match = file.format_(**format_args) |
| 94 |
with ignore_fault: |
| 95 |
match += " " + os.fsdecode(d_path(file.f_path)) |
| 96 |
return match |
| 97 |
|
| 98 |
|
| 99 |
def _format_inode_with_path(inode: Object) -> str: |
| 100 |
match = inode.format_(**format_args) |
| 101 |
with ignore_fault: |
| 102 |
path = inode_path(inode) |
| 103 |
if path: |
| 104 |
match += " " + os.fsdecode(path) |
| 105 |
return match |
| 106 |
|
| 107 |
|
| 108 |
def _format_path_with_path(path: Object) -> str: |
| 109 |
match = path.format_(**format_args) |
| 110 |
with ignore_fault: |
| 111 |
match += " " + os.fsdecode(d_path(path)) |
| 112 |
return match |
| 113 |
|
| 114 |
|
| 115 |
class SuperBlockVisitor: |
| 116 |
def __init__(self, sb: Object) -> None: |
| 117 |
self._sb = sb.read_() |
| 118 |
|
| 119 |
def visit_file(self, file: Object) -> Optional[str]: |
| 120 |
if file.f_inode.i_sb != self._sb: |
| 121 |
return None |
| 122 |
return _format_file_with_path(file) |
| 123 |
|
| 124 |
def visit_inode(self, inode: Object) -> Optional[str]: |
| 125 |
if inode.i_sb != self._sb: |
| 126 |
return None |
| 127 |
return _format_inode_with_path(inode) |
| 128 |
|
| 129 |
def visit_path(self, path: Object) -> Optional[str]: |
| 130 |
if path.mnt.mnt_sb != self._sb: |
| 131 |
return None |
| 132 |
return _format_path_with_path(path) |
| 133 |
|
| 134 |
|
| 135 |
class BtrfsSubvolumeVisitor: |
| 136 |
def __init__(self, root: Object) -> None: |
| 137 |
self._root = root.read_() |
| 138 |
# We compare the super block first to easily filter out non-Btrfs |
| 139 |
# inodes. |
| 140 |
self._sb = self._root.fs_info.sb.read_() |
| 141 |
|
| 142 |
def visit_file(self, file: Object) -> Optional[str]: |
| 143 |
f_inode = file.f_inode.read_() |
| 144 |
if ( |
| 145 |
f_inode.i_sb != self._sb |
| 146 |
or container_of(f_inode, "struct btrfs_inode", "vfs_inode").root |
| 147 |
!= self._root |
| 148 |
): |
| 149 |
return None |
| 150 |
return _format_file_with_path(file) |
| 151 |
|
| 152 |
def visit_inode(self, inode: Object) -> Optional[str]: |
| 153 |
if ( |
| 154 |
inode.i_sb != self._sb |
| 155 |
or container_of(inode, "struct btrfs_inode", "vfs_inode").root != self._root |
| 156 |
): |
| 157 |
return None |
| 158 |
return _format_inode_with_path(inode) |
| 159 |
|
| 160 |
def visit_path(self, path: Object) -> Optional[str]: |
| 161 |
inode = path.dentry.d_inode.read_() |
| 162 |
if ( |
| 163 |
inode.i_sb != self._sb |
| 164 |
or container_of(inode, "struct btrfs_inode", "vfs_inode").root != self._root |
| 165 |
): |
| 166 |
return None |
| 167 |
return _format_path_with_path(path) |
| 168 |
|
| 169 |
|
| 170 |
def super_block_on_bdev(bdev: Object) -> Optional[Object]: |
| 171 |
prog = bdev.prog_ |
| 172 |
|
| 173 |
try: |
| 174 |
btrfs_fs_info_type = prog.type("struct btrfs_fs_info *") |
| 175 |
except LookupError: |
| 176 |
btrfs_fs_info_type = None |
| 177 |
|
| 178 |
for sb in list_for_each_entry( |
| 179 |
"struct super_block", prog["super_blocks"].address_of_(), "s_list" |
| 180 |
): |
| 181 |
if btrfs_fs_info_type is not None and sb.s_type.name.string_() == b"btrfs": |
| 182 |
# Btrfs is a special case because of its multi-device support. |
| 183 |
fs_info = cast(btrfs_fs_info_type, sb.s_fs_info) |
| 184 |
for device in list_for_each_entry( |
| 185 |
"struct btrfs_device", |
| 186 |
fs_info.fs_devices.devices.address_of_(), |
| 187 |
"dev_list", |
| 188 |
): |
| 189 |
if device.bdev == bdev: |
| 190 |
return sb |
| 191 |
elif sb.s_bdev == bdev: |
| 192 |
return sb |
| 193 |
|
| 194 |
return None |
| 195 |
|
| 196 |
|
| 197 |
def visit_tasks( |
| 198 |
prog: Program, visitor: "Visitor", *, check_mounts: bool, check_tasks: bool |
| 199 |
) -> None: |
| 200 |
check_mounts = check_mounts and isinstance(visitor, SuperBlockVisitor) |
| 201 |
if check_mounts: |
| 202 |
init_mnt_ns = prog["init_task"].nsproxy.mnt_ns |
| 203 |
checked_mnt_ns = {0} |
| 204 |
with warn_on_fault("iterating tasks"): |
| 205 |
for task in for_each_task(prog): |
| 206 |
cached_task_id = None |
| 207 |
|
| 208 |
def task_id() -> str: |
| 209 |
nonlocal cached_task_id |
| 210 |
if cached_task_id is None: |
| 211 |
pid = task.pid.value_() |
| 212 |
comm = os.fsdecode(task.comm.string_()) |
| 213 |
cached_task_id = f"pid {pid} ({comm})" |
| 214 |
return cached_task_id |
| 215 |
|
| 216 |
def task_fault_warning() -> str: |
| 217 |
try: |
| 218 |
return f"checking {task_id()}" |
| 219 |
except FaultError: |
| 220 |
return "checking task" |
| 221 |
|
| 222 |
with warn_on_fault(task_fault_warning): |
| 223 |
files: Optional[Object] = task.files.read_() |
| 224 |
fs: Optional[Object] = task.fs.read_() |
| 225 |
mm: Optional[Object] = task.mm.read_() |
| 226 |
|
| 227 |
# If this task is not the thread group leader, don't bother |
| 228 |
# checking it again unless it has its own context. |
| 229 |
group_leader = task.group_leader.read_() |
| 230 |
if task != group_leader: |
| 231 |
if files and files == group_leader.files: |
| 232 |
files = None |
| 233 |
if fs and fs == group_leader.fs: |
| 234 |
fs = None |
| 235 |
if mm and mm == group_leader.mm: |
| 236 |
mm = None |
| 237 |
|
| 238 |
if check_mounts: |
| 239 |
nsproxy = task.nsproxy.read_() |
| 240 |
if nsproxy: |
| 241 |
mnt_ns = nsproxy.mnt_ns.read_() |
| 242 |
if mnt_ns.value_() not in checked_mnt_ns: |
| 243 |
for mount in for_each_mount(mnt_ns): |
| 244 |
with ignore_fault: |
| 245 |
if mount.mnt.mnt_sb == visitor._sb: # type: ignore [attr-defined] |
| 246 |
if mnt_ns == init_mnt_ns: |
| 247 |
mnt_ns_note = "" |
| 248 |
else: |
| 249 |
mnt_ns_note = f" (mount namespace {mnt_ns.ns.inum.value_()})" |
| 250 |
print( |
| 251 |
f"mount {os.fsdecode(mount_dst(mount))}{mnt_ns_note} " |
| 252 |
f"{mount.format_(**format_args)}" |
| 253 |
) |
| 254 |
|
| 255 |
checked_mnt_ns.add(mnt_ns.value_()) |
| 256 |
|
| 257 |
if check_tasks: |
| 258 |
if files: |
| 259 |
for fd, file in for_each_file(task): |
| 260 |
with ignore_fault: |
| 261 |
match = visitor.visit_file(file) |
| 262 |
if match: |
| 263 |
print(f"{task_id()} fd {fd} {match}") |
| 264 |
|
| 265 |
if fs: |
| 266 |
with ignore_fault: |
| 267 |
match = visitor.visit_path(fs.root.address_of_()) |
| 268 |
if match: |
| 269 |
print(f"{task_id()} root {match}") |
| 270 |
with ignore_fault: |
| 271 |
match = visitor.visit_path(fs.pwd.address_of_()) |
| 272 |
if match: |
| 273 |
print(f"{task_id()} cwd {match}") |
| 274 |
|
| 275 |
if mm: |
| 276 |
exe_file = mm.exe_file.read_() |
| 277 |
if exe_file: |
| 278 |
match = visitor.visit_file(exe_file) |
| 279 |
if match: |
| 280 |
print(f"{task_id()} exe {match}") |
| 281 |
|
| 282 |
for vma in for_each_vma(mm): |
| 283 |
with ignore_fault: |
| 284 |
file = vma.vm_file.read_() |
| 285 |
if file: |
| 286 |
match = visitor.visit_file(file) |
| 287 |
if match: |
| 288 |
print( |
| 289 |
f"{task_id()} vma {hex(vma.vm_start)}-{hex(vma.vm_end)} {match}" |
| 290 |
) |
| 291 |
|
| 292 |
|
| 293 |
def visit_binfmt_misc(prog: Program, visitor: "Visitor") -> None: |
| 294 |
try: |
| 295 |
Node = prog.type("Node", filename="binfmt_misc.c") |
| 296 |
except LookupError: |
| 297 |
# If the Node type doesn't exist, then CONFIG_BINFMT_MISC=n or the |
| 298 |
# binfmt_misc module isn't loaded. |
| 299 |
return |
| 300 |
with warn_on_fault("iterating binfmt_misc instances"): |
| 301 |
for sb in hlist_for_each_entry( |
| 302 |
"struct super_block", prog["bm_fs_type"].fs_supers, "s_instances" |
| 303 |
): |
| 304 |
# Since Linux kernel commit 21ca59b365c0 ("binfmt_misc: enable |
| 305 |
# sandboxed mounts") (in v6.7), each user namespace can have its |
| 306 |
# own binfmt_misc instance. Before that, there is one global |
| 307 |
# instance. |
| 308 |
user_ns = cast("struct user_namespace *", sb.s_fs_info) |
| 309 |
try: |
| 310 |
binfmt_misc = user_ns.binfmt_misc |
| 311 |
except AttributeError: |
| 312 |
entries = prog.object("entries", filename="binfmt_misc.c") |
| 313 |
have_user_ns = False |
| 314 |
else: |
| 315 |
entries = binfmt_misc.entries |
| 316 |
have_user_ns = True |
| 317 |
|
| 318 |
for node in list_for_each_entry(Node, entries.address_of_(), "list"): |
| 319 |
with ignore_fault: |
| 320 |
match = visitor.visit_file(node.interp_file) |
| 321 |
if match: |
| 322 |
if have_user_ns and user_ns.level: |
| 323 |
user_ns_note = ( |
| 324 |
f" (user namespace {user_ns.ns.inum.value_()})" |
| 325 |
) |
| 326 |
else: |
| 327 |
user_ns_note = "" |
| 328 |
print( |
| 329 |
f"binfmt_misc{user_ns_note} {os.fsdecode(node.name.string_())} {node.format_(**format_args)} {match}" |
| 330 |
) |
| 331 |
|
| 332 |
|
| 333 |
def visit_loop_devices(prog: Program, visitor: "Visitor") -> None: |
| 334 |
try: |
| 335 |
loop_index_idr = prog["loop_index_idr"] |
| 336 |
except KeyError: |
| 337 |
# If loop_index_idr doesn't exist, then CONFIG_BLK_DEV_LOOP=n or the |
| 338 |
# loop module isn't loaded. |
| 339 |
return |
| 340 |
with warn_on_fault("iterating loop devices"): |
| 341 |
for i, lo in idr_for_each_entry( |
| 342 |
loop_index_idr.address_of_(), "struct loop_device" |
| 343 |
): |
| 344 |
with ignore_fault: |
| 345 |
file = lo.lo_backing_file.read_() |
| 346 |
if file: |
| 347 |
match = visitor.visit_file(file) |
| 348 |
if match: |
| 349 |
print(f"loop device {i} {lo.format_(**format_args)} {match}") |
| 350 |
|
| 351 |
|
| 352 |
def visit_swap_files(prog: Program, visitor: "Visitor") -> None: |
| 353 |
try: |
| 354 |
swap_active_head = prog["swap_active_head"] |
| 355 |
except KeyError: |
| 356 |
# If swap_active_head doesn't exist, then CONFIG_SWAP=n. |
| 357 |
return |
| 358 |
with warn_on_fault("iterating swap files"): |
| 359 |
for swap_info in plist_for_each_entry( |
| 360 |
"struct swap_info_struct", swap_active_head.address_of_(), "list" |
| 361 |
): |
| 362 |
with ignore_fault: |
| 363 |
match = visitor.visit_file(swap_info.swap_file) |
| 364 |
if match: |
| 365 |
print(f"swap file {swap_info.format_(**format_args)} {match}") |
| 366 |
|
| 367 |
|
| 368 |
# call was moved from struct trace_probe to struct trace_event Linux kernel |
| 369 |
# commit 60d53e2c3b75 ("tracing/probe: Split trace_event related data from |
| 370 |
# trace_probe") (in v5.4). |
| 371 |
def _trace_probe_call(tp: Object) -> Object: |
| 372 |
try: |
| 373 |
event = tp.event |
| 374 |
except AttributeError: |
| 375 |
return tp.call |
| 376 |
return event.call |
| 377 |
|
| 378 |
|
| 379 |
def trace_probe_group_name(tp: Object) -> str: |
| 380 |
return os.fsdecode(_trace_probe_call(tp).member_("class").system.string_()) |
| 381 |
|
| 382 |
|
| 383 |
def trace_probe_name(tp: Object) -> str: |
| 384 |
prog = tp.prog_ |
| 385 |
call = _trace_probe_call(tp).read_() |
| 386 |
|
| 387 |
# TRACE_EVENT_FL_CUSTOM was added in Linux kernel commit 3a73333fb370 |
| 388 |
# ("tracing: Add TRACE_CUSTOM_EVENT() macro") (in v5.18). |
| 389 |
try: |
| 390 |
TRACE_EVENT_FL_CUSTOM = prog["TRACE_EVENT_FL_CUSTOM"] |
| 391 |
except KeyError: |
| 392 |
pass |
| 393 |
else: |
| 394 |
if call.flags & TRACE_EVENT_FL_CUSTOM: |
| 395 |
return os.fsdecode(call.name.string_()) |
| 396 |
|
| 397 |
if call.flags & prog["TRACE_EVENT_FL_TRACEPOINT"]: |
| 398 |
tracepoint = call.tp.read_() |
| 399 |
return os.fsdecode(tracepoint.name.string_()) if tracepoint else "" |
| 400 |
else: |
| 401 |
return os.fsdecode(call.name.string_()) |
| 402 |
|
| 403 |
|
| 404 |
def visit_uprobes(prog: Program, visitor: "Visitor") -> None: |
| 405 |
try: |
| 406 |
uprobes_tree = prog["uprobes_tree"] |
| 407 |
except KeyError: |
| 408 |
# If uprobes_tree doesn't exist, then CONFIG_UPROBES=n. |
| 409 |
return |
| 410 |
try: |
| 411 |
uprobe_dispatcher = prog["uprobe_dispatcher"] |
| 412 |
except KeyError: |
| 413 |
# uprobe_dispatcher only exists if CONFIG_UPROBE_EVENTS=y, which is |
| 414 |
# theoretically separate from CONFIG_UPROBES, although as of Linux 6.8 |
| 415 |
# they will always be the same. |
| 416 |
uprobe_dispatcher = None |
| 417 |
with warn_on_fault("iterating uprobes"): |
| 418 |
for uprobe in rbtree_inorder_for_each_entry( |
| 419 |
"struct uprobe", uprobes_tree.address_of_(), "rb_node" |
| 420 |
): |
| 421 |
try: |
| 422 |
match = visitor.visit_inode(uprobe.inode) |
| 423 |
except FaultError: |
| 424 |
continue |
| 425 |
if not match: |
| 426 |
continue |
| 427 |
found_consumer = False |
| 428 |
with warn_on_fault("iterating uprobe consumers"): |
| 429 |
consumer_list = uprobe.consumers |
| 430 |
# Since Linux kernel commit cc01bd044e6a ("uprobes: travers |
| 431 |
# uprobe's consumer list locklessly under SRCU protection") (in |
| 432 |
# v6.12), struct uprobe::consumers is a struct list_head. |
| 433 |
# Before that, it was a hand-rolled singly-linked list. |
| 434 |
if consumer_list.type_.kind == TypeKind.STRUCT: |
| 435 |
consumers = list_for_each_entry( |
| 436 |
"struct uprobe_consumer", |
| 437 |
consumer_list.address_of_(), |
| 438 |
"cons_node", |
| 439 |
) |
| 440 |
else: |
| 441 |
|
| 442 |
def for_each_consumer(first: Object) -> Iterator[Object]: |
| 443 |
consumer = first.read_() |
| 444 |
while consumer: |
| 445 |
yield consumer |
| 446 |
consumer = consumer.next.read_() |
| 447 |
|
| 448 |
consumers = for_each_consumer(consumer_list) |
| 449 |
|
| 450 |
for consumer in consumers: |
| 451 |
handler = consumer.handler.read_() |
| 452 |
if handler == uprobe_dispatcher: |
| 453 |
tu = container_of(consumer, "struct trace_uprobe", "consumer") |
| 454 |
# uprobe events created through tracefs are in a list |
| 455 |
# anchored on devent.list since Linux kernel commit |
| 456 |
# 0597c49c69d5 ("tracing/uprobes: Use dyn_event |
| 457 |
# framework for uprobe events") (in v5.0) and list |
| 458 |
# before that. |
| 459 |
try: |
| 460 |
event_list = tu.devent.list |
| 461 |
except AttributeError: |
| 462 |
event_list = tu.list |
| 463 |
if list_empty(event_list.address_of_()): |
| 464 |
found_perf_event = False |
| 465 |
with ignore_fault: |
| 466 |
call = _trace_probe_call(tu.tp) |
| 467 |
# uprobes created with perf_event_open have a |
| 468 |
# struct perf_event in call.perf_events, which |
| 469 |
# only exists if CONFIG_PERF_EVENTS=y. |
| 470 |
try: |
| 471 |
perf_events = call.perf_events |
| 472 |
except AttributeError: |
| 473 |
pass |
| 474 |
else: |
| 475 |
for cpu in for_each_possible_cpu(prog): |
| 476 |
for perf_event in hlist_for_each_entry( |
| 477 |
"struct perf_event", |
| 478 |
per_cpu_ptr(perf_events, cpu), |
| 479 |
"hlist_entry", |
| 480 |
): |
| 481 |
owner = perf_event.owner.read_() |
| 482 |
if owner: |
| 483 |
owner_pid = owner.pid.value_() |
| 484 |
owner_comm = os.fsdecode( |
| 485 |
owner.comm.string_() |
| 486 |
) |
| 487 |
print( |
| 488 |
f"perf uprobe (owned by pid {owner_pid} ({owner_comm})) {perf_event.format_(**format_args)} {match}" |
| 489 |
) |
| 490 |
else: |
| 491 |
print( |
| 492 |
f"perf uprobe (no owner) {perf_event.format_(**format_args)} {match}" |
| 493 |
) |
| 494 |
found_perf_event = True |
| 495 |
if not found_perf_event: |
| 496 |
print( |
| 497 |
f"unknown trace uprobe {tu.format_(**format_args)} {match}" |
| 498 |
) |
| 499 |
else: |
| 500 |
c = "r" if tu.consumer.ret_handler else "p" |
| 501 |
group_name = trace_probe_group_name(tu.tp) |
| 502 |
event_name = trace_probe_name(tu.tp) |
| 503 |
print( |
| 504 |
f"uprobe event {c}:{group_name}/{event_name} {tu.format_(**format_args)} {match}" |
| 505 |
) |
| 506 |
else: |
| 507 |
print( |
| 508 |
f"unknown uprobe consumer {consumer.format_(**format_args)}" |
| 509 |
) |
| 510 |
found_consumer = True |
| 511 |
if not found_consumer: |
| 512 |
print(f"unknown uprobe {uprobe.format_(**format_args)} {match}") |
| 513 |
|
| 514 |
|
| 515 |
def hexint(x: str) -> int: |
| 516 |
return int(x, 16) |
| 517 |
|
| 518 |
|
| 519 |
def main(prog: Program, argv: Sequence[str]) -> None: |
| 520 |
parser = argparse.ArgumentParser( |
| 521 |
description="find what is referencing a filesystem object" |
| 522 |
) |
| 523 |
|
| 524 |
parser.add_argument( |
| 525 |
"-L", |
| 526 |
"--dereference", |
| 527 |
action="store_true", |
| 528 |
help="if the given path is a symbolic link, follow it", |
| 529 |
) |
| 530 |
|
| 531 |
object_group = parser.add_argument_group( |
| 532 |
title="filesystem object selection" |
| 533 |
).add_mutually_exclusive_group(required=True) |
| 534 |
object_group.add_argument( |
| 535 |
"--inode", metavar="PATH", help="find references to the inode at the given path" |
| 536 |
) |
| 537 |
object_group.add_argument( |
| 538 |
"--inode-pointer", |
| 539 |
metavar="ADDRESS", |
| 540 |
type=hexint, |
| 541 |
help="find references to the given struct inode pointer", |
| 542 |
) |
| 543 |
object_group.add_argument( |
| 544 |
"--super-block", |
| 545 |
metavar="PATH", |
| 546 |
help="find references to the filesystem (super block) containing the given path", |
| 547 |
) |
| 548 |
object_group.add_argument( |
| 549 |
"--super-block-on-block-device", |
| 550 |
metavar="PATH", |
| 551 |
help="find references to the filesystem (super block) on the block device at the given path", |
| 552 |
) |
| 553 |
object_group.add_argument( |
| 554 |
"--super-block-pointer", |
| 555 |
metavar="ADDRESS", |
| 556 |
type=hexint, |
| 557 |
help="find references to the given struct super_block pointer", |
| 558 |
) |
| 559 |
object_group.add_argument( |
| 560 |
"--btrfs-subvolume", |
| 561 |
metavar="PATH", |
| 562 |
help="find references to the Btrfs subvolume containing the given path", |
| 563 |
) |
| 564 |
object_group.add_argument( |
| 565 |
"--btrfs-subvolume-pointer", |
| 566 |
metavar="ADDRESS", |
| 567 |
type=hexint, |
| 568 |
help="find references to the given struct btrfs_root pointer", |
| 569 |
) |
| 570 |
|
| 571 |
CHECKS = [ |
| 572 |
"binfmt_misc", |
| 573 |
"loop", |
| 574 |
"mounts", |
| 575 |
"swap", |
| 576 |
"tasks", |
| 577 |
"uprobes", |
| 578 |
] |
| 579 |
check_group = parser.add_argument_group( |
| 580 |
title="check selection" |
| 581 |
).add_mutually_exclusive_group() |
| 582 |
check_group.add_argument( |
| 583 |
"--check", |
| 584 |
choices=CHECKS, |
| 585 |
action="append", |
| 586 |
help="only check for references from the given source; may be given multiple times (default: all)", |
| 587 |
) |
| 588 |
check_group.add_argument( |
| 589 |
"--no-check", |
| 590 |
choices=CHECKS, |
| 591 |
action="append", |
| 592 |
help="don't check for references from the given source; may be given multiple times", |
| 593 |
) |
| 594 |
|
| 595 |
args = parser.parse_args(argv) |
| 596 |
|
| 597 |
visitor: "Visitor" |
| 598 |
if args.inode is not None: |
| 599 |
fd = os.open(args.inode, os.O_PATH | (0 if args.dereference else os.O_NOFOLLOW)) |
| 600 |
try: |
| 601 |
visitor = InodeVisitor(fget(find_task(prog, os.getpid()), fd).f_inode) |
| 602 |
finally: |
| 603 |
os.close(fd) |
| 604 |
elif args.inode_pointer is not None: |
| 605 |
visitor = InodeVisitor(Object(prog, "struct inode *", args.inode_pointer)) |
| 606 |
elif args.super_block is not None: |
| 607 |
fd = os.open( |
| 608 |
args.super_block, os.O_PATH | (0 if args.dereference else os.O_NOFOLLOW) |
| 609 |
) |
| 610 |
try: |
| 611 |
visitor = SuperBlockVisitor( |
| 612 |
fget(find_task(prog, os.getpid()), fd).f_inode.i_sb |
| 613 |
) |
| 614 |
finally: |
| 615 |
os.close(fd) |
| 616 |
elif args.super_block_on_block_device: |
| 617 |
fd = os.open( |
| 618 |
args.super_block_on_block_device, |
| 619 |
# O_PATH doesn't look up the bdev inode, so we can't use it here. |
| 620 |
os.O_RDONLY | (0 if args.dereference else os.O_NOFOLLOW), |
| 621 |
) |
| 622 |
try: |
| 623 |
file = fget(find_task(prog, os.getpid()), fd) |
| 624 |
if file.f_op != prog["def_blk_fops"].address_of_(): |
| 625 |
sys.exit( |
| 626 |
f"{args.super_block_on_block_device} is not a block device. Do you need --dereference?" |
| 627 |
) |
| 628 |
sb = super_block_on_bdev( |
| 629 |
container_of( |
| 630 |
file.f_mapping.host, "struct bdev_inode", "vfs_inode" |
| 631 |
).bdev.address_of_() |
| 632 |
) |
| 633 |
if sb is None: |
| 634 |
sys.exit(f"no filesystem found on {args.super_block_on_block_device}") |
| 635 |
visitor = SuperBlockVisitor(sb) |
| 636 |
finally: |
| 637 |
os.close(fd) |
| 638 |
elif args.super_block_pointer is not None: |
| 639 |
visitor = SuperBlockVisitor( |
| 640 |
Object(prog, "struct super_block *", args.super_block_pointer) |
| 641 |
) |
| 642 |
elif args.btrfs_subvolume is not None: |
| 643 |
fd = os.open( |
| 644 |
args.btrfs_subvolume, os.O_PATH | (0 if args.dereference else os.O_NOFOLLOW) |
| 645 |
) |
| 646 |
try: |
| 647 |
inode = fget(find_task(prog, os.getpid()), fd).f_inode.read_() |
| 648 |
# It'd be better to check the filesystem type directly from |
| 649 |
# userspace using fstatfs(2), but Python doesn't provide |
| 650 |
# {,f}statfs() (and os.statvfs() doesn't include f_type). |
| 651 |
if inode.i_sb.s_type.name.string_() != b"btrfs": |
| 652 |
sys.exit(f"{args.btrfs_subvolume} is not on Btrfs") |
| 653 |
visitor = BtrfsSubvolumeVisitor( |
| 654 |
container_of(inode, "struct btrfs_inode", "vfs_inode").root |
| 655 |
) |
| 656 |
finally: |
| 657 |
os.close(fd) |
| 658 |
elif args.btrfs_subvolume_pointer is not None: |
| 659 |
visitor = BtrfsSubvolumeVisitor( |
| 660 |
Object(prog, "struct btrfs_root *", args.btrfs_subvolume_pointer) |
| 661 |
) |
| 662 |
else: |
| 663 |
assert False |
| 664 |
|
| 665 |
if args.check: |
| 666 |
enabled_checks = set(args.check) |
| 667 |
else: |
| 668 |
enabled_checks = set(CHECKS) |
| 669 |
if args.no_check: |
| 670 |
enabled_checks -= set(args.no_check) |
| 671 |
|
| 672 |
if "mounts" in enabled_checks or "tasks" in enabled_checks: |
| 673 |
visit_tasks( |
| 674 |
prog, |
| 675 |
visitor, |
| 676 |
check_mounts="mounts" in enabled_checks, |
| 677 |
check_tasks="tasks" in enabled_checks, |
| 678 |
) |
| 679 |
|
| 680 |
if "binfmt_misc" in enabled_checks: |
| 681 |
visit_binfmt_misc(prog, visitor) |
| 682 |
|
| 683 |
if "loop" in enabled_checks: |
| 684 |
visit_loop_devices(prog, visitor) |
| 685 |
|
| 686 |
if "swap" in enabled_checks: |
| 687 |
visit_swap_files(prog, visitor) |
| 688 |
|
| 689 |
if "uprobes" in enabled_checks: |
| 690 |
visit_uprobes(prog, visitor) |
| 691 |
|
| 692 |
|
| 693 |
if __name__ == "__main__": |
| 694 |
prog: Program |
| 695 |
main(prog, sys.argv[1:]) |