Skip to content

Commit 340bd6d

Browse files
authored
Merge pull request #1348 from gcmoreira/linux_kthreads_plugin
Linux: Add kthreads plugin to enumerate kernel threads start address
2 parents 1e871af + 787e15a commit 340bd6d

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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, 11, 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+
requirements.PluginRequirement(
38+
name="pslist", plugin=pslist.PsList, version=(2, 3, 0)
39+
),
40+
requirements.PluginRequirement(
41+
name="lsmod", plugin=lsmod.Lsmod, version=(2, 0, 0)
42+
),
43+
]
44+
45+
def _generator(self):
46+
vmlinux = self.context.modules[self.config["kernel"]]
47+
48+
modules = lsmod.Lsmod.list_modules(self.context, vmlinux.name)
49+
handlers = linux.LinuxUtilities.generate_kernel_handler_info(
50+
self.context, vmlinux.name, modules
51+
)
52+
53+
kthread_type = vmlinux.get_type(
54+
vmlinux.symbol_table_name + constants.BANG + "kthread"
55+
)
56+
57+
if not kthread_type.has_member("threadfn"):
58+
raise exceptions.VolatilityException(
59+
"Unsupported kthread implementation. This plugin only works with kernels >= 5.8"
60+
)
61+
62+
for task in pslist.PsList.list_tasks(
63+
self.context, vmlinux.name, include_threads=True
64+
):
65+
if not task.is_kernel_thread:
66+
continue
67+
68+
if task.has_member("worker_private"):
69+
# kernels >= 5.17 e32cf5dfbe227b355776948b2c9b5691b84d1cbd
70+
ktread_base_pointer = task.worker_private
71+
else:
72+
# 5.8 <= kernels < 5.17 in 52782c92ac85c4e393eb4a903a62e6c24afa633f threadfn
73+
# was added to struct kthread. task.set_child_tid is safe on those versions.
74+
ktread_base_pointer = task.set_child_tid
75+
76+
if not ktread_base_pointer.is_readable():
77+
continue
78+
79+
kthread = ktread_base_pointer.dereference().cast("kthread")
80+
threadfn = kthread.threadfn
81+
if not (threadfn and threadfn.is_readable()):
82+
continue
83+
84+
task_name = utility.array_to_string(task.comm)
85+
86+
# kernels >= 5.17 in d6986ce24fc00b0638bd29efe8fb7ba7619ed2aa full_name was added to kthread
87+
thread_name = (
88+
utility.pointer_to_string(kthread.full_name, count=255)
89+
if kthread.has_member("full_name")
90+
else task_name
91+
)
92+
module_name, symbol_name = linux.LinuxUtilities.lookup_module_address(
93+
vmlinux, handlers, threadfn
94+
)
95+
96+
fields = [
97+
task.pid,
98+
thread_name,
99+
format_hints.Hex(threadfn),
100+
module_name,
101+
symbol_name,
102+
]
103+
yield 0, fields
104+
105+
def run(self):
106+
return renderers.TreeGrid(
107+
[
108+
("TID", int),
109+
("Thread Name", str),
110+
("Handler Address", format_hints.Hex),
111+
("Module", str),
112+
("Symbol", str),
113+
],
114+
self._generator(),
115+
)

0 commit comments

Comments
 (0)