stack-smash.cpp 754 B

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <cstdio>
  7. // Note: Needs to be 'noline' so stack canary isn't optimized out.
  8. static void __attribute__((noinline)) smasher(char* string)
  9. {
  10. #pragma GCC diagnostic push
  11. #pragma GCC diagnostic ignored "-Warray-bounds"
  12. for (int i = 0; i < 256; i++) {
  13. string[i] = 'A';
  14. }
  15. #pragma GCC diagnostic pop
  16. }
  17. // Note: Needs to be 'noline' so stack canary isn't optimized out.
  18. static void __attribute__((noinline)) stack_to_smash()
  19. {
  20. char string[8] = {};
  21. smasher(string);
  22. }
  23. int main()
  24. {
  25. puts("[+] Starting the stack smash...");
  26. stack_to_smash();
  27. puts("[+] Stack smash wasn't detected!");
  28. return 0;
  29. }