syscall_nt.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * syscall_nt.c - checks syscalls with NT set
  4. * Copyright (c) 2014-2015 Andrew Lutomirski
  5. *
  6. * Some obscure user-space code requires the ability to make system calls
  7. * with FLAGS.NT set. Make sure it works.
  8. */
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <signal.h>
  13. #include <err.h>
  14. #include <sys/syscall.h>
  15. #include "helpers.h"
  16. static unsigned int nerrs;
  17. static void sigtrap(int sig, siginfo_t *si, void *ctx_void)
  18. {
  19. }
  20. static void do_it(unsigned long extraflags)
  21. {
  22. unsigned long flags;
  23. set_eflags(get_eflags() | extraflags);
  24. syscall(SYS_getpid);
  25. flags = get_eflags();
  26. set_eflags(X86_EFLAGS_IF | X86_EFLAGS_FIXED);
  27. if ((flags & extraflags) == extraflags) {
  28. printf("[OK]\tThe syscall worked and flags are still set\n");
  29. } else {
  30. printf("[FAIL]\tThe syscall worked but flags were cleared (flags = 0x%lx but expected 0x%lx set)\n",
  31. flags, extraflags);
  32. nerrs++;
  33. }
  34. }
  35. int main(void)
  36. {
  37. printf("[RUN]\tSet NT and issue a syscall\n");
  38. do_it(X86_EFLAGS_NT);
  39. printf("[RUN]\tSet AC and issue a syscall\n");
  40. do_it(X86_EFLAGS_AC);
  41. printf("[RUN]\tSet NT|AC and issue a syscall\n");
  42. do_it(X86_EFLAGS_NT | X86_EFLAGS_AC);
  43. /*
  44. * Now try it again with TF set -- TF forces returns via IRET in all
  45. * cases except non-ptregs-using 64-bit full fast path syscalls.
  46. */
  47. sethandler(SIGTRAP, sigtrap, 0);
  48. printf("[RUN]\tSet TF and issue a syscall\n");
  49. do_it(X86_EFLAGS_TF);
  50. printf("[RUN]\tSet NT|TF and issue a syscall\n");
  51. do_it(X86_EFLAGS_NT | X86_EFLAGS_TF);
  52. printf("[RUN]\tSet AC|TF and issue a syscall\n");
  53. do_it(X86_EFLAGS_AC | X86_EFLAGS_TF);
  54. printf("[RUN]\tSet NT|AC|TF and issue a syscall\n");
  55. do_it(X86_EFLAGS_NT | X86_EFLAGS_AC | X86_EFLAGS_TF);
  56. /*
  57. * Now try DF. This is evil and it's plausible that we will crash
  58. * glibc, but glibc would have to do something rather surprising
  59. * for this to happen.
  60. */
  61. printf("[RUN]\tSet DF and issue a syscall\n");
  62. do_it(X86_EFLAGS_DF);
  63. printf("[RUN]\tSet TF|DF and issue a syscall\n");
  64. do_it(X86_EFLAGS_TF | X86_EFLAGS_DF);
  65. return nerrs == 0 ? 0 : 1;
  66. }