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. #define _GNU_SOURCE
  18. #undef NDEBUG
  19. #include <sys/syscall.h>
  20. #include <assert.h>
  21. #include <dirent.h>
  22. #include <limits.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <unistd.h>
  29. #include "proc.h"
  30. #define PF_KHTREAD 0x00200000
  31. /*
  32. * Test for kernel threadness atomically with openat().
  33. *
  34. * Return /proc/$PID/fd descriptor if process is kernel thread.
  35. * Return -1 if a process is userspace process.
  36. */
  37. static int kernel_thread_fd(unsigned int pid)
  38. {
  39. unsigned int flags = 0;
  40. char buf[4096];
  41. int dir_fd, fd;
  42. ssize_t rv;
  43. snprintf(buf, sizeof(buf), "/proc/%u", pid);
  44. dir_fd = open(buf, O_RDONLY|O_DIRECTORY);
  45. if (dir_fd == -1)
  46. return -1;
  47. /*
  48. * Believe it or not, struct task_struct::flags is directly exposed
  49. * to userspace!
  50. */
  51. fd = openat(dir_fd, "stat", O_RDONLY);
  52. if (fd == -1) {
  53. close(dir_fd);
  54. return -1;
  55. }
  56. rv = read(fd, buf, sizeof(buf));
  57. close(fd);
  58. if (0 < rv && rv <= sizeof(buf)) {
  59. unsigned long long flags_ull;
  60. char *p, *end;
  61. int i;
  62. assert(buf[rv - 1] == '\n');
  63. buf[rv - 1] = '\0';
  64. /* Search backwards: ->comm can contain whitespace and ')'. */
  65. for (i = 0; i < 43; i++) {
  66. p = strrchr(buf, ' ');
  67. assert(p);
  68. *p = '\0';
  69. }
  70. p = strrchr(buf, ' ');
  71. assert(p);
  72. flags_ull = xstrtoull(p + 1, &end);
  73. assert(*end == '\0');
  74. assert(flags_ull == (unsigned int)flags_ull);
  75. flags = flags_ull;
  76. }
  77. fd = -1;
  78. if (flags & PF_KHTREAD) {
  79. fd = openat(dir_fd, "fd", O_RDONLY|O_DIRECTORY);
  80. }
  81. close(dir_fd);
  82. return fd;
  83. }
  84. static void test_readdir(int fd)
  85. {
  86. DIR *d;
  87. struct dirent *de;
  88. d = fdopendir(fd);
  89. assert(d);
  90. de = xreaddir(d);
  91. assert(streq(de->d_name, "."));
  92. assert(de->d_type == DT_DIR);
  93. de = xreaddir(d);
  94. assert(streq(de->d_name, ".."));
  95. assert(de->d_type == DT_DIR);
  96. de = xreaddir(d);
  97. assert(!de);
  98. }
  99. static inline int sys_statx(int dirfd, const char *pathname, int flags,
  100. unsigned int mask, void *stx)
  101. {
  102. return syscall(SYS_statx, dirfd, pathname, flags, mask, stx);
  103. }
  104. static void test_lookup_fail(int fd, const char *pathname)
  105. {
  106. char stx[256] __attribute__((aligned(8)));
  107. int rv;
  108. rv = sys_statx(fd, pathname, AT_SYMLINK_NOFOLLOW, 0, (void *)stx);
  109. assert(rv == -1 && errno == ENOENT);
  110. }
  111. static void test_lookup(int fd)
  112. {
  113. char buf[64];
  114. unsigned int u;
  115. int i;
  116. for (i = INT_MIN; i < INT_MIN + 1024; i++) {
  117. snprintf(buf, sizeof(buf), "%d", i);
  118. test_lookup_fail(fd, buf);
  119. }
  120. for (i = -1024; i < 1024; i++) {
  121. snprintf(buf, sizeof(buf), "%d", i);
  122. test_lookup_fail(fd, buf);
  123. }
  124. for (u = INT_MAX - 1024; u < (unsigned int)INT_MAX + 1024; u++) {
  125. snprintf(buf, sizeof(buf), "%u", u);
  126. test_lookup_fail(fd, buf);
  127. }
  128. for (u = UINT_MAX - 1024; u != 0; u++) {
  129. snprintf(buf, sizeof(buf), "%u", u);
  130. test_lookup_fail(fd, buf);
  131. }
  132. }
  133. int main(void)
  134. {
  135. unsigned int pid;
  136. int fd;
  137. /*
  138. * In theory this will loop indefinitely if kernel threads are exiled
  139. * from /proc.
  140. *
  141. * Start with kthreadd.
  142. */
  143. pid = 2;
  144. while ((fd = kernel_thread_fd(pid)) == -1 && pid < 1024) {
  145. pid++;
  146. }
  147. /* EACCES if run as non-root. */
  148. if (pid >= 1024)
  149. return 1;
  150. test_readdir(fd);
  151. test_lookup(fd);
  152. return 0;
  153. }