Skip to contentSkip to conversation

module: Modify module_flags() to accept show_state argument

PATCH Superseded Aaron Tomlin 3 participants 9 messages Jun 27, 2022 – Jul 6, 2022 Module support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 module: Modify module_flags() to accept show_state argument

No functional change.

With this patch a given module's state information (i.e. 'mod->state') can be omitted from the specified buffer. Please note that this is in preparation to include the last unloaded module's taint flag(s), if available.

---
 kernel/module/internal.h | 2 +-
 kernel/module/main.c     | 8 ++++----
 kernel/module/procfs.c   | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)
kernel/module/internal.h +1 -1
index bc5507ab8450..60312f23c7d0 100644
@@ -100,7 +100,7 @@ struct module *find_module_all(const char *name, size_t len, bool even_unformed)
 int cmp_name(const void *name, const void *sym);
 long module_get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr,
 		       unsigned int section);
-char *module_flags(struct module *mod, char *buf);
+char *module_flags(struct module *mod, char *buf, bool show_state);
 size_t module_flags_taint(unsigned long taints, char *buf);
 
 static inline void module_assert_mutex_or_preempt(void)
kernel/module/main.c +4 -4
index fed58d30725d..dcb83cf18d84 100644
@@ -2970,7 +2970,7 @@ static void cfi_cleanup(struct module *mod)
 }
 
 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
-char *module_flags(struct module *mod, char *buf)
+char *module_flags(struct module *mod, char *buf, bool show_state)
 {
 	int bx = 0;
 
@@ -2981,10 +2981,10 @@ char *module_flags(struct module *mod, char *buf)
 		buf[bx++] = '(';
 		bx += module_flags_taint(mod->taints, buf + bx);
 		/* Show a - for module-is-being-unloaded */
-		if (mod->state == MODULE_STATE_GOING)
+		if (mod->state == MODULE_STATE_GOING && show_state)
 			buf[bx++] = '-';
 		/* Show a + for module-is-being-loaded */
-		if (mod->state == MODULE_STATE_COMING)
+		if (mod->state == MODULE_STATE_COMING && show_state)
 			buf[bx++] = '+';
 		buf[bx++] = ')';
 	}
@@ -3120,7 +3120,7 @@ void print_modules(void)
 	list_for_each_entry_rcu(mod, &modules, list) {
 		if (mod->state == MODULE_STATE_UNFORMED)
 			continue;
-		pr_cont(" %s%s", mod->name, module_flags(mod, buf));
+		pr_cont(" %s%s", mod->name, module_flags(mod, buf, true));
 	}
 
 	print_unloaded_tainted_modules();
kernel/module/procfs.c +1 -1
index 9a8f4f0f6329..cf5b9f1e6ec4 100644
@@ -91,7 +91,7 @@ static int m_show(struct seq_file *m, void *p)
 
 	/* Taints info */
 	if (mod->taints)
-		seq_printf(m, " %s", module_flags(mod, buf));
+		seq_printf(m, " %s", module_flags(mod, buf, true));
 
 	seq_puts(m, "\n");
 	return 0;
Signature · 1 line
2.34.3
#1 lore raw

PATCH 2/2 module: Show the last unloaded module's taint flag(s)

For diagnostic purposes, this patch, in addition to keeping a record/or track of the last known unloaded module, we now will include the module's taint flag(s) too e.g: " [last unloaded: fpga_mgr_mod(OE)]"

---
 kernel/module/main.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/module/main.c +8 -2
index dcb83cf18d84..0ca6fd38b903 100644
@@ -524,7 +524,12 @@ static struct module_attribute modinfo_##field = {                    \
 MODINFO_ATTR(version);
 MODINFO_ATTR(srcversion);
 
-static char last_unloaded_module[MODULE_NAME_LEN+1];
+/*
+ * Maximum number of characters written by module_flags()
+ * without a module's state information.
+ */
+#define LAST_UNLOADED_MODULE_NAME_LEN (MODULE_NAME_LEN + MODULE_FLAGS_BUF_SIZE - 2 + 1)
+static char last_unloaded_module[LAST_UNLOADED_MODULE_NAME_LEN];
 
 #ifdef CONFIG_MODULE_UNLOAD
 
