Last active
January 10, 2025 09:02
-
-
Save amouat/837b993048a2aaadebc1f883dae1cb0e to your computer and use it in GitHub Desktop.
Exercising _FORTIFY_SOURCE in gcc (clang should work as well)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <string.h> | |
| #include <stdlib.h> | |
| /* | |
| * Simple test program for FORTIFY_SOURCE. | |
| * | |
| * Compile with the following to test with no source fortification and stack protection off: | |
| * gcc -D_FORTIFY_SOURCE=0 -fno-stack-protector fortify.c -o fortify | |
| * | |
| * Then try different values for FORTIFY_SOURCE (1-3) and -fstack-protector to enable stack protection. | |
| */ | |
| void memsetOverflow(int b) { | |
| printf("Memset Overflow\n"); | |
| char small_buf[8]; | |
| char *sbp = small_buf; | |
| if (b) { | |
| sbp = malloc(23); | |
| } | |
| memset(sbp, 0, 22); | |
| printf("%s\n", sbp); | |
| printf("Memset Overflow End\n"); | |
| } | |
| void overflowStruct() { | |
| printf("%s\n", "Overflow Struct Test"); | |
| char large_input[] = "MoreTextThanBuffer"; | |
| struct outerStruct { | |
| struct innerStruct { | |
| char buf[4]; | |
| int n; | |
| } inner; | |
| char buf[20]; | |
| }; | |
| struct outerStruct test_struct; | |
| strcpy(test_struct.inner.buf, large_input); | |
| printf("%s\n", test_struct.inner.buf); | |
| printf("%s\n", "Overflow Struct Test End"); | |
| } | |
| void overflowBuffer() { | |
| printf("Simple Buffer Overflow\n"); | |
| char large_input[] = "MoreTextThanBuffer"; | |
| char small_buf[8]; | |
| strcpy(small_buf, large_input); | |
| printf("%s\n", small_buf); | |
| printf("Simple Buffer Overflow Test End\n"); | |
| } | |
| int main(int argc, char **argv) { | |
| printf("Exercising my buffers\n"); | |
| memsetOverflow(0); | |
| overflowStruct(); | |
| overflowBuffer(); | |
| printf("My buffers hurt\n"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment