PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/path/proc.c

https://gitlab.com/cedric-vincent/proot
C | 195 lines | 103 code | 30 blank | 62 comment | 34 complexity | 5ba272e016156a26ac31c0caf8b53aa8 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
  2. *
  3. * This file is part of PRoot.
  4. *
  5. * Copyright (C) 2014 STMicroelectronics
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301 USA.
  21. */
  22. #include <stdio.h> /* snprintf(3), */
  23. #include <string.h> /* strcmp(3), */
  24. #include <stdlib.h> /* atoi(3), strtol(3), */
  25. #include <errno.h> /* E*, */
  26. #include <assert.h> /* assert(3), */
  27. #include "path/proc.h"
  28. #include "tracee/tracee.h"
  29. #include "path/path.h"
  30. #include "path/binding.h"
  31. /**
  32. * This function emulates the @result of readlink("@base/@component")
  33. * with respect to @tracee, where @base belongs to "/proc" (according
  34. * to @comparison). This function returns -errno on error, an enum
  35. * @action otherwise (c.f. above).
  36. *
  37. * Unlike readlink(), this function includes the nul terminating byte
  38. * to @result.
  39. */
  40. Action readlink_proc(const Tracee *tracee, char result[PATH_MAX],
  41. const char base[PATH_MAX], const char component[NAME_MAX],
  42. Comparison comparison)
  43. {
  44. const Tracee *known_tracee;
  45. char proc_path[64]; /* 64 > sizeof("/proc//fd/") + 2 * sizeof(#ULONG_MAX) */
  46. int status;
  47. pid_t pid;
  48. assert(comparison == compare_paths("/proc", base));
  49. /* Remember: comparison = compare_paths("/proc", base) */
  50. switch (comparison) {
  51. case PATHS_ARE_EQUAL:
  52. /* Substitute "/proc/self" with "/proc/<PID>". */
  53. if (strcmp(component, "self") != 0)
  54. return DEFAULT;
  55. status = snprintf(result, PATH_MAX, "/proc/%d", tracee->pid);
  56. if (status < 0 || status >= PATH_MAX)
  57. return -EPERM;
  58. return CANONICALIZE;
  59. case PATH1_IS_PREFIX:
  60. /* Handle "/proc/<PID>" below, where <PID> is process
  61. * monitored by PRoot. */
  62. break;
  63. default:
  64. return DEFAULT;
  65. }
  66. pid = atoi(base + strlen("/proc/"));
  67. if (pid == 0)
  68. return DEFAULT;
  69. /* Handle links in "/proc/<PID>/". */
  70. status = snprintf(proc_path, sizeof(proc_path), "/proc/%d", pid);
  71. if (status < 0 || (size_t) status >= sizeof(proc_path))
  72. return -EPERM;
  73. comparison = compare_paths(proc_path, base);
  74. switch (comparison) {
  75. case PATHS_ARE_EQUAL:
  76. known_tracee = get_tracee(tracee, pid, false);
  77. if (known_tracee == NULL)
  78. return DEFAULT;
  79. #define SUBSTITUTE(name, string) \
  80. do { \
  81. if (strcmp(component, #name) != 0) \
  82. break; \
  83. \
  84. status = strlen(string); \
  85. if (status >= PATH_MAX) \
  86. return -EPERM; \
  87. \
  88. strncpy(result, string, status + 1); \
  89. return CANONICALIZE; \
  90. } while (0)
  91. /* Substitute link "/proc/<PID>/???" with the content
  92. * of tracee->???. */
  93. SUBSTITUTE(exe, known_tracee->exe);
  94. SUBSTITUTE(cwd, known_tracee->fs->cwd);
  95. SUBSTITUTE(root, get_root(known_tracee));
  96. #undef SUBSTITUTE
  97. return DEFAULT;
  98. case PATH1_IS_PREFIX:
  99. /* Handle "/proc/<PID>/???" below. */
  100. break;
  101. default:
  102. return DEFAULT;
  103. }
  104. /* Handle links in "/proc/<PID>/fd/". */
  105. status = snprintf(proc_path, sizeof(proc_path), "/proc/%d/fd", pid);
  106. if (status < 0 || (size_t) status >= sizeof(proc_path))
  107. return -EPERM;
  108. comparison = compare_paths(proc_path, base);
  109. switch (comparison) {
  110. char *end_ptr;
  111. case PATHS_ARE_EQUAL:
  112. /* Sanity check: a number is expected. */
  113. errno = 0;
  114. (void) strtol(component, &end_ptr, 10);
  115. if (errno != 0 || end_ptr == component)
  116. return -EPERM;
  117. /* Don't dereference "/proc/<PID>/fd/???" now: they
  118. * can point to anonymous pipe, socket, ... otherwise
  119. * they point to a path already canonicalized by the
  120. * kernel.
  121. *
  122. * Note they are still correctly detranslated in
  123. * syscall/exit.c if a monitored process uses
  124. * readlink() against any of them. */
  125. status = snprintf(result, PATH_MAX, "%s/%s", base, component);
  126. if (status < 0 || status >= PATH_MAX)
  127. return -EPERM;
  128. return DONT_CANONICALIZE;
  129. default:
  130. break;
  131. }
  132. return DEFAULT;
  133. }
  134. /**
  135. * This function emulates the @result of readlink("@referer") with
  136. * respect to @tracee, where @referer is a strict subpath of "/proc".
  137. * This function returns -errno if an error occured, the length of
  138. * @result if the readlink was emulated, 0 otherwise.
  139. *
  140. * Unlike readlink(), this function includes the nul terminating byte
  141. * to @result (but this byte is not counted in the returned value).
  142. */
  143. ssize_t readlink_proc2(const Tracee *tracee, char result[PATH_MAX], const char referer[PATH_MAX])
  144. {
  145. Action action;
  146. char base[PATH_MAX];
  147. char *component;
  148. /* Sanity check. */
  149. if (strnlen(referer, PATH_MAX) >= PATH_MAX)
  150. return -ENAMETOOLONG;
  151. assert(compare_paths("/proc", referer) == PATH1_IS_PREFIX);
  152. /* It's safe to use strrchr() here since @referer was
  153. * previously canonicalized. */
  154. strcpy(base, referer);
  155. component = strrchr(base, '/');
  156. /* These cases are not possible: @referer is supposed to be a
  157. * canonicalized subpath of "/proc". */
  158. assert(component != NULL && component != base);
  159. component[0] = '\0';
  160. component++;
  161. if (component[0] == '\0')
  162. return 0;
  163. action = readlink_proc(tracee, result, base, component, PATH1_IS_PREFIX);
  164. return (action == CANONICALIZE ? strlen(result) : 0);
  165. }