Skip to content

Commit 27e9faf

Browse files
committed
gcc-plugins/stackleak: Exactly match strings instead of prefixes
Since STRING_CST may not be NUL terminated, strncmp() was used for check for equality. However, this may lead to mismatches for longer section names where the start matches the tested-for string. Test for exact equality by checking for the presences of NUL termination. Cc: Alexander Popov <alex.popov@linux.com> Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent f154066 commit 27e9faf

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

scripts/gcc-plugins/stackleak_plugin.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,23 @@ static unsigned int stackleak_cleanup_execute(void)
429429
return 0;
430430
}
431431

432+
/*
433+
* STRING_CST may or may not be NUL terminated:
434+
* https://gcc.gnu.org/onlinedocs/gccint/Constant-expressions.html
435+
*/
436+
static inline bool string_equal(tree node, const char *string, int length)
437+
{
438+
if (TREE_STRING_LENGTH(node) < length)
439+
return false;
440+
if (TREE_STRING_LENGTH(node) > length + 1)
441+
return false;
442+
if (TREE_STRING_LENGTH(node) == length + 1 &&
443+
TREE_STRING_POINTER(node)[length] != '\0')
444+
return false;
445+
return !memcmp(TREE_STRING_POINTER(node), string, length);
446+
}
447+
#define STRING_EQUAL(node, str) string_equal(node, str, strlen(str))
448+
432449
static bool stackleak_gate(void)
433450
{
434451
tree section;
@@ -438,13 +455,13 @@ static bool stackleak_gate(void)
438455
if (section && TREE_VALUE(section)) {
439456
section = TREE_VALUE(TREE_VALUE(section));
440457

441-
if (!strncmp(TREE_STRING_POINTER(section), ".init.text", 10))
458+
if (STRING_EQUAL(section, ".init.text"))
442459
return false;
443-
if (!strncmp(TREE_STRING_POINTER(section), ".devinit.text", 13))
460+
if (STRING_EQUAL(section, ".devinit.text"))
444461
return false;
445-
if (!strncmp(TREE_STRING_POINTER(section), ".cpuinit.text", 13))
462+
if (STRING_EQUAL(section, ".cpuinit.text"))
446463
return false;
447-
if (!strncmp(TREE_STRING_POINTER(section), ".meminit.text", 13))
464+
if (STRING_EQUAL(section, ".meminit.text"))
448465
return false;
449466
}
450467

0 commit comments

Comments
 (0)