Line data Source code
1 : /* 2 : * Copyright (c) 2016 - 2017 Kungliga Tekniska Högskolan 3 : * (Royal Institute of Technology, Stockholm, Sweden). 4 : * All rights reserved. 5 : * 6 : * Redistribution and use in source and binary forms, with or without 7 : * modification, are permitted provided that the following conditions 8 : * are met: 9 : * 10 : * 1. Redistributions of source code must retain the above copyright 11 : * notice, this list of conditions and the following disclaimer. 12 : * 13 : * 2. Redistributions in binary form must reproduce the above copyright 14 : * notice, this list of conditions and the following disclaimer in the 15 : * documentation and/or other materials provided with the distribution. 16 : * 17 : * 3. Neither the name of the Institute nor the names of its contributors 18 : * may be used to endorse or promote products derived from this software 19 : * without specific prior written permission. 20 : * 21 : * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 : * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 : * SUCH DAMAGE. 32 : */ 33 : 34 : #include <config.h> 35 : 36 : #ifdef HAVE_SYS_AUXV_H 37 : #include <sys/auxv.h> 38 : #endif 39 : 40 : #if defined(ENABLE_PTHREAD_SUPPORT) && defined(HAVE_PTHREAD_H) 41 : #include <pthread.h> 42 : #endif 43 : 44 : #include <errno.h> 45 : 46 : #include "roken.h" 47 : #include "getauxval.h" 48 : 49 : int rk_injected_auxv = 0; /* shared with issuid() for testing */ 50 : static int has_proc_auxv = 1; 51 : static int proc_auxv_ret = 0; 52 : 53 : #if defined(ENABLE_PTHREAD_SUPPORT) && defined(HAVE_PTHREAD_H) 54 : pthread_once_t readprocauxv_once = PTHREAD_ONCE_INIT; 55 : #endif 56 : 57 : /* 58 : * There's no standard maximum. 59 : * 60 : * At the time of this writing we observe some 20 or so auxv entries. 61 : * If eventually that grows much larger then rk_getprocaux*() will see a 62 : * truncated auxv. 63 : */ 64 : #define MAX_AUXV_COUNT 128 65 : static auxv_t auxv[MAX_AUXV_COUNT]; 66 : 67 : static void 68 0 : do_readprocauxv(void) 69 : { 70 0 : char *p = (void *)auxv; 71 0 : ssize_t bytes = 0; 72 0 : size_t sz = sizeof(auxv) - sizeof(auxv[0]); /* leave terminator */ 73 0 : int save_errno = errno; 74 0 : int fd; 75 : 76 0 : errno = 0; 77 0 : memset(auxv, 0, sizeof(auxv)); /* terminates our copy */ 78 0 : if ((fd = open("/proc/self/auxv", O_RDONLY)) == -1) { 79 0 : if (errno == ENOENT) 80 0 : has_proc_auxv = 0; 81 0 : goto out; 82 : } 83 : 84 0 : do { 85 0 : if ((bytes = read(fd, p, sz)) > 0) { 86 0 : sz -= bytes; 87 0 : p += bytes; 88 : } 89 0 : } while (sz && ((bytes == -1 && errno == EINTR) || bytes > 0)); 90 : 91 0 : out: 92 0 : proc_auxv_ret = errno; 93 0 : if (fd != -1) 94 0 : (void) close(fd); 95 0 : if (sz == 0 && bytes > 0) 96 0 : warnx("/proc/self/auxv has more entries than expected"); 97 0 : errno = save_errno; 98 0 : return; 99 : } 100 : 101 : static int 102 0 : readprocauxv(void) 103 : { 104 : #if defined(ENABLE_PTHREAD_SUPPORT) && defined(HAVE_PTHREAD_H) 105 : pthread_once(&readprocauxv_once, do_readprocauxv); 106 : #else 107 0 : do_readprocauxv(); 108 : #endif 109 0 : return proc_auxv_ret; 110 : } 111 : 112 : /** 113 : * Looks up an auxv entry in /proc/self/auxv. Preserves errno. 114 : * 115 : * @return a pointer to an auxv_t if found, else NULL. 116 : */ 117 : ROKEN_LIB_FUNCTION const auxv_t * ROKEN_LIB_CALL 118 0 : rk_getauxv(unsigned long type) 119 : { 120 0 : auxv_t *a; 121 : 122 0 : if (!has_proc_auxv || type > INT_MAX) 123 0 : return NULL; 124 : 125 0 : if (readprocauxv() != 0) 126 0 : return NULL; 127 : 128 0 : for (a = auxv; a - auxv < MAX_AUXV_COUNT; a++) { 129 0 : if ((int)a->a_type == (int)type) 130 0 : return a; 131 0 : if (a->a_type == 0 && a->a_un.a_val == 0) 132 0 : break; 133 : } 134 0 : return NULL; 135 : } 136 : 137 : #ifdef HAVE_GETAUXVAL 138 : static unsigned long 139 0 : rk_getprocauxval(unsigned long type) 140 : { 141 0 : const auxv_t *a = rk_getauxv(type); 142 : 143 0 : if (a == NULL) { 144 0 : errno = ENOENT; 145 0 : return 0; 146 : } 147 0 : return a->a_un.a_val; 148 : } 149 : #endif 150 : 151 : /** 152 : * Like the nearly-standard getauxval(). If the auxval is not found 153 : * returns zero and always sets errno to ENOENT. Otherwise if auxval is 154 : * found it leaves errno as it was, even if the value is zero. 155 : * 156 : * @return The value of the ELF auxiliary value for the given type, or 157 : * zero and sets errno to ENOENT. 158 : */ 159 : ROKEN_LIB_FUNCTION unsigned long ROKEN_LIB_CALL 160 33851 : rk_getauxval(unsigned long type) 161 : { 162 : #ifdef HAVE_GETAUXVAL 163 : #ifdef GETAUXVAL_SETS_ERRNO 164 33851 : if (rk_injected_auxv) 165 0 : return rk_getprocauxval(type); 166 33851 : return getauxval(type); 167 : #else 168 : unsigned long ret; 169 : unsigned long ret2; 170 : static int getauxval_sets_errno = -1; 171 : const auxv_t *a; 172 : int save_errno = errno; 173 : 174 : if (rk_injected_auxv) 175 : return rk_getprocauxval(type); 176 : 177 : errno = 0; 178 : ret = getauxval(type); 179 : if (ret != 0 || errno == ENOENT || getauxval_sets_errno == 1) { 180 : if (ret != 0) 181 : errno = save_errno; 182 : else if (getauxval_sets_errno > 0 && errno == 0) 183 : errno = save_errno; 184 : return ret; 185 : } 186 : 187 : if (getauxval_sets_errno == 0) { 188 : errno = save_errno; 189 : a = rk_getauxv(type); 190 : if (a == NULL) { 191 : errno = ENOENT; 192 : return 0; 193 : } 194 : return a->a_un.a_val; 195 : } 196 : 197 : /* 198 : * We've called getauxval() and it returned 0, but we don't know if 199 : * getauxval() sets errno = ENOENT when entries are not found. 200 : * 201 : * Attempt to detect whether getauxval() sets errno = ENOENT by 202 : * calling it with what should be a bogus type. 203 : */ 204 : 205 : errno = 0; 206 : ret2 = getauxval(~type); 207 : if (ret2 == 0 && errno == ENOENT) { 208 : getauxval_sets_errno = 1; 209 : errno = save_errno; 210 : return ret; 211 : } 212 : 213 : getauxval_sets_errno = 0; 214 : errno = save_errno; 215 : if ((a = rk_getauxv(type)) == NULL) { 216 : errno = ENOENT; 217 : return 0; 218 : } 219 : return a->a_un.a_val; 220 : #endif 221 : #else 222 : const auxv_t *a; 223 : 224 : if ((a = rk_getauxv(type)) == NULL) { 225 : errno = ENOENT; 226 : return 0; 227 : } 228 : return a->a_un.a_val; 229 : #endif 230 : } 231 : 232 : /** 233 : * *Internal* function for testing by injecting or overwriting an ELF 234 : * auxiliary vector entry. 235 : * 236 : * @return zero on success or ENOSPC if there are too many ELF auxiliary 237 : * entries. 238 : */ 239 : ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 240 0 : rk_injectauxv(auxv_t *e) 241 : { 242 0 : size_t i; 243 0 : int ret; 244 : 245 : /* 246 : * This function is racy, but as an internal function never meant to 247 : * be called in a threaded program, we don't care. 248 : */ 249 : 250 0 : if ((ret = readprocauxv()) != 0) 251 0 : return ret; 252 : 253 0 : rk_injected_auxv = 1; 254 0 : for (i = 0; i < MAX_AUXV_COUNT - 1 && auxv[i].a_type != 0; i++) { 255 : /* e->a_type == 0 -> truncate auxv, delete all entries */ 256 0 : if (auxv[i].a_type == e->a_type || e->a_type == 0) 257 : break; 258 : } 259 0 : if (i == MAX_AUXV_COUNT - 1) 260 0 : return ENOSPC; 261 0 : auxv[i] = e[0]; 262 0 : return 0; 263 : }