Back to index

glibc  2.9
chroot_canon.c
Go to the documentation of this file.
00001 /* Return the canonical absolute name of a given file inside chroot.
00002    Copyright (C) 1996,1997,1998,1999,2000,2001,2004,2005
00003        Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published
00008    by the Free Software Foundation; version 2 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software Foundation,
00018    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00019 
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include <unistd.h>
00023 #include <limits.h>
00024 #include <sys/param.h>
00025 #include <sys/stat.h>
00026 #include <errno.h>
00027 #include <stddef.h>
00028 #include <stdint.h>
00029 
00030 #include <ldconfig.h>
00031 
00032 #ifndef PATH_MAX
00033 #define PATH_MAX 1024
00034 #endif
00035 
00036 /* Return the canonical absolute name of file NAME as if chroot(CHROOT) was
00037    done first.  A canonical name does not contain any `.', `..' components
00038    nor any repeated path separators ('/') or symlinks.  All path components
00039    must exist and NAME must be absolute filename.  The result is malloc'd.
00040    The returned name includes the CHROOT prefix.  */
00041 
00042 char *
00043 chroot_canon (const char *chroot, const char *name)
00044 {
00045   char *rpath;
00046   char *dest;
00047   char *extra_buf = NULL;
00048   char *rpath_root;
00049   const char *start;
00050   const char *end;
00051   const char *rpath_limit;
00052   int num_links = 0;
00053   size_t chroot_len = strlen (chroot);
00054 
00055   if (chroot_len < 1)
00056     {
00057       __set_errno (EINVAL);
00058       return NULL;
00059     }
00060 
00061   rpath = malloc (chroot_len + PATH_MAX);
00062   if (rpath == NULL)
00063     return NULL;
00064 
00065   rpath_limit = rpath + chroot_len + PATH_MAX;
00066 
00067   rpath_root = (char *) mempcpy (rpath, chroot, chroot_len) - 1;
00068   if (*rpath_root != '/')
00069     *++rpath_root = '/';
00070   dest = rpath_root + 1;
00071 
00072   for (start = end = name; *start; start = end)
00073     {
00074       struct stat64 st;
00075       int n;
00076 
00077       /* Skip sequence of multiple path-separators.  */
00078       while (*start == '/')
00079        ++start;
00080 
00081       /* Find end of path component.  */
00082       for (end = start; *end && *end != '/'; ++end)
00083        /* Nothing.  */;
00084 
00085       if (end - start == 0)
00086        break;
00087       else if (end - start == 1 && start[0] == '.')
00088        /* nothing */;
00089       else if (end - start == 2 && start[0] == '.' && start[1] == '.')
00090        {
00091          /* Back up to previous component, ignore if at root already.  */
00092          if (dest > rpath_root + 1)
00093            while ((--dest)[-1] != '/');
00094        }
00095       else
00096        {
00097          size_t new_size;
00098 
00099          if (dest[-1] != '/')
00100            *dest++ = '/';
00101 
00102          if (dest + (end - start) >= rpath_limit)
00103            {
00104              ptrdiff_t dest_offset = dest - rpath;
00105              char *new_rpath;
00106 
00107              new_size = rpath_limit - rpath;
00108              if (end - start + 1 > PATH_MAX)
00109               new_size += end - start + 1;
00110              else
00111               new_size += PATH_MAX;
00112              new_rpath = (char *) realloc (rpath, new_size);
00113              if (new_rpath == NULL)
00114               goto error;
00115              rpath = new_rpath;
00116              rpath_limit = rpath + new_size;
00117 
00118              dest = rpath + dest_offset;
00119            }
00120 
00121          dest = mempcpy (dest, start, end - start);
00122          *dest = '\0';
00123 
00124          if (lstat64 (rpath, &st) < 0)
00125            {
00126              if (*end == '\0')
00127               goto done;
00128              goto error;
00129            }
00130 
00131          if (S_ISLNK (st.st_mode))
00132            {
00133              char *buf = alloca (PATH_MAX);
00134              size_t len;
00135 
00136              if (++num_links > MAXSYMLINKS)
00137               {
00138                 __set_errno (ELOOP);
00139                 goto error;
00140               }
00141 
00142              n = readlink (rpath, buf, PATH_MAX);
00143              if (n < 0)
00144               {
00145                 if (*end == '\0')
00146                   goto done;
00147                 goto error;
00148               }
00149              buf[n] = '\0';
00150 
00151              if (!extra_buf)
00152               extra_buf = alloca (PATH_MAX);
00153 
00154              len = strlen (end);
00155              if ((long int) (n + len) >= PATH_MAX)
00156               {
00157                 __set_errno (ENAMETOOLONG);
00158                 goto error;
00159               }
00160 
00161              /* Careful here, end may be a pointer into extra_buf... */
00162              memmove (&extra_buf[n], end, len + 1);
00163              name = end = memcpy (extra_buf, buf, n);
00164 
00165              if (buf[0] == '/')
00166               dest = rpath_root + 1;      /* It's an absolute symlink */
00167              else
00168               /* Back up to previous component, ignore if at root already: */
00169               if (dest > rpath_root + 1)
00170                 while ((--dest)[-1] != '/');
00171            }
00172        }
00173     }
00174  done:
00175   if (dest > rpath_root + 1 && dest[-1] == '/')
00176     --dest;
00177   *dest = '\0';
00178 
00179   return rpath;
00180 
00181  error:
00182   free (rpath);
00183   return NULL;
00184 }