sod.c revision 1.36
1/* $OpenBSD: sod.c,v 1.36 2022/01/08 06:49:41 guenther Exp $ */ 2 3/* 4 * Copyright (c) 1993 Paul Kranenburg 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Paul Kranenburg. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 */ 33 34#include <sys/types.h> 35#include <sys/mman.h> 36#include <sys/stat.h> 37 38#include <limits.h> 39#include <fcntl.h> 40#include <link.h> /* for _PATH_LD_HINTS */ 41#include <string.h> 42 43#include "syscall.h" 44#include "path.h" 45#include "util.h" 46#include "sod.h" 47 48int _dl_hinthash(char *cp, int vmajor, int vminor); 49void _dl_maphints(void); 50 51/* 52 * Populate sod struct for dlopen's call to map_object 53 */ 54void 55_dl_build_sod(const char *name, struct sod *sodp) 56{ 57 unsigned int tuplet; 58 int major, minor; 59 char *realname, *tok, *etok, *cp; 60 61 /* default is an absolute or relative path */ 62 sodp->sod_name = (long)_dl_strdup(name); /* strtok is destructive */ 63 if (sodp->sod_name == 0) 64 _dl_oom(); 65 sodp->sod_library = 0; 66 sodp->sod_major = sodp->sod_minor = 0; 67 68 /* does it look like /^lib/ ? */ 69 if (_dl_strncmp((char *)sodp->sod_name, "lib", 3) != 0) 70 goto backout; 71 72 /* is this a filename? */ 73 if (_dl_strchr((char *)sodp->sod_name, '/')) 74 goto backout; 75 76 /* skip over 'lib' */ 77 cp = (char *)sodp->sod_name + 3; 78 79 realname = cp; 80 81 /* dot guardian */ 82 if ((_dl_strchr(cp, '.') == NULL) || (*(cp+_dl_strlen(cp)-1) == '.')) 83 goto backout; 84 85 cp = _dl_strstr(cp, ".so"); 86 if (cp == NULL) 87 goto backout; 88 89 /* default */ 90 major = minor = -1; 91 92 /* loop through name - parse skipping name */ 93 for (tuplet = 0; (tok = _dl_strsep(&cp, ".")) != NULL; tuplet++) { 94 switch (tuplet) { 95 case 0: 96 /* empty tok, we already skipped to "\.so.*" */ 97 break; 98 case 1: 99 /* 'so' extension */ 100 break; 101 case 2: 102 /* major version extension */ 103 major = _dl_strtol(tok, &etok, 10); 104 if (*tok == '\0' || *etok != '\0') 105 goto backout; 106 break; 107 case 3: 108 /* minor version extension */ 109 minor = _dl_strtol(tok, &etok, 10); 110 if (*tok == '\0' || *etok != '\0') 111 goto backout; 112 break; 113 /* if we get here, it must be weird */ 114 default: 115 goto backout; 116 } 117 } 118 if (realname == NULL) 119 goto backout; 120 cp = (char *)sodp->sod_name; 121 sodp->sod_name = (long)_dl_strdup(realname); 122 if (sodp->sod_name == 0) 123 _dl_oom(); 124 _dl_free(cp); 125 sodp->sod_library = 1; 126 sodp->sod_major = major; 127 sodp->sod_minor = minor; 128 return; 129 130backout: 131 _dl_free((char *)sodp->sod_name); 132 sodp->sod_name = (long)_dl_strdup(name); 133 if (sodp->sod_name == 0) 134 _dl_oom(); 135} 136 137void 138_dl_set_sod(const char *path, struct sod *sod) 139{ 140 char *fname = _dl_strrchr(path, '/'); 141 142 if (fname != NULL) 143 _dl_build_sod(++fname, sod); 144 else 145 _dl_build_sod(path, sod); 146} 147 148static struct hints_header *hheader = NULL; 149static struct hints_bucket *hbuckets; 150static char *hstrtab; 151char **_dl_hint_search_path = NULL; 152 153#define HINTS_VALID (hheader != NULL && hheader != (struct hints_header *)-1) 154 155void 156_dl_maphints(void) 157{ 158 struct stat sb; 159 caddr_t addr = MAP_FAILED; 160 long hsize = 0; 161 int hfd; 162 163 if ((hfd = _dl_open(_PATH_LD_HINTS, O_RDONLY | O_CLOEXEC)) < 0) { 164 hfd = -1; 165 goto bad_hints; 166 } 167 168 if (_dl_fstat(hfd, &sb) != 0 || !S_ISREG(sb.st_mode) || 169 sb.st_size < sizeof(struct hints_header) || sb.st_size > LONG_MAX) 170 goto bad_hints; 171 172 hsize = (long)sb.st_size; 173 addr = (void *)_dl_mmap(0, hsize, PROT_READ, MAP_PRIVATE, hfd, 0); 174 if (_dl_mmap_error(addr)) 175 goto bad_hints; 176 177 hheader = (struct hints_header *)addr; 178 if (HH_BADMAG(*hheader) || hheader->hh_ehints > hsize) 179 goto bad_hints; 180 181 if (hheader->hh_version != LD_HINTS_VERSION_2) 182 goto bad_hints; 183 184 hbuckets = (struct hints_bucket *)(addr + hheader->hh_hashtab); 185 hstrtab = (char *)(addr + hheader->hh_strtab); 186 if (hheader->hh_version >= LD_HINTS_VERSION_2) 187 _dl_hint_search_path = _dl_split_path(hstrtab + hheader->hh_dirlist); 188 189 /* close the file descriptor, leaving the hints mapped */ 190 _dl_close(hfd); 191 192 return; 193 194bad_hints: 195 if (!_dl_mmap_error(addr)) 196 _dl_munmap(addr, hsize); 197 if (hfd != -1) 198 _dl_close(hfd); 199 hheader = (struct hints_header *)-1; 200} 201 202char * 203_dl_findhint(char *name, int major, int minor, char *preferred_path) 204{ 205 struct hints_bucket *bp; 206 207 /* 208 * If not mapped, and we have not tried before, try to map the 209 * hints, if previous attempts failed hheader is -1 and we 210 * do not wish to retry it. 211 */ 212 if (hheader == NULL) 213 _dl_maphints(); 214 215 /* if it failed to map, return failure */ 216 if (!(HINTS_VALID)) 217 return NULL; 218 219 if (hheader->hh_nbucket == 0) 220 return NULL; 221 222 bp = hbuckets + (_dl_hinthash(name, major, minor) % hheader->hh_nbucket); 223 224 while (1) { 225 /* Sanity check */ 226 if (bp->hi_namex >= hheader->hh_strtab_sz) 227 _dl_die("bad name index: %#x", bp->hi_namex); 228 if (bp->hi_pathx >= hheader->hh_strtab_sz) 229 _dl_die("bad path index: %#x", bp->hi_pathx); 230 231 if (_dl_strcmp(name, hstrtab + bp->hi_namex) == 0) { 232 /* It's `name', check version numbers */ 233 if (bp->hi_major == major && 234 (bp->hi_ndewey < 2 || bp->hi_minor >= minor)) { 235 if (preferred_path == NULL) { 236 return hstrtab + bp->hi_pathx; 237 } else { 238 char *path = hstrtab + bp->hi_pathx; 239 char *edir = _dl_strrchr(path, '/'); 240 241 if ((_dl_strncmp(preferred_path, path, 242 (edir - path)) == 0) && 243 (preferred_path[edir - path] == '\0')) 244 return path; 245 } 246 } 247 } 248 249 if (bp->hi_next == -1) 250 break; 251 252 /* Move on to next in bucket */ 253 bp = &hbuckets[bp->hi_next]; 254 } 255 256 /* No hints available for name */ 257 return NULL; 258} 259 260int 261_dl_hinthash(char *cp, int vmajor, int vminor) 262{ 263 int k = 0; 264 265 while (*cp) 266 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 267 268 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff; 269 270 return k; 271} 272