Skip to content

Instantly share code, notes, and snippets.

@csukuangfj
Forked from mina86/aux.c
Created April 6, 2017 10:28
Show Gist options
  • Save csukuangfj/92ed14e63737240e5e4362e2a0e16cd3 to your computer and use it in GitHub Desktop.
Save csukuangfj/92ed14e63737240e5e4362e2a0e16cd3 to your computer and use it in GitHub Desktop.
Code reading auxiliary vector present in executable binary.
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment