1 /*- 2 * Copyright (c) 1998 John D. Polstra 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/mman.h> 31 #include <sys/stat.h> 32 33 #include <ctype.h> 34 #include <dirent.h> 35 #include <elf.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 44 #include "ldconfig.h" 45 46 #define MAXDIRS 1024 /* Maximum directories in path */ 47 #define MAXFILESIZE (16*1024) /* Maximum hints file size */ 48 49 static void add_dir(const char *, const char *, int); 50 static void read_dirs_from_file(const char *, const char *); 51 static void read_elf_hints(const char *, int); 52 static void write_elf_hints(const char *); 53 54 static const char *dirs[MAXDIRS]; 55 static int ndirs; 56 57 static void 58 add_dir(const char *hintsfile, const char *name, int trusted) 59 { 60 struct stat stbuf; 61 int i; 62 63 /* Do some security checks */ 64 if (!trusted && !insecure) { 65 if (stat(name, &stbuf) == -1) { 66 warn("%s", name); 67 return; 68 } 69 if (stbuf.st_uid != 0) { 70 warnx("%s: ignoring directory not owned by root", name); 71 return; 72 } 73 if ((stbuf.st_mode & S_IWOTH) != 0) { 74 warnx("%s: ignoring world-writable directory", name); 75 return; 76 } 77 } 78 79 for (i = 0; i < ndirs; i++) 80 if (strcmp(dirs[i], name) == 0) 81 return; 82 if (ndirs >= MAXDIRS) 83 errx(1, "\"%s\": Too many directories in path", hintsfile); 84 dirs[ndirs++] = name; 85 } 86 87 void 88 list_elf_hints(const char *hintsfile) 89 { 90 int i; 91 int nlibs; 92 93 read_elf_hints(hintsfile, 1); 94 printf("%s:\n", hintsfile); 95 printf("\tsearch directories:"); 96 for (i = 0; i < ndirs; i++) 97 printf("%c%s", i == 0 ? ' ' : ':', dirs[i]); 98 printf("\n"); 99 100 nlibs = 0; 101 for (i = 0; i < ndirs; i++) { 102 DIR *dirp; 103 struct dirent *dp; 104 105 if ((dirp = opendir(dirs[i])) == NULL) 106 continue; 107 while ((dp = readdir(dirp)) != NULL) { 108 int len; 109 int namelen; 110 const char *name; 111 const char *vers; 112 113 /* Name can't be shorter than "libx.so.0" */ 114 if ((len = strlen(dp->d_name)) < 9 || 115 strncmp(dp->d_name, "lib", 3) != 0) 116 continue; 117 name = dp->d_name + 3; 118 vers = dp->d_name + len; 119 while (vers > dp->d_name && isdigit(*(vers-1))) 120 vers--; 121 if (vers == dp->d_name + len) 122 continue; 123 if (vers < dp->d_name + 4 || 124 strncmp(vers - 4, ".so.", 4) != 0) 125 continue; 126 127 /* We have a valid shared library name. */ 128 namelen = (vers - 4) - name; 129 printf("\t%d:-l%.*s.%s => %s/%s\n", nlibs, 130 namelen, name, vers, dirs[i], dp->d_name); 131 nlibs++; 132 } 133 closedir(dirp); 134 } 135 } 136 137 static void 138 read_dirs_from_file(const char *hintsfile, const char *listfile) 139 { 140 FILE *fp; 141 char buf[MAXPATHLEN]; 142 int linenum; 143 144 if ((fp = fopen(listfile, "r")) == NULL) 145 err(1, "%s", listfile); 146 147 linenum = 0; 148 while (fgets(buf, sizeof buf, fp) != NULL) { 149 char *cp, *sp; 150 151 linenum++; 152 cp = buf; 153 /* Skip leading white space. */ 154 while (isspace(*cp)) 155 cp++; 156 if (*cp == '#' || *cp == '\0') 157 continue; 158 sp = cp; 159 /* Advance over the directory name. */ 160 while (!isspace(*cp) && *cp != '\0') 161 cp++; 162 /* Terminate the string and skip trailing white space. */ 163 if (*cp != '\0') { 164 *cp++ = '\0'; 165 while (isspace(*cp)) 166 cp++; 167 } 168 /* Now we had better be at the end of the line. */ 169 if (*cp != '\0') 170 warnx("%s:%d: trailing characters ignored", 171 listfile, linenum); 172 173 if ((sp = strdup(sp)) == NULL) 174 errx(1, "Out of memory"); 175 add_dir(hintsfile, sp, 0); 176 } 177 178 fclose(fp); 179 } 180 181 static void 182 read_elf_hints(const char *hintsfile, int must_exist) 183 { 184 int fd; 185 struct stat s; 186 void *mapbase; 187 struct elfhints_hdr *hdr; 188 char *strtab; 189 char *dirlist; 190 char *p; 191 192 if ((fd = open(hintsfile, O_RDONLY)) == -1) { 193 if (errno == ENOENT && !must_exist) 194 return; 195 err(1, "Cannot open \"%s\"", hintsfile); 196 } 197 if (fstat(fd, &s) == -1) 198 err(1, "Cannot stat \"%s\"", hintsfile); 199 if (s.st_size > MAXFILESIZE) 200 errx(1, "\"%s\" is unreasonably large", hintsfile); 201 /* 202 * We use a read-write, private mapping so that we can null-terminate 203 * some strings in it without affecting the underlying file. 204 */ 205 mapbase = mmap(NULL, s.st_size, PROT_READ|PROT_WRITE, 206 MAP_PRIVATE, fd, 0); 207 if (mapbase == MAP_FAILED) 208 err(1, "Cannot mmap \"%s\"", hintsfile); 209 close(fd); 210 211 hdr = (struct elfhints_hdr *)mapbase; 212 if (hdr->magic != ELFHINTS_MAGIC) 213 errx(1, "\"%s\": invalid file format", hintsfile); 214 if (hdr->version != 1) 215 errx(1, "\"%s\": unrecognized file version (%d)", hintsfile, 216 hdr->version); 217 218 strtab = (char *)mapbase + hdr->strtab; 219 dirlist = strtab + hdr->dirlist; 220 221 if (*dirlist != '\0') 222 while ((p = strsep(&dirlist, ":")) != NULL) 223 add_dir(hintsfile, p, 1); 224 } 225 226 void 227 update_elf_hints(const char *hintsfile, int argc, char **argv, int merge) 228 { 229 int i; 230 231 if (merge) 232 read_elf_hints(hintsfile, 0); 233 for (i = 0; i < argc; i++) { 234 struct stat s; 235 236 if (stat(argv[i], &s) == -1) 237 warn("warning: %s", argv[i]); 238 else if (S_ISREG(s.st_mode)) 239 read_dirs_from_file(hintsfile, argv[i]); 240 else 241 add_dir(hintsfile, argv[i], 0); 242 } 243 write_elf_hints(hintsfile); 244 } 245 246 static void 247 write_elf_hints(const char *hintsfile) 248 { 249 struct elfhints_hdr hdr; 250 char *tempname; 251 int fd; 252 FILE *fp; 253 int i; 254 255 if (asprintf(&tempname, "%s.XXXXXX", hintsfile) == -1) 256 errx(1, "Out of memory"); 257 if ((fd = mkstemp(tempname)) == -1) 258 err(1, "mkstemp(%s)", tempname); 259 if (fchmod(fd, 0444) == -1) 260 err(1, "fchmod(%s)", tempname); 261 if ((fp = fdopen(fd, "wb")) == NULL) 262 err(1, "fdopen(%s)", tempname); 263 264 hdr.magic = ELFHINTS_MAGIC; 265 hdr.version = 1; 266 hdr.strtab = sizeof hdr; 267 hdr.strsize = 0; 268 hdr.dirlist = 0; 269 memset(hdr.spare, 0, sizeof hdr.spare); 270 271 /* Count up the size of the string table. */ 272 if (ndirs > 0) { 273 hdr.strsize += strlen(dirs[0]); 274 for (i = 1; i < ndirs; i++) 275 hdr.strsize += 1 + strlen(dirs[i]); 276 } 277 hdr.dirlistlen = hdr.strsize; 278 hdr.strsize++; /* For the null terminator */ 279 280 /* Write the header. */ 281 if (fwrite(&hdr, 1, sizeof hdr, fp) != sizeof hdr) 282 err(1, "%s: write error", tempname); 283 /* Write the strings. */ 284 if (ndirs > 0) { 285 if (fputs(dirs[0], fp) == EOF) 286 err(1, "%s: write error", tempname); 287 for (i = 1; i < ndirs; i++) 288 if (fprintf(fp, ":%s", dirs[i]) < 0) 289 err(1, "%s: write error", tempname); 290 } 291 if (putc('\0', fp) == EOF || fclose(fp) == EOF) 292 err(1, "%s: write error", tempname); 293 294 if (rename(tempname, hintsfile) == -1) 295 err(1, "rename %s to %s", tempname, hintsfile); 296 free(tempname); 297 } 298