@@ -694,6 +699,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
 {
 	struct module *mod;
 	char name[MODULE_NAME_LEN];
+	char buf[LAST_UNLOADED_MODULE_NAME_LEN];
 	int ret, forced = 0;
 
 	if (!capable(CAP_SYS_MODULE) || modules_disabled)
@@ -753,8 +759,8 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
 
 	async_synchronize_full();
 
-	/* Store the name of the last unloaded module for diagnostic purposes */
 	strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
+	strcat(last_unloaded_module, module_flags(mod, buf, false));
 
 	free_module(mod);
 	/* someone could wait for the module in add_unformed_module() */
Signature · 1 line
2.34.3
#2 lore raw

Re: [PATCH 2/2] module: Show the last unloaded module's taint flag(s)

Le 27/06/2022 à 18:40, Aaron Tomlin a écrit :

Quoted 36 lines

For diagnostic purposes, this patch, in addition to keeping a record/or track of the last known unloaded module, we now will include the module's taint flag(s) too e.g: " [last unloaded: fpga_mgr_mod(OE)]"

---
  kernel/module/main.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
kernel/module/main.c +7 -1
index dcb83cf18d84..0ca6fd38b903 100644
@@ -524,7 +524,12 @@ static struct module_attribute modinfo_##field = {                    \
  MODINFO_ATTR(version);
  MODINFO_ATTR(srcversion);
 
-static char last_unloaded_module[MODULE_NAME_LEN+1];
+/*
+ * Maximum number of characters written by module_flags()
+ * without a module's state information.
+ */
+#define LAST_UNLOADED_MODULE_NAME_LEN (MODULE_NAME_LEN + MODULE_FLAGS_BUF_SIZE - 2 + 1)
+static char last_unloaded_module[LAST_UNLOADED_MODULE_NAME_LEN];
 
  #ifdef CONFIG_MODULE_UNLOAD
 
@@ -694,6 +699,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
  {
  	struct module *mod;
  	char name[MODULE_NAME_LEN];
+	char buf[LAST_UNLOADED_MODULE_NAME_LEN];
  	int ret, forced = 0;
 
  	if (!capable(CAP_SYS_MODULE) || modules_disabled)

kernel/module/main.c SYSCALL_DEFINE2(delete_module, const char __user *, name_user, · line 763

Quoted 6 lines
kernel/module/main.c +1 -1
@@ -753,8 +759,8 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
 
  	async_synchronize_full();
 
-	/* Store the name of the last unloaded module for diagnostic purposes */
  	strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
+	strcat(last_unloaded_module, module_flags(mod, buf, false));

You replace a bounded string copy by an unbounded strict contat.

Should you use strlcat() instead ?

Quoted 3 lines
kernel/module/main.c +0 -0
  	free_module(mod);
  	/* someone could wait for the module in add_unformed_module() */
#3 lore raw

Re: [PATCH 2/2] module: Show the last unloaded module's taint flag(s)

Quoted 7 lines · Christophe Leroy

kernel/module/main.c SYSCALL_DEFINE2(delete_module, const char __user *, name_user, · line 763

Quoted 3 lines
kernel/module/main.c +1 -1
-	/* Store the name of the last unloaded module for diagnostic purposes */
  	strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
+	strcat(last_unloaded_module, module_flags(mod, buf, false));

You replace a bounded string copy by an unbounded strict contat.

Should you use strlcat() instead ?

Hi Christophe,

Why? If I understand correctly, both the destination and source string are NULL-terminated (see module_flags()). So, strcat() should be sufficient.

Kind regards,

Signature · 1 line
Aaron Tomlin
#4 lore raw

Re: [PATCH 2/2] module: Show the last unloaded module's taint flag(s)

Le 28/06/2022 à 09:47, Aaron Tomlin a écrit :

Quoted 7 lines · Christophe Leroy
Quoted 6 lines

kernel/module/main.c SYSCALL_DEFINE2(delete_module, const char __user *, name_user, · line 763

Quoted 3 lines
kernel/module/main.c +1 -1
-	/* Store the name of the last unloaded module for diagnostic purposes */
   	strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
+	strcat(last_unloaded_module, module_flags(mod, buf, false));

You replace a bounded string copy by an unbounded strict contat.

Quoted 7 lines
Quoted 1 line

Should you use strlcat() instead ?

Hi Christophe,

Why? If I understand correctly, both the destination and source string are NULL-terminated (see module_flags()). So, strcat() should be sufficient.

Are you sure you will never ever end up with a string longer than the length of last_unloaded_module ?

#5 lore raw

Re: [PATCH 2/2] module: Show the last unloaded module's taint flag(s)

Quoted 2 lines · Christophe Leroy

Are you sure you will never ever end up with a string longer than the length of last_unloaded_module ?

If I am not mistaken, the length of 'last_unloaded_module' has been increased to accommodate for: the maximum module name, total module flag count, '(', ')' and '\0'.

Kind regards,

Signature · 1 line
Aaron Tomlin
#6 lore raw

Re: [PATCH 2/2] module: Show the last unloaded module's taint flag(s)

In reply to Aaron Tomlin
Quoted 7 lines · Aaron Tomlin
Quoted 6 lines · Christophe Leroy

kernel/module/main.c SYSCALL_DEFINE2(delete_module, const char __user *, name_user, · line 763

Quoted 3 lines
kernel/module/main.c +1 -1
-	/* Store the name of the last unloaded module for diagnostic purposes */
  	strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
+	strcat(last_unloaded_module, module_flags(mod, buf, false));

You replace a bounded string copy by an unbounded strict contat.

Quoted 6 lines
Quoted 1 line

Should you use strlcat() instead ?

Hi Christophe,

Why? If I understand correctly, both the destination and source string are NULL-terminated (see module_flags()). So, strcat() should be sufficient.

Hi Luis,

Any thoughts on this? As far as I can tell, the destination string cannot be overwritten; albeit, I can use the safer option if you prefer.

Kind regards,

Signature · 1 line
Aaron Tomlin
maintainer
#7 lore raw

Re: [PATCH 2/2] module: Show the last unloaded module's taint flag(s)

Quoted 12 lines · Aaron Tomlin
Quoted 11 lines · Aaron Tomlin
Quoted 7 lines · Christophe Leroy

kernel/module/main.c SYSCALL_DEFINE2(delete_module, const char __user *, name_user, · line 763

Quoted 3 lines
kernel/module/main.c +1 -1
-	/* Store the name of the last unloaded module for diagnostic purposes */
  	strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
+	strcat(last_unloaded_module, module_flags(mod, buf, false));

You replace a bounded string copy by an unbounded strict contat.

Should you use strlcat() instead ?

Hi Christophe,

Quoted 7 lines
Quoted 2 lines

Why? If I understand correctly, both the destination and source string are NULL-terminated (see module_flags()). So, strcat() should be sufficient.

Hi Luis,

Any thoughts on this? As far as I can tell, the destination string cannot be overwritten; albeit, I can use the safer option if you prefer.

Yeah I don't like this as-is, it makes it not so easy to follow, I'll reply in my review of your patch now.

Luis

maintainer
#8 lore raw

Re: [PATCH 2/2] module: Show the last unloaded module's taint flag(s)

In reply to Aaron Tomlin
Quoted 14 lines · Aaron Tomlin

For diagnostic purposes, this patch, in addition to keeping a record/or track of the last known unloaded module, we now will include the module's taint flag(s) too e.g: " [last unloaded: fpga_mgr_mod(OE)]"

---
 kernel/module/main.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/module/main.c +0 -0
index dcb83cf18d84..0ca6fd38b903 100644

kernel/module/main.c static struct module_attribute modinfo_##field = { \ · line 532

Quoted 10 lines
kernel/module/main.c +6 -1
@@ -524,7 +524,12 @@ static struct module_attribute modinfo_##field = {                    \
 MODINFO_ATTR(version);
 MODINFO_ATTR(srcversion);
 
-static char last_unloaded_module[MODULE_NAME_LEN+1];
+/*
+ * Maximum number of characters written by module_flags()
+ * without a module's state information.
+ */
+#define LAST_UNLOADED_MODULE_NAME_LEN (MODULE_NAME_LEN + MODULE_FLAGS_BUF_SIZE - 2 + 1)
+static char last_unloaded_module[LAST_UNLOADED_MODULE_NAME_LEN];

First of all this then confuses a reader easily as one would expect last_unloaded_module always just has the module. Second, "module flags" really throws a user off, as one would expect a module flag is some sort of parameter, but in reality these are just taint flags.

So I'd much prefer we split the taint flags out to its own buffer and dump the data to it alone.

The first patch seems sensible.

Luis

Quoted 23 lines
kernel/module/main.c +2 -1
 #ifdef CONFIG_MODULE_UNLOAD
 
@@ -694,6 +699,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
 {
 	struct module *mod;
 	char name[MODULE_NAME_LEN];
+	char buf[LAST_UNLOADED_MODULE_NAME_LEN];
 	int ret, forced = 0;
 
 	if (!capable(CAP_SYS_MODULE) || modules_disabled)
@@ -753,8 +759,8 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
 
 	async_synchronize_full();
 
-	/* Store the name of the last unloaded module for diagnostic purposes */
 	strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
+	strcat(last_unloaded_module, module_flags(mod, buf, false));
 
 	free_module(mod);
 	/* someone could wait for the module in add_unformed_module() */
Signature · 1 line
2.34.3