Talk about how Linux kernel invokes your module's init function.
Note: When you view the the slide deck via web browser, the screenshots may be blurred. You can download and view them offline (Screenshots are clear).
2. Agenda
From insmod command
Call path for LKMs init function
.gnu.linkonce.this_module section
Deep Dive into call path
modinfo
Note
Kernel source: 5.10
Mainly focus on the init function call path
3. From `insmod` command
Hello World Kernel Module strace
finit_module() system call loads an ELF image into kernel space
4. From `insmod` command
Hello World Kernel Module strace
finit_module() system call loads an ELF image into kernel space
finit_module()
Load an ELF image into kernel space
Perform symbol relocations
Initialize module parameters to values
Run the modules init function
5. Call path for LKMs init function
finit_module
load_module
do_init_module
do_one_initcall(mod->init)
mod->init();
Analysis
Key: mod->init()
How to assign the address of mod->init()?
kernel_read_file_from_fd
elf_header_check
setup_load_info
blacklisted(info->name)?
cleanup & return
module_sig_check
Y
N
mod = layout_and_allocate(info, ..)
rewrite_section_headers
apply_relocations
12. Deep Dive into call path (1/7)
finit_module
load_module
do_init_module
do_one_initcall(mod->init)
mod->init();
kernel_read_file_from_fd
elf_header_check
setup_load_info
blacklisted(info->name)?
cleanup & return
module_sig_check
Y
N
mod = layout_and_allocate(info, ..)
rewrite_section_headers
apply_relocations
ELF Header
Section 1
.init.text
.exit.text
.gnu.linkonce.this_module
Section n
Section Header 1
.init.text
.exit.text
.gnu.linkonce.this_module
Section Header n
load_info
name
mod
hdr
len
sechdrs
secstrings
strtab
A (kernel addr)
A + e_shoff
18. Deep Dive into call path (7/7)
finit_module
load_module
do_init_module
do_one_initcall(mod->init)
mod->init();
kernel_read_file_from_fd
elf_header_check
setup_load_info
blacklisted(info->name)?
cleanup & return
module_sig_check
Y
N
mod = layout_and_allocate(info, ..)
rewrite_section_headers
apply_relocations
free & cleanup init_layout
Free memory space of init_layout after calling mod->init()