简体   繁体   English

如何访问内核模块中的任何内核符号?

[英]How do I access any kernel symbol in a kernel module?

I am wanting to use the function getname in my kernel module.我想在我的内核模块中使用函数getname It is not exported.它没有导出。 Since I am running into this problem right now, I would like to know how to access and use any kernel symbol that is not exported.由于我现在遇到了这个问题,我想知道如何访问和使用任何未导出的内核符号。 I figure that the steps necessary to use one will differ depending what the symbol is, so I'd like to see how it would be done for a type (eg, a struct), a variable, a table of pointers (like the system call table), and a function.我认为使用一个符号所需的步骤会有所不同,因此我想看看如何为类型(例如,结构)、变量、指针表(如系统调用表)和一个函数。 How can these be done in either of these cases:如何在以下任一情况下完成这些操作:

  • When I know the address of the symbol from System.map or /proc/kallsyms .当我从System.map/proc/kallsyms知道符号的地址时。
  • When I know the name of the symbol and want to use kallsyms_lookup_name in retrieving it.当我知道符号的名称并想使用kallsyms_lookup_name检索它时。

I currently know how to hijack system calls and this requires declaring something like我目前知道如何劫持系统调用,这需要声明类似

asmlinkage <return_type> (*<name_for_system_call>)(<the types of the its arguments separated by commas>);

Would something like that be used?会使用这样的东西吗? In this answer to another question, the example presented by the poster is这个对另一个问题的回答中,海报展示的例子是

#include <linux/kallsyms.h>

static void (*machine_power_off_p)(void);
machine_power_off = (void*) kallsyms_lookup_name("machine_power_off");

But what if the symbol returns a pointer?但是如果符号返回一个指针呢? Would I place an asterisk to the left of (*machine_power_off_p) ?我会在(*machine_power_off_p)的左边放一个星号吗?

#include <linux/fs.h> declares extern struct filename *getname(const char __user *); #include <linux/fs.h>声明extern struct filename *getname(const char __user *); . . A pointer to this function has type struct filename *(*)(const char __user *) .指向此函数的指针的类型为struct filename *(*)(const char __user *) If declaring a variable of that type, the variable name goes after the * in (*) .如果声明该类型的变量,则变量名称位于* in (*) So you can declare a variable of that type and assign the return value of kallsyms_lookup_name("getname") to it as follows:因此,您可以声明该类型的变量并将kallsyms_lookup_name("getname")的返回值分配给它,如下所示:

static struct filename *(*getname_p)(const char __user *);

/* within a function body... */
getname_p = (struct filename *(*)(const char __user *))
            kallsyms_lookup_name("getname");

For your other case where you want to use a numeric address, just replace the kallsyms_lookup_name function call with the actual number ( kallsyms_lookup_name returns the symbol value as a number anyway).对于您想要使用数字地址的其他情况,只需将kallsyms_lookup_name函数调用替换为实际数字( kallsyms_lookup_name将符号值作为数字返回)。

EDIT 2021-01-19编辑 2021-01-19

The GCC typeof extension can be used to copy the prototype of getname from #include <linux/fs.h> to the getname_p pointer as follows: GCC typeof扩展可用于将getname的原型从#include <linux/fs.h>复制到getname_p指针,如下所示:

#include <linux/fs.h>

static typeof(&getname) getname_p;

/* within a function body... */
getname_p = (typeof(&getname))kallsyms_lookup_name("getname");

EDIT 2021-05-17编辑 2021-05-17

From the 5.7 kernel onwards kallsyms_lookup_name and kallsyms_on_each_symbol are no longer exported to loadable kernel modules.从 5.7 内核开始, kallsyms_lookup_namekallsyms_on_each_symbol不再导出到可加载的内核模块。

Accessing not exported function doesn't differ from accessing exported functions, except that you can't resolve its address in kernel.访问未导出的函数与访问导出的函数没有区别,只是您无法在内核中解析其地址。 You can do trick like this:你可以做这样的伎俩:

static void (*your_func)(void);
your_func=0xhex_addr;

or for struct或者对于结构

strucr your_struct{int i;double j} *stru;
stru=0xhex_addr;

Type of a pointer just defines how many bytes would be read or written from or to pointer's address.指针的类型仅定义将从或向指针地址读取或写入的字节数。

For structure or variable hex address even may reside in kernel code segment, but you'll get segmentation fault if you'll try to write something to that struct or var - reading would be legit.对于结构或变量十六进制地址,甚至可能驻留在内核代码段中,但是如果您尝试向该结构或 var 写入内容,则会出现分段错误 - 读取是合法的。 And one more thing... when doing this trick with structure don't forget about structure data alignment.还有一件事......在用结构做这个技巧时不要忘记结构数据对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM