Update.
[glibc.git] / elf / chroot_canon.c
blobd29a032163e5e7c8cb4a8ee415be5694f3694e58
1 /* Return the canonical absolute name of a given file inside chroot.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2004
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <limits.h>
25 #include <sys/param.h>
26 #include <sys/stat.h>
27 #include <errno.h>
28 #include <stddef.h>
29 #include <stdint.h>
31 #include "ldconfig.h"
33 #ifndef PATH_MAX
34 #define PATH_MAX 1024
35 #endif
37 /* Return the canonical absolute name of file NAME as if chroot(CHROOT) was
38 done first. A canonical name does not contain any `.', `..' components
39 nor any repeated path separators ('/') or symlinks. All path components
40 must exist and NAME must be absolute filename. The result is malloc'd.
41 The returned name includes the CHROOT prefix. */
43 char *
44 chroot_canon (const char *chroot, const char *name)
46 char *rpath;
47 char *dest;
48 char *extra_buf = NULL;
49 char *rpath_root;
50 const char *start;
51 const char *end;
52 const char *rpath_limit;
53 int num_links = 0;
54 size_t chroot_len = strlen (chroot);
56 if (chroot_len < 1)
58 __set_errno (EINVAL);
59 return NULL;
62 rpath = malloc (chroot_len + PATH_MAX);
63 if (rpath == NULL)
64 return NULL;
66 rpath_limit = rpath + chroot_len + PATH_MAX;
68 rpath_root = (char *) mempcpy (rpath, chroot, chroot_len) - 1;
69 if (*rpath_root != '/')
70 *++rpath_root = '/';
71 dest = rpath_root + 1;
73 for (start = end = name; *start; start = end)
75 struct stat64 st;
76 int n;
78 /* Skip sequence of multiple path-separators. */
79 while (*start == '/')
80 ++start;
82 /* Find end of path component. */
83 for (end = start; *end && *end != '/'; ++end)
84 /* Nothing. */;
86 if (end - start == 0)
87 break;
88 else if (end - start == 1 && start[0] == '.')
89 /* nothing */;
90 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
92 /* Back up to previous component, ignore if at root already. */
93 if (dest > rpath_root + 1)
94 while ((--dest)[-1] != '/');
96 else
98 size_t new_size;
100 if (dest[-1] != '/')
101 *dest++ = '/';
103 if (dest + (end - start) >= rpath_limit)
105 ptrdiff_t dest_offset = dest - rpath;
106 char *new_rpath;
108 new_size = rpath_limit - rpath;
109 if (end - start + 1 > PATH_MAX)
110 new_size += end - start + 1;
111 else
112 new_size += PATH_MAX;
113 new_rpath = (char *) realloc (rpath, new_size);
114 if (new_rpath == NULL)
115 goto error;
116 rpath = new_rpath;
117 rpath_limit = rpath + new_size;
119 dest = rpath + dest_offset;
122 dest = mempcpy (dest, start, end - start);
123 *dest = '\0';
125 if (lstat64 (rpath, &st) < 0)
127 if (*end == '\0')
128 goto done;
129 goto error;
132 if (S_ISLNK (st.st_mode))
134 char *buf = alloca (PATH_MAX);
135 size_t len;
137 if (++num_links > MAXSYMLINKS)
139 __set_errno (ELOOP);
140 goto error;
143 n = readlink (rpath, buf, PATH_MAX);
144 if (n < 0)
146 if (*end == '\0')
147 goto done;
148 goto error;
150 buf[n] = '\0';
152 if (!extra_buf)
153 extra_buf = alloca (PATH_MAX);
155 len = strlen (end);
156 if ((long int) (n + len) >= PATH_MAX)
158 __set_errno (ENAMETOOLONG);
159 goto error;
162 /* Careful here, end may be a pointer into extra_buf... */
163 memmove (&extra_buf[n], end, len + 1);
164 name = end = memcpy (extra_buf, buf, n);
166 if (buf[0] == '/')
167 dest = rpath_root + 1; /* It's an absolute symlink */
168 else
169 /* Back up to previous component, ignore if at root already: */
170 if (dest > rpath_root + 1)
171 while ((--dest)[-1] != '/');
175 done:
176 if (dest > rpath_root + 1 && dest[-1] == '/')
177 --dest;
178 *dest = '\0';
180 return rpath;
182 error:
183 free (rpath);
184 return NULL;