The "symbol does not fit completely in referenced section" checks added
st_value (or st_value - sh_addr) to st_size in 64-bit unsigned
arithmetic. A large st_size from the ELF symbol table wraps the sum and
makes elflint accept a symbol that extends past its section.

Subtract st_size from the already-validated section bound instead, which
cannot wrap.

Signed-off-by: Mohammed Mustafa <[email protected]>
---
 src/elflint.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/elflint.c b/src/elflint.c
index dd42dcb4..167cb93f 100644
--- a/src/elflint.c
+++ b/src/elflint.c
@@ -833,8 +833,10 @@ section [%2d] '%s': symbol %zu (%s): function in
COMMON section is nonsense\n"),
 section [%2d] '%s': symbol %zu (%s): st_value out of bounds\n"),
                                   idx, section_name (ebl, idx), cnt, name);
                        }
-                     else if ((st_value - sh_addr
-                               + sym->st_size) > destshdr->sh_size)
+                     /* Subtract instead of adding st_size: the sum can wrap
+                        around for a large symbol size from the ELF file.  */
+                     else if (sym->st_size
+                              > destshdr->sh_size - (st_value - sh_addr))
                        ERROR (_("\
 section [%2d] '%s': symbol %zu (%s) does not fit completely in
referenced section [%2d] '%s'\n"),
                               idx, section_name (ebl, idx), cnt, name,
@@ -858,8 +860,7 @@ section [%2d] '%s': symbol %zu (%s): referenced
section [%2d] '%s' does not have
 section [%2d] '%s': symbol %zu (%s): st_value out of bounds of
referenced section [%2d] '%s'\n"),
                               idx, section_name (ebl, idx), cnt, name,
                               (int) xndx, section_name (ebl, xndx));
-                     else if (st_value + sym->st_size
-                              > destshdr->sh_size)
+                     else if (sym->st_size > destshdr->sh_size - st_value)
                        ERROR (_("\
 section [%2d] '%s': symbol %zu (%s) does not fit completely in
referenced section [%2d] '%s'\n"),
                               idx, section_name (ebl, idx), cnt, name,
@@ -906,9 +907,9 @@ section [%2d] '%s': symbol %zu (%s): st_value
short of referenced section [%2d]
 section [%2d] '%s': symbol %zu (%s): st_value out of bounds of
referenced section [%2d] '%s'\n"),
                                   idx, section_name (ebl, idx), cnt, name,
                                   (int) xndx, section_name (ebl, xndx));
-                         else if (st_value + sym->st_size
+                         else if (sym->st_size
                                   > (destshdr->sh_offset - phdr->p_offset
-                                     + destshdr->sh_size))
+                                     + destshdr->sh_size) - st_value)
                            ERROR (_("\
 section [%2d] '%s': symbol %zu (%s) does not fit completely in
referenced section [%2d] '%s'\n"),
                                   idx, section_name (ebl, idx), cnt, name,
-- 
2.56.1

Reply via email to