fd-003-kthread.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. // Test that /proc/$KERNEL_THREAD/fd/ is empty.
  17. #undef NDEBUG
  18. #include <sys/syscall.h>
  19. #include <assert.h>
  20. #include <dirent.h>
  21. #include <limits.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include "proc.h"
  29. #define PF_KHTREAD 0x00200000
  30. /*
  31. * Test for kernel threadness atomically with openat().
  32. *
  33. * Return /proc/$PID/fd descriptor if process is kernel thread.
  34. * Return -1 if a process is userspace process.
  35. */
  36. static int kernel_thread_fd(unsigned int pid)
  37. {
  38. unsigned int flags = 0;
  39. char buf[4096];
  40. int dir_fd, fd;
  41. ssize_t rv;
  42. snprintf(buf, sizeof(buf), "/proc/%u", pid);
  43. dir_fd = open(buf, O_RDONLY|O_DIRECTORY);
  44. if (dir_fd == -1)
  45. return -1;
  46. /*
  47. * Believe it or not, struct task_struct::flags is directly exposed
  48. * to userspace!
  49. */
  50. fd = openat(dir_fd, "stat", O_RDONLY);
  51. if (fd == -1) {
  52. close(dir_fd);
  53. return -1;
  54. }
  55. rv = read(fd, buf, sizeof(buf));
  56. close(fd);
  57. if (0 < rv && rv <= sizeof(buf)) {
  58. unsigned long long flags_ull;
  59. char *p, *end;
  60. int i;
  61. assert(buf[rv - 1] == '\n');
  62. buf[rv - 1] = '\0';
  63. /* Search backwards: ->comm can contain whitespace and ')'. */
  64. for (i = 0; i < 43; i++) {
  65. p = strrchr(buf, ' ');
  66. assert(p);
  67. *p = '\0';
  68. }
  69. p = strrchr(buf, ' ');
  70. assert(p);
  71. flags_ull = xstrtoull(p + 1, &end);
  72. assert(*end == '\0');
  73. assert(flags_ull == (unsigned int)flags_ull);
  74. flags = flags_ull;
  75. }
  76. fd = -1;
  77. if (flags & PF_KHTREAD) {
  78. fd = openat(dir_fd, "fd", O_RDONLY|O_DIRECTORY);
  79. }
  80. close(dir_fd);
  81. return fd;
  82. }
  83. static void test_readdir(int fd)
  84. {
  85. DIR *d;
  86. struct dirent *de;
  87. d = fdopendir(fd);
  88. assert(d);
  89. de = xreaddir(d);
  90. assert(streq(de->d_name, "."));
  91. assert(de->d_type == DT_DIR);
  92. de = xreaddir(d);
  93. assert(streq(de->d_name, ".."));
  94. assert(de->d_type == DT_DIR);
  95. de = xreaddir(d);
  96. assert(!de);
  97. }
  98. static inline int sys_statx(int dirfd, const char *pathname, int flags,
  99. unsigned int mask, void *stx)
  100. {
  101. return syscall(SYS_statx, dirfd, pathname, flags, mask, stx);
  102. }
  103. static void test_lookup_fail(int fd, const char *pathname)
  104. {
  105. char stx[256] __attribute__((aligned(8)));
  106. int rv;
  107. rv = sys_statx(fd, pathname, AT_SYMLINK_NOFOLLOW, 0, (void *)stx);
  108. assert(rv == -1 && errno == ENOENT);
  109. }
  110. static void test_lookup(int fd)
  111. {
  112. char buf[64];
  113. unsigned int u;
  114. int i;
  115. for (i = INT_MIN; i < INT_MIN + 1024; i++) {
  116. snprintf(buf, sizeof(buf), "%d", i);
  117. test_lookup_fail(fd, buf);
  118. }
  119. for (i = -1024; i < 1024; i++) {
  120. snprintf(buf, sizeof(buf), "%d", i);
  121. test_lookup_fail(fd, buf);
  122. }
  123. for (u = INT_MAX - 1024; u < (unsigned int)INT_MAX + 1024; u++) {
  124. snprintf(buf, sizeof(buf), "%u", u);
  125. test_lookup_fail(fd, buf);
  126. }
  127. for (u = UINT_MAX - 1024; u != 0; u++) {
  128. snprintf(buf, sizeof(buf), "%u", u);
  129. test_lookup_fail(fd, buf);
  130. }
  131. }
  132. int main(void)
  133. {
  134. unsigned int pid;
  135. int fd;
  136. /*
  137. * In theory this will loop indefinitely if kernel threads are exiled
  138. * from /proc.
  139. *
  140. * Start with kthreadd.
  141. */
  142. pid = 2;
  143. while ((fd = kernel_thread_fd(pid)) == -1 && pid < 1024) {
  144. pid++;
  145. }
  146. /* EACCES if run as non-root. */
  147. if (pid >= 1024)
  148. return 1;
  149. test_readdir(fd);
  150. test_lookup(fd);
  151. return 0;
  152. }