public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Yaakov Selkowitz <yselkowi@redhat.com>
To: newlib@sourceware.org
Subject: [PATCH v5 01/10] ssp: add APIs for Stack Smashing Protection
Date: Wed, 29 Nov 2017 09:43:00 -0000	[thread overview]
Message-ID: <20171129094300.20296-2-yselkowi@redhat.com> (raw)
In-Reply-To: <20171129094300.20296-1-yselkowi@redhat.com>

Compiling with any of the -fstack-protector* flags requires the
__stack_chk_guard data import (which needs to be initialized) and the
__stack_chk_fail{,_local} functions.  While GCC's own libssp can provide
these, it is better that we provide these ourselves.  The implementation
is custom due to being OS-specific.

Signed-off-by: Yaakov Selkowitz <yselkowi@redhat.com>
---
 newlib/libc/ssp/stack_protector.c | 45 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
 create mode 100644 newlib/libc/ssp/stack_protector.c

diff --git a/newlib/libc/ssp/stack_protector.c b/newlib/libc/ssp/stack_protector.c
new file mode 100644
index 000000000..ee014b69d
--- /dev/null
+++ b/newlib/libc/ssp/stack_protector.c
@@ -0,0 +1,45 @@
+#include <sys/cdefs.h>
+#include <sys/param.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+uintptr_t __stack_chk_guard = 0;
+
+void
+__attribute__((__constructor__))
+__stack_chk_init (void)
+{
+  if (__stack_chk_guard != 0)
+    return;
+
+#if defined(__CYGWIN__) || defined(__rtems__)
+  arc4random_buf(&__stack_chk_guard, sizeof(__stack_chk_guard));
+#else
+  /* If getentropy is not available, use the "terminator canary". */
+  ((unsigned char *)&__stack_chk_guard)[0] = 0;
+  ((unsigned char *)&__stack_chk_guard)[1] = 0;
+  ((unsigned char *)&__stack_chk_guard)[2] = '\n';
+  ((unsigned char *)&__stack_chk_guard)[3] = 255;
+#endif
+}
+
+void
+__attribute__((__noreturn__))
+__stack_chk_fail (void)
+{
+  char msg[] = "*** stack smashing detected ***: terminated\n";
+  write (2, msg, strlen (msg));
+  raise (SIGABRT);
+  _exit (127);
+}
+
+#ifdef __ELF__
+void
+__attribute__((visibility ("hidden")))
+__stack_chk_fail_local (void)
+{
+	__stack_chk_fail();
+}
+#endif
-- 
2.15.0

  reply	other threads:[~2017-11-29  9:43 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-29  9:49 [PATCH v5 00/10] Add Stack Smashing Protection and Object Size Checking Yaakov Selkowitz
2017-11-29  9:43 ` Yaakov Selkowitz [this message]
2017-11-29  9:43 ` [PATCH v5 02/10] ssp: add Object Size Checking common code Yaakov Selkowitz
2017-11-29  9:43 ` [PATCH v5 07/10] ssp: add documentation Yaakov Selkowitz
2017-11-29  9:43 ` [PATCH v5 05/10] ssp: add Object Size Checking for stdio.h, part 1 Yaakov Selkowitz
2017-11-29  9:43 ` [PATCH v5 03/10] ssp: add Object Size Checking for string.h Yaakov Selkowitz
2017-11-29  9:51 ` [PATCH v5 09/10] cygwin: export SSP functions Yaakov Selkowitz
2017-11-29  9:51 ` [PATCH v5 10/10] cygwin: create libssp compatibility import library Yaakov Selkowitz
2017-11-29  9:51 ` [PATCH v5 06/10] ssp: add Object Size Checking for unistd.h, part 1 Yaakov Selkowitz
2017-11-29  9:53 ` [PATCH v5 08/10] ssp: add build infrastructure Yaakov Selkowitz
2017-11-29 17:22 ` [PATCH v5 04/10] ssp: add Object Size Checking for strings.h Yaakov Selkowitz
2017-11-30  1:43 ` [PATCH v5 00/10] Add Stack Smashing Protection and Object Size Checking Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171129094300.20296-2-yselkowi@redhat.com \
    --to=yselkowi@redhat.com \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).