模块加载load_module()函数调用的功能函数如下:
module_sig_check()函数用于检查模块的签名验证elf_header_check()函数用于检查模块的elf头和区段有效性layout_and_allocate()函数用于分配内核内存空间,把模块相关的节区复制过来audit_log_kern_module()函数用于检查是否开启了安全审计add_unformed_module()函数用于判断模块是否已经加载,若没有则将模块添加到内核模块链表中percpu_modalloc()函数用于申请percpu变量内存,这个变量每个cpu都有一份,无相独立module_unload_init()函数用于初始化化模块依赖和引用链表,并对模块引用计数加1init_param_lock()函数用于初始化互斥锁find_module_sections()函数用于遍历模块中的其它分段check_module_license_and_versions()函数用于检测模块的crc是否正确,license授权是否正确setup_modinfo()函数根据.modinfo段设置模块信息simplify_symbols()函数用于将模块内存中的静态链接重定位表中的符号,全部指向其真实的内存地址apply_relocations()函数用于遍历目标文件中的所有内存节区的重定位节区,并遍历每个节区中的每个静态链接重定位表项,对其做静态链接post_relocation()函数用于对重定位后的percpu变量重新赋值,并将即将加载完成的模块的符号加入到内核模块的符号链表中,如果成功加载此模块且内核配置了config_kallsyms,那么在/proc/kallsyms下可以看到此模块的符号flush_module_icache()函数用于刷新模块的init_layout和core_layout的cache。strndup_user()函数用于复制用户空间的参数到内核空间dynamic_debug_setup()函数用于处理debug节区,需要开启内核config_dynamic_debug才会启用ftrace_module_init()函数需要开启相关的ftrace配置complete_formation()函数用于遍历模块中的所有导出函数,并检查在内核中是否有同名的导出符号,为模块的init_layout/core_layout做ronx保护prepare_coming_module()函数用于发送模块加载通知链parse_args()函数用于参数解析与sysfs、livepatch的设置mod_sysfs_setup()函数用于在sysfs中创建模块相应的项is_livepatch_module()函数用于检查模块是否是热补丁模块copy_module_elf()函数用于拷贝模块的elf头free_copy()函数用于释放最初内核申请的用于保存模块原文件信息的内存trace_module_load()函数用于加载一些和trace相关的内容,便于后期跟踪调试do_init_module()函数用于调用模块的初始化函数static int load_module(struct load_info *info, const char __user *uargs, int flags){ struct module *mod; long err; char *after_dashes; err = module_sig_check(info, flags); if (err) goto free_copy; err = elf_header_check(info); if (err) goto free_copy; /* figure out module layout, and allocate all the memory. */ mod = layout_and_allocate(info, flags); if (is_err(mod)) { err = ptr_err(mod); goto free_copy; } audit_log_kern_module(mod- >name); /* reserve our place in the list. */ err = add_unformed_module(mod); if (err) goto free_module;#ifdef config_module_sig mod- >sig_ok = info- >sig_ok; if (!mod- >sig_ok) { pr_notice_once(%s: module verification failed: signature and/or required key missing - tainting kernel\\n, mod- >name); add_taint_module(mod, taint_unsigned_module, lockdep_still_ok); }#endif /* to avoid stressing percpu allocator, do this once we're unique. */ err = percpu_modalloc(mod, info); if (err) goto unlink_mod; /* now module is in final location, initialize linked lists, etc. */ err = module_unload_init(mod); if (err) goto unlink_mod; init_param_lock(mod); /* now we've got everything in the final locations, we can * find optional sections. */ err = find_module_sections(mod, info); if (err) goto free_unload; err = check_module_license_and_versions(mod); if (err) goto free_unload; /* set up modinfo_attr fields */ setup_modinfo(mod, info); /* fix up syms, so that st_value is a pointer to location. */ err = simplify_symbols(mod, info); if (err < 0) goto free_modinfo; err = apply_relocations(mod, info); if (err < 0) goto free_modinfo; err = post_relocation(mod, info); if (err args = strndup_user(uargs, ~0ul > > 1); if (is_err(mod- >args)) { err = ptr_err(mod- >args); goto free_arch_cleanup; } dynamic_debug_setup(mod, info- >debug, info- >num_debug); /* ftrace init must be called in the module_state_unformed state */ ftrace_module_init(mod); /* finally it's fully formed, ready to start executing. */ err = complete_formation(mod, info); if (err) goto ddebug_cleanup; err = prepare_coming_module(mod); if (err) goto bug_cleanup; /* module is ready to execute: parsing args may do that. */ after_dashes = parse_args(mod- >name, mod- >args, mod- >kp, mod- >num_kp, -32768, 32767, mod, unknown_module_param_cb); if (is_err(after_dashes)) { err = ptr_err(after_dashes); goto coming_cleanup; } else if (after_dashes) { pr_warn(%s: parameters '%s' after `--' ignored\\n, mod- >name, after_dashes); } /* link in to sysfs. */ err = mod_sysfs_setup(mod, info, mod- >kp, mod- >num_kp); if (err < 0) goto coming_cleanup; if (is_livepatch_module(mod)) { err = copy_module_elf(mod, info); if (err state = module_state_going; destroy_params(mod- >kp, mod- >num_kp); blocking_notifier_call_chain(&module_notify_list, module_state_going, mod); klp_module_going(mod); bug_cleanup: mod- >state = module_state_going; /* module_bug_cleanup needs module_mutex protection */ mutex_lock(&module_mutex); module_bug_cleanup(mod); mutex_unlock(&module_mutex); /* we can't deallocate the module until we clear memory protection */ module_disable_ro(mod); module_disable_nx(mod); ddebug_cleanup: dynamic_debug_remove(mod, info- >debug); synchronize_sched(); kfree(mod- >args); free_arch_cleanup: module_arch_cleanup(mod); free_modinfo: free_modinfo(mod); free_unload: module_unload_free(mod); unlink_mod: mutex_lock(&module_mutex); /* unlink carefully: kallsyms could be walking list. */ list_del_rcu(&mod- >list); mod_tree_remove(mod); wake_up_all(&module_wq); /* wait for rcu-sched synchronizing before releasing mod- >list. */ synchronize_sched(); mutex_unlock(&module_mutex); free_module: /* * ftrace needs to clean up what it initialized. * this does nothing if ftrace_module_init() wasn't called, * but it must be called outside of module_mutex. */ ftrace_release_mod(mod); /* free lock-classes; relies on the preceding sync_rcu() */ lockdep_free_key_range(mod- >core_layout.base, mod- >core_layout.size); module_deallocate(mod, info); free_copy: free_copy(info); return err;}module_sig_check()函数首先判断elf模块文件长度是否大于签名字符串长度。然后比较该elf格式的模块文件是否在文件末尾有signature string,若有的话则将该文件长度减去该字符串长度。最后调用mod_verify_sig()函数验证模块签名。
static int module_sig_check(struct load_info *info, int flags){ int err = -enokey; const unsigned long markerlen = sizeof(module_sig_string) - 1; const void *mod = info- >hdr; /* * require flags == 0, as a module with version information * removed is no longer the module that was signed */ if (flags == 0 && info- >len > markerlen && memcmp(mod + info- >len - markerlen, module_sig_string, markerlen) == 0) { /* we truncate the module to discard the signature */ info- >len -= markerlen; err = mod_verify_sig(mod, &info- >len); } if (!err) { info- >sig_ok = true; return 0; } /* not having a signature is only an error if we're strict. */ if (err == -enokey && !sig_enforce) err = 0; return err;}mod_verify_sig()函数首先提取模块签名字符串中的module_signature结构并保存在变量ms中,然后修改模块的文件长度,然后获取签名数据长度保存在变量sig_len变量中,最后调用verify_pkcs7_signature()函数对内核模块中的pkcs#7格式的消息进行验证。
int mod_verify_sig(const void *mod, unsigned long *_modlen){ struct module_signature ms; size_t modlen = *_modlen, sig_len; pr_devel(== >%s(,%zu)\\n, __func__, modlen); if (modlen = modlen) return -ebadmsg; modlen -= sig_len; *_modlen = modlen; if (ms.id_type != pkey_id_pkcs7) { pr_err(module is not signed with expected pkcs#7 message\\n); return -enopkg; } if (ms.algo != 0 || ms.hash != 0 || ms.signer_len != 0 || ms.key_id_len != 0 || ms.__pad[0] != 0 || ms.__pad[1] != 0 || ms.__pad[2] != 0) { pr_err(pkcs#7 signature info has unexpected non-zero params\\n); return -ebadmsg; } return verify_pkcs7_signature(mod, modlen, mod + modlen, sig_len, null, verifying_module_signature, null, null);}elf_header_check()函数检查elf文件头,以及文件类型,架构以及节区大小是否设置正确,检查节区的偏移地址
static int elf_header_check(struct load_info *info){ if (info- >len hdr))) return -enoexec; if (memcmp(info- >hdr- >e_ident, elfmag, selfmag) != 0 || info- >hdr- >e_type != et_rel || !elf_check_arch(info- >hdr) || info- >hdr- >e_shentsize != sizeof(elf_shdr)) return -enoexec; if (info- >hdr- >e_shoff >= info- >len || (info- >hdr- >e_shnum * sizeof(elf_shdr) > info- >len - info- >hdr- >e_shoff)) return -enoexec; return 0;}layout_and_allocate()函数调用setup_load_info()函数初始化load_info结构体变量info,调用blacklisted()函数检查模块名字字符串是否在模块加载黑名单中,调用check_modinfo()函数检查modinfo节区信息,module_frob_arch_sections()函数在arm32架构下为空,调用find_sec()函数为.data..ro_after_init节区段加上ro_after_init属性,调用layout_sections()函数计算模块内存elf节区最终需要的内存大小和总大小,并记录到core_layout/init_layout中,调用layout_symtab()函数在init_layout为静态链接符号表,core_layout为核心符号表预留空间,调用move_module()函数分配模块内存布局,复制模块二进制代码到真正运行时内存,修复所有模块内存elf对应节区头部表指向内存布局,切换模块的struct module结构体变量mod指向最终模块内存布局中的位置,调用kmemleak_load_module()函数扫描检查各个节区的内存分布。
static struct module *layout_and_allocate(struct load_info *info, int flags){ /* module within temporary copy. */ struct module *mod; unsigned int ndx; int err; mod = setup_load_info(info, flags); if (is_err(mod)) return mod; if (blacklisted(info- >name)) return err_ptr(-eperm); err = check_modinfo(mod, info, flags); if (err) return err_ptr(err); /* allow arches to frob section contents and sizes. */ err = module_frob_arch_sections(info- >hdr, info- >sechdrs, info- >secstrings, mod); if (err sechdrs[info- >index.pcpu].sh_flags &= ~(unsigned long)shf_alloc; /* * mark ro_after_init section with shf_ro_after_init so that * layout_sections() can put it in the right place. * note: ro_after_init sections also have shf_{write,alloc} set. */ ndx = find_sec(info, .data..ro_after_init); if (ndx) info- >sechdrs[ndx].sh_flags |= shf_ro_after_init; /* determine total sizes, and put offsets in sh_entsize. for now this is done generically; there doesn't appear to be any special cases for the architectures. */ layout_sections(mod, info); layout_symtab(mod, info); /* allocate and move to the final place */ err = move_module(mod, info); if (err) return err_ptr(err); /* module has been copied to its final place now: return it. */ mod = (void *)info- >sechdrs[info- >index.mod].sh_addr; kmemleak_load_module(mod, info); return mod;}add_unformed_module()函数调用find_module_all()函数遍历内核中的所有模块检查是否有同名模块已经存在或正在加载,最后调用mod_update_bounds()函数更新内核已有模块的地址范围边界。
static int add_unformed_module(struct module *mod){ int err; struct module *old; mod- >state = module_state_unformed;again: mutex_lock(&module_mutex); old = find_module_all(mod- >name, strlen(mod- >name), true); if (old != null) { if (old- >state != module_state_live) { /* wait in case it fails to load. */ mutex_unlock(&module_mutex); err = wait_event_interruptible(module_wq, finished_loading(mod- >name)); if (err) goto out_unlocked; goto again; } err = -eexist; goto out; } mod_update_bounds(mod); list_add_rcu(&mod- >list, &modules); mod_tree_insert(mod); err = 0;out: mutex_unlock(&module_mutex);out_unlocked: return err;}未完待续。。。。。。总结本篇主要介绍了load_module()函数的前半部分实现,主要是模块的签名验签和拷贝模块到真正运行时内存。
华为Balong 5000率先通过中国5G增强技术研发试验终端芯片测试
华为mate9最新消息,华为Mate9力推荐“托帕蓝”
4G价格入手5G旗舰!iQOO Neo3加量不加价,618换机的不二之选
英特尔10nm量产推迟,AMD赢得同类产品的制造工艺优势
两步避免在BUCK电路中出现“地弹”
Linux内核模块加载过程解析(2)
电源线结构名称
透视笔记本键盘原理(故障自己DIY )
如何搭配室内全彩屏规格才是性价比之选?
天地一体化网络迎来发展新机遇,应对预定式全覆盖组网挑战
关于在挑选工业插座时的一些提议
TASCAM公司推出全新Series话放产品
基于OpenHarmony的OneNet移动云IoT服务实现
O-RAN为何贴有5G标签
新品手机扎堆预热!联想拯救者坐不住了:我们首发双线性马达
电线的接线方法详细图文讲解
常见的几种汽车电感
人工智能如何影响5G网络标准
如何实现PCB精度深度铣控制
Splashtop: 远程制作的游戏规则改变者