lib: code tagging module support

Add support for code tagging from dynamically loaded modules.

Link: https://lkml.kernel.org/r/20240321163705.3067592-12-surenb@google.com
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Co-developed-by: Kent Overstreet <kent.overstreet@linux.dev>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Tested-by: Kees Cook <keescook@chromium.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Alex Gaynor <alex.gaynor@gmail.com>
Cc: Alice Ryhl <aliceryhl@google.com>
Cc: Andreas Hindborg <a.hindborg@samsung.com>
Cc: Benno Lossin <benno.lossin@proton.me>
Cc: "Björn Roy Baron" <bjorn3_gh@protonmail.com>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: Dennis Zhou <dennis@kernel.org>
Cc: Gary Guo <gary@garyguo.net>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Wedson Almeida Filho <wedsonaf@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
Suren Baghdasaryan 2024-03-21 09:36:33 -07:00 committed by Andrew Morton
parent 916cc5167c
commit a473573964
3 changed files with 72 additions and 2 deletions

View File

@ -33,6 +33,10 @@ union codetag_ref {
struct codetag_type_desc { struct codetag_type_desc {
const char *section; const char *section;
size_t tag_size; size_t tag_size;
void (*module_load)(struct codetag_type *cttype,
struct codetag_module *cmod);
void (*module_unload)(struct codetag_type *cttype,
struct codetag_module *cmod);
}; };
struct codetag_iterator { struct codetag_iterator {
@ -65,4 +69,12 @@ void codetag_to_text(struct seq_buf *out, struct codetag *ct);
struct codetag_type * struct codetag_type *
codetag_register_type(const struct codetag_type_desc *desc); codetag_register_type(const struct codetag_type_desc *desc);
#if defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES)
void codetag_load_module(struct module *mod);
void codetag_unload_module(struct module *mod);
#else
static inline void codetag_load_module(struct module *mod) {}
static inline void codetag_unload_module(struct module *mod) {}
#endif
#endif /* _LINUX_CODETAG_H */ #endif /* _LINUX_CODETAG_H */

View File

@ -56,6 +56,7 @@
#include <linux/dynamic_debug.h> #include <linux/dynamic_debug.h>
#include <linux/audit.h> #include <linux/audit.h>
#include <linux/cfi.h> #include <linux/cfi.h>
#include <linux/codetag.h>
#include <linux/debugfs.h> #include <linux/debugfs.h>
#include <uapi/linux/module.h> #include <uapi/linux/module.h>
#include "internal.h" #include "internal.h"
@ -1242,6 +1243,7 @@ static void free_module(struct module *mod)
{ {
trace_module_free(mod); trace_module_free(mod);
codetag_unload_module(mod);
mod_sysfs_teardown(mod); mod_sysfs_teardown(mod);
/* /*
@ -2995,6 +2997,8 @@ static int load_module(struct load_info *info, const char __user *uargs,
/* Get rid of temporary copy. */ /* Get rid of temporary copy. */
free_copy(info, flags); free_copy(info, flags);
codetag_load_module(mod);
/* Done! */ /* Done! */
trace_module_load(mod); trace_module_load(mod);

View File

@ -124,15 +124,20 @@ static void *get_symbol(struct module *mod, const char *prefix, const char *name
{ {
DECLARE_SEQ_BUF(sb, KSYM_NAME_LEN); DECLARE_SEQ_BUF(sb, KSYM_NAME_LEN);
const char *buf; const char *buf;
void *ret;
seq_buf_printf(&sb, "%s%s", prefix, name); seq_buf_printf(&sb, "%s%s", prefix, name);
if (seq_buf_has_overflowed(&sb)) if (seq_buf_has_overflowed(&sb))
return NULL; return NULL;
buf = seq_buf_str(&sb); buf = seq_buf_str(&sb);
return mod ? preempt_disable();
ret = mod ?
(void *)find_kallsyms_symbol_value(mod, buf) : (void *)find_kallsyms_symbol_value(mod, buf) :
(void *)kallsyms_lookup_name(buf); (void *)kallsyms_lookup_name(buf);
preempt_enable();
return ret;
} }
static struct codetag_range get_section_range(struct module *mod, static struct codetag_range get_section_range(struct module *mod,
@ -173,8 +178,11 @@ static int codetag_module_init(struct codetag_type *cttype, struct module *mod)
down_write(&cttype->mod_lock); down_write(&cttype->mod_lock);
err = idr_alloc(&cttype->mod_idr, cmod, 0, 0, GFP_KERNEL); err = idr_alloc(&cttype->mod_idr, cmod, 0, 0, GFP_KERNEL);
if (err >= 0) if (err >= 0) {
cttype->count += range_size(cttype, &range); cttype->count += range_size(cttype, &range);
if (cttype->desc.module_load)
cttype->desc.module_load(cttype, cmod);
}
up_write(&cttype->mod_lock); up_write(&cttype->mod_lock);
if (err < 0) { if (err < 0) {
@ -185,6 +193,52 @@ static int codetag_module_init(struct codetag_type *cttype, struct module *mod)
return 0; return 0;
} }
void codetag_load_module(struct module *mod)
{
struct codetag_type *cttype;
if (!mod)
return;
mutex_lock(&codetag_lock);
list_for_each_entry(cttype, &codetag_types, link)
codetag_module_init(cttype, mod);
mutex_unlock(&codetag_lock);
}
void codetag_unload_module(struct module *mod)
{
struct codetag_type *cttype;
if (!mod)
return;
mutex_lock(&codetag_lock);
list_for_each_entry(cttype, &codetag_types, link) {
struct codetag_module *found = NULL;
struct codetag_module *cmod;
unsigned long mod_id, tmp;
down_write(&cttype->mod_lock);
idr_for_each_entry_ul(&cttype->mod_idr, cmod, tmp, mod_id) {
if (cmod->mod && cmod->mod == mod) {
found = cmod;
break;
}
}
if (found) {
if (cttype->desc.module_unload)
cttype->desc.module_unload(cttype, cmod);
cttype->count -= range_size(cttype, &cmod->range);
idr_remove(&cttype->mod_idr, mod_id);
kfree(cmod);
}
up_write(&cttype->mod_lock);
}
mutex_unlock(&codetag_lock);
}
#else /* CONFIG_MODULES */ #else /* CONFIG_MODULES */
static int codetag_module_init(struct codetag_type *cttype, struct module *mod) { return 0; } static int codetag_module_init(struct codetag_type *cttype, struct module *mod) { return 0; }
#endif /* CONFIG_MODULES */ #endif /* CONFIG_MODULES */