chroot_realpath.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * chroot_realpath.c -- resolve pathname as if inside chroot
  3. * Based on realpath.c Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; see the file COPYING.LIB. If not,
  17. * see <http://www.gnu.org/licenses/>.
  18. *
  19. * 2005/09/12: Dan Howell (modified from realpath.c to emulate chroot)
  20. */
  21. #include "porting.h"
  22. #define MAX_READLINKS 32
  23. char *chroot_realpath(const char *root, const char *path,
  24. char resolved_path[]);
  25. char *chroot_realpath(const char *root, const char *path,
  26. char resolved_path[])
  27. {
  28. char copy_path[PATH_MAX];
  29. char link_path[PATH_MAX];
  30. char got_path[PATH_MAX];
  31. char *got_path_root = got_path;
  32. char *new_path = got_path;
  33. char *max_path;
  34. int readlinks = 0;
  35. int n;
  36. int chroot_len;
  37. /* Trivial case. */
  38. if (root == NULL || *root == '\0' ||
  39. (*root == '/' && root[1] == '\0')) {
  40. strcpy(resolved_path, path);
  41. return resolved_path;
  42. }
  43. chroot_len = strlen(root);
  44. if (chroot_len + strlen(path) >= PATH_MAX - 3) {
  45. errno = ENAMETOOLONG;
  46. return NULL;
  47. }
  48. /* Make a copy of the source path since we may need to modify it. */
  49. strcpy(copy_path, path);
  50. path = copy_path;
  51. max_path = copy_path + PATH_MAX - chroot_len - 3;
  52. /* Start with the chroot path. */
  53. strcpy(new_path, root);
  54. new_path += chroot_len;
  55. while (*new_path == '/' && new_path > got_path)
  56. new_path--;
  57. got_path_root = new_path;
  58. *new_path++ = '/';
  59. /* Expand each slash-separated pathname component. */
  60. while (*path != '\0') {
  61. /* Ignore stray "/". */
  62. if (*path == '/') {
  63. path++;
  64. continue;
  65. }
  66. if (*path == '.') {
  67. /* Ignore ".". */
  68. if (path[1] == '\0' || path[1] == '/') {
  69. path++;
  70. continue;
  71. }
  72. if (path[1] == '.') {
  73. if (path[2] == '\0' || path[2] == '/') {
  74. path += 2;
  75. /* Ignore ".." at root. */
  76. if (new_path == got_path_root + 1)
  77. continue;
  78. /* Handle ".." by backing up. */
  79. while ((--new_path)[-1] != '/') ;
  80. continue;
  81. }
  82. }
  83. }
  84. /* Safely copy the next pathname component. */
  85. while (*path != '\0' && *path != '/') {
  86. if (path > max_path) {
  87. errno = ENAMETOOLONG;
  88. return NULL;
  89. }
  90. *new_path++ = *path++;
  91. }
  92. if (*path == '\0')
  93. /* Don't follow symlink for last pathname component. */
  94. break;
  95. #ifdef S_IFLNK
  96. /* Protect against infinite loops. */
  97. if (readlinks++ > MAX_READLINKS) {
  98. errno = ELOOP;
  99. return NULL;
  100. }
  101. /* See if latest pathname component is a symlink. */
  102. *new_path = '\0';
  103. n = readlink(got_path, link_path, PATH_MAX - 1);
  104. if (n < 0) {
  105. /* EINVAL means the file exists but isn't a symlink. */
  106. if (errno != EINVAL) {
  107. /* Make sure it's null terminated. */
  108. *new_path = '\0';
  109. strcpy(resolved_path, got_path);
  110. return NULL;
  111. }
  112. } else {
  113. /* Note: readlink doesn't add the null byte. */
  114. link_path[n] = '\0';
  115. if (*link_path == '/')
  116. /* Start over for an absolute symlink. */
  117. new_path = got_path_root;
  118. else
  119. /* Otherwise back up over this component. */
  120. while (*(--new_path) != '/') ;
  121. /* Safe sex check. */
  122. if (strlen(path) + n >= PATH_MAX - 2) {
  123. errno = ENAMETOOLONG;
  124. return NULL;
  125. }
  126. /* Insert symlink contents into path. */
  127. strcat(link_path, path);
  128. strcpy(copy_path, link_path);
  129. path = copy_path;
  130. }
  131. #endif /* S_IFLNK */
  132. *new_path++ = '/';
  133. }
  134. /* Delete trailing slash but don't whomp a lone slash. */
  135. if (new_path != got_path + 1 && new_path[-1] == '/')
  136. new_path--;
  137. /* Make sure it's null terminated. */
  138. *new_path = '\0';
  139. strcpy(resolved_path, got_path);
  140. return resolved_path;
  141. }