public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug string/31332] New: Improve detection of buffer overflow at compile-time with FORTIFY_SOURCE
@ 2024-02-03  0:14 Vojislav.Tomasevic at Syrmia dot com
  2024-02-03 13:38 ` [Bug string/31332] " schwab@linux-m68k.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Vojislav.Tomasevic at Syrmia dot com @ 2024-02-03  0:14 UTC (permalink / raw)
  To: glibc-bugs

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

            Bug ID: 31332
           Summary: Improve detection of buffer overflow at compile-time
                    with FORTIFY_SOURCE
           Product: glibc
           Version: unspecified
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: string
          Assignee: unassigned at sourceware dot org
          Reporter: Vojislav.Tomasevic at Syrmia dot com
  Target Milestone: ---

Created attachment 15350
  --> https://sourceware.org/bugzilla/attachment.cgi?id=15350&action=edit
Test case with buffer overflow in memcpy call

FORTIFY_SOURCE currently reports run-time errors when detecting buffer
overflows, both with clang and gcc. However, it would be more beneficial to
catch the issues earlier (at compile-time), when possible.

There is room for improvement in fortified implementations of functions
memcpy/memmove/memset/strncpy/bcopy/bzero as buffer overflows can be detected
at compile-time and reported as compile-time errors.

Consider the example memcpy.c in the attached test case which contains buffer
overflow:


bash-4.4$ clang -O2 -D_FORTIFY_SOURCE=2 memcpy.c   // no compile-time warning
bash-4.4$ ./a.out
*** buffer overflow detected ***: terminated
Aborted (core dumped)


Note that the overflow is caught at run-time only. However, in this case, we
should be able to detect it at compile-time as both the length and size of the
destination pointer is known at compile-time, when compiled with optimizations.

With changes to memcpy definition as below, the issue can be caught at
compile-time itself. Similar changes could be done to
memmove/memset/strncpy/bcopy/bzero functions as well. Both clang and gcc
compilers support error/warning attribute, builtin_object_size and
builtin_constant_p functions.


@@ -26,6 +26,13 @@ __fortify_function void *
 __NTH (memcpy (void *__restrict __dest, const void *__restrict __src,
               size_t __len))
 {
+  if (__bos (__dest) != (size_t) -1
+      && __builtin_constant_p (__len)
+      && __len > __bos (__dest))
+    {
+      void __fortify_error (void) __attribute__((error("dest is too small")));
+      __fortify_error ();
+    }
   return __builtin___memcpy_chk (__dest, __src, __len,
                                 __glibc_objsize0 (__dest));
 }


The above patch could be improved by using _errordecl macro to declare the
prototype of the __fortify_error function, which is already used in glibc for
similar purposes.

If the attached test case is considered now (after applying this patch), there
is a compile-time error like the following one:


bash-4.4$ clang -O2 -D_FORTIFY_SOURCE=2 memcpy.c
In file included from memcpy.c:1:
In file included from string.h:535:
glibc/install_dir/include/bits/string_fortified.h:34:7: error: call to
'__fortify_error' declared with 'error' attribute: dest is too small
   34 |       __fortify_error ();
      |       ^
1 error generated.


If this is agreeable, I would be interested to work on a patch which improves
buffer overflow detection at compile-time.

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-02-05 15:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-03  0:14 [Bug string/31332] New: Improve detection of buffer overflow at compile-time with FORTIFY_SOURCE Vojislav.Tomasevic at Syrmia dot com
2024-02-03 13:38 ` [Bug string/31332] " schwab@linux-m68k.org
2024-02-03 13:40 ` sam at gentoo dot org
2024-02-05 15:07 ` fweimer at redhat dot com
2024-02-05 15:08 ` fweimer at redhat dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).