From 9896a6726081a97576e0d66cc3248440ed0d8542 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Wed, 17 Dec 2014 18:24:35 -0800 Subject: [PATCH 1/2] 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# --- stage1/rootfs/Makefile | 2 +- stage1/rootfs/diagexec/Makefile | 12 +++ stage1/rootfs/diagexec/diagexec.c | 141 ++++++++++++++++++++++++++++++ stage1/rootfs/diagexec/elf.h | 97 ++++++++++++++++++++ 4 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 stage1/rootfs/diagexec/Makefile create mode 100644 stage1/rootfs/diagexec/diagexec.c create mode 100644 stage1/rootfs/diagexec/elf.h diff --git a/stage1/rootfs/Makefile b/stage1/rootfs/Makefile index 5cd4f51..f227f66 100644 --- a/stage1/rootfs/Makefile +++ b/stage1/rootfs/Makefile @@ -1,4 +1,4 @@ -_SUBDIRS=usr shim +_SUBDIRS=usr shim diagexec SUBDIRS=$(_SUBDIRS) aggregate export CFLAGS=-Wall -Os diff --git a/stage1/rootfs/diagexec/Makefile b/stage1/rootfs/diagexec/Makefile new file mode 100644 index 0000000..cbc6c46 --- /dev/null +++ b/stage1/rootfs/diagexec/Makefile @@ -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 diff --git a/stage1/rootfs/diagexec/diagexec.c b/stage1/rootfs/diagexec/diagexec.c new file mode 100644 index 0000000..d9a1782 --- /dev/null +++ b/stage1/rootfs/diagexec/diagexec.c @@ -0,0 +1,141 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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; +} diff --git a/stage1/rootfs/diagexec/elf.h b/stage1/rootfs/diagexec/elf.h new file mode 100644 index 0000000..5d4c08b --- /dev/null +++ b/stage1/rootfs/diagexec/elf.h @@ -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; +} From e210200b4cfcd5dea4e60746955468a14abf2661 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Thu, 18 Dec 2014 13:00:01 -0800 Subject: [PATCH 2/2] stage1: integrate /diagexec wrapper into rkt run Use /diagexec for launching the container's apps to assist understanding execution interpreter problems. No substantial overhead is introduced in the normal/success case, diagnostics are only attempted when execv() of the target fails. Examples of the assistance provided: ~# bin/rkt run imgs/nobash.aci # bash script without /bin/bash Error: Unable to open "/bin/bash": No such file or directory Sending SIGTERM to remaining processes... Sending SIGKILL to remaining processes... Unmounting file systems. Unmounting /proc/sys/kernel/random/boot_id. All filesystems unmounted. Halting system. ~# bin/rkt run imgs/noexec.aci # file with no execute bits Error: "/noexec.sh" is not executable Sending SIGTERM to remaining processes... Sending SIGKILL to remaining processes... Unmounting file systems. Unmounting /proc/sys/kernel/random/boot_id. All filesystems unmounted. Halting system. ~# bin/rkt run imgs/noldlinux.aci # ELF program without interp Error: Unable to open "/lib64/ld-linux-x86-64.so.2": No such file or directory Sending SIGTERM to remaining processes... Sending SIGKILL to remaining processes... Unmounting file systems. Unmounting /proc/sys/kernel/random/boot_id. All filesystems unmounted. Halting system. ~# bin/rkt run imgs/noldlinuxbash.aci # script with bash without ELF interp Error: Unable to open "/lib64/ld-linux-x86-64.so.2": No such file or directory Sending SIGTERM to remaining processes... Sending SIGKILL to remaining processes... Unmounting file systems. Unmounting /proc/sys/kernel/random/boot_id. All filesystems unmounted. Halting system. Fixes #286 --- stage1/init/container.go | 6 +++--- stage1/rootfs/aggregate/aggregate.sh | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/stage1/init/container.go b/stage1/init/container.go index 3d3969e..2c9cc16 100644 --- a/stage1/init/container.go +++ b/stage1/init/container.go @@ -83,7 +83,8 @@ func LoadContainer(root string) (*Container, error) { func (c *Container) appToSystemd(am *schema.ImageManifest, id types.Hash) error { name := am.Name.String() app := am.App - execStart := strings.Join(app.Exec, " ") + execWrap := []string{"/diagexec", rktpath.RelAppRootfsPath(id)} + execStart := strings.Join(append(execWrap, app.Exec...), " ") opts := []*unit.UnitOption{ &unit.UnitOption{"Unit", "Description", name}, &unit.UnitOption{"Unit", "DefaultDependencies", "false"}, @@ -91,7 +92,6 @@ func (c *Container) appToSystemd(am *schema.ImageManifest, id types.Hash) error &unit.UnitOption{"Unit", "OnFailure", "reaper.service"}, &unit.UnitOption{"Unit", "Wants", "exit-watcher.service"}, &unit.UnitOption{"Service", "Restart", "no"}, - &unit.UnitOption{"Service", "RootDirectory", rktpath.RelAppRootfsPath(id)}, &unit.UnitOption{"Service", "ExecStart", execStart}, &unit.UnitOption{"Service", "User", app.User}, &unit.UnitOption{"Service", "Group", app.Group}, @@ -107,7 +107,7 @@ func (c *Container) appToSystemd(am *schema.ImageManifest, id types.Hash) error default: return fmt.Errorf("unrecognized eventHandler: %v", eh.Name) } - exec := strings.Join(eh.Exec, " ") + exec := strings.Join(append(execWrap, eh.Exec...), " ") opts = append(opts, &unit.UnitOption{"Service", typ, exec}) } diff --git a/stage1/rootfs/aggregate/aggregate.sh b/stage1/rootfs/aggregate/aggregate.sh index ef39b29..b3db458 100755 --- a/stage1/rootfs/aggregate/aggregate.sh +++ b/stage1/rootfs/aggregate/aggregate.sh @@ -21,6 +21,7 @@ install -m 0644 units/reaper.service "$RD/usr/lib/systemd/system" install -m 0644 units/sockets.target "$RD/usr/lib/systemd/system" install -m 0755 scripts/reaper.sh "$RD" install -m 0755 ../shim/shim.so "$RD" +install -m 0755 ../diagexec/diagexec "$RD" ln -s shim.so "$RD/fakesdboot.so" install -d "$RD/etc"