From nobody Sun Feb 8 12:36:44 2026 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 4B2C22D7DCF for ; Thu, 8 Jan 2026 05:07:56 +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=1767848878; cv=none; b=ce8Yy8yiSJpOcgEdgI8y4mBYlt0mlwbuVEZggpjrVJqXORqSzDjmE+7QvesseDln7V8ufj6QOO4sjEsCAa+1ILaKjyXwu5ZQwmv25ZdkWV7arVp2jzqSUrN/cLqdidktqFrbCNtG7iannlPjnkNVPbeXzljin97ErzP4pMfTiCQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767848878; c=relaxed/simple; bh=MP0u1zrI0fnXYNh85S3yYQJ3ZSNyI4Rv0qWzsYrRyFg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UTNYAyGuuvwS7XKJCo8W8+gzhOk8daef9R7Rcf+20Fof4PWR5unwbzAsjWEE/QNcLgpKZGJ+0asmR0U8SaSMCLU5nsIr6rN4u8poh86q2UFGEt5Fhq9s89Lq31GLNRqssEf3pHpDzOWJ/rXGijlQxqfZN1o9q8Qsn9j1h030ex8= 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=uO/D3DK1; 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="uO/D3DK1" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-65cfc52c912so4253275eaf.2 for ; Wed, 07 Jan 2026 21:07:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1767848875; x=1768453675; 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=Q1hhEQ/dcIyjHD8R0p3IwfdlLsdWEz7YcUZfHbTaGDU=; b=uO/D3DK1TyKgddVvxSuZgMgRcGbGhQycuCtzvG6S+bt7DUKSGpBz3dzSE7ftmb7sJq zE/NARxC/NDERLW9yeO0dspWPreNG0+IQjT9XlqCKNE3FKs6vsE8/ukuzgTPzUztCmpN LTJRqFzY/Kd5REdqOyTejFbpT2yqxKnjQgyNdGHQv7XVYga+r1Ea6+ZOb9BOcdszAdcE 0G7BWaNHUz9klzToS1CvatS1bjVgBLvEWDLRWycV4KCnKA4zLbQAmLQVQwwq07+71dn3 mPuNcq714HFK4c/n1sZpH3XcLlzSHiLPI8aOZEMJNzfmFbMnYFmkRAz5/W8eExZsPLI7 iBYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767848875; x=1768453675; 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=Q1hhEQ/dcIyjHD8R0p3IwfdlLsdWEz7YcUZfHbTaGDU=; b=BpnbkPJXTB9rQN9twv/XAUnyXZmT3dyuJJk2LIbqbii5ZzwwTXnfEtm505d1BjCtIG 4CC8c3dJaxHt4VoB6u0jLNtZtCq+tjsd3WR3pY84x0UED/bX2iJOBD7WsxEYaYHQJe2y PjsyLy4juPCVHApoLH+s5Jyffjt7n1tJHHMqRSIRy1c7+zv4fO47U1FWxHcE8h77AznE k/4d0tQmk/8wyOCO+q6WEHYNvUfkAf2RvwFmX8U7CiWZFwKuHx7yxoiWEy2LZvwiL+t5 dZITf8SCYJeHQ4j2Da+46QvxJf3CvcPt+Wd3APPUBhp0/8XWSocX0U7pCJU0ydeiWi9d KeNw== X-Gm-Message-State: AOJu0YwqNN3ZYWYCEoB+X4dHfzzbQSFH8FOxiBjaIq2iy0FDFt5VT5+R ShUYZ8dVzQdmIfQ4S2B4aezl5rWYFD5FepNsC94yJ6L0bYrj7CnbpmcWLKQNyNDiz830NipcDsv sxjc/BQ== X-Google-Smtp-Source: AGHT+IG6PRfdl7A+xXyBMjsVyyTg1K6TF0yM6mRQxM81LAutvzKwdfc7WseETy3uSVVUyTVIXphzxa2bYvw= X-Received: from iobid13.prod.google.com ([2002:a05:6602:6a8d:b0:954:45e5:5098]) (user=avagin job=prod-delivery.src-stubby-dispatcher) by 2002:a4a:cb10:0:b0:65b:38e2:33b5 with SMTP id 006d021491bc7-65f54f7b279mr1489724eaf.49.1767848875303; Wed, 07 Jan 2026 21:07:55 -0800 (PST) Date: Thu, 8 Jan 2026 05:07:48 +0000 In-Reply-To: <20260108050748.520792-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: <20260108050748.520792-1-avagin@google.com> X-Mailer: git-send-email 2.52.0.351.gbe84eed79e-goog Message-ID: <20260108050748.520792-4-avagin@google.com> Subject: [PATCH 3/3] selftests/exec: add test for HWCAP inheritance From: Andrei Vagin To: Kees Cook Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, criu@lists.linux.dev, Andrew Morton , Chen Ridong , Christian Brauner , David Hildenbrand , Eric Biederman , Lorenzo Stoakes , Michal Koutny , Andrei Vagin 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. Signed-off-by: Andrei Vagin --- tools/testing/selftests/exec/.gitignore | 1 + tools/testing/selftests/exec/Makefile | 1 + tools/testing/selftests/exec/hwcap_inherit.c | 104 +++++++++++++++++++ 3 files changed, 106 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..ef80a010765d --- /dev/null +++ b/tools/testing/selftests/exec/hwcap_inherit.c @@ -0,0 +1,104 @@ +// 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"); + 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.52.0.351.gbe84eed79e-goog