Skip to content

Commit d900196

Browse files
committed
Linux: Add kthreads plugin to enumerate kernel thread functions
1 parent 1e871af commit d900196

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# This file is Copyright 2024 Volatility Foundation and licensed under the Volatility Software License 1.0
2+
# which is available at https://www.volatilityfoundation.org/license/vsl-v1.0
3+
#
4+
import logging
5+
from typing import List
6+
7+
from volatility3.framework import constants, exceptions, interfaces, renderers
8+
from volatility3.framework.configuration import requirements
9+
from volatility3.framework.interfaces import plugins
10+
from volatility3.framework.renderers import format_hints
11+
from volatility3.framework.symbols import linux
12+
from volatility3.framework.constants import architectures
13+
from volatility3.framework.objects import utility
14+
from volatility3.plugins.linux import pslist, lsmod
15+
16+
vollog = logging.getLogger(__name__)
17+
18+
19+
class Kthreads(plugins.PluginInterface):
20+
"""Enumerates kthread functions"""
21+
22+
_required_framework_version = (2, 0, 0)
23+
24+
_version = (1, 0, 0)
25+
26+
@classmethod
27+
def get_requirements(cls) -> List[interfaces.configuration.RequirementInterface]:
28+
return [
29+
requirements.ModuleRequirement(
30+
name="kernel",
31+
description="Linux kernel",
32+
architectures=architectures.LINUX_ARCHS,
33+
),
34+
requirements.VersionRequirement(
35+
name="linuxutils", component=linux.LinuxUtilities, version=(2, 1, 0)
36+
),
37+
]
38+
39+
def _generator(self):
40+
vmlinux = self.context.modules[self.config["kernel"]]
41+
42+
modules = lsmod.Lsmod.list_modules(self.context, vmlinux.name)
43+
handlers = linux.LinuxUtilities.generate_kernel_handler_info(
44+
self.context, vmlinux.name, modules
45+
)
46+
47+
kthread_type = vmlinux.get_type(
48+
vmlinux.symbol_table_name + constants.BANG + "kthread"
49+
)
50+
51+
if not kthread_type.has_member("threadfn"):
52+
raise exceptions.VolatilityException(
53+
"Unsupported kthread implementation. This plugin only works with kernels >= 5.8"
54+
)
55+
56+
for task in pslist.PsList.list_tasks(
57+
self.context, vmlinux.name, include_threads=True
58+
):
59+
if not task.is_kernel_thread:
60+
continue
61+
62+
if task.has_member("worker_private"):
63+
# kernels >= 5.17 e32cf5dfbe227b355776948b2c9b5691b84d1cbd
64+
ktread_base_pointer = task.worker_private
65+
else:
66+
# 5.8 <= kernels < 5.17 in 52782c92ac85c4e393eb4a903a62e6c24afa633f threadfn
67+
# was added to struct kthread. task.set_child_tid is safe on those versions.
68+
ktread_base_pointer = task.set_child_tid
69+
70+
if not ktread_base_pointer.is_readable():
71+
continue
72+
73+
kthread = ktread_base_pointer.dereference().cast("kthread")
74+
threadfn = kthread.threadfn
75+
if not (threadfn and threadfn.is_readable()):
76+
continue
77+
78+
task_name = utility.array_to_string(task.comm)
79+
80+
# kernels >= 5.17 in d6986ce24fc00b0638bd29efe8fb7ba7619ed2aa full_name was added to kthread
81+
thread_name = (
82+
utility.pointer_to_string(kthread.full_name, count=255)
83+
if kthread.has_member("full_name")
84+
else task_name
85+
)
86+
module_name, symbol_name = linux.LinuxUtilities.lookup_module_address(
87+
vmlinux, handlers, threadfn
88+
)
89+
90+
fields = [
91+
task.pid,
92+
thread_name,
93+
format_hints.Hex(threadfn),
94+
module_name,
95+
symbol_name,
96+
]
97+
yield 0, fields
98+
99+
def run(self):
100+
return renderers.TreeGrid(
101+
[
102+
("TID", int),
103+
("Thread Name", str),
104+
("Handler Address", format_hints.Hex),
105+
("Module", str),
106+
("Symbol", str),
107+
],
108+
self._generator(),
109+
)

0 commit comments

Comments
 (0)