eBPF-based security platform
testing: verify a bytecode decoder is refused
Daniel Borkmannb532a89cbf5b
diff --git a/testing/helper/test-upatch-decode.c b/testing/helper/test-upatch-decode.cindex 0b923b18..73077c75 100644--- a/testing/helper/test-upatch-decode.c+++ b/testing/helper/test-upatch-decode.c@@ -4,10 +4,33 @@#include <stdio.h>#include <string.h>+#include <lua.h>+#include <lauxlib.h>+#include "util/malloc.h"#include "util/upatch_decode.h"#include "util/upatch_maps.h"+static int dump_chunk(lua_State *L, const void *p, size_t sz, void *ud)+{+ (void)L;+ return fwrite(p, 1, sz, ud) == sz ? 0 : 1;+}++/* The bytecode LuaJIT makes of a source file, for a decoder that is none. */+static int dump(const char *source)+{+ lua_State *L = luaL_newstate();+ int rc = 1;++ if (!L)+ return 1;+ if (!luaL_loadfile(L, source))+ rc = lua_dump(L, dump_chunk, stdout);+ lua_close(L);+ return rc ? 1 : 0;+}+int main(int argc, char **argv){struct shirudo_map_view view = {@@ -19,14 +42,17 @@ int main(int argc, char **argv)size_t out_len = 0;int rc;+ if (argc == 3 && strcmp(argv[1], "dump") == 0)+ return dump(argv[2]);if (argc != 3 || (strcmp(argv[1], "json") != 0 &&strcmp(argv[1], "otel") != 0 &&strcmp(argv[1], "loop") != 0 &&strcmp(argv[1], "memory") != 0 &&strcmp(argv[1], "utf8") != 0)) {fprintf(stderr,- "usage: %s <json|otel|loop|memory|utf8> <file.upatch>\n",- argv[0]);+ "usage: %s <json|otel|loop|memory|utf8> <file.upatch>\n"+ " %s dump <decoder.lua>\n",+ argv[0], argv[0]);return 2;}snprintf(view.upatch, sizeof(view.upatch), "%s", "t0005");diff --git a/testing/t0005-upatch-decoders.sh b/testing/t0005-upatch-decoders.shindex e8ce7bbb..b7d10b63 100755--- a/testing/t0005-upatch-decoders.sh+++ b/testing/t0005-upatch-decoders.sh@@ -112,6 +112,20 @@ assert dec[\"source_len\"] > 0, dec"'+# The same decoder as LuaJIT bytecode, which the loader verifies no part+# of: refused, where the source it came from runs.+test_expect_success 'a bytecode decoder is refused' '+ mkdir -p "$WORK/bytecode/decoders" &&+ ./helper/test-upatch-decode dump "$WORK/decoders/t0005.lua" \+ >"$WORK/bytecode/decoders/t0005.lua" &&+ [ -s "$WORK/bytecode/decoders/t0005.lua" ] &&+ cp "$WORK/t.bpf.c" "$WORK/bytecode/t.bpf.c" &&+ ( cd .. && "$B" "$WORK/bytecode/t.bpf.c" -o "$WORK/bytecode/u.upatch" \+ --vmlinux vendor/linux/vmlinux.h --name t0005 \+ --key "$WORK/k.pem" --cert "$WORK/c.der" ) >/dev/null 2>&1 &&+ ! ./helper/test-upatch-decode json "$WORK/bytecode/u.upatch" >/dev/null 2>&1+'+test_expect_success 'signed Lua decoder returns JSON' './helper/test-upatch-decode json "$WORK/u.upatch" |python3 -c "