From 8afeccc82768cdfa148991d0f83e7d9e918b3b16 Mon Sep 17 00:00:00 2001 From: Rodrigo Campos Date: Fri, 12 Jan 2024 12:16:49 +0100 Subject: [PATCH 1/2] libct/dmz: Print execve() errors This error code is using functions that are present in nolibc too. When using nolibc, the error is printed like: exec /runc.armel: errno=8 When using libc, as its perror() implementation translates the errno to a message, it is printed like: exec /runc.armel: exec format error Note that when using libc, the error is printed in the same way as before. Signed-off-by: Rodrigo Campos --- libcontainer/dmz/_dmz.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libcontainer/dmz/_dmz.c b/libcontainer/dmz/_dmz.c index 2855e60d5cd..e4cfcf087e7 100644 --- a/libcontainer/dmz/_dmz.c +++ b/libcontainer/dmz/_dmz.c @@ -1,4 +1,7 @@ #ifdef RUNC_USE_STDLIB +# include +# include +# include # include #else # include "xstat.h" @@ -11,5 +14,14 @@ int main(int argc, char **argv) { if (argc < 1) return 127; - return execve(argv[0], argv, environ); + int ret = execve(argv[0], argv, environ); + if (ret) { + /* NOTE: This error message format MUST match Go's format. */ + char err_msg[5 + PATH_MAX + 1] = "exec "; // "exec " + argv[0] + '\0' + strncat(err_msg, argv[0], PATH_MAX); + err_msg[sizeof(err_msg) - 1] = '\0'; + + perror(err_msg); + } + return ret; } From fe95a2a06a7bfce160f3941930ba2564a62baddc Mon Sep 17 00:00:00 2001 From: Rodrigo Campos Date: Thu, 18 Jan 2024 17:46:43 +0100 Subject: [PATCH 2/2] tests/integration: Test exec failures Signed-off-by: Rodrigo Campos --- tests/integration/run.bats | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/integration/run.bats b/tests/integration/run.bats index f6bd3c86500..bf23d22a103 100644 --- a/tests/integration/run.bats +++ b/tests/integration/run.bats @@ -230,3 +230,20 @@ function teardown() { grep -E '^monotonic\s+7881\s+2718281$' <<<"$output" grep -E '^boottime\s+1337\s+3141519$' <<<"$output" } + +@test "runc run [exec error]" { + cat <rootfs/run.sh +#!/mmnnttbb foo bar +sh +EOF + chmod +x rootfs/run.sh + update_config '.process.args = [ "/run.sh" ]' + runc run test_hello + + # Ensure that the output contains the right error message. For runc-dmz, both + # nolibc and libc have the same formatting string (but libc will print the + # errno description rather than just the number), and for runc_nodmz the error + # message from Go starts with the same string. + [ "$status" -ne 0 ] + [[ "$output" = *"exec /run.sh: "* ]] +}