stateless classmethods

This commit is contained in:
Abyss Watcher
2025-01-11 14:05:36 +01:00
parent d956742db9
commit 0e4e751844
@@ -17,26 +17,22 @@ class Tainting(interfaces.configuration.VersionableInterface):
framework.require_interface_version(*_required_framework_version)
def __init__(
self,
@classmethod
def _get_kernel_taint_flags_list(
cls,
context: interfaces.context.ContextInterface,
kernel_module_name: str,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)
self._kernel = context.modules[kernel_module_name]
@property
def _kernel_taint_flags_list(
self,
) -> Optional[List[interfaces.objects.ObjectInterface]]:
if self._kernel.has_symbol("taint_flags"):
return list(self._kernel.object_from_symbol("taint_flags"))
kernel = context.modules[kernel_module_name]
if kernel.has_symbol("taint_flags"):
return list(kernel.object_from_symbol("taint_flags"))
return None
@classmethod
def _module_flags_taint_pre_4_10_rc1(
self, taints: int, is_module: bool = False
cls,
taints: int,
is_module: bool = False,
) -> str:
"""Convert the module's taints value to a 1-1 character mapping.
Relies on statically defined taints mappings in the framework.
@@ -58,8 +54,13 @@ class Tainting(interfaces.configuration.VersionableInterface):
return taints_string
@classmethod
def _module_flags_taint_post_4_10_rc1(
self, taints: int, is_module: bool = False
cls,
context: interfaces.context.ContextInterface,
kernel_module_name: str,
taints: int,
is_module: bool = False,
) -> str:
"""Convert the module's taints value to a 1-1 character mapping.
Relies on kernel symbol embedded taints definitions.
@@ -78,7 +79,9 @@ class Tainting(interfaces.configuration.VersionableInterface):
The raw taints string.
"""
taints_string = ""
for taint_bit, taint_flag in enumerate(self._kernel_taint_flags_list):
for taint_bit, taint_flag in enumerate(
cls._get_kernel_taint_flags_list(context, kernel_module_name)
):
if is_module and not taint_flag.module:
continue
c_true = chr(taint_flag.c_true)
@@ -90,7 +93,14 @@ class Tainting(interfaces.configuration.VersionableInterface):
return taints_string
def get_taints_as_plain_string(self, taints: int, is_module: bool = False) -> str:
@classmethod
def get_taints_as_plain_string(
cls,
context: interfaces.context.ContextInterface,
kernel_module_name: str,
taints: int,
is_module: bool = False,
) -> str:
"""Convert the taints value to a 1-1 character mapping.
Args:
@@ -103,11 +113,22 @@ class Tainting(interfaces.configuration.VersionableInterface):
- module_flags_taint kernel function
"""
if self._kernel_taint_flags_list:
return self._module_flags_taint_post_4_10_rc1(taints, is_module)
return self._module_flags_taint_pre_4_10_rc1(taints, is_module)
if cls._get_kernel_taint_flags_list(context, kernel_module_name):
return cls._module_flags_taint_post_4_10_rc1(
context, kernel_module_name, taints, is_module
)
return cls._module_flags_taint_pre_4_10_rc1(
context, kernel_module_name, taints, is_module
)
def get_taints_parsed(self, taints: int, is_module: bool = False) -> List[str]:
@classmethod
def get_taints_parsed(
cls,
context: interfaces.context.ContextInterface,
kernel_module_name: str,
taints: int,
is_module: bool = False,
) -> List[str]:
"""Convert the taints string to a 1-1 descriptor mapping.
Args:
@@ -121,7 +142,9 @@ class Tainting(interfaces.configuration.VersionableInterface):
- module_flags_taint kernel function
"""
comprehensive_taints = []
for character in self.get_taints_as_plain_string(taints, is_module):
for character in cls.get_taints_as_plain_string(
context, kernel_module_name, taints, is_module
):
taint_flag = linux_constants.TAINT_FLAGS.get(character)
if not taint_flag:
comprehensive_taints.append(f"<UNKNOWN_TAINT_CHAR_{character}>")