stage1: implementation of exec diagnostic wrapper

Wrapper performing the chroot() and exec() of apps for providing more
informative errors in the face of low-level interpreter failures.

Demonstration:
/tmp/ene# ls -l
total 2044
-rwxr-xr-x 1 root root 1033720 Dec 17 12:37 bash_no_interp
-rw-r--r-- 1 root root 1033720 Dec 17 12:37 bash_non_executable
-rwxr-xr-x 1 root root      34 Dec 17 18:32 shebang_enoent.sh
-rwxr-xr-x 1 root root      18 Dec 17 18:31 shebang_slash_bash_no_interp.sh
-rwxr-xr-x 1 root root      21 Dec 17 18:30 shebang_recursive_slash_shebang_enoent.sh
/tmp/ene# for i in *.sh; do echo $i:; cat $i; done
shebang_enoent.sh:
#!/this/shebang/interp/is/missing
shebang_slash_bash_no_interp.sh:
#!/bash_no_interp
shebang_slash_shebang_enoent.sh:
#!/shebang_enoent.sh

/tmp# ./diagexec ene/ /bash_no_interp
Error: Unable to open "/lib64/ld-linux-x86-64.so.2": No such file or directory
/tmp# ./diagexec ene/ /bash_non_executable
Error: "/bash_non_executable" is not executable
/tmp# ./diagexec ene/ /shebang_enoent.sh
Error: Unable to open "/this/shebang/interp/is/missing": No such file or directory
/tmp# ./diagexec ene/ /shebang_slash_bash_no_interp.sh
Error: Unable to open "/lib64/ld-linux-x86-64.so.2": No such file or directory
/tmp# ./diagexec ene/ /shebang_recursive_slash_shebang_enoent.sh
Error: Unable to open "/this/shebang/interp/is/missing": No such file or directory
/tmp#
This commit is contained in:
Vito Caputo
2014-12-30 15:43:59 -08:00
parent 74df42fa57
commit 9896a67260
4 changed files with 251 additions and 1 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
_SUBDIRS=usr shim
_SUBDIRS=usr shim diagexec
SUBDIRS=$(_SUBDIRS) aggregate
export CFLAGS=-Wall -Os
+12
View File
@@ -0,0 +1,12 @@
BIN=diagexec
SRC=diagexec.c
$(BIN): $(SRC) elf.h Makefile
$(CC) $(CFLAGS) -o $@ $(SRC) -static -s
.PHONY: clean
clean:
rm -f $(BIN)
test:
echo TODO
+141
View File
@@ -0,0 +1,141 @@
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "elf.h"
static int exit_err;
#define exit_if(_cond, _fmt, _args...) \
exit_err++; \
if(_cond) { \
fprintf(stderr, "Error: " _fmt "\n", ##_args); \
exit(exit_err); \
}
#define pexit_if(_cond, _fmt, _args...) \
exit_if(_cond, _fmt ": %s", ##_args, strerror(errno))
#define MAX_DIAG_DEPTH 10
#define MIN(_a, _b) (((_a) < (_b)) ? (_a) : (_b))
static void diag(const char *exe)
{
static const uint8_t elf[] = {0x7f, 'E', 'L', 'F'};
static const uint8_t shebang[] = {'#','!'};
static int diag_depth;
int fd;
struct stat st;
const uint8_t *mm;
const char *itrp = NULL;
pexit_if((fd = open(exe, O_RDONLY)) == -1,
"Unable to open \"%s\"", exe);
pexit_if(fstat(fd, &st) == -1,
"Cannot stat \"%s\"", exe);
exit_if(!S_ISREG(st.st_mode), "\"%s\" is not a regular file", exe);
exit_if(!((S_IXUSR|S_IXGRP|S_IXOTH) & st.st_mode),
"\"%s\" is not executable", exe)
pexit_if(!(mm = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)),
"Mmap of \"%s\" failed", exe);
if(st.st_size >= sizeof(shebang) &&
!memcmp(mm, shebang, sizeof(shebang))) {
const uint8_t *nl;
int maxlen = MIN(PATH_MAX, st.st_size - sizeof(shebang));
/* TODO(vc): EOF-terminated shebang lines are technically possible */
exit_if(!(nl = memchr(&mm[sizeof(shebang)], '\n', maxlen)),
"Shebang line too long");
pexit_if(!(itrp = strndup((char *)&mm[sizeof(shebang)], (nl - mm) - 2)),
"Failed to dup interpreter path");
} else if(st.st_size >= sizeof(elf) &&
!memcmp(mm, elf, sizeof(elf))) {
uint64_t (*lget)(const uint8_t *) = NULL;
uint32_t (*iget)(const uint8_t *) = NULL;
uint16_t (*sget)(const uint8_t *) = NULL;
const void *phoff = NULL, *phesz = NULL, *phecnt = NULL;
const uint8_t *ph = NULL;
int i, phreloff, phrelsz;
exit_if(mm[ELF_VERSION] != 1,
"Unsupported ELF version: %hhx", mm[ELF_VERSION]);
/* determine which accessors to use and where */
if(mm[ELF_BITS] == ELF_BITS_32) {
if(mm[ELF_ENDIAN] == ELF_ENDIAN_LITL) {
lget = le32_lget;
sget = le_sget;
iget = le_iget;
} else if(mm[ELF_ENDIAN] == ELF_ENDIAN_BIG) {
lget = be32_lget;
sget = be_sget;
iget = be_iget;
}
phoff = &mm[ELF32_PHT_OFF];
phesz = &mm[ELF32_PHTE_SIZE];
phecnt = &mm[ELF32_PHTE_CNT];
phreloff = ELF32_PHE_OFF;
phrelsz = ELF32_PHE_SIZE;
} else if(mm[ELF_BITS] == ELF_BITS_64) {
if(mm[ELF_ENDIAN] == ELF_ENDIAN_LITL) {
lget = le64_lget;
sget = le_sget;
iget = le_iget;
} else if(mm[ELF_ENDIAN] == ELF_ENDIAN_BIG) {
lget = be64_lget;
sget = be_sget;
iget = be_iget;
}
phoff = &mm[ELF64_PHT_OFF];
phesz = &mm[ELF64_PHTE_SIZE];
phecnt = &mm[ELF64_PHTE_CNT];
phreloff = ELF64_PHE_OFF;
phrelsz = ELF64_PHE_SIZE;
}
exit_if(!lget, "Unsupported ELF format");
if(!phoff) /* program header may be absent, don't make it an error */
return;
/* TODO(vc): sanity checks on values before using them */
for(ph = &mm[lget(phoff)], i = 0; i < sget(phecnt); i++, ph += sget(phesz)) {
if(iget(ph) == ELF_PT_INTERP) {
itrp = strndup((char *)&mm[lget(&ph[phreloff])], lget(&ph[phrelsz]));
break;
}
}
} else {
exit_if(1, "Unsupported file type");
}
exit_if(!itrp, "Unable to determine interpreter for \"%s\"", exe);
exit_if(*itrp != '/', "Path must be absolute: \"%s\"", itrp);
exit_if(++diag_depth > MAX_DIAG_DEPTH,
"Excessive interpreter recursion, giving up");
diag(itrp);
}
int main(int argc, char *argv[])
{
const char *root, *exe;
exit_if(argc < 3,
"Usage: %s /path/to/root /to/exec [args ...]", argv[0]);
root = argv[1];
exe = argv[2];
pexit_if(chroot(root) == -1, "Chroot failed");
pexit_if(chdir("/") == -1, "Chdir failed");
pexit_if(execv(exe, &argv[2]) == -1 &&
errno != ENOENT && errno != EACCES,
"Exec of \"%s\" failed", exe);
diag(exe);
return EXIT_FAILURE;
}
+97
View File
@@ -0,0 +1,97 @@
/* just enough ELF support for finding the interpreter in the program header
* table, this should theoretically work as-is on both big-endian and
* little-endian
*/
/* values of interest */
#define ELF_BITS_32 0x1
#define ELF_BITS_64 0x2
#define ELF_ENDIAN_LITL 0x1
#define ELF_ENDIAN_BIG 0x2
#define ELF_PT_INTERP 0x3
/* offsets of interest */
#define ELF_BITS 0x4
#define ELF_ENDIAN 0x5
#define ELF_VERSION 0x6
#define ELF32_PHT_OFF 0x1c
#define ELF32_PHTE_SIZE 0x2a
#define ELF32_PHTE_CNT 0x2c
#define ELF32_PHE_OFF 0x4
#define ELF32_PHE_SIZE 0x10
#define ELF64_PHT_OFF 0x20
#define ELF64_PHTE_SIZE 0x36
#define ELF64_PHTE_CNT 0x38
#define ELF64_PHE_OFF 0x8
#define ELF64_PHE_SIZE 0x20
/* multibyte value accessors, choose which based on ELF_BITS and ELF_ENDIAN */
#define SHIFT(_val, _bytes) ((unsigned long long)(_val) << ((_bytes) * 8))
static uint64_t le32_lget(const uint8_t *addr)
{
uint64_t val = 0;
val += SHIFT(addr[3], 3);
val += SHIFT(addr[2], 2);
val += SHIFT(addr[1], 1);
val += SHIFT(addr[0], 0);
return val;
}
static uint64_t be32_lget(const uint8_t *addr)
{
uint64_t val = 0;
val += SHIFT(addr[0], 3);
val += SHIFT(addr[1], 2);
val += SHIFT(addr[2], 1);
val += SHIFT(addr[3], 0);
return val;
}
static uint64_t le64_lget(const uint8_t *addr)
{
uint64_t val = 0;
val += SHIFT(addr[7], 7);
val += SHIFT(addr[6], 6);
val += SHIFT(addr[5], 5);
val += SHIFT(addr[4], 4);
val += SHIFT(addr[3], 3);
val += SHIFT(addr[2], 2);
val += SHIFT(addr[1], 1);
val += SHIFT(addr[0], 0);
return val;
}
static uint64_t be64_lget(const uint8_t *addr)
{
uint64_t val = 0;
val += SHIFT(addr[0], 7);
val += SHIFT(addr[1], 6);
val += SHIFT(addr[2], 5);
val += SHIFT(addr[3], 4);
val += SHIFT(addr[4], 3);
val += SHIFT(addr[5], 2);
val += SHIFT(addr[6], 1);
val += SHIFT(addr[7], 0);
return val;
}
static uint32_t le_iget(const uint8_t *addr)
{
return (uint32_t)le32_lget(addr);
}
static uint32_t be_iget(const uint8_t *addr)
{
return (uint32_t)be32_lget(addr);
}
static uint16_t le_sget(const uint8_t *addr)
{
uint16_t val = 0;
val += SHIFT(addr[1], 1);
val += SHIFT(addr[0], 0);
return val;
}
static uint16_t be_sget(const uint8_t *addr)
{
uint16_t val = 0;
val += SHIFT(addr[0], 0);
val += SHIFT(addr[1], 1);
return val;
}