syscall_nt.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * syscall_nt.c - checks syscalls with NT set
  3. * Copyright (c) 2014-2015 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. * Some obscure user-space code requires the ability to make system calls
  15. * with FLAGS.NT set. Make sure it works.
  16. */
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <signal.h>
  21. #include <err.h>
  22. #include <sys/syscall.h>
  23. #include <asm/processor-flags.h>
  24. #ifdef __x86_64__
  25. # define WIDTH "q"
  26. #else
  27. # define WIDTH "l"
  28. #endif
  29. static unsigned int nerrs;
  30. static unsigned long get_eflags(void)
  31. {
  32. unsigned long eflags;
  33. asm volatile ("pushf" WIDTH "\n\tpop" WIDTH " %0" : "=rm" (eflags));
  34. return eflags;
  35. }
  36. static void set_eflags(unsigned long eflags)
  37. {
  38. asm volatile ("push" WIDTH " %0\n\tpopf" WIDTH
  39. : : "rm" (eflags) : "flags");
  40. }
  41. static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
  42. int flags)
  43. {
  44. struct sigaction sa;
  45. memset(&sa, 0, sizeof(sa));
  46. sa.sa_sigaction = handler;
  47. sa.sa_flags = SA_SIGINFO | flags;
  48. sigemptyset(&sa.sa_mask);
  49. if (sigaction(sig, &sa, 0))
  50. err(1, "sigaction");
  51. }
  52. static void sigtrap(int sig, siginfo_t *si, void *ctx_void)
  53. {
  54. }
  55. static void do_it(unsigned long extraflags)
  56. {
  57. unsigned long flags;
  58. set_eflags(get_eflags() | extraflags);
  59. syscall(SYS_getpid);
  60. flags = get_eflags();
  61. set_eflags(X86_EFLAGS_IF | X86_EFLAGS_FIXED);
  62. if ((flags & extraflags) == extraflags) {
  63. printf("[OK]\tThe syscall worked and flags are still set\n");
  64. } else {
  65. printf("[FAIL]\tThe syscall worked but flags were cleared (flags = 0x%lx but expected 0x%lx set)\n",
  66. flags, extraflags);
  67. nerrs++;
  68. }
  69. }
  70. int main(void)
  71. {
  72. printf("[RUN]\tSet NT and issue a syscall\n");
  73. do_it(X86_EFLAGS_NT);
  74. /*
  75. * Now try it again with TF set -- TF forces returns via IRET in all
  76. * cases except non-ptregs-using 64-bit full fast path syscalls.
  77. */
  78. sethandler(SIGTRAP, sigtrap, 0);
  79. printf("[RUN]\tSet NT|TF and issue a syscall\n");
  80. do_it(X86_EFLAGS_NT | X86_EFLAGS_TF);
  81. return nerrs == 0 ? 0 : 1;
  82. }