public
Last active

Code reading auxiliary vector present in executable binary.

  • Download Gist
aux.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include <stdio.h>
#include <stdlib.h>
#include <linux/auxvec.h>
 
 
static unsigned long *getauxv(char **env) {
while (*env++ != NULL) {
/* nop */
}
return (void*)env;
}
 
 
struct aux_handler {
unsigned long key;
const char *name;
void (*handler)(const struct aux_handler *h, unsigned long *aux);
const char *desc, *arg;
};
 
static void handle_print(const struct aux_handler *h, unsigned long *aux) {
printf(h->arg, h->name, aux[0], aux[1], h->desc);
}
 
static void handle_random(const struct aux_handler *h, unsigned long *aux) {
const unsigned char *ch = (void *)aux[1], *end = ch + 16;
printf("%-16s %2lu ", h->name, aux[0]);
for (; ch != end; ++ch) {
printf("%02x", (unsigned)*ch);
}
printf(" // %s\n", h->desc);
}
 
#define AUX_HANDLER(key, handler, desc, arg) { key, #key, handler, desc, arg }
#define AUX_NUMBER(key, desc) { key, #key, handle_print, desc, "%-16s %2lu %-15lu // %s\n" }
#define AUX_HEX(key, desc) { key, #key, handle_print, desc, "%-16s %2lu 0x%-13lx // %s\n" }
#define AUX_STRING(key, desc) { key, #key, handle_print, desc, "%-16s %2lu \"%s\" // %s\n" }
 
static const struct aux_handler aux_handlers[] = {
AUX_HANDLER(AT_IGNORE, NULL, NULL, NULL),
AUX_NUMBER(AT_EXECFD, "file descriptor of program"),
AUX_NUMBER(AT_PHDR, "program headers for program"),
AUX_NUMBER(AT_PHENT, "size of program header entry"),
AUX_NUMBER(AT_PHNUM, "number of program headers"),
AUX_NUMBER(AT_PAGESZ, "system page size"),
AUX_NUMBER(AT_BASE, "base address of interpreter"),
AUX_NUMBER(AT_FLAGS, "flags"),
AUX_NUMBER(AT_ENTRY, "entry point of program"),
AUX_NUMBER(AT_NOTELF, "program is not ELF"),
AUX_NUMBER(AT_UID, "real uid"),
AUX_NUMBER(AT_EUID, "effective uid"),
AUX_NUMBER(AT_GID, "real gid"),
AUX_NUMBER(AT_EGID, "effective gid"),
AUX_STRING(AT_PLATFORM, "string identifying CPU for optimizations"),
AUX_HEX(AT_HWCAP, "arch dependent hints at CPU capabilities"),
AUX_NUMBER(AT_CLKTCK, "frequency at which times() increments"),
AUX_NUMBER(AT_SECURE, "secure mode boolean"),
AUX_STRING(AT_BASE_PLATFORM, "string identifying real platform, may differ from AT_PLATFORM."),
AUX_HANDLER(AT_RANDOM, handle_random, "16 random bytes", NULL),
AUX_STRING(AT_EXECFN, "filename of program"),
{ AT_NULL, "(unknown)", handle_print, "unknown field", "%-16s %2lu %-15lu\n" }
};
 
#undef AUX_HANDLER
#undef AUX_NUMBER
#undef AUX_STRING
 
 
int main(int argc, char **argv, char **env) {
unsigned long *auxv = getauxv(env), key;
const struct aux_handler *h;
 
argc = argc; /* make compiler happy */
argv = argv;
 
while (*auxv != AT_NULL) {
h = aux_handlers;
key = *auxv;
while (h->key != key && h->key != AT_NULL) {
++h;
}
if (h->handler) {
h->handler(h, auxv);
}
auxv += 2;
}
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.