Skip to contentSkip to conversation

objtool: fix .cold. functions parent symbols search

PATCH Superseded Artem Savkov 2 participants 17 messages Nov 7, 2018 – Nov 20, 2018 objtool

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 objtool: fix .cold. functions parent symbols search

The way it is currently done it is possible for read_symbols() to find the same symbol as parent for ".cold" functions. This leads to a bunch of complications such as func length being set to 0 and a segfault in add_switch_table(). Fix by copying the search string instead of modifying it in place.

---
 tools/objtool/elf.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/objtool/elf.c +4 -3
index 6dbb9fae0f9d..781c8afb29b9 100644
@@ -298,6 +298,7 @@ static int read_symbols(struct elf *elf)
 	/* Create parent/child links for any cold subfunctions */
 	list_for_each_entry(sec, &elf->sections, list) {
 		list_for_each_entry(sym, &sec->symbol_list, list) {
+			char *pname;
 			if (sym->type != STT_FUNC)
 				continue;
 			sym->pfunc = sym->cfunc = sym;
@@ -305,9 +306,9 @@ static int read_symbols(struct elf *elf)
 			if (!coldstr)
 				continue;
 
-			coldstr[0] = '\0';
-			pfunc = find_symbol_by_name(elf, sym->name);
-			coldstr[0] = '.';
+			pname = strndup(sym->name, coldstr - sym->name);
+			pfunc = find_symbol_by_name(elf, pname);
+			free(pname);
 
 			if (!pfunc) {
 				WARN("%s(): can't find parent function",
Signature · 1 line
2.17.2
#1 lore raw
Quoted 2 lines · Artem Savkov

The way it is currently done it is possible for read_symbols() to find the same symbol as parent for ".cold" functions.

I seem to remember having this discussion for kpatch-build, but I forget the details. Can you explain how this can happen (and also add that detail to the commit message)? I haven't seen any bug reports, is it purely theoretical?

Quoted 38 lines

This leads to a bunch of
complications such as func length being set to 0 and a segfault in add_switch_table(). Fix by copying the search string instead of modifying it in place.

---
 tools/objtool/elf.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/objtool/elf.c +4 -3
index 6dbb9fae0f9d..781c8afb29b9 100644
@@ -298,6 +298,7 @@ static int read_symbols(struct elf *elf)
 	/* Create parent/child links for any cold subfunctions */
 	list_for_each_entry(sec, &elf->sections, list) {
 		list_for_each_entry(sym, &sec->symbol_list, list) {
+			char *pname;
 			if (sym->type != STT_FUNC)
 				continue;
 			sym->pfunc = sym->cfunc = sym;
@@ -305,9 +306,9 @@ static int read_symbols(struct elf *elf)
 			if (!coldstr)
 				continue;
 
-			coldstr[0] = '\0';
-			pfunc = find_symbol_by_name(elf, sym->name);
-			coldstr[0] = '.';
+			pname = strndup(sym->name, coldstr - sym->name);
+			pfunc = find_symbol_by_name(elf, pname);
+			free(pname);
 
 			if (!pfunc) {
 				WARN("%s(): can't find parent function",
Signature · 1 line
2.17.2
Signature · 1 line
Josh
#2 lore raw
Quoted 7 lines · Josh Poimboeuf
Quoted 2 lines · Artem Savkov

The way it is currently done it is possible for read_symbols() to find the same symbol as parent for ".cold" functions.

I seem to remember having this discussion for kpatch-build, but I forget the details. Can you explain how this can happen (and also add that detail to the commit message)?

find_symbol_by_name() traverses the same lists as read_symbols and when we change sym->name in place without copying it it changes in the list as well. So if child function is before parent in sec->symbol_list the same function will be returned as "parent". It is hard for me to put it into words worthy to be included into commit message.

Quoted 1 line

I haven't seen any bug reports, is it purely theoretical?

No, 4.20-rc1 (actually anything after 4a60aa05a063 "objtool: Support per-function rodata sections", before that add_switch_table() doesn't seem to be called for those .cold. funcs) fails to build for mewith KCFLAGS="-ffunction-sections -fdata-sections" because objtool is segfaulting.

Quoted 41 lines
Quoted 38 lines

This leads to a bunch of
complications such as func length being set to 0 and a segfault in add_switch_table(). Fix by copying the search string instead of modifying it in place.

---
 tools/objtool/elf.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/objtool/elf.c +4 -3
index 6dbb9fae0f9d..781c8afb29b9 100644
@@ -298,6 +298,7 @@ static int read_symbols(struct elf *elf)
 	/* Create parent/child links for any cold subfunctions */
 	list_for_each_entry(sec, &elf->sections, list) {
 		list_for_each_entry(sym, &sec->symbol_list, list) {
+			char *pname;
 			if (sym->type != STT_FUNC)
 				continue;
 			sym->pfunc = sym->cfunc = sym;
@@ -305,9 +306,9 @@ static int read_symbols(struct elf *elf)
 			if (!coldstr)
 				continue;
 
-			coldstr[0] = '\0';
-			pfunc = find_symbol_by_name(elf, sym->name);
-			coldstr[0] = '.';
+			pname = strndup(sym->name, coldstr - sym->name);
+			pfunc = find_symbol_by_name(elf, pname);
+			free(pname);
 
 			if (!pfunc) {
 				WARN("%s(): can't find parent function",
Signature · 1 line
2.17.2
Signature · 1 line
Josh
Signature · 2 lines
Regards,
  Artem
#3 lore raw
Quoted 5 lines · Artem Savkov
Quoted 4 lines · Josh Poimboeuf
Quoted 2 lines · Artem Savkov

The way it is currently done it is possible for read_symbols() to find the same symbol as parent for ".cold" functions.

Quoted 8 lines
Quoted 3 lines

I seem to remember having this discussion for kpatch-build, but I forget the details. Can you explain how this can happen (and also add that detail to the commit message)?

find_symbol_by_name() traverses the same lists as read_symbols and when we change sym->name in place without copying it it changes in the list as well. So if child function is before parent in sec->symbol_list the same function will be returned as "parent".

Ah, I see.

Quoted 2 lines

It is hard for me to put it into words worthy to be included into commit message.

The above description made sense to me, so it sounds like you're on the right path :-)

Quoted 7 lines
Quoted 1 line

I haven't seen any bug reports, is it purely theoretical?

No, 4.20-rc1 (actually anything after 4a60aa05a063 "objtool: Support per-function rodata sections", before that add_switch_table() doesn't seem to be called for those .cold. funcs) fails to build for mewith KCFLAGS="-ffunction-sections -fdata-sections" because objtool is segfaulting.

If you only see it triggered with -ffunction-sections, that would be another useful nugget for the commit log.

Also, if it's fixing a regression, be sure to add the 'Fixes' tag.

Thanks!

Signature · 1 line
Josh
#4 lore raw

PATCH v2 1/1 objtool: fix .cold. functions parent symbols search

Because find_symbol_by_name() traverses the same lists as read_symbols() changing sym->name in place without copying it affects the result of find_symbol_by_name() and, in case when ".cold" function precedes it's parent in sec->symbol_list, can result in function being considered a parent of itself. This leads to function length being set to 0 and other consequent side-effects including a segfault in add_switch_table(). The effects of this bug are only visible when building with -ffunction-sections in KCFLAGS.

Fix by copying the search string instead of modifying it in place.

---
 tools/objtool/elf.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/objtool/elf.c +4 -3
index 6dbb9fae0f9d..781c8afb29b9 100644
@@ -298,6 +298,7 @@ static int read_symbols(struct elf *elf)
 	/* Create parent/child links for any cold subfunctions */
 	list_for_each_entry(sec, &elf->sections, list) {
 		list_for_each_entry(sym, &sec->symbol_list, list) {
+			char *pname;
 			if (sym->type != STT_FUNC)
 				continue;
 			sym->pfunc = sym->cfunc = sym;
@@ -305,9 +306,9 @@ static int read_symbols(struct elf *elf)
 			if (!coldstr)
 				continue;
 
-			coldstr[0] = '\0';
-			pfunc = find_symbol_by_name(elf, sym->name);
-			coldstr[0] = '.';
+			pname = strndup(sym->name, coldstr - sym->name);
+			pfunc = find_symbol_by_name(elf, pname);
+			free(pname);
 
 			if (!pfunc) {
 				WARN("%s(): can't find parent function",
Signature · 1 line
2.17.2
#5 lore raw
Quoted 9 lines · Artem Savkov

Because find_symbol_by_name() traverses the same lists as read_symbols() changing sym->name in place without copying it affects the result of find_symbol_by_name() and, in case when ".cold" function precedes it's parent in sec->symbol_list, can result in function being considered a parent of itself. This leads to function length being set to 0 and other consequent side-effects including a segfault in add_switch_table(). The effects of this bug are only visible when building with -ffunction-sections in KCFLAGS.

Quoted 3 lines

Fix by copying the search string instead of modifying it in place.

This needs a "Fixes" tag to identify the patch which introduced the bug.

Quoted 19 lines
---
 tools/objtool/elf.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/objtool/elf.c +1 -0
index 6dbb9fae0f9d..781c8afb29b9 100644
@@ -298,6 +298,7 @@ static int read_symbols(struct elf *elf)
 	/* Create parent/child links for any cold subfunctions */
 	list_for_each_entry(sec, &elf->sections, list) {
 		list_for_each_entry(sym, &sec->symbol_list, list) {
+			char *pname;
 			if (sym->type != STT_FUNC)
 				continue;
 			sym->pfunc = sym->cfunc = sym;
@@ -305,9 +306,9 @@ static int read_symbols(struct elf *elf)
 			if (!coldstr)
 				continue;

tools/objtool/elf.c static int read_symbols(struct elf *elf) · line 314

Quoted 10 lines
tools/objtool/elf.c +3 -3
 
-			coldstr[0] = '\0';
-			pfunc = find_symbol_by_name(elf, sym->name);
-			coldstr[0] = '.';
+			pname = strndup(sym->name, coldstr - sym->name);
+			pfunc = find_symbol_by_name(elf, pname);
+			free(pname);
 
 			if (!pfunc) {
 				WARN("%s(): can't find parent function",

strndup()'s return code needs to be checked.

Also, for such a short-lived allocation, I think a stack-allocated string would be better.

Signature · 1 line
Josh
#6 lore raw
Quoted 10 lines · Josh Poimboeuf
Quoted 9 lines · Artem Savkov

Because find_symbol_by_name() traverses the same lists as read_symbols() changing sym->name in place without copying it affects the result of find_symbol_by_name() and, in case when ".cold" function precedes it's parent in sec->symbol_list, can result in function being considered a parent of itself. This leads to function length being set to 0 and other consequent side-effects including a segfault in add_switch_table(). The effects of this bug are only visible when building with -ffunction-sections in KCFLAGS.

Quoted 5 lines
Quoted 3 lines

Fix by copying the search string instead of modifying it in place.

This needs a "Fixes" tag to identify the patch which introduced the bug.

Ok, will do.

Quoted 27 lines
Quoted 27 lines
---
 tools/objtool/elf.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/objtool/elf.c +4 -3
index 6dbb9fae0f9d..781c8afb29b9 100644
@@ -298,6 +298,7 @@ static int read_symbols(struct elf *elf)
 	/* Create parent/child links for any cold subfunctions */
 	list_for_each_entry(sec, &elf->sections, list) {
 		list_for_each_entry(sym, &sec->symbol_list, list) {
+			char *pname;
 			if (sym->type != STT_FUNC)
 				continue;
 			sym->pfunc = sym->cfunc = sym;
@@ -305,9 +306,9 @@ static int read_symbols(struct elf *elf)
 			if (!coldstr)
 				continue;
 
-			coldstr[0] = '\0';
-			pfunc = find_symbol_by_name(elf, sym->name);
-			coldstr[0] = '.';
+			pname = strndup(sym->name, coldstr - sym->name);
+			pfunc = find_symbol_by_name(elf, pname);
+			free(pname);
 
Quoted 7 lines

tools/objtool/elf.c static int read_symbols(struct elf *elf) · line 314

Quoted 2 lines
tools/objtool/elf.c +0 -0
 			if (!pfunc) {
 				WARN("%s(): can't find parent function",

strndup()'s return code needs to be checked.

Also, for such a short-lived allocation, I think a stack-allocated string would be better.

Hm, there seems to be no limit on lengths of strings in string table. What size would you consider reasonable for this stack-allocated string?

Signature · 2 lines
Regards,
  Artem
#7 lore raw
Quoted 51 lines · Artem Savkov
Quoted 10 lines · Josh Poimboeuf
Quoted 9 lines · Artem Savkov

Because find_symbol_by_name() traverses the same lists as read_symbols() changing sym->name in place without copying it affects the result of find_symbol_by_name() and, in case when ".cold" function precedes it's parent in sec->symbol_list, can result in function being considered a parent of itself. This leads to function length being set to 0 and other consequent side-effects including a segfault in add_switch_table(). The effects of this bug are only visible when building with -ffunction-sections in KCFLAGS.

Quoted 5 lines
Quoted 3 lines

Fix by copying the search string instead of modifying it in place.

This needs a "Fixes" tag to identify the patch which introduced the bug.

Ok, will do.

Quoted 32 lines
Quoted 19 lines
---
 tools/objtool/elf.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/objtool/elf.c +1 -0
index 6dbb9fae0f9d..781c8afb29b9 100644
@@ -298,6 +298,7 @@ static int read_symbols(struct elf *elf)
 	/* Create parent/child links for any cold subfunctions */
 	list_for_each_entry(sec, &elf->sections, list) {
 		list_for_each_entry(sym, &sec->symbol_list, list) {
+			char *pname;
 			if (sym->type != STT_FUNC)
 				continue;
 			sym->pfunc = sym->cfunc = sym;
@@ -305,9 +306,9 @@ static int read_symbols(struct elf *elf)
 			if (!coldstr)
 				continue;

tools/objtool/elf.c static int read_symbols(struct elf *elf) · line 314

Quoted 10 lines
tools/objtool/elf.c +3 -3
 
-			coldstr[0] = '\0';
-			pfunc = find_symbol_by_name(elf, sym->name);
-			coldstr[0] = '.';
+			pname = strndup(sym->name, coldstr - sym->name);
+			pfunc = find_symbol_by_name(elf, pname);
+			free(pname);
 
 			if (!pfunc) {
 				WARN("%s(): can't find parent function",

strndup()'s return code needs to be checked.

Quoted 5 lines
Quoted 2 lines

Also, for such a short-lived allocation, I think a stack-allocated string would be better.

Hm, there seems to be no limit on lengths of strings in string table. What size would you consider reasonable for this stack-allocated string?

I think it's fine to pick a reasonable maximum (128?) and then verify the string fits in the array before copying it.

Signature · 1 line
Josh
#8 lore raw

v3 cover objtool: read_symbols() fixes

The series started with 'parent symbol search' patch, but I found another issue in read_symbols() while testing the failure-path.

Artem Savkov (2):
objtool: fix failed cold symbol doublefree
objtool: fix .cold functions parent symbols search

 tools/objtool/elf.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.2
#9 lore raw

PATCH v3 1/2 objtool: fix failed cold symbol doublefree

If read_symbols() fails during second list traversal (the one dealing with ".cold" subfunctions) it frees the symbol, but never deletes it from the list/hash_table resulting in symbol being freed again in elf_close().

Fixes 13810435b9a7 "objtool: Support GCC 8's cold subfunctions"
---
 tools/objtool/elf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/objtool/elf.c +4 -1
index 6dbb9fae0f9d..3decd43477df 100644
@@ -312,7 +312,7 @@ static int read_symbols(struct elf *elf)
 			if (!pfunc) {
 				WARN("%s(): can't find parent function",
 				     sym->name);
-				goto err;
+				goto cold_err;
 			}
 
 			sym->pfunc = pfunc;
@@ -336,6 +336,9 @@ static int read_symbols(struct elf *elf)
 
 	return 0;
 
+cold_err:
+	list_del(&sym->list);
+	hash_del(&sym->hash);
 err:
 	free(sym);
 	return -1;
Signature · 1 line
2.17.2
#10 lore raw

Re: [PATCH v3 1/2] objtool: fix failed cold symbol doublefree

Quoted 6 lines · Artem Savkov

If read_symbols() fails during second list traversal (the one dealing with ".cold" subfunctions) it frees the symbol, but never deletes it from the list/hash_table resulting in symbol being freed again in elf_close().

Fixes 13810435b9a7 "objtool: Support GCC 8's cold subfunctions"

This needs parentheses, like:

Fixes 13810435b9a7 ("objtool: Support GCC 8's cold subfunctions")
Quoted 10 lines
---
 tools/objtool/elf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/objtool/elf.c +0 -0
index 6dbb9fae0f9d..3decd43477df 100644

tools/objtool/elf.c static int read_symbols(struct elf *elf) · line 315

Quoted 5 lines
tools/objtool/elf.c +1 -1
@@ -312,7 +312,7 @@ static int read_symbols(struct elf *elf)
 			if (!pfunc) {
 				WARN("%s(): can't find parent function",
 				     sym->name);
-				goto err;
+				goto cold_err;

Since it will get freed properly in elf_close() anyway, maybe it would be simpler to just 'return -1' here.

Quoted 16 lines
tools/objtool/elf.c +3 -0
 			}
 
 			sym->pfunc = pfunc;
@@ -336,6 +336,9 @@ static int read_symbols(struct elf *elf)
 
 	return 0;
 
+cold_err:
+	list_del(&sym->list);
+	hash_del(&sym->hash);
 err:
 	free(sym);
 	return -1;
Signature · 1 line
2.17.2
Signature · 1 line
Josh
#11 lore raw

PATCH v3 2/2 objtool: fix .cold functions parent symbols search

In reply to Artem Savkov

Because find_symbol_by_name() traverses the same lists as read_symbols() changing sym->name in place without copying it affects the result of find_symbol_by_name() and, in case when ".cold" function precedes it's parent in sec->symbol_list, can result in function being considered a parent of itself. This leads to function length being set to 0 and other consequent side-effects including a segfault in add_switch_table(). The effects of this bug are only visible when building with -ffunction-sections in KCFLAGS.

Fix by copying the search string instead of modifying it in place.

Fixes 13810435b9a7 "objtool: Support GCC 8's cold subfunctions"
---
 tools/objtool/elf.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/objtool/elf.c +14 -3
index 3decd43477df..15d9acfb2c97 100644
@@ -31,6 +31,8 @@
 #include "elf.h"
 #include "warn.h"
 
+#define MAX_NAME_LEN 128
+
 struct section *find_section_by_name(struct elf *elf, const char *name)
 {
 	struct section *sec;
@@ -298,6 +300,8 @@ static int read_symbols(struct elf *elf)
 	/* Create parent/child links for any cold subfunctions */
 	list_for_each_entry(sec, &elf->sections, list) {
 		list_for_each_entry(sym, &sec->symbol_list, list) {
+			char pname[MAX_NAME_LEN + 1];
+			size_t pnamelen;
 			if (sym->type != STT_FUNC)
 				continue;
 			sym->pfunc = sym->cfunc = sym;
@@ -305,9 +309,16 @@ static int read_symbols(struct elf *elf)
 			if (!coldstr)
 				continue;
 
-			coldstr[0] = '\0';
-			pfunc = find_symbol_by_name(elf, sym->name);
-			coldstr[0] = '.';
+			pnamelen = coldstr - sym->name;
+			if (pnamelen > MAX_NAME_LEN) {
+				WARN("%s(): parent function name exceeds maximum length of %d characters",
+				     sym->name, MAX_NAME_LEN);
+				goto cold_err;
+			}
+
+			strncpy(pname, sym->name, pnamelen);
+			pname[pnamelen] = '\0';
+			pfunc = find_symbol_by_name(elf, pname);
 
 			if (!pfunc) {
 				WARN("%s(): can't find parent function",
Signature · 1 line
2.17.2
#12 lore raw

Re: [PATCH v3 2/2] objtool: fix .cold functions parent symbols search

Quoted 9 lines · Artem Savkov

Because find_symbol_by_name() traverses the same lists as read_symbols() changing sym->name in place without copying it affects the result of find_symbol_by_name() and, in case when ".cold" function precedes it's parent in sec->symbol_list, can result in function being considered a parent of itself. This leads to function length being set to 0 and other consequent side-effects including a segfault in add_switch_table(). The effects of this bug are only visible when building with -ffunction-sections in KCFLAGS.

Quoted 3 lines

Fix by copying the search string instead of modifying it in place.

Fixes 13810435b9a7 "objtool: Support GCC 8's cold subfunctions"

Same here, needs parentheses.

Quoted 29 lines
---
 tools/objtool/elf.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/objtool/elf.c +4 -0
index 3decd43477df..15d9acfb2c97 100644
@@ -31,6 +31,8 @@
 #include "elf.h"
 #include "warn.h"
 
+#define MAX_NAME_LEN 128
+
 struct section *find_section_by_name(struct elf *elf, const char *name)
 {
 	struct section *sec;
@@ -298,6 +300,8 @@ static int read_symbols(struct elf *elf)
 	/* Create parent/child links for any cold subfunctions */
 	list_for_each_entry(sec, &elf->sections, list) {
 		list_for_each_entry(sym, &sec->symbol_list, list) {
+			char pname[MAX_NAME_LEN + 1];
+			size_t pnamelen;
 			if (sym->type != STT_FUNC)
 				continue;
 			sym->pfunc = sym->cfunc = sym;
@@ -305,9 +309,16 @@ static int read_symbols(struct elf *elf)
 			if (!coldstr)

tools/objtool/elf.c static int read_symbols(struct elf *elf) · line 316

Quoted 10 lines
tools/objtool/elf.c +5 -3
 				continue;
 
-			coldstr[0] = '\0';
-			pfunc = find_symbol_by_name(elf, sym->name);
-			coldstr[0] = '.';
+			pnamelen = coldstr - sym->name;
+			if (pnamelen > MAX_NAME_LEN) {
+				WARN("%s(): parent function name exceeds maximum length of %d characters",
+				     sym->name, MAX_NAME_LEN);
+				goto cold_err;

Same here, makes more sense to just return -1 and let elf_close() clean it up I think.

Signature · 1 line
Josh
#13 lore raw

v4 cover objtool: read_symbols() fixes

In reply to Artem Savkov

The series started with 'parent symbol search' patch, but I found another issue in read_symbols() while testing the failure-path.

Artem Savkov (2):
objtool: fix failed cold symbol doublefree
objtool: fix .cold functions parent symbols search

 tools/objtool/elf.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
Signature · 1 line
2.19.1
#14 lore raw

PATCH v4 1/2 objtool: fix failed cold symbol doublefree

If read_symbols() fails during second list traversal (the one dealing with ".cold" subfunctions) it frees the symbol, but never deletes it from the list/hash_table resulting in symbol being freed again in elf_close(). Fix by just returning an error leaving cleanup to elf_close().

Fixes 13810435b9a7 ("objtool: Support GCC 8's cold subfunctions")
---
 tools/objtool/elf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/objtool/elf.c +1 -1
index 6dbb9fae0f9d..e7a7ac40e045 100644
@@ -312,7 +312,7 @@ static int read_symbols(struct elf *elf)
 			if (!pfunc) {
 				WARN("%s(): can't find parent function",
 				     sym->name);
-				goto err;
+				return -1;
 			}
 
 			sym->pfunc = pfunc;
Signature · 1 line
2.19.1
#15 lore raw

PATCH v4 2/2 objtool: fix .cold functions parent symbols search

In reply to Artem Savkov

Because find_symbol_by_name() traverses the same lists as read_symbols() changing sym->name in place without copying it affects the result of find_symbol_by_name() and, in case when ".cold" function precedes it's parent in sec->symbol_list, can result in function being considered a parent of itself. This leads to function length being set to 0 and other consequent side-effects including a segfault in add_switch_table(). The effects of this bug are only visible when building with -ffunction-sections in KCFLAGS.

Fix by copying the search string instead of modifying it in place.

Fixes 13810435b9a7 ("objtool: Support GCC 8's cold subfunctions")
---
 tools/objtool/elf.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/objtool/elf.c +14 -3
index e7a7ac40e045..b8f3cca8e58b 100644
@@ -31,6 +31,8 @@
 #include "elf.h"
 #include "warn.h"
 
+#define MAX_NAME_LEN 128
+
 struct section *find_section_by_name(struct elf *elf, const char *name)
 {
 	struct section *sec;
@@ -298,6 +300,8 @@ static int read_symbols(struct elf *elf)
 	/* Create parent/child links for any cold subfunctions */
 	list_for_each_entry(sec, &elf->sections, list) {
 		list_for_each_entry(sym, &sec->symbol_list, list) {
+			char pname[MAX_NAME_LEN + 1];
+			size_t pnamelen;
 			if (sym->type != STT_FUNC)
 				continue;
 			sym->pfunc = sym->cfunc = sym;
@@ -305,9 +309,16 @@ static int read_symbols(struct elf *elf)
 			if (!coldstr)
 				continue;
 
-			coldstr[0] = '\0';
-			pfunc = find_symbol_by_name(elf, sym->name);
-			coldstr[0] = '.';
+			pnamelen = coldstr - sym->name;
+			if (pnamelen > MAX_NAME_LEN) {
+				WARN("%s(): parent function name exceeds maximum length of %d characters",
+				     sym->name, MAX_NAME_LEN);
+				return -1;
+			}
+
+			strncpy(pname, sym->name, pnamelen);
+			pname[pnamelen] = '\0';
+			pfunc = find_symbol_by_name(elf, pname);
 
 			if (!pfunc) {
 				WARN("%s(): can't find parent function",
Signature · 1 line
2.19.1
#16 lore raw

Re: [PATCH v4 0/2] objtool: read_symbols() fixes

In reply to Artem Savkov
Quoted 9 lines · Artem Savkov

The series started with 'parent symbol search' patch, but I found another issue in read_symbols() while testing the failure-path.

Artem Savkov (2):
objtool: fix failed cold symbol doublefree
objtool: fix .cold functions parent symbols search

 tools/objtool/elf.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

Thanks, looking good. I'll run the patches through 0-day testing and then send them onto the -tip tree.

Signature · 1 line
Josh