syscall_nt.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 <sys/syscall.h>
  20. #include <asm/processor-flags.h>
  21. #ifdef __x86_64__
  22. # define WIDTH "q"
  23. #else
  24. # define WIDTH "l"
  25. #endif
  26. static unsigned long get_eflags(void)
  27. {
  28. unsigned long eflags;
  29. asm volatile ("pushf" WIDTH "\n\tpop" WIDTH " %0" : "=rm" (eflags));
  30. return eflags;
  31. }
  32. static void set_eflags(unsigned long eflags)
  33. {
  34. asm volatile ("push" WIDTH " %0\n\tpopf" WIDTH
  35. : : "rm" (eflags) : "flags");
  36. }
  37. int main()
  38. {
  39. printf("[RUN]\tSet NT and issue a syscall\n");
  40. set_eflags(get_eflags() | X86_EFLAGS_NT);
  41. syscall(SYS_getpid);
  42. if (get_eflags() & X86_EFLAGS_NT) {
  43. printf("[OK]\tThe syscall worked and NT is still set\n");
  44. return 0;
  45. } else {
  46. printf("[FAIL]\tThe syscall worked but NT was cleared\n");
  47. return 1;
  48. }
  49. }