From b28620e7db22f4db4e09c33f8e544b1e697e92d8 Mon Sep 17 00:00:00 2001 From: Kuan-Ying Lee Date: Wed, 12 Apr 2023 14:25:07 +0800 Subject: [PATCH] ANDROID: module: Add vendor hooks Add vendor hook for module init, so we can get memory type and use it to do memory type check for architecture dependent page table setting. To make sure the architecture dependent tables are created correctly, we need to know when module parts are initialized and their attributes. For releasing modules, corresponding tables and attributes should be destroyed and restored. These hooks may be invoked in non-atomic context, so it's necessary to use restricted ones. Bug: 248994334 Change-Id: Ie9f415c36bca1fb98e021522b627e562d27cdef4 Signed-off-by: Kuan-Ying Lee --- drivers/android/vendor_hooks.c | 5 +++++ include/trace/hooks/module.h | 32 ++++++++++++++++++++++++++++++++ kernel/module/main.c | 10 ++++++++++ 3 files changed, 47 insertions(+) create mode 100644 include/trace/hooks/module.h diff --git a/drivers/android/vendor_hooks.c b/drivers/android/vendor_hooks.c index 0d62f320fa57..3ce018a5950e 100644 --- a/drivers/android/vendor_hooks.c +++ b/drivers/android/vendor_hooks.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -136,6 +137,10 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_commit_creds); EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_exit_creds); EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_override_creds); EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_revert_creds); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_set_module_core_rw_nx); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_set_module_init_rw_nx); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_set_module_permit_before_init); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_set_module_permit_after_init); EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_selinux_is_initialized); EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_shmem_get_folio); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_check_mmap_file); diff --git a/include/trace/hooks/module.h b/include/trace/hooks/module.h new file mode 100644 index 000000000000..780e767cc23e --- /dev/null +++ b/include/trace/hooks/module.h @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM module + +#define TRACE_INCLUDE_PATH trace/hooks +#if !defined(_TRACE_HOOK_MODULE_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_HOOK_MODULE_H +#include +/* + * Following tracepoints are not exported in tracefs and provide a + * mechanism for vendor modules to hook and extend functionality + */ +struct module; +DECLARE_RESTRICTED_HOOK(android_rvh_set_module_permit_before_init, + TP_PROTO(const struct module *mod), + TP_ARGS(mod), 1); + +DECLARE_RESTRICTED_HOOK(android_rvh_set_module_permit_after_init, + TP_PROTO(const struct module *mod), + TP_ARGS(mod), 1); + +DECLARE_RESTRICTED_HOOK(android_rvh_set_module_core_rw_nx, + TP_PROTO(const struct module *mod), + TP_ARGS(mod), 1); + +DECLARE_RESTRICTED_HOOK(android_rvh_set_module_init_rw_nx, + TP_PROTO(const struct module *mod), + TP_ARGS(mod), 1); + +#endif /* _TRACE_HOOK_MODULE_H */ +/* This part must be outside protection */ +#include diff --git a/kernel/module/main.c b/kernel/module/main.c index 194e865f6adc..0f18b6ee980c 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -60,6 +60,9 @@ #define CREATE_TRACE_POINTS #include +#undef CREATE_TRACE_POINTS +#include + /* * Mutex protects: * 1) List of modules (also safely readable with preempt_disable), @@ -1208,6 +1211,7 @@ static void free_module(struct module *mod) /* This may be empty, but that's OK */ module_arch_freeing_init(mod); + trace_android_rvh_set_module_init_rw_nx(mod); module_memfree(mod->init_layout.base); kfree(mod->args); percpu_modfree(mod); @@ -1216,6 +1220,7 @@ static void free_module(struct module *mod) lockdep_free_key_range(mod->data_layout.base, mod->data_layout.size); /* Finally, free the core (containing the module structure) */ + trace_android_rvh_set_module_core_rw_nx(mod); module_memfree(mod->core_layout.base); #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC vfree(mod->data_layout.base); @@ -2373,7 +2378,9 @@ static void module_deallocate(struct module *mod, struct load_info *info) { percpu_modfree(mod); module_arch_freeing_init(mod); + trace_android_rvh_set_module_init_rw_nx(mod); module_memfree(mod->init_layout.base); + trace_android_rvh_set_module_core_rw_nx(mod); module_memfree(mod->core_layout.base); #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC vfree(mod->data_layout.base); @@ -2526,8 +2533,10 @@ static noinline int do_init_module(struct module *mod) rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms); #endif module_enable_ro(mod, true); + trace_android_rvh_set_module_permit_after_init(mod); mod_tree_remove_init(mod); module_arch_freeing_init(mod); + trace_android_rvh_set_module_init_rw_nx(mod); mod->init_layout.base = NULL; mod->init_layout.size = 0; mod->init_layout.ro_size = 0; @@ -2657,6 +2666,7 @@ static int complete_formation(struct module *mod, struct load_info *info) module_enable_ro(mod, false); module_enable_nx(mod); module_enable_x(mod); + trace_android_rvh_set_module_permit_before_init(mod); /* * Mark state as coming so strong_try_module_get() ignores us,