check-execstack.awk 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # This awk script expects to get command-line files that are each
  2. # the output of 'readelf -l' on a single shared object.
  3. # But the first file should contain just "execstack-no" or "execstack-yes",
  4. # indicating what the default is in the absence of PT_GNU_STACK.
  5. # It exits successfully (0) if none indicated executable stack.
  6. # It fails (1) if any did indicate executable stack.
  7. # It fails (2) if the input did not take the expected form.
  8. BEGIN {
  9. result = sanity = 0; default_exec = -1;
  10. split(xfail, xfails, " ");
  11. for (x in xfails)
  12. expected_fails[xfails[x] ".phdr"] = 1;
  13. }
  14. /^execstack-no$/ { default_exec = 0; next }
  15. /^execstack-yes$/ { default_exec = 1; next }
  16. function check_one(name) {
  17. if (default_exec == -1) {
  18. print "*** missing execstack-default file?";
  19. result = 2;
  20. }
  21. n = split(name, parts, "/");
  22. basename = parts[n];
  23. expected_fail = basename in expected_fails;
  24. if (!sanity) {
  25. print name ": *** input did not look like readelf -l output";
  26. result = 2;
  27. } else if (stack_line) {
  28. if (stack_line ~ /^.*RW .*$/) {
  29. print name ": OK";
  30. } else if (stack_line ~ /^.*E.*$/) {
  31. if (expected_fail) {
  32. print name ": *** executable stack signaled, expected";
  33. } else {
  34. print name ": *** executable stack signaled";
  35. result = result ? result : 1;
  36. }
  37. }
  38. } else if (default_exec) {
  39. if (expected_fail) {
  40. print name ": *** no PT_GNU_STACK entry, expected";
  41. } else {
  42. print name ": *** no PT_GNU_STACK entry";
  43. result = result ? result : 1;
  44. }
  45. } else {
  46. print name ": no PT_GNU_STACK but default is OK";
  47. }
  48. sanity = 0;
  49. }
  50. FILENAME != lastfile {
  51. if (lastfile)
  52. check_one(lastfile);
  53. lastfile = FILENAME;
  54. }
  55. $1 == "Type" && $7 == "Flg" { sanity = 1; stack_line = "" }
  56. $1 == "GNU_STACK" { stack_line = $0 }
  57. END {
  58. check_one(lastfile);
  59. exit(result);
  60. }