2022-01-29 22:09:15 +00:00
|
|
|
#include "atomic.h"
|
2022-07-17 14:26:00 +00:00
|
|
|
#include <sys/auxv.h>
|
|
|
|
#include <fcntl.h> // open
|
|
|
|
#include <sys/stat.h> // O_RDONLY
|
|
|
|
#include <unistd.h> // read, close
|
|
|
|
#include <stdlib.h> // ssize_t
|
|
|
|
#include <stdio.h> // perror, fprintf
|
|
|
|
#include <link.h> // ElfW
|
2020-11-14 08:36:44 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
2022-07-17 14:26:00 +00:00
|
|
|
#define ARRAY_SIZE(a) sizeof((a))/sizeof((a[0]))
|
|
|
|
|
|
|
|
// We don't have libc struct available here.
|
|
|
|
// Compute aux vector manually (from /proc/self/auxv).
|
|
|
|
//
|
|
|
|
// Right now there is only 51 AT_* constants,
|
|
|
|
// so 64 should be enough until this implementation will be replaced with musl.
|
|
|
|
static unsigned long __auxv_procfs[64];
|
2022-01-24 15:11:54 +00:00
|
|
|
static unsigned long __auxv_secure = 0;
|
2022-07-17 14:26:00 +00:00
|
|
|
// Common
|
|
|
|
static unsigned long * __auxv_environ = NULL;
|
|
|
|
|
|
|
|
static void * volatile getauxval_func;
|
|
|
|
|
|
|
|
static unsigned long __auxv_init_environ(unsigned long type);
|
|
|
|
|
|
|
|
//
|
|
|
|
// auxv from procfs interface
|
|
|
|
//
|
|
|
|
ssize_t __retry_read(int fd, void * buf, size_t count)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
ssize_t ret = read(fd, buf, count);
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
perror("Cannot read /proc/self/auxv");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsigned long __getauxval_procfs(unsigned long type)
|
|
|
|
{
|
|
|
|
if (type == AT_SECURE)
|
|
|
|
{
|
|
|
|
return __auxv_secure;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type >= ARRAY_SIZE(__auxv_procfs))
|
|
|
|
{
|
|
|
|
errno = ENOENT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return __auxv_procfs[type];
|
|
|
|
}
|
|
|
|
static unsigned long __auxv_init_procfs(unsigned long type)
|
|
|
|
{
|
|
|
|
// For debugging:
|
|
|
|
// - od -t dL /proc/self/auxv
|
|
|
|
// - LD_SHOW_AUX= ls
|
|
|
|
int fd = open("/proc/self/auxv", O_RDONLY);
|
|
|
|
// It is possible in case of:
|
|
|
|
// - no procfs mounted
|
|
|
|
// - on android you are not able to read it unless running from shell or debugging
|
|
|
|
// - some other issues
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
// Fallback to environ.
|
|
|
|
a_cas_p(&getauxval_func, (void *)__auxv_init_procfs, (void *)__auxv_init_environ);
|
|
|
|
return __auxv_init_environ(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
ElfW(auxv_t) aux;
|
2020-11-14 08:36:44 +00:00
|
|
|
|
2022-07-17 14:26:00 +00:00
|
|
|
/// NOTE: sizeof(aux) is very small (less then PAGE_SIZE), so partial read should not be possible.
|
|
|
|
_Static_assert(sizeof(aux) < 4096, "Unexpected sizeof(aux)");
|
|
|
|
while (__retry_read(fd, &aux, sizeof(aux)) == sizeof(aux))
|
|
|
|
{
|
|
|
|
if (aux.a_type >= ARRAY_SIZE(__auxv_procfs))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "AT_* is out of range: %li (maximum allowed is %zu)\n", aux.a_type, ARRAY_SIZE(__auxv_procfs));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if (__auxv_procfs[aux.a_type])
|
|
|
|
{
|
|
|
|
fprintf(stderr, "AUXV already has value (%zu)\n", __auxv_procfs[aux.a_type]);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
__auxv_procfs[aux.a_type] = aux.a_un.a_val;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
__auxv_secure = __getauxval_procfs(AT_SECURE);
|
|
|
|
|
|
|
|
// Now we've initialized __auxv_procfs, next time getauxval() will only call __get_auxval().
|
|
|
|
a_cas_p(&getauxval_func, (void *)__auxv_init_procfs, (void *)__getauxval_procfs);
|
|
|
|
|
|
|
|
return __getauxval_procfs(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// auxv from environ interface
|
|
|
|
//
|
|
|
|
// NOTE: environ available only after static initializers,
|
|
|
|
// so you cannot rely on this if you need getauxval() before.
|
|
|
|
//
|
|
|
|
// Good example of such user is sanitizers, for example
|
|
|
|
// LSan will not work with __auxv_init_environ(),
|
|
|
|
// since it needs getauxval() before.
|
|
|
|
//
|
2022-01-29 22:09:15 +00:00
|
|
|
static size_t __find_auxv(unsigned long type)
|
|
|
|
{
|
|
|
|
size_t i;
|
2022-07-17 14:26:00 +00:00
|
|
|
for (i = 0; __auxv_environ[i]; i += 2)
|
2022-01-29 22:09:15 +00:00
|
|
|
{
|
2022-07-17 14:26:00 +00:00
|
|
|
if (__auxv_environ[i] == type)
|
|
|
|
{
|
2022-01-29 22:09:15 +00:00
|
|
|
return i + 1;
|
2022-07-17 14:26:00 +00:00
|
|
|
}
|
2022-01-29 22:09:15 +00:00
|
|
|
}
|
|
|
|
return (size_t) -1;
|
|
|
|
}
|
2022-07-17 14:26:00 +00:00
|
|
|
unsigned long __getauxval_environ(unsigned long type)
|
2020-11-14 08:36:44 +00:00
|
|
|
{
|
|
|
|
if (type == AT_SECURE)
|
|
|
|
return __auxv_secure;
|
|
|
|
|
2022-07-17 14:26:00 +00:00
|
|
|
if (__auxv_environ)
|
2020-11-14 08:36:44 +00:00
|
|
|
{
|
2022-01-29 22:09:15 +00:00
|
|
|
size_t index = __find_auxv(type);
|
|
|
|
if (index != ((size_t) -1))
|
2022-07-17 14:26:00 +00:00
|
|
|
return __auxv_environ[index];
|
2020-11-14 08:36:44 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 22:09:15 +00:00
|
|
|
errno = ENOENT;
|
|
|
|
return 0;
|
2020-11-14 08:36:44 +00:00
|
|
|
}
|
2022-07-17 14:26:00 +00:00
|
|
|
static unsigned long __auxv_init_environ(unsigned long type)
|
2022-01-24 15:11:54 +00:00
|
|
|
{
|
2022-01-29 22:09:15 +00:00
|
|
|
if (!__environ)
|
2022-01-24 15:11:54 +00:00
|
|
|
{
|
2022-07-17 14:26:00 +00:00
|
|
|
// __environ is not initialized yet so we can't initialize __auxv_environ right now.
|
2022-01-29 22:09:15 +00:00
|
|
|
// That's normally occurred only when getauxval() is called from some sanitizer's internal code.
|
|
|
|
errno = ENOENT;
|
|
|
|
return 0;
|
2022-01-24 15:11:54 +00:00
|
|
|
}
|
2021-08-25 11:19:20 +00:00
|
|
|
|
2022-07-17 14:26:00 +00:00
|
|
|
// Initialize __auxv_environ and __auxv_secure.
|
2022-01-29 22:09:15 +00:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; __environ[i]; i++);
|
2022-07-17 14:26:00 +00:00
|
|
|
__auxv_environ = (unsigned long *) (__environ + i + 1);
|
2022-01-29 22:09:15 +00:00
|
|
|
|
|
|
|
size_t secure_idx = __find_auxv(AT_SECURE);
|
|
|
|
if (secure_idx != ((size_t) -1))
|
2022-07-17 14:26:00 +00:00
|
|
|
__auxv_secure = __auxv_environ[secure_idx];
|
2021-08-25 11:19:20 +00:00
|
|
|
|
2022-07-17 14:26:00 +00:00
|
|
|
// Now we need to switch to __getauxval_environ for all later calls, since
|
|
|
|
// everything is initialized.
|
|
|
|
a_cas_p(&getauxval_func, (void *)__auxv_init_environ, (void *)__getauxval_environ);
|
2021-08-25 11:19:20 +00:00
|
|
|
|
2022-07-17 14:26:00 +00:00
|
|
|
return __getauxval_environ(type);
|
2021-08-25 11:19:20 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 14:26:00 +00:00
|
|
|
// Callchain:
|
|
|
|
// - __auxv_init_procfs -> __getauxval_environ
|
|
|
|
// - __auxv_init_procfs -> __auxv_init_environ -> __getauxval_environ
|
|
|
|
static void * volatile getauxval_func = (void *)__auxv_init_procfs;
|
2021-08-25 11:19:20 +00:00
|
|
|
|
|
|
|
unsigned long getauxval(unsigned long type)
|
|
|
|
{
|
|
|
|
return ((unsigned long (*)(unsigned long))getauxval_func)(type);
|
|
|
|
}
|