syscall_nt.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
  18. int flags)
  19. {
  20. struct sigaction sa;
  21. memset(&sa, 0, sizeof(sa));
  22. sa.sa_sigaction = handler;
  23. sa.sa_flags = SA_SIGINFO | flags;
  24. sigemptyset(&sa.sa_mask);
  25. if (sigaction(sig, &sa, 0))
  26. err(1, "sigaction");
  27. }
  28. static void sigtrap(int sig, siginfo_t *si, void *ctx_void)
  29. {
  30. }
  31. static void do_it(unsigned long extraflags)
  32. {
  33. unsigned long flags;
  34. set_eflags(get_eflags() | extraflags);
  35. syscall(SYS_getpid);
  36. flags = get_eflags();
  37. set_eflags(X86_EFLAGS_IF | X86_EFLAGS_FIXED);
  38. if ((flags & extraflags) == extraflags) {
  39. printf("[OK]\tThe syscall worked and flags are still set\n");
  40. } else {
  41. printf("[FAIL]\tThe syscall worked but flags were cleared (flags = 0x%lx but expected 0x%lx set)\n",
  42. flags, extraflags);
  43. nerrs++;
  44. }
  45. }
  46. int main(void)
  47. {
  48. printf("[RUN]\tSet NT and issue a syscall\n");
  49. do_it(X86_EFLAGS_NT);
  50. printf("[RUN]\tSet AC and issue a syscall\n");
  51. do_it(X86_EFLAGS_AC);
  52. printf("[RUN]\tSet NT|AC and issue a syscall\n");
  53. do_it(X86_EFLAGS_NT | X86_EFLAGS_AC);
  54. /*
  55. * Now try it again with TF set -- TF forces returns via IRET in all
  56. * cases except non-ptregs-using 64-bit full fast path syscalls.
  57. */
  58. sethandler(SIGTRAP, sigtrap, 0);
  59. printf("[RUN]\tSet TF and issue a syscall\n");
  60. do_it(X86_EFLAGS_TF);
  61. printf("[RUN]\tSet NT|TF and issue a syscall\n");
  62. do_it(X86_EFLAGS_NT | X86_EFLAGS_TF);
  63. printf("[RUN]\tSet AC|TF and issue a syscall\n");
  64. do_it(X86_EFLAGS_AC | X86_EFLAGS_TF);
  65. printf("[RUN]\tSet NT|AC|TF and issue a syscall\n");
  66. do_it(X86_EFLAGS_NT | X86_EFLAGS_AC | X86_EFLAGS_TF);
  67. /*
  68. * Now try DF. This is evil and it's plausible that we will crash
  69. * glibc, but glibc would have to do something rather surprising
  70. * for this to happen.
  71. */
  72. printf("[RUN]\tSet DF and issue a syscall\n");
  73. do_it(X86_EFLAGS_DF);
  74. printf("[RUN]\tSet TF|DF and issue a syscall\n");
  75. do_it(X86_EFLAGS_TF | X86_EFLAGS_DF);
  76. return nerrs == 0 ? 0 : 1;
  77. }