Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795039334; b=l+dLNLGeX0a9PhDaB04x0oVKyjvKYhsjfdnxKV7DVL3Grpoe6V5pIDQR8dEEntGolyR/ AXLr+XnEHUzVT6X1EsMDBnLGyDApug9jUeVWItFq8Z+somSc/Vcq7NMh8hfgJrf2FmBP7 WVyiZXR2OHVLRtV0AFFdr5Ipe8S3YoEL9Pctn429b+giAh2YnTKmktA+Vq1bO88pD+3xN ZGLT9LvlDVHEXgODWV61qHbyCOLMSm5gc5ooQYNgPLhMW57oSRNn+n9FkoMshlfptHP8J aPj2TdEKOGqasyU4M6Q9+WtmbaN96lNLFkb/X4CmUhHDWLopd4LYt/Ekn3SIkE4buPQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795039334; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=6+1cbTtKjzYbytoyojlUsdwP5Pdze51sDkGTDzp7Bao=; b=LzGwMXc53aW8lShm8TFujczWd3lpAP9lWN5vsyyUbIYJ1CUd7AhIoLRFQBN/wUP6JuNt JjR6ddoSRPtE95Y9nzyAQE1QLxrnZlyxdvAwiNyOuNiw8JKKCnQUPfufL9xFWRf0dLn8R lRLmpxxqDWfjKgiyx+e5vP4dzgr5Soxxpp2djr5k8Zl9PxZTNuqW4fzc3jmLJu6kjvAyI G1SZscfkxu6O02mai8EdD0HNAYt0TQyiGCviZ7OIIISfpYYnL1Dn/kn/mUlVSn3vaUKP+ h6YnM31TSiSwyL8l2KWi/Uf55dIvcje2yEnL5xRiLYPfC+v0C3mlNxN8ALNa0S5igrA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Rs9bOVXn; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA84A60A260F; Wed, 18 Nov 2026 22:02:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Rs9bOVXn Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF7331F000CF; Wed, 18 Nov 2026 22:02:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795039334; bh=6+1cbTtKjzYbytoyojlUsdwP5Pdze51sDkGTDzp7Bao=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Rs9bOVXnUS4jXRaL9Y0qAU3SHXbn8wCdmEmoC7JUhJo+V/YpN1wHsPZkQ8Fzs9oSB jLgPbWkHzCEmD9Pg1VadUT7RMJPBmo0Ag8B3Oxp/XDC2Q+ZWtO8xp/0HuNeti5kMI4 NKmoe3K9heFkg4tipyGn2nyrH3DZzCDoBOVW7sgU= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Ian Rogers , Mark Amirkan , Jiri Olsa , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 6.6 2064/3318] perf evsel: Validate RAW sample before byte swapping Date: Wed, 18 Nov 2026 15:44:46 +0100 Message-ID: <20261118150423.994544975@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150313.527275416@linuxfoundation.org> References: <20261118150313.527275416@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Mark Amirkan [ Upstream commit 02f6847e1822714a4201b87e42f92b0d43e8549d ] For an opposite-endian RAW sample, __evsel__parse_sample() passes the input-controlled size to mem_bswap_64() before checking whether the payload fits in the event. A truncated record can therefore make the helper read and write past the event boundary. A crafted perf.data file makes perf report crash with SIGSEGV. ASan reports the out-of-bounds access. A regression test puts backed data past the declared end and shows that it is changed before the parser returns -EFAULT. Move the bounds checks before mem_bswap_64(). Check the rounded length too, because the helper accesses complete 64-bit words. Complete records are handled as before. Fixes: f9d8adb345d7adbb ("perf evsel: Fix swap for samples with raw data") Reviewed-by: Ian Rogers Assisted-by: Symbolic Signed-off-by: Mark Amirkan Cc: Jiri Olsa Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/tests/sample-parsing.c | 61 +++++++++++++++++++++++++++++++ tools/perf/util/evsel.c | 17 +++++---- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/tools/perf/tests/sample-parsing.c b/tools/perf/tests/sample-parsing.c index 95d204f908f0f..eaac9a8b9a25a 100644 --- a/tools/perf/tests/sample-parsing.c +++ b/tools/perf/tests/sample-parsing.c @@ -410,6 +410,55 @@ static int test_truncated_branch_stack(void) return 0; } +static int test_truncated_swapped_raw(u16 event_size, u32 raw_size) +{ + struct perf_event_attr attr = { + .sample_type = PERF_SAMPLE_RAW, + }; + struct { + struct perf_event_header header; + union { + u64 value; + u32 words[2]; + } raw; + u64 canary; + } input = { + .header = { + .type = PERF_RECORD_SAMPLE, + .size = event_size, + }, + /* Parsing a pre-swapped word exchanges these two u32 values. */ + .raw.words = { 0x12345678, raw_size }, + .canary = 0x8877665544332211ULL, + }; + struct perf_sample sample; + struct evsel *evsel; + u64 raw = input.raw.value; + u64 canary = input.canary; + int err; + + evsel = evsel__new(&attr); + if (!evsel) + return -1; + + evsel->sample_size = __evsel__sample_size(attr.sample_type); + err = __evsel__parse_sample(evsel, (union perf_event *)&input, + &sample, /*needs_swap=*/true); + perf_sample__exit(&sample); + evsel__put(evsel); + + if (err != -EFAULT) { + pr_debug("truncated swapped RAW sample (size %u, raw %u) returned %d, expected -EFAULT\n", + event_size, raw_size, err); + return -1; + } + if (input.raw.value != raw || input.canary != canary) { + pr_debug("truncated swapped RAW sample modified data before validation\n"); + return -1; + } + return 0; +} + /** * test__sample_parsing - test sample parsing. * @@ -430,6 +479,18 @@ static int test__sample_parsing(struct test_suite *test __maybe_unused, int subt if (err) return err; + /* The declared RAW payload extends past an otherwise aligned event. */ + err = test_truncated_swapped_raw(sizeof(struct perf_event_header) + + sizeof(u64), 16); + if (err) + return err; + + /* The final complete word touched by mem_bswap_64() extends past it. */ + err = test_truncated_swapped_raw(sizeof(struct perf_event_header) + + sizeof(u32) + 9, 9); + if (err) + return err; + /* * Fail the test if it has not been updated when new sample format bits * were added. Please actually update the test rather than just change diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index c02178eb3b1b3..b2be92bc7c5e7 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2515,7 +2515,10 @@ int evsel__parse_sample(struct evsel *evsel, union perf_event *event, } if (type & PERF_SAMPLE_RAW) { + const __u64 *raw; + OVERFLOW_CHECK_u64(array); + raw = array; u.val64 = *array; /* @@ -2531,16 +2534,14 @@ int evsel__parse_sample(struct evsel *evsel, union perf_event *event, } data->raw_size = u.val32[0]; - /* - * The raw data is aligned on 64bits including the - * u32 size, so it's safe to use mem_bswap_64. - */ - if (swapped) - mem_bswap_64((void *) array, data->raw_size); - array = (void *)array + sizeof(u32); - OVERFLOW_CHECK(array, data->raw_size, max_size); + if (swapped) { + /* mem_bswap_64() accesses complete 64-bit words. */ + sz = roundup((u64)data->raw_size, sizeof(u64)); + OVERFLOW_CHECK(raw, sz, max_size); + mem_bswap_64((void *)raw, data->raw_size); + } data->raw_data = (void *)array; array = (void *)array + data->raw_size; } -- 2.53.0