fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers
Log | Files | Refs | README | LICENSE

commit 4f1380a48da7cb0375810f70e68ca6282486dec1
parent 3e6704d0be707487d7a9dccfdc75203c7261e11b
Author: jvoisin <julien.voisin@dustri.org>
Date:   Sat, 18 Mar 2023 13:29:22 +0100

Add a basic testsuite

Diffstat:
A.github/workflows/testsuite.yaml | 19+++++++++++++++++++
Atests/Makefile | 25+++++++++++++++++++++++++
Atests/common.h | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/test_memcpy_dynamic_read.c | 16++++++++++++++++
Atests/test_memcpy_dynamic_write.c | 16++++++++++++++++
Atests/test_memcpy_static_read.c | 16++++++++++++++++
Atests/test_memcpy_static_write.c | 16++++++++++++++++
7 files changed, 170 insertions(+), 0 deletions(-)

diff --git a/.github/workflows/testsuite.yaml b/.github/workflows/testsuite.yaml @@ -0,0 +1,19 @@ +name: Test suite +on: + pull_request: + push: + schedule: + - cron: '0 16 * * 5' + +jobs: + ci: + runs-on: ubuntu-latest + steps: + - name: Checking out the code + uses: actions/checkout@v3 + - name: Downloading musl-based toolchain + run: wget https://musl.cc/x86_64-linux-musl-native.tgz + - name: Extracting musl-based toolchain + run: tar xzf ./x86_64-linux-musl-native.tgz + - name: Running the testsuite + run: make -C tests | grep -zqv FAIL diff --git a/tests/Makefile b/tests/Makefile @@ -0,0 +1,25 @@ +CC=../x86_64-linux-musl-native/bin/gcc +CFLAGS=-I../include/ -D_FORTIFY_SOURCE=3 -static -O2 + +TARGETS=test_memcpy_static_write \ + test_memcpy_dynamic_write \ + test_memcpy_static_read \ + test_memcpy_dynamic_read + +.SILENT: + +all: $(TARGETS) run + +$(TARGETS): %: %.c + $(CC) $(CFLAGS) -o $@ $< + +run: $(TARGETS) + $(foreach EXE, $(TARGETS), \ + ./$(EXE) 1 2 3 4 5 6 7 8 9 0 >/dev/null && echo "$(EXE) OK" || echo "$(EXE) FAIL" ; \ + ) + +clean: + $(foreach EXE, $(TARGETS), \ + rm -f ./$(EXE) \ + ) + diff --git a/tests/common.h b/tests/common.h @@ -0,0 +1,62 @@ +/* Copyright (C) 2004-2020 Free Software Foundation, Inc. + This snippet is taken from debug/tst-chk1 in the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <https://www.gnu.org/licenses/>. */ + +#include <setjmp.h> +#include <unistd.h> +#include <signal.h> +#include <stdio.h> + +volatile int chk_fail_ok; +volatile int ret; +jmp_buf chk_fail_buf; + +static void +handler (int sig) +{ + if (chk_fail_ok) + { + chk_fail_ok = 0; + longjmp (chk_fail_buf, 1); + } + else + _exit (127); +} + +void +__attribute__((constructor)) +set_fortify_handler (void) +{ + struct sigaction sa; + + sa.sa_handler = handler; + sa.sa_flags = 0; + sigemptyset (&sa.sa_mask); + + sigaction (SIGILL, &sa, NULL); +} + +#define FAIL() \ + do { fprintf (stderr, "Failure on line %d\n", __LINE__); ret = 1; } while (0) +#define CHK_FAIL_START \ + chk_fail_ok = 1; \ + if (! setjmp (chk_fail_buf)) \ + { +#define CHK_FAIL_END \ + chk_fail_ok = 0; \ + FAIL (); \ + } + diff --git a/tests/test_memcpy_dynamic_read.c b/tests/test_memcpy_dynamic_read.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include <string.h> + +int main(int argc, char** argv) { + char buffer[12] = {0}; + memcpy(buffer, "1234567890", sizeof(buffer) - 1); + puts(buffer); + + CHK_FAIL_START + memcpy(buffer, "123456", argc); + CHK_FAIL_END + + puts(buffer); + return ret; +} diff --git a/tests/test_memcpy_dynamic_write.c b/tests/test_memcpy_dynamic_write.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include <string.h> + +int main(int argc, char** argv) { + char buffer[8] = {0}; + memcpy(buffer, "1234567890", sizeof(buffer) - 1); + puts(buffer); + + CHK_FAIL_START + memcpy(buffer, "1234567890", argc); + CHK_FAIL_END + + puts(buffer); + return ret; +} diff --git a/tests/test_memcpy_static_read.c b/tests/test_memcpy_static_read.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include <string.h> + +int main(int argc, char** argv) { + char buffer[8] = {0}; + memcpy(buffer, "123456", 4); + puts(buffer); + + CHK_FAIL_START + memcpy(buffer, "123456", sizeof(buffer)); + CHK_FAIL_END + + puts(buffer); + return ret; +} diff --git a/tests/test_memcpy_static_write.c b/tests/test_memcpy_static_write.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include <string.h> + +int main(int argc, char** argv) { + char buffer[8] = {0}; + memcpy(buffer, "1234567890", sizeof(buffer) - 1); + puts(buffer); + + CHK_FAIL_START + memcpy(buffer, "1234567890", sizeof(buffer) + 1); + CHK_FAIL_END + + puts(buffer); + return ret; +}