From nobody Thu Dec 18 18:57:13 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1EA9CE732DB for ; Thu, 28 Sep 2023 14:38:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230483AbjI1Oid (ORCPT ); Thu, 28 Sep 2023 10:38:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230201AbjI1Oi3 (ORCPT ); Thu, 28 Sep 2023 10:38:29 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E5BA136; Thu, 28 Sep 2023 07:38:26 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5255C433C8; Thu, 28 Sep 2023 14:38:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695911906; bh=q8UFTGCW+PEFQaExarcKjuMastWfb/GcwHIY56H9Tcw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=BZQIp7cN2GL57vChOU1kT8bhTIHyB461hx5VZi7EQHsucGSvA9HIFgljqIkq95OkW IapfOzlVzrqPm3gZkrIYLzry6pCbW8Kc7JGUXZ2DFiZQnrJOxUXcUtEupIgSJZmELh 9piUigJVyNW4S/Qn23Qz5inxoA++YG7wr/P5dS3S2ypSplxnXdcDo7TFWXslxuoeVX kOQqIpdkbBW9/3L0vXy6+tAMfe5otOlq9G8eA4aeZrbDEhOjrqPjxhsC5qLSIdggQF iP+QB3U8QFWreajhTBTyT51Urc00S1OlJpdwS8wphP9Vc7KyrTsn6QANcFNfaKFrsu e0m8ZBWmcE6cg== From: Mark Brown Date: Thu, 28 Sep 2023 16:38:11 +0200 Subject: [PATCH 1/2] kselftest: Add a ksft_perror() helper MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230928-ktap-exec-v1-1-1013a2db0426@kernel.org> References: <20230928-ktap-exec-v1-0-1013a2db0426@kernel.org> In-Reply-To: <20230928-ktap-exec-v1-0-1013a2db0426@kernel.org> To: Shuah Khan , Eric Biederman , Kees Cook Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Mark Brown X-Mailer: b4 0.13-dev-099c9 X-Developer-Signature: v=1; a=openpgp-sha256; l=1471; i=broonie@kernel.org; h=from:subject:message-id; bh=q8UFTGCW+PEFQaExarcKjuMastWfb/GcwHIY56H9Tcw=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlFY/dyR2RkI4Mf3AjCLdtgSIbCYIqcP2/TW/+f QjOYN+RisqJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZRWP3QAKCRAk1otyXVSH 0LZxB/9rF9qh6VkytpRTD4uzNLpwiQHxuWBhc9JCr9kLoPxnRbAJTvaRe530xMNWvRPC0cIWeDk hhw6WbIo53maSZJl56bQngArBk29ZLRxINJrFzslh7L2PqNf2uFuvOAPN9hrej6d8cxWWUR+KMh LjCExIZCY8nR+W/+OqzUxP9ZjUv3nhIQ6yl5AkhLKfrPYDO9wP2iTjwuaHK160uXwZcwYCRytgh l9Jwk4IupXGCV3vZ6Eggv6zf1fJL1EBkjAc9V40uMaVKwznhKA9ANctrnmLFGnDK/luMYGZc0g8 fGyy026bp0/uCnZAzkF+UE96ngvOl/uimO47/k9nHs22kiy7 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The standard library perror() function provides a convenient way to print an error message based on the current errno but this doesn't play nicely with KTAP output. Provide a helper which does an equivalent thing in a KTAP compatible format. nolibc doesn't have a strerror() and adding the table of strings required doesn't seem like a good fit for what it's trying to do so when we're using that only print the errno. Signed-off-by: Mark Brown Reviewed-by: Kees Cook --- tools/testing/selftests/kselftest.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/= kselftest.h index 529d29a35900..af9f1202d423 100644 --- a/tools/testing/selftests/kselftest.h +++ b/tools/testing/selftests/kselftest.h @@ -48,6 +48,7 @@ #include #include #include +#include #include #endif =20 @@ -155,6 +156,19 @@ static inline void ksft_print_msg(const char *msg, ...) va_end(args); } =20 +static inline void ksft_perror(const char *msg) +{ +#ifndef NOLIBC + ksft_print_msg("%s: %s (%d)\n", msg, strerror(errno), errno); +#else + /* + * nolibc doesn't provide strerror() and it seems + * inappropriate to add one, just print the errno. + */ + ksft_print_msg("%s: %d)\n", msg, errno); +#endif +} + static inline void ksft_test_result_pass(const char *msg, ...) { int saved_errno =3D errno; --=20 2.39.2 From nobody Thu Dec 18 18:57:13 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D222DE732DB for ; Thu, 28 Sep 2023 14:38:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231206AbjI1Oig (ORCPT ); Thu, 28 Sep 2023 10:38:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229713AbjI1Oia (ORCPT ); Thu, 28 Sep 2023 10:38:30 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17EAA19E; Thu, 28 Sep 2023 07:38:28 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77B60C433C7; Thu, 28 Sep 2023 14:38:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695911907; bh=sOssdkEDOu+oIP+T5MSwmPgEXystV1oGz6QEO2yuBlw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=I97GdR3XyY/ltTQZT/rviiLJPoX0sl+qvjaUkaGpr+dt8Sx3caPcJgDF3+i9vIpUH UEQpU+6T7BaBloSLmoTeVB/ElkqrkI4rT8O0S2aOBMZtH5AhV/0V9o+FmK03MiC4h3 PA39xZZ/4rE63fIM09OqrqGEtCUe46Dq0nPoBxQbfazh48cBP7KgeEFGO8u3pFBxWp 9bkE72dqGo3LC0GFZh7GwOpWsnESK/+sFpA5K1bC75OIdrZ46ZTG8BeWp0Rwdiv7Uz lyba8iTWOxmCFbr9uZDNL2oolGido1i7bxn6zEbNOiG+/WRdIAWWilsi812RSpInqQ 6du9RipFhe92w== From: Mark Brown Date: Thu, 28 Sep 2023 16:38:12 +0200 Subject: [PATCH 2/2] selftests/exec: Convert execveat test to generate KTAP output MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20230928-ktap-exec-v1-2-1013a2db0426@kernel.org> References: <20230928-ktap-exec-v1-0-1013a2db0426@kernel.org> In-Reply-To: <20230928-ktap-exec-v1-0-1013a2db0426@kernel.org> To: Shuah Khan , Eric Biederman , Kees Cook Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Mark Brown X-Mailer: b4 0.13-dev-099c9 X-Developer-Signature: v=1; a=openpgp-sha256; l=7284; i=broonie@kernel.org; h=from:subject:message-id; bh=sOssdkEDOu+oIP+T5MSwmPgEXystV1oGz6QEO2yuBlw=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlFY/exx+aGs0kTE5g6OXj8ApwZm43FD63znXZR F6AffJwc9SJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZRWP3gAKCRAk1otyXVSH 0HwGCACBRHBT8cww7vfUB5X5v/kNEMKGqvcT34gJ7FjV5qKozSGvfJ2Lz6Ylcxqu8ri3q8WVZdQ K9KEzo7y+a015hv7U4cEdlokDVpd20LaQb2O5EfN0U1Ve/5eYO/481VpMMCIkBQnnipRNeP+V15 lJrkWdwigTKGutND2V9CAwgYLDho0OG2RANWJHr14YaoowHQ1pcHOF42C2QAwNzronMhXy2YbWm EP2kP464BNTg4oSP9XqAWVATNoz9o1MAhxagyRAZSRkUfE2N8qwWgjPnC1/9X0z2TeWjJOkhakd mOUicmTdtxj2n6cYJpQnO5Yl6uDmmc0/lWERHkBRCAqjZg1e X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently the execveat test does not produce KTAP output but rather a custom format. This means that we only get a pass/fail for the suite, not for each individual test that the suite does. Convert to using the standard kselftest output functions which result in KTAP output being generated. The main trick with this is that, being an exec() related test, the program executes itself and returns specific exit codes to verify success meaning that we need to only use the top level kselftest header/summary functions when invoked directly rather than when run as part of a test. Signed-off-by: Mark Brown Reviewed-by: Kees Cook --- tools/testing/selftests/exec/execveat.c | 87 ++++++++++++++++++++---------= ---- 1 file changed, 52 insertions(+), 35 deletions(-) diff --git a/tools/testing/selftests/exec/execveat.c b/tools/testing/selfte= sts/exec/execveat.c index 67bf7254a48f..bf79d664c8e6 100644 --- a/tools/testing/selftests/exec/execveat.c +++ b/tools/testing/selftests/exec/execveat.c @@ -23,6 +23,9 @@ =20 #include "../kselftest.h" =20 +#define TESTS_EXPECTED 51 +#define TEST_NAME_LEN (PATH_MAX * 4) + static char longpath[2 * PATH_MAX] =3D ""; static char *envp[] =3D { "IN_TEST=3Dyes", NULL, NULL }; static char *argv[] =3D { "execveat", "99", NULL }; @@ -43,71 +46,85 @@ static int execveat_(int fd, const char *path, char **a= rgv, char **envp, static int _check_execveat_fail(int fd, const char *path, int flags, int expected_errno, const char *errno_str) { + char test_name[TEST_NAME_LEN]; int rc; =20 errno =3D 0; - printf("Check failure of execveat(%d, '%s', %d) with %s... ", - fd, path?:"(null)", flags, errno_str); + snprintf(test_name, sizeof(test_name), + "Check failure of execveat(%d, '%s', %d) with %s", + fd, path?:"(null)", flags, errno_str); rc =3D execveat_(fd, path, argv, envp, flags); =20 if (rc > 0) { - printf("[FAIL] (unexpected success from execveat(2))\n"); + ksft_print_msg("unexpected success from execveat(2)\n"); + ksft_test_result_fail("%s\n", test_name); return 1; } if (errno !=3D expected_errno) { - printf("[FAIL] (expected errno %d (%s) not %d (%s)\n", - expected_errno, strerror(expected_errno), - errno, strerror(errno)); + ksft_print_msg("expected errno %d (%s) not %d (%s)\n", + expected_errno, strerror(expected_errno), + errno, strerror(errno)); + ksft_test_result_fail("%s\n", test_name); return 1; } - printf("[OK]\n"); + ksft_test_result_pass("%s\n", test_name); return 0; } =20 static int check_execveat_invoked_rc(int fd, const char *path, int flags, int expected_rc, int expected_rc2) { + char test_name[TEST_NAME_LEN]; int status; int rc; pid_t child; int pathlen =3D path ? strlen(path) : 0; =20 if (pathlen > 40) - printf("Check success of execveat(%d, '%.20s...%s', %d)... ", - fd, path, (path + pathlen - 20), flags); + snprintf(test_name, sizeof(test_name), + "Check success of execveat(%d, '%.20s...%s', %d)... ", + fd, path, (path + pathlen - 20), flags); else - printf("Check success of execveat(%d, '%s', %d)... ", - fd, path?:"(null)", flags); + snprintf(test_name, sizeof(test_name), + "Check success of execveat(%d, '%s', %d)... ", + fd, path?:"(null)", flags); + child =3D fork(); if (child < 0) { - printf("[FAIL] (fork() failed)\n"); + ksft_perror("fork() failed"); + ksft_test_result_fail("%s\n", test_name); return 1; } if (child =3D=3D 0) { /* Child: do execveat(). */ rc =3D execveat_(fd, path, argv, envp, flags); - printf("[FAIL]: execveat() failed, rc=3D%d errno=3D%d (%s)\n", - rc, errno, strerror(errno)); + ksft_print_msg("execveat() failed, rc=3D%d errno=3D%d (%s)\n", + rc, errno, strerror(errno)); + ksft_test_result_fail("%s\n", test_name); exit(1); /* should not reach here */ } /* Parent: wait for & check child's exit status. */ rc =3D waitpid(child, &status, 0); if (rc !=3D child) { - printf("[FAIL] (waitpid(%d,...) returned %d)\n", child, rc); + ksft_print_msg("waitpid(%d,...) returned %d\n", child, rc); + ksft_test_result_fail("%s\n", test_name); return 1; } if (!WIFEXITED(status)) { - printf("[FAIL] (child %d did not exit cleanly, status=3D%08x)\n", - child, status); + ksft_print_msg("child %d did not exit cleanly, status=3D%08x\n", + child, status); + ksft_test_result_fail("%s\n", test_name); return 1; } if ((WEXITSTATUS(status) !=3D expected_rc) && (WEXITSTATUS(status) !=3D expected_rc2)) { - printf("[FAIL] (child %d exited with %d not %d nor %d)\n", - child, WEXITSTATUS(status), expected_rc, expected_rc2); + ksft_print_msg("child %d exited with %d not %d nor %d\n", + child, WEXITSTATUS(status), expected_rc, + expected_rc2); + ksft_test_result_fail("%s\n", test_name); return 1; } - printf("[OK]\n"); + ksft_test_result_pass("%s\n", test_name); return 0; } =20 @@ -129,11 +146,9 @@ static int open_or_die(const char *filename, int flags) { int fd =3D open(filename, flags); =20 - if (fd < 0) { - printf("Failed to open '%s'; " + if (fd < 0) + ksft_exit_fail_msg("Failed to open '%s'; " "check prerequisites are available\n", filename); - exit(1); - } return fd; } =20 @@ -162,8 +177,7 @@ static int check_execveat_pathmax(int root_dfd, const c= har *src, int is_script) char *cwd =3D getcwd(NULL, 0); =20 if (!cwd) { - printf("Failed to getcwd(), errno=3D%d (%s)\n", - errno, strerror(errno)); + ksft_perror("Failed to getcwd()"); return 2; } strcpy(longpath, cwd); @@ -193,12 +207,12 @@ static int check_execveat_pathmax(int root_dfd, const= char *src, int is_script) */ fd =3D open(longpath, O_RDONLY); if (fd > 0) { - printf("Invoke copy of '%s' via filename of length %zu:\n", - src, strlen(longpath)); + ksft_print_msg("Invoke copy of '%s' via filename of length %zu:\n", + src, strlen(longpath)); fail +=3D check_execveat(fd, "", AT_EMPTY_PATH); } else { - printf("Failed to open length %zu filename, errno=3D%d (%s)\n", - strlen(longpath), errno, strerror(errno)); + ksft_print_msg("Failed to open length %zu filename, errno=3D%d (%s)\n", + strlen(longpath), errno, strerror(errno)); fail++; } =20 @@ -405,28 +419,31 @@ int main(int argc, char **argv) const char *in_test =3D getenv("IN_TEST"); =20 if (verbose) { - printf(" invoked with:"); + ksft_print_msg("invoked with:\n"); for (ii =3D 0; ii < argc; ii++) - printf(" [%d]=3D'%s'", ii, argv[ii]); - printf("\n"); + ksft_print_msg("\t[%d]=3D'%s\n'", ii, argv[ii]); } =20 /* Check expected environment transferred. */ if (!in_test || strcmp(in_test, "yes") !=3D 0) { - printf("[FAIL] (no IN_TEST=3Dyes in env)\n"); + ksft_print_msg("no IN_TEST=3Dyes in env\n"); return 1; } =20 /* Use the final argument as an exit code. */ rc =3D atoi(argv[argc - 1]); - fflush(stdout); + exit(rc); } else { + ksft_print_header(); + ksft_set_plan(TESTS_EXPECTED); prerequisites(); if (verbose) envp[1] =3D "VERBOSE=3D1"; rc =3D run_tests(); if (rc > 0) printf("%d tests failed\n", rc); + ksft_finished(); } + return rc; } --=20 2.39.2