bsd-closefrom.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2004-2005 Todd C. Miller <Todd.Miller@courtesan.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. #include "includes.h"
  17. #ifndef HAVE_CLOSEFROM
  18. #include <sys/types.h>
  19. #include <sys/param.h>
  20. #include <unistd.h>
  21. #include <stdio.h>
  22. #ifdef HAVE_FCNTL_H
  23. # include <fcntl.h>
  24. #endif
  25. #include <limits.h>
  26. #include <stdlib.h>
  27. #include <stddef.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #ifdef HAVE_DIRENT_H
  31. # include <dirent.h>
  32. # define NAMLEN(dirent) strlen((dirent)->d_name)
  33. #else
  34. # define dirent direct
  35. # define NAMLEN(dirent) (dirent)->d_namlen
  36. # ifdef HAVE_SYS_NDIR_H
  37. # include <sys/ndir.h>
  38. # endif
  39. # ifdef HAVE_SYS_DIR_H
  40. # include <sys/dir.h>
  41. # endif
  42. # ifdef HAVE_NDIR_H
  43. # include <ndir.h>
  44. # endif
  45. #endif
  46. #if defined(HAVE_LIBPROC_H)
  47. # include <libproc.h>
  48. #endif
  49. #ifndef OPEN_MAX
  50. # define OPEN_MAX 256
  51. #endif
  52. #if 0
  53. __unused static const char rcsid[] = "$Sudo: closefrom.c,v 1.11 2006/08/17 15:26:54 millert Exp $";
  54. #endif /* lint */
  55. #ifndef HAVE_FCNTL_CLOSEM
  56. /*
  57. * Close all file descriptors greater than or equal to lowfd.
  58. */
  59. static void
  60. closefrom_fallback(int lowfd)
  61. {
  62. long fd, maxfd;
  63. /*
  64. * Fall back on sysconf() or getdtablesize(). We avoid checking
  65. * resource limits since it is possible to open a file descriptor
  66. * and then drop the rlimit such that it is below the open fd.
  67. */
  68. #ifdef HAVE_SYSCONF
  69. maxfd = sysconf(_SC_OPEN_MAX);
  70. #else
  71. maxfd = getdtablesize();
  72. #endif /* HAVE_SYSCONF */
  73. if (maxfd < 0)
  74. maxfd = OPEN_MAX;
  75. for (fd = lowfd; fd < maxfd; fd++)
  76. (void) close((int) fd);
  77. }
  78. #endif /* HAVE_FCNTL_CLOSEM */
  79. #ifdef HAVE_FCNTL_CLOSEM
  80. void
  81. closefrom(int lowfd)
  82. {
  83. (void) fcntl(lowfd, F_CLOSEM, 0);
  84. }
  85. #elif defined(HAVE_LIBPROC_H) && defined(HAVE_PROC_PIDINFO)
  86. void
  87. closefrom(int lowfd)
  88. {
  89. int i, r, sz;
  90. pid_t pid = getpid();
  91. struct proc_fdinfo *fdinfo_buf = NULL;
  92. sz = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0);
  93. if (sz == 0)
  94. return; /* no fds, really? */
  95. else if (sz == -1)
  96. goto fallback;
  97. if ((fdinfo_buf = malloc(sz)) == NULL)
  98. goto fallback;
  99. r = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, fdinfo_buf, sz);
  100. if (r < 0 || r > sz)
  101. goto fallback;
  102. for (i = 0; i < r / (int)PROC_PIDLISTFD_SIZE; i++) {
  103. if (fdinfo_buf[i].proc_fd >= lowfd)
  104. close(fdinfo_buf[i].proc_fd);
  105. }
  106. free(fdinfo_buf);
  107. return;
  108. fallback:
  109. free(fdinfo_buf);
  110. closefrom_fallback(lowfd);
  111. return;
  112. }
  113. #elif defined(HAVE_DIRFD) && defined(HAVE_PROC_PID)
  114. void
  115. closefrom(int lowfd)
  116. {
  117. long fd;
  118. char fdpath[PATH_MAX], *endp;
  119. struct dirent *dent;
  120. DIR *dirp;
  121. int len;
  122. /* Check for a /proc/$$/fd directory. */
  123. len = snprintf(fdpath, sizeof(fdpath), "/proc/%ld/fd", (long)getpid());
  124. if (len > 0 && (size_t)len < sizeof(fdpath) && (dirp = opendir(fdpath))) {
  125. while ((dent = readdir(dirp)) != NULL) {
  126. fd = strtol(dent->d_name, &endp, 10);
  127. if (dent->d_name != endp && *endp == '\0' &&
  128. fd >= 0 && fd < INT_MAX && fd >= lowfd && fd != dirfd(dirp))
  129. #ifdef __s390__
  130. {
  131. /*
  132. * the filedescriptors used to communicate with
  133. * the device drivers to provide hardware support
  134. * should survive. HF <freude@de.ibm.com>
  135. */
  136. char fpath[PATH_MAX], lpath[PATH_MAX];
  137. len = snprintf(fpath, sizeof(fpath), "%s/%s",
  138. fdpath, dent->d_name);
  139. if (len > 0 && (size_t)len <= sizeof(fpath)) {
  140. len = readlink(fpath, lpath, sizeof(lpath));
  141. if (len > 0) {
  142. lpath[len] = 0;
  143. if (strstr(lpath, "dev/z90crypt")
  144. || strstr(lpath, "dev/zcrypt")
  145. || strstr(lpath, "dev/prandom")
  146. || strstr(lpath, "dev/shm/icastats"))
  147. fd = -1;
  148. }
  149. }
  150. if (fd >= 0)
  151. (void) close((int) fd);
  152. }
  153. #else
  154. (void) close((int) fd);
  155. #endif
  156. }
  157. (void) closedir(dirp);
  158. return;
  159. }
  160. /* /proc/$$/fd strategy failed, fall back to brute force closure */
  161. closefrom_fallback(lowfd);
  162. }
  163. #else
  164. void
  165. closefrom(int lowfd)
  166. {
  167. closefrom_fallback(lowfd);
  168. }
  169. #endif /* !HAVE_FCNTL_CLOSEM */
  170. #endif /* HAVE_CLOSEFROM */