From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 65F8AF483E0 for ; Mon, 23 Mar 2026 17:54:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Vz01D+Qvtpuy+jqrKUW1E7zaDqaJMt9I2Ue5ziSPeb0=; b=QiGKvX6bZCVWmreL1p0b8R6dEA nSE+ji+IMgvZNYmC13TiwelEr8Zl2+oqyPOEdtGdHHBvudxk37DezP6wNF6Ua6q92sPeJ0k9uTJ26 t60OyxJYmC3hjoSPYfuUlYI1K6wuyN9Vlj1ongLbgtiza2skouu//BedF2qBfyigcPXEdZq4lAOh/ SfXUkNlSsgfvEPshk1am91IdvEr/AJr2kToGC/EXBLte+EO3x6MuP3XQYIhkjnSoWPRnMEDx8dUu4 JMU/YtYjLmhQsQWf+Ct5pjLEr7Re0mBM6lS/pwt9CRViSJrqJ6XVGXA8DKQ1KWKLqy+7HKTKXvEct 1iI04iPA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1w4jTg-0000000HHBC-3dub; Mon, 23 Mar 2026 17:54:12 +0000 Received: from mail-ot1-x34a.google.com ([2607:f8b0:4864:20::34a]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1w4jTc-0000000HH7R-1xNn for linux-arm-kernel@lists.infradead.org; Mon, 23 Mar 2026 17:54:09 +0000 Received: by mail-ot1-x34a.google.com with SMTP id 46e09a7af769-7d7f7164b12so19032629a34.1 for ; Mon, 23 Mar 2026 10:54:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774288447; x=1774893247; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Vz01D+Qvtpuy+jqrKUW1E7zaDqaJMt9I2Ue5ziSPeb0=; b=poFXi80j+EhzZBRh7kVQrLsE4K/+wWsVOYgCAjgYm23Dl1zH+nCjDE6wLe25gVSZ+z 1eyLJ5t7upvBV36PyTFos3bhTN28gYAifUjADRjXj/vWgcwffdUR18sqsj+5H013AX5i Lc+fO1WXhpC384nal9QzK1xgLNbV3/fkqxRS59LEDzHrLKJSSuvZ6WA1iCsJAaQ8/NLE 8fhOxvo2WZLrkq8UMndWSM27qMAc+M+NiznXJb6cOaaXsEEoBH35FUSh9hUMbmoAMjHQ jKXPJHJUHjnDF11qdhyXfB41ivjssFZPcqAmpVjNLgYT4e0ZDCFBg4Hqz8ONOwiqXFTL 57gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774288447; x=1774893247; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Vz01D+Qvtpuy+jqrKUW1E7zaDqaJMt9I2Ue5ziSPeb0=; b=KBly7nQdLr84tEb/Peqn23TEVzvL6p/of20pNa4LmbdMTsohRygOlNNuZ5Wekkhbh0 3HpvRmehVbcg9PuB8q2bKImd4P1V36C26JgiKv/d6JSc17vm/FJAeltZlKLEMWvVPqMt CRuF5pfDql0EEeFybmiNQaRQsc0+kIVwwVUWBoXRbMWSRF8wAgy7lBKM7v4G8lK86dls 2l2dZVz8/LBkiZwth3IBPYOjB9dv8hJ4WMJhzuy1qcKZw39wtPquyysn1oyZRACkIw5Y tGmM//RTL1JNjbzV2vSNP/UsK2TIcx3af3qnSyie1iki8+Tb8vpcdubjCC+rXcrm0n6D zksQ== X-Forwarded-Encrypted: i=1; AJvYcCX3qsMFr41Ys4HS9tbe9SI+BCJKhmQOB2SfzlAJNhJYR+PUid/+ANoL1q/8ZixSbpfd23ud32MmlMtx+M2V4zsp@lists.infradead.org X-Gm-Message-State: AOJu0YyMLOJ67q3FXOEJJv++/7wWrG1BC5+Fi6wmBWAFNseC1As3UpzU 6VPpbMpX5HFfEF4bj5ZdIUdkxodCBq8WzzTCcU0g0H+61aNLyuQD/gohs0QjnLi5BD2+K7g2N27 4bXkDRQ== X-Received: from jalw26.prod.google.com ([2002:a05:6638:379a:b0:5d4:6a6a:67ca]) (user=avagin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:200e:b0:67d:eeb7:476f with SMTP id 006d021491bc7-67deeb74d00mr2420094eaf.0.1774288447326; Mon, 23 Mar 2026 10:54:07 -0700 (PDT) Date: Mon, 23 Mar 2026 17:53:40 +0000 In-Reply-To: <20260323175340.3361311-1-avagin@google.com> Mime-Version: 1.0 References: <20260323175340.3361311-1-avagin@google.com> X-Mailer: git-send-email 2.53.0.983.g0bb29b3bc5-goog Message-ID: <20260323175340.3361311-5-avagin@google.com> Subject: [PATCH 4/4] selftests/exec: add test for HWCAP inheritance From: Andrei Vagin To: Kees Cook , Andrew Morton Cc: Marek Szyprowski , Cyrill Gorcunov , Mike Rapoport , Alexander Mikhalitsyn , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, criu@lists.linux.dev, Catalin Marinas , Will Deacon , linux-arm-kernel@lists.infradead.org, Chen Ridong , Christian Brauner , David Hildenbrand , Eric Biederman , Lorenzo Stoakes , Michal Koutny , Andrei Vagin , Alexander Mikhalitsyn Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260323_105408_513610_F7A2E125 X-CRM114-Status: GOOD ( 21.78 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Verify that HWCAPs are correctly inherited/preserved across execve() when modified via prctl(PR_SET_MM_AUXV). The test performs the following steps: * reads the current AUXV using prctl(PR_GET_AUXV); * finds an HWCAP entry and toggles its most significant bit; * replaces the AUXV of the current process with the modified one using prctl(PR_SET_MM, PR_SET_MM_AUXV); * executes itself to verify that the new program sees the modified HWCAP value. Reviewed-by: Alexander Mikhalitsyn Reviewed-by: Cyrill Gorcunov Reviewed-by: Kees Cook Signed-off-by: Andrei Vagin --- tools/testing/selftests/exec/.gitignore | 1 + tools/testing/selftests/exec/Makefile | 1 + tools/testing/selftests/exec/hwcap_inherit.c | 105 +++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 tools/testing/selftests/exec/hwcap_inherit.c diff --git a/tools/testing/selftests/exec/.gitignore b/tools/testing/selftests/exec/.gitignore index 7f3d1ae762ec..2ff245fd0ba6 100644 --- a/tools/testing/selftests/exec/.gitignore +++ b/tools/testing/selftests/exec/.gitignore @@ -19,3 +19,4 @@ null-argv xxxxxxxx* pipe S_I*.test +hwcap_inherit \ No newline at end of file diff --git a/tools/testing/selftests/exec/Makefile b/tools/testing/selftests/exec/Makefile index 45a3cfc435cf..e73005965e05 100644 --- a/tools/testing/selftests/exec/Makefile +++ b/tools/testing/selftests/exec/Makefile @@ -20,6 +20,7 @@ TEST_FILES := Makefile TEST_GEN_PROGS += recursion-depth TEST_GEN_PROGS += null-argv TEST_GEN_PROGS += check-exec +TEST_GEN_PROGS += hwcap_inherit EXTRA_CLEAN := $(OUTPUT)/subdir.moved $(OUTPUT)/execveat.moved $(OUTPUT)/xxxxx* \ $(OUTPUT)/S_I*.test diff --git a/tools/testing/selftests/exec/hwcap_inherit.c b/tools/testing/selftests/exec/hwcap_inherit.c new file mode 100644 index 000000000000..1b43b2dbb1d0 --- /dev/null +++ b/tools/testing/selftests/exec/hwcap_inherit.c @@ -0,0 +1,105 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +static int find_msb(unsigned long v) +{ + return sizeof(v)*8 - __builtin_clzl(v) - 1; +} + +int main(int argc, char *argv[]) +{ + unsigned long auxv[1024], hwcap, new_hwcap, hwcap_idx; + int size, hwcap_type = 0, hwcap_feature, count, status; + char hwcap_str[32], hwcap_type_str[32]; + pid_t pid; + + if (argc > 1 && strcmp(argv[1], "verify") == 0) { + unsigned long type = strtoul(argv[2], NULL, 16); + unsigned long expected = strtoul(argv[3], NULL, 16); + unsigned long hwcap = getauxval(type); + + if (hwcap != expected) { + ksft_print_msg("HWCAP mismatch: type %lx, expected %lx, got %lx\n", + type, expected, hwcap); + return 1; + } + ksft_print_msg("HWCAP matched: %lx\n", hwcap); + return 0; + } + + ksft_print_header(); + ksft_set_plan(1); + + size = prctl(PR_GET_AUXV, auxv, sizeof(auxv), 0, 0); + if (size == -1) + ksft_exit_fail_perror("prctl(PR_GET_AUXV)"); + + count = size / sizeof(unsigned long); + + /* Find the "latest" feature and try to mask it out. */ + for (int i = 0; i < count - 1; i += 2) { + hwcap = auxv[i + 1]; + if (hwcap == 0) + continue; + switch (auxv[i]) { + case AT_HWCAP4: + case AT_HWCAP3: + case AT_HWCAP2: + case AT_HWCAP: + hwcap_type = auxv[i]; + hwcap_feature = find_msb(hwcap); + hwcap_idx = i + 1; + break; + default: + continue; + } + } + if (hwcap_type == 0) + ksft_exit_skip("No features found, skipping test\n"); + hwcap = auxv[hwcap_idx]; + new_hwcap = hwcap ^ (1UL << hwcap_feature); + auxv[hwcap_idx] = new_hwcap; + + if (prctl(PR_SET_MM, PR_SET_MM_AUXV, auxv, size, 0) < 0) { + if (errno == EPERM) + ksft_exit_skip("prctl(PR_SET_MM_AUXV) requires CAP_SYS_RESOURCE\n"); + ksft_exit_fail_perror("prctl(PR_SET_MM_AUXV)"); + } + + pid = fork(); + if (pid < 0) + ksft_exit_fail_perror("fork"); + if (pid == 0) { + char *new_argv[] = { argv[0], "verify", hwcap_type_str, hwcap_str, NULL }; + + snprintf(hwcap_str, sizeof(hwcap_str), "%lx", new_hwcap); + snprintf(hwcap_type_str, sizeof(hwcap_type_str), "%x", hwcap_type); + + execv(argv[0], new_argv); + perror("execv"); + exit(1); + } + + if (waitpid(pid, &status, 0) == -1) + ksft_exit_fail_perror("waitpid"); + if (status != 0) + ksft_exit_fail_msg("HWCAP inheritance failed (status %d)\n", status); + + ksft_test_result_pass("HWCAP inheritance succeeded\n"); + ksft_exit_pass(); + return 0; +} -- 2.53.0.983.g0bb29b3bc5-goog