From nobody Fri Mar 27 03:11:14 2026 Received: from mail-ot1-f73.google.com (mail-ot1-f73.google.com [209.85.210.73]) (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 9418C3CCA09 for ; Mon, 23 Mar 2026 17:54:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774288451; cv=none; b=N9QOY3+Z9mKURkz3EHUvV7j/B3H39tZxIvzoe+fGuTJ6GOKp495sIaXpg5ux8nwE28GyviX/s5ykNyEtz7J9ZunjjohVMi/RXLYtUOBhHp87C6gC1rb/G43cR3dgHm/XecWUdz/EAkaPh+8icMQOqp34n89FAb+78vS/toDs/Og= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774288451; c=relaxed/simple; bh=L2zTtGTNTw3phxOrswp+iUaPq/4BsNNONZQKwH6jDDA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mwsM1YHLTG/kEFpITlqCQgf40Y/XeRATMAh2qbmcKWtxGympfzg21useNGAuQ+x85hbUzVIVgiTYyHcgh4Y5HG+2FclC7KOVq44pKiASOnycKQobZRj7jzVzSYVcAdCq37lGq9cZbL2AWI/Wg+uIKnntl8KYNZg6Y9rzZ5bXpa8= 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=W4cK75Uy; arc=none smtp.client-ip=209.85.210.73 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="W4cK75Uy" Received: by mail-ot1-f73.google.com with SMTP id 46e09a7af769-7d7f7164b12so19032628a34.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=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=Vz01D+Qvtpuy+jqrKUW1E7zaDqaJMt9I2Ue5ziSPeb0=; b=W4cK75UyBcl7snsjvR7ZgphxNiZClz8sYs3aDFPe/nYIy2WEr/FwzB7rtaTkcEaa/5 vXkHvrursI4QcGyn3sYyvgSDa/5O0RLZAp5fpLlSPE0eRUmyKcDVi/LgrCEwslAEvc7P D0/AxJ3LPJX/g+wHwSgQe6f7bnDySXI39b5v0GrCM8BiOs9spi2P21OchCi1kjv1iBPv nOer0zVuRg7Ni1x2WeXc3LxcJZIYax8gUhsOv6wWJbp1aXRALQqlqUw4z7F9mXeyAzwi cP7U+RmYh79kPvC/qwyyYHPWlu9p73WEZ+aaMwC/3Uj9Frl1jFHlSvGibqGFJgXf4DSh A67w== 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=TXgWqDcJY4k2DfhSi7y1fYTuN176ooRLg9SjK4TdBlw03aKro19qjTbh1Kgt+fgt1M da5DiFXkBsEI6+WxBgu41iunxz4dbXu2l1ZhCLY8FYUuBY61Id/3pHlymbkdDpN+50IO eV9t3NBrFXXDsp+IMfxgkJgeXVssyH7AkWONZCxt3OpnTTIBYpfAX/fiyInsnC6DYO7U B4GvUHEfMKGzY5j4Hzoh+hFJ2DEvEJqyuT/cYvGAhWR9Pf+MCU/HZZglK02xh1i7o4lc nAo9sTXu01qBxBu4UtrTSWmQ0zyXpNsgXiR4sLLfQNuIFoncR1+Z7Yr+Rsc2zDre292W YAXQ== X-Forwarded-Encrypted: i=1; AJvYcCUSuFy8165KmULb8ltPJoZ50UUqUJ37Bs8ppa7MVxkRlf6i4usAy13065qpcOD0Xs9ATYnZnVavqspef+k=@vger.kernel.org X-Gm-Message-State: AOJu0YzTki3PO296FzGS23D7ZMomI90qEnKK/Ll1O0o+y10PC8UQ/kLd dTWBX1vNifXSvp7uzlWxCW14nEcx8nJf/qZwWkdkZYF6dbG5OkCIAGZUlVuWjCke08kyADHM4kU OiBe41w== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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-Transfer-Encoding: quoted-printable 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/selfte= sts/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/selftest= s/exec/Makefile index 45a3cfc435cf..e73005965e05 100644 --- a/tools/testing/selftests/exec/Makefile +++ b/tools/testing/selftests/exec/Makefile @@ -20,6 +20,7 @@ TEST_FILES :=3D Makefile TEST_GEN_PROGS +=3D recursion-depth TEST_GEN_PROGS +=3D null-argv TEST_GEN_PROGS +=3D check-exec +TEST_GEN_PROGS +=3D hwcap_inherit =20 EXTRA_CLEAN :=3D $(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/s= elftests/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 =3D 0, hwcap_feature, count, status; + char hwcap_str[32], hwcap_type_str[32]; + pid_t pid; + + if (argc > 1 && strcmp(argv[1], "verify") =3D=3D 0) { + unsigned long type =3D strtoul(argv[2], NULL, 16); + unsigned long expected =3D strtoul(argv[3], NULL, 16); + unsigned long hwcap =3D getauxval(type); + + if (hwcap !=3D 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 =3D prctl(PR_GET_AUXV, auxv, sizeof(auxv), 0, 0); + if (size =3D=3D -1) + ksft_exit_fail_perror("prctl(PR_GET_AUXV)"); + + count =3D size / sizeof(unsigned long); + + /* Find the "latest" feature and try to mask it out. */ + for (int i =3D 0; i < count - 1; i +=3D 2) { + hwcap =3D auxv[i + 1]; + if (hwcap =3D=3D 0) + continue; + switch (auxv[i]) { + case AT_HWCAP4: + case AT_HWCAP3: + case AT_HWCAP2: + case AT_HWCAP: + hwcap_type =3D auxv[i]; + hwcap_feature =3D find_msb(hwcap); + hwcap_idx =3D i + 1; + break; + default: + continue; + } + } + if (hwcap_type =3D=3D 0) + ksft_exit_skip("No features found, skipping test\n"); + hwcap =3D auxv[hwcap_idx]; + new_hwcap =3D hwcap ^ (1UL << hwcap_feature); + auxv[hwcap_idx] =3D new_hwcap; + + if (prctl(PR_SET_MM, PR_SET_MM_AUXV, auxv, size, 0) < 0) { + if (errno =3D=3D EPERM) + ksft_exit_skip("prctl(PR_SET_MM_AUXV) requires CAP_SYS_RESOURCE\n"); + ksft_exit_fail_perror("prctl(PR_SET_MM_AUXV)"); + } + + pid =3D fork(); + if (pid < 0) + ksft_exit_fail_perror("fork"); + if (pid =3D=3D 0) { + char *new_argv[] =3D { argv[0], "verify", hwcap_type_str, hwcap_str, NUL= L }; + + 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) =3D=3D -1) + ksft_exit_fail_perror("waitpid"); + if (status !=3D 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; +} --=20 2.53.0.983.g0bb29b3bc5-goog