From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oo1-f74.google.com (mail-oo1-f74.google.com [209.85.161.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8318372B36 for ; Tue, 17 Feb 2026 18:01:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771351279; cv=none; b=H/EmxH3AUYy0fFL88+i91raoJdmXx0jo6Tl89Peamg3ykeuE1mOE85cpldJMvZWGsw9XKx8PYmKaJOI/Bu6om5lGfo/ucLJeN4fDRZclUemSVN/VulcMKnx+X8TFI3gRw0NUZ8M+z0cLKFljbS8tW8GWekwb+cOTs/v/DqsaWLA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771351279; c=relaxed/simple; bh=d+eR56orlGEr6nfiFJte0fb3pZg0EvdkJb1SI7KO4d4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gBpEmFlb1KSgYJTUQjGJAZcYhJmcu5lXPB+v2Atb0s9c5Z8IaL9rCZl4OPAInhYci+HKFazMwQWz3cfyZKG6ajuAQ/QYdSJAtqPQBytg+bil6Qz/sK5QNAStpkLaJIyvxmZ7Qate8me5WPdEo+lQaQZOGTnaiidX2Pzwj0JDdtc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--avagin.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4FRdk027; arc=none smtp.client-ip=209.85.161.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--avagin.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4FRdk027" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-67999893008so6457488eaf.2 for ; Tue, 17 Feb 2026 10:01:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771351277; x=1771956077; darn=vger.kernel.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=2fimtsUyJng+2uYEqG9RtpHODLieBKKBpfLX8kY/jM0=; b=4FRdk027ohoO0aIhWLO5hjCOxWfKbe6wWPq/AOKVq+BeXlU6gib3RIePIRokafda9B r5+YdHqMcPD7tjCAhG7X2YWJb5hE6Y0KZAYtf7JuD2Z+k8yNfb0bMqnE6Oe70JgI71Ow q0ym8Zx+KWqlnscJzpAoC8x0SgiHGVqsgERnlrvVQPsv5krKRzG1OxWpwt3f7ZC5bhqy wQl++Os/fPODqggaCjcgFugtZ2uwzqkcnJHm+q/n8s6vj/sfzcf2tTv8lmEy72bslEZd Tb3lDcuxd+pMbXpBu3DnVLGbH2wyyUbyXqdRhs5IClihdDQXApGQYTZDlv+voTuBZEdN rDcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771351277; x=1771956077; 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=2fimtsUyJng+2uYEqG9RtpHODLieBKKBpfLX8kY/jM0=; b=Cam7jpMdJjCDzxQf03/t6t/dJ20m5aBPleq/OL1cUr6lRG1foUK/MCeCHGepj0Y3HN t/6U1atot+GUtH90EAQugI7uJgYONRmBudQMYWsiexClXi5hMgg/6wv0keUzP2GQpHWO LGXsTcqLkm3Xt0cBEcdrPFRAxtPmadXaL5SIpgT9xCUFy/IGE8IpWj4WkjkSYC9p4mJ1 lEpkBJ0MhDm+5x6IGY/7mLEAMLVIh/OGFL666PAqtvduJ8GLSLMnZcE9smBvS+wvWyVk YobL3o/vTKmPs/GLyIvkyhI3Z9k48tI1x5Az0Fx2SqD5hXEn6XSyKFkTpCEPpNHANrd6 o9aA== X-Forwarded-Encrypted: i=1; AJvYcCXt2TMOOjfWP7sTm+SSzi8j5iBrHagKjMGDw8Rgy8X1vP131o9+4R+AOMGXa/D0ifhTAV0kEDlTbZW7+TRC@vger.kernel.org X-Gm-Message-State: AOJu0Yw24bhMA+y+SH2un+Ov4DD+yZgxyUmjvNZ8jITMfu2exsZpjhBP OICikjdFfmquYT2aWHqiOAKkOrQaNrjpue4h9AY16XSDSLV+/6hddfu6Unf6MduX6D/ElJcLti4 Fhqo4qg== X-Received: from jabel13.prod.google.com ([2002:a05:6638:4d8d:b0:5ca:f895:d03b]) (user=avagin job=prod-delivery.src-stubby-dispatcher) by 2002:a4a:ec44:0:b0:659:890b:3f9 with SMTP id 006d021491bc7-67858ef3a65mr6049899eaf.4.1771351276862; Tue, 17 Feb 2026 10:01:16 -0800 (PST) Date: Tue, 17 Feb 2026 18:01:08 +0000 In-Reply-To: <20260217180108.1420024-1-avagin@google.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260217180108.1420024-1-avagin@google.com> X-Mailer: git-send-email 2.53.0.310.g728cabbaf7-goog Message-ID: <20260217180108.1420024-5-avagin@google.com> Subject: [PATCH 4/4] selftests/exec: add test for HWCAP inheritance From: Andrei Vagin To: Kees Cook , Andrew Morton Cc: Cyrill Gorcunov , Mike Rapoport , Alexander Mikhalitsyn , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, criu@lists.linux.dev, Chen Ridong , Christian Brauner , David Hildenbrand , Eric Biederman , Lorenzo Stoakes , Michal Koutny , Andrei Vagin , Alexander Mikhalitsyn Content-Type: text/plain; charset="UTF-8" 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.310.g728cabbaf7-goog