check_initial_reg_state.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * check_initial_reg_state.c - check that execve sets the correct state
  3. * Copyright (c) 2014-2016 Andrew Lutomirski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. */
  14. #define _GNU_SOURCE
  15. #include <stdio.h>
  16. unsigned long ax, bx, cx, dx, si, di, bp, sp, flags;
  17. unsigned long r8, r9, r10, r11, r12, r13, r14, r15;
  18. asm (
  19. ".pushsection .text\n\t"
  20. ".type real_start, @function\n\t"
  21. ".global real_start\n\t"
  22. "real_start:\n\t"
  23. #ifdef __x86_64__
  24. "mov %rax, ax\n\t"
  25. "mov %rbx, bx\n\t"
  26. "mov %rcx, cx\n\t"
  27. "mov %rdx, dx\n\t"
  28. "mov %rsi, si\n\t"
  29. "mov %rdi, di\n\t"
  30. "mov %rbp, bp\n\t"
  31. "mov %rsp, sp\n\t"
  32. "mov %r8, r8\n\t"
  33. "mov %r9, r9\n\t"
  34. "mov %r10, r10\n\t"
  35. "mov %r11, r11\n\t"
  36. "mov %r12, r12\n\t"
  37. "mov %r13, r13\n\t"
  38. "mov %r14, r14\n\t"
  39. "mov %r15, r15\n\t"
  40. "pushfq\n\t"
  41. "popq flags\n\t"
  42. #else
  43. "mov %eax, ax\n\t"
  44. "mov %ebx, bx\n\t"
  45. "mov %ecx, cx\n\t"
  46. "mov %edx, dx\n\t"
  47. "mov %esi, si\n\t"
  48. "mov %edi, di\n\t"
  49. "mov %ebp, bp\n\t"
  50. "mov %esp, sp\n\t"
  51. "pushfl\n\t"
  52. "popl flags\n\t"
  53. #endif
  54. "jmp _start\n\t"
  55. ".size real_start, . - real_start\n\t"
  56. ".popsection");
  57. int main()
  58. {
  59. int nerrs = 0;
  60. if (sp == 0) {
  61. printf("[FAIL]\tTest was built incorrectly\n");
  62. return 1;
  63. }
  64. if (ax || bx || cx || dx || si || di || bp
  65. #ifdef __x86_64__
  66. || r8 || r9 || r10 || r11 || r12 || r13 || r14 || r15
  67. #endif
  68. ) {
  69. printf("[FAIL]\tAll GPRs except SP should be 0\n");
  70. #define SHOW(x) printf("\t" #x " = 0x%lx\n", x);
  71. SHOW(ax);
  72. SHOW(bx);
  73. SHOW(cx);
  74. SHOW(dx);
  75. SHOW(si);
  76. SHOW(di);
  77. SHOW(bp);
  78. SHOW(sp);
  79. #ifdef __x86_64__
  80. SHOW(r8);
  81. SHOW(r9);
  82. SHOW(r10);
  83. SHOW(r11);
  84. SHOW(r12);
  85. SHOW(r13);
  86. SHOW(r14);
  87. SHOW(r15);
  88. #endif
  89. nerrs++;
  90. } else {
  91. printf("[OK]\tAll GPRs except SP are 0\n");
  92. }
  93. if (flags != 0x202) {
  94. printf("[FAIL]\tFLAGS is 0x%lx, but it should be 0x202\n", flags);
  95. nerrs++;
  96. } else {
  97. printf("[OK]\tFLAGS is 0x202\n");
  98. }
  99. return nerrs ? 1 : 0;
  100. }