[libc-commits] [libc] Supporting stack protectors in linux x86-64 (PR #66456)

Siva Chandra via libc-commits libc-commits at lists.llvm.org
Mon Sep 18 13:55:35 PDT 2023


================
@@ -0,0 +1,68 @@
+//===--- Stack smashing test to check stack canary set up  ----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/string.h"
+#include "src/__support/OSUtil/io.h"
+#include "src/pthread/pthread_atfork.h"
+#include "src/signal/raise.h"
+#include "src/sys/wait/wait.h"
+#include "src/sys/wait/wait4.h"
+#include "src/sys/wait/waitpid.h"
+#include "src/unistd/fork.h"
+
+#include "test/IntegrationTest/test.h"
+
+#include <errno.h>
+#include <signal.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+void no_stack_smashing_normal_exit() {
+  pid_t pid = __llvm_libc::fork();
+  if (pid == 0) {
+    // Child process
+    char foo[30];
+    for (int i = 0; i < 30; i++)
+      foo[i] = 42;
+    return;
+  }
+  ASSERT_TRUE(pid > 0);
+  int status;
+  pid_t cpid = __llvm_libc::wait(&status);
+  ASSERT_TRUE(cpid > 0);
+  ASSERT_EQ(cpid, pid);
+  ASSERT_TRUE(WIFEXITED(status));
+}
+
+void stack_smashing_abort() {
+  pid_t pid = __llvm_libc::fork();
+  if (pid == 0) {
+    // Child process
+    char foo[30];
+    char *frame_ptr = static_cast<char *>(__builtin_frame_address(0));
+    char *cur_ptr = &foo[0];
+    // Corrupt the stack
+    while (cur_ptr != frame_ptr) {
+      *cur_ptr = 42;
----------------
sivachandra wrote:

Instead of just writing 42, write 42 if the byte is not already 42. Else, write 24. This way, we ensure that you will definitely scramble the stack guard.

https://github.com/llvm/llvm-project/pull/66456


More information about the libc-commits mailing list