I'm working on a kernel live-patch, some code in the live-patch module looks like this:
void list_checker() {
struct list_head *head, *iter;
head = (struct list_head *)kallsyms_lookup_name("module_name:symbol_name");
for (iter = head->next; iter != head; iter = iter->next) {
// do something.
}
}
This code gets the address of a kernel symbol (which is type struct list_head
) and tries to iterate the list. But for some reason, some nodes in the list may be broken, resulting in the next
pointer of some node being invalid (like NULL, 0xABABABAB, or other random numbers), and dereferencing the next
pointer may cause the kernel to crash.
So is there a way to check whether a pointer is safe to access?
I have checked two previous answers:
How to check a memory address is valid inside Linux kernel?
How can I tell if a virtual address has valid mapping in ARM linux kernel?
They tell me to use virt_addr_valid
. I have some surely accessible address, like 0xFFFFFFFFA032C040, but virt_addr_valid
always returns false, which makes me unable to distinguish "accessible" and "non-accessible" addresses in my live-patch module.