Skip to contentSkip to conversation

NULL terminate over-long /proc/kallsyms symbols

PATCH Andreas Gruenbacher 6 participants 7 messages Jul 5, 2006 – Jul 6, 2006 Module support

lore
Share & tools
lore
#0 lore raw

Got a customer bug report (https://bugzilla.novell.com/190296)
about kernel symbols longer than 127 characters which end up in a string buffer that is not NULL terminated, leading to garbage in /proc/kallsyms. Using strlcpy prevents this from happening, even though such symbols still won't come out right.

A better fix would be to not use a fixed-size buffer, but it's probably not worth the trouble. (Modversion'ed symbols even have a length limit of 60.)

(This patch has been ested on a 2.6.16 kernel.)

kernel/module.c +1 -1
Index: linux-2.6.17/kernel/module.c
===================================================================
@@ -1935,7 +1935,7 @@ struct module *module_get_kallsym(unsign
 		if (symnum < mod->num_symtab) {
 			*value = mod->symtab[symnum].st_value;
 			*type = mod->symtab[symnum].st_info;
-			strncpy(namebuf,
+			strlcpy(namebuf,
 				mod->strtab + mod->symtab[symnum].st_name,
 				127);
 			mutex_unlock(&module_mutex);
#1 lore raw

Got a " You are not authorized to access bug #190296. To see this bug, you must first log in to an account with the appropriate permissions." on the referred bugzilla page.

What kind of symbol uses more than 127 characters, anyways ?

Daniel

Signature · 1 line
What this world needs is a good five-dollar plasma weapon.
#2 lore raw
Quoted 5 lines · Daniel Bonekeeper

Got a " You are not authorized to access bug #190296. To see this bug, you must first log in to an account with the appropriate permissions." on the referred bugzilla page.

What kind of symbol uses more than 127 characters, anyways ?

Yes, good question. Maybe we should just put an upper limit on symbol length in the module postprocessing so people don't do such stupid things.

#3 lore raw
In reply to Daniel Bonekeeper
Quoted 7 lines · Daniel Bonekeeper

Got a " You are not authorized to access bug #190296. To see this bug, you must first log in to an account with the appropriate permissions." on the referred bugzilla page.

What kind of symbol uses more than 127 characters, anyways ?

Maybe C++ mangled names.

I've seen names longer than early machines' main memory.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#4 lore raw
In reply to Andreas Gruenbacher

kernel/module.c struct module *module_get_kallsym(unsign · line 1940

Quoted 12 lines · Andreas Gruenbacher

Index: linux-2.6.17/kernel/module.c
===================================================================

kernel/module.c +1 -1
@@ -1935,7 +1935,7 @@ struct module *module_get_kallsym(unsign
 		if (symnum < mod->num_symtab) {
 			*value = mod->symtab[symnum].st_value;
 			*type = mod->symtab[symnum].st_info;
-			strncpy(namebuf,
+			strlcpy(namebuf,
 				mod->strtab + mod->symtab[symnum].st_name,
 				127);

Just a minor point: you probably also want to change 127 to 128. Unfortunately sizeof does not work here, despite the declaration.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#5 lore raw
In reply to Andreas Gruenbacher
Quoted 20 lines · Andreas Gruenbacher

Got a customer bug report (https://bugzilla.novell.com/190296)
about kernel symbols longer than 127 characters which end up in a string buffer that is not NULL terminated, leading to garbage in /proc/kallsyms. Using strlcpy prevents this from happening, even though such symbols still won't come out right.

A better fix would be to not use a fixed-size buffer, but it's probably not worth the trouble. (Modversion'ed symbols even have a length limit of 60.)

(This patch has been ested on a 2.6.16 kernel.)

Index: linux-2.6.17/kernel/module.c
===================================================================

kernel/module.c struct module *module_get_kallsym(unsign · line 1941

Quoted 8 lines
kernel/module.c +1 -1
@@ -1935,7 +1935,7 @@ struct module *module_get_kallsym(unsign
 		if (symnum < mod->num_symtab) {
 			*value = mod->symtab[symnum].st_value;
 			*type = mod->symtab[symnum].st_info;
-			strncpy(namebuf,
+			strlcpy(namebuf,
 				mod->strtab + mod->symtab[symnum].st_name,
 				127);
 			mutex_unlock(&module_mutex);

Yeah, that assume-the-caller-gave-us-a-128-byte-buffer is a bit rude. How about this?

 include/linux/module.h |    6 ++----
 kernel/kallsyms.c      |    4 ++--
 kernel/module.c        |   11 ++++-------
 3 files changed, 8 insertions(+), 13 deletions(-)
kernel/module.c renamed from kernel/module.c~null-terminate-over-long-proc-kallsyms-symbols +4 -7
diff -puN kernel/module.c~null-terminate-over-long-proc-kallsyms-symbols kernel/module.c
@@ -2019,10 +2019,8 @@ const char *module_address_lookup(unsign
 	return NULL;
 }
 
-struct module *module_get_kallsym(unsigned int symnum,
-				  unsigned long *value,
-				  char *type,
-				  char namebuf[128])
+struct module *module_get_kallsym(unsigned int symnum, unsigned long *value,
+				char *type, char *name, size_t namelen)
 {
 	struct module *mod;
 
@@ -2031,9 +2029,8 @@ struct module *module_get_kallsym(unsign
 		if (symnum < mod->num_symtab) {
 			*value = mod->symtab[symnum].st_value;
 			*type = mod->symtab[symnum].st_info;
-			strncpy(namebuf,
-				mod->strtab + mod->symtab[symnum].st_name,
-				127);
+			strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
+				namelen);
 			mutex_unlock(&module_mutex);
 			return mod;
 		}
include/linux/module.h renamed from include/linux/module.h~null-terminate-over-long-proc-kallsyms-symbols +2 -4
diff -puN include/linux/module.h~null-terminate-over-long-proc-kallsyms-symbols include/linux/module.h
@@ -362,10 +362,8 @@ int is_module_address(unsigned long addr
 
 /* Returns module and fills in value, defined and namebuf, or NULL if
    symnum out of range. */
-struct module *module_get_kallsym(unsigned int symnum,
-				  unsigned long *value,
-				  char *type,
-				  char namebuf[128]);
+struct module *module_get_kallsym(unsigned int symnum, unsigned long *value,
+				char *type, char *name, size_t namelen);
 
 /* Look for this name: can be of form module:name. */
 unsigned long module_kallsyms_lookup_name(const char *name);
kernel/kallsyms.c renamed from kernel/kallsyms.c~null-terminate-over-long-proc-kallsyms-symbols +2 -2
diff -puN kernel/kallsyms.c~null-terminate-over-long-proc-kallsyms-symbols kernel/kallsyms.c
@@ -275,8 +275,8 @@ static void upcase_if_global(struct kall
 static int get_ksymbol_mod(struct kallsym_iter *iter)
 {
 	iter->owner = module_get_kallsym(iter->pos - kallsyms_num_syms,
-					 &iter->value,
-					 &iter->type, iter->name);
+					 &iter->value, &iter->type,
+					 iter->name, sizeof(iter->name));
 	if (iter->owner == NULL)
 		return 0;
 

_

#6 lore raw
Quoted 2 lines · Andrew Morton

Yeah, that assume-the-caller-gave-us-a-128-byte-buffer is a bit rude. How about this?

That's better, thanks.

Andreas