bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * canonicalize.c -- canonicalize pathname by removing symlinks |
| 3 | * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Library Public License as published by |
| 7 | * the Free Software Foundation; either version 2, or (at your option) |
| 8 | * any later version. |
| 9 | * |
| 10 | * This program 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 |
| 13 | * GNU Library Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * This routine is part of libc. We include it nevertheless, |
| 19 | * since the libc version has some security flaws. |
| 20 | * |
| 21 | * TODO: use canonicalize_file_name() when exist in glibc |
| 22 | */ |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <ctype.h> |
| 26 | #include <unistd.h> |
| 27 | #include <errno.h> |
| 28 | #include <stdlib.h> |
| 29 | |
| 30 | #include "canonicalize.h" |
| 31 | |
| 32 | #ifndef MAXSYMLINKS |
| 33 | # define MAXSYMLINKS 256 |
| 34 | #endif |
| 35 | |
| 36 | static char * |
| 37 | myrealpath(const char *path, char *resolved_path, int maxreslth) { |
| 38 | int readlinks = 0; |
| 39 | char *npath; |
| 40 | char link_path[PATH_MAX+1]; |
| 41 | int n; |
| 42 | char *buf = NULL; |
| 43 | |
| 44 | npath = resolved_path; |
| 45 | |
| 46 | /* If it's a relative pathname use getcwd for starters. */ |
| 47 | if (*path != '/') { |
| 48 | if (!getcwd(npath, maxreslth-2)) |
| 49 | return NULL; |
| 50 | npath += strlen(npath); |
| 51 | if (npath[-1] != '/') |
| 52 | *npath++ = '/'; |
| 53 | } else { |
| 54 | *npath++ = '/'; |
| 55 | path++; |
| 56 | } |
| 57 | |
| 58 | /* Expand each slash-separated pathname component. */ |
| 59 | while (*path != '\0') { |
| 60 | /* Ignore stray "/" */ |
| 61 | if (*path == '/') { |
| 62 | path++; |
| 63 | continue; |
| 64 | } |
| 65 | if (*path == '.' && (path[1] == '\0' || path[1] == '/')) { |
| 66 | /* Ignore "." */ |
| 67 | path++; |
| 68 | continue; |
| 69 | } |
| 70 | if (*path == '.' && path[1] == '.' && |
| 71 | (path[2] == '\0' || path[2] == '/')) { |
| 72 | /* Backup for ".." */ |
| 73 | path += 2; |
| 74 | while (npath > resolved_path+1 && |
| 75 | (--npath)[-1] != '/') |
| 76 | ; |
| 77 | continue; |
| 78 | } |
| 79 | /* Safely copy the next pathname component. */ |
| 80 | while (*path != '\0' && *path != '/') { |
| 81 | if (npath-resolved_path > maxreslth-2) { |
| 82 | errno = ENAMETOOLONG; |
| 83 | goto err; |
| 84 | } |
| 85 | *npath++ = *path++; |
| 86 | } |
| 87 | |
| 88 | /* Protect against infinite loops. */ |
| 89 | if (readlinks++ > MAXSYMLINKS) { |
| 90 | errno = ELOOP; |
| 91 | goto err; |
| 92 | } |
| 93 | |
| 94 | /* See if last pathname component is a symlink. */ |
| 95 | *npath = '\0'; |
| 96 | n = readlink(resolved_path, link_path, PATH_MAX); |
| 97 | if (n < 0) { |
| 98 | /* EINVAL means the file exists but isn't a symlink. */ |
| 99 | if (errno != EINVAL) |
| 100 | goto err; |
| 101 | } else { |
| 102 | int m; |
| 103 | char *newbuf; |
| 104 | |
| 105 | /* Note: readlink doesn't add the null byte. */ |
| 106 | link_path[n] = '\0'; |
| 107 | if (*link_path == '/') |
| 108 | /* Start over for an absolute symlink. */ |
| 109 | npath = resolved_path; |
| 110 | else |
| 111 | /* Otherwise back up over this component. */ |
| 112 | while (*(--npath) != '/') |
| 113 | ; |
| 114 | |
| 115 | /* Insert symlink contents into path. */ |
| 116 | m = strlen(path); |
| 117 | newbuf = malloc(m + n + 1); |
| 118 | if (!newbuf) |
| 119 | goto err; |
| 120 | memcpy(newbuf, link_path, n); |
| 121 | memcpy(newbuf + n, path, m + 1); |
| 122 | free(buf); |
| 123 | path = buf = newbuf; |
| 124 | } |
| 125 | *npath++ = '/'; |
| 126 | } |
| 127 | /* Delete trailing slash but don't whomp a lone slash. */ |
| 128 | if (npath != resolved_path+1 && npath[-1] == '/') |
| 129 | npath--; |
| 130 | /* Make sure it's null terminated. */ |
| 131 | *npath = '\0'; |
| 132 | |
| 133 | free(buf); |
| 134 | return resolved_path; |
| 135 | |
| 136 | err: |
| 137 | free(buf); |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Converts private "dm-N" names to "/dev/mapper/<name>" |
| 143 | * |
| 144 | * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs |
| 145 | * provides the real DM device names in /sys/block/<ptname>/dm/name |
| 146 | */ |
| 147 | char * |
| 148 | canonicalize_dm_name(const char *ptname) |
| 149 | { |
| 150 | FILE *f; |
| 151 | size_t sz; |
| 152 | char path[256], name[256], *res = NULL; |
| 153 | |
| 154 | snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname); |
| 155 | if (!(f = fopen(path, "r"))) |
| 156 | return NULL; |
| 157 | |
| 158 | /* read "<name>\n" from sysfs */ |
| 159 | if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) { |
| 160 | name[sz - 1] = '\0'; |
| 161 | snprintf(path, sizeof(path), "/dev/mapper/%s", name); |
| 162 | res = strdup(path); |
| 163 | } |
| 164 | fclose(f); |
| 165 | return res; |
| 166 | } |
| 167 | |
| 168 | char * |
| 169 | canonicalize_path(const char *path) |
| 170 | { |
| 171 | char canonical[PATH_MAX+2]; |
| 172 | char *p; |
| 173 | |
| 174 | if (path == NULL) |
| 175 | return NULL; |
| 176 | |
| 177 | if (!myrealpath(path, canonical, PATH_MAX+1)) |
| 178 | return strdup(path); |
| 179 | |
| 180 | |
| 181 | p = strrchr(canonical, '/'); |
| 182 | if (p && strncmp(p, "/dm-", 4) == 0 && isdigit(*(p + 4))) { |
| 183 | p = canonicalize_dm_name(p+1); |
| 184 | if (p) |
| 185 | return p; |
| 186 | } |
| 187 | |
| 188 | return strdup(canonical); |
| 189 | } |
| 190 | |
| 191 | char * |
| 192 | canonicalize_path_restricted(const char *path) |
| 193 | { |
| 194 | char canonical[PATH_MAX+2]; |
| 195 | char *p = NULL; |
| 196 | int errsv; |
| 197 | uid_t euid; |
| 198 | gid_t egid; |
| 199 | |
| 200 | if (path == NULL) |
| 201 | return NULL; |
| 202 | |
| 203 | euid = geteuid(); |
| 204 | egid = getegid(); |
| 205 | |
| 206 | /* drop permissions */ |
| 207 | if (setegid(getgid()) < 0 || seteuid(getuid()) < 0) |
| 208 | return NULL; |
| 209 | |
| 210 | errsv = errno = 0; |
| 211 | |
| 212 | if (myrealpath(path, canonical, PATH_MAX+1)) { |
| 213 | p = strrchr(canonical, '/'); |
| 214 | if (p && strncmp(p, "/dm-", 4) == 0 && isdigit(*(p + 4))) |
| 215 | p = canonicalize_dm_name(p+1); |
| 216 | else |
| 217 | p = NULL; |
| 218 | if (!p) |
| 219 | p = strdup(canonical); |
| 220 | } else |
| 221 | errsv = errno; |
| 222 | |
| 223 | /* restore */ |
| 224 | if (setegid(egid) < 0 || seteuid(euid) < 0) { |
| 225 | free(p); |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | errno = errsv; |
| 230 | return p; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | #ifdef TEST_PROGRAM_CANONICALIZE |
| 235 | int main(int argc, char **argv) |
| 236 | { |
| 237 | if (argc < 2) { |
| 238 | fprintf(stderr, "usage: %s <device>\n", argv[0]); |
| 239 | exit(EXIT_FAILURE); |
| 240 | } |
| 241 | |
| 242 | fprintf(stdout, "orig: %s\n", argv[1]); |
| 243 | fprintf(stdout, "real: %s\n", canonicalize_path(argv[1])); |
| 244 | |
| 245 | exit(EXIT_SUCCESS); |
| 246 | } |
| 247 | #endif |