[Bug libelf/28101] elf_strptr slow with address sanitizer, passes entire section range to memrchr.

mark at klomp dot org sourceware-bugzilla@sourceware.org
Mon Jul 19 08:10:11 GMT 2021


https://sourceware.org/bugzilla/show_bug.cgi?id=28101

Mark Wielaard <mark at klomp dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mark at klomp dot org

--- Comment #1 from Mark Wielaard <mark at klomp dot org> ---
I think it really is a bug/performance issue in asan. But "optimizing" it in
libelf by first checking the last char is zero, before calling memrchr wouldn't
hurt (and should normally prevent a function call). Does the following help?

diff --git a/libelf/elf_strptr.c b/libelf/elf_strptr.c
index 76f2caf1..dc9b76c0 100644
--- a/libelf/elf_strptr.c
+++ b/libelf/elf_strptr.c
@@ -56,7 +56,9 @@ get_zdata (Elf_Scn *strscn)
 static bool validate_str (const char *str, size_t from, size_t to)
 {
 #if HAVE_DECL_MEMRCHR
-  return memrchr (&str[from], '\0', to - from) != NULL;
+  // Check end first, which is likely a zero terminator, to prevent function
call
+  return (str[to - 1]  == '\0'
+         || (to - from > 0 && memrchr (&str[from], '\0', to - from - 1) !=
NULL));
 #else
   do {
     if (to <= from)

-- 
You are receiving this mail because:
You are on the CC list for the bug.


More information about the Elfutils-devel mailing list