summaryrefslogtreecommitdiffstats
path: root/tools/perf/tests/util.c
diff options
context:
space:
mode:
authorMuhammad Bilal <meatuni001@gmail.com>2026-07-28 17:54:55 +0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2026-09-01 12:15:16 +0200
commitff917923f4fb9c83717ba135ee47d7e4c1567bb7 (patch)
treec38fd8fcf58e1937b17fdbc6518b6f8b797fc15a /tools/perf/tests/util.c
downloadlinux-stable-ff917923f4fb9c83717ba135ee47d7e4c1567bb7.tar.gz
linux-stable-ff917923f4fb9c83717ba135ee47d7e4c1567bb7.zip
staging: rtl8723bs: fix OOB read in rtw_action_frame_parse()grafted
rtw_action_frame_parse() takes a frame_len parameter but never actually checks it before indexing into the frame body: const u8 *frame_body = frame + sizeof(struct ieee80211_hdr_3addr); ... c = frame_body[0]; ... a = frame_body[1]; frame_body already points 24 bytes (sizeof(struct ieee80211_hdr_3addr)) into frame, so reading frame_body[0] and frame_body[1] requires frame_len >= 26. A management action frame shorter than that (e.g. exactly 24 bytes, the minimum a malicious peer can send) causes a 1-2 byte out-of-bounds read. This is reachable from rtw_cfg80211_monitor_if_xmit_entry() and cfg80211_rtw_mgmt_tx() in ioctl_cfg80211.c, both of which pass attacker/user-influenced frame buffers and lengths straight through. Add the missing length check before frame_body is dereferenced. Fixes: 554c0a3abf216 ("staging: Add rtl8723bs sdio wifi driver") Cc: stable@vger.kernel.org Signed-off-by: Muhammad Bilal <meatuni001@gmail.com> Link: https://patch.msgid.link/20260728125456.32359-3-meatuni001@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'tools/perf/tests/util.c')
-rw-r--r--tools/perf/tests/util.c115
1 files changed, 115 insertions, 0 deletions
diff --git a/tools/perf/tests/util.c b/tools/perf/tests/util.c
new file mode 100644
index 000000000..f9abd9911
--- /dev/null
+++ b/tools/perf/tests/util.c
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "tests.h"
+#include "util/blake2s.h"
+#include "util/debug.h"
+
+#include <linux/compiler.h>
+#include <stdlib.h>
+#include <string2.h>
+
+static int test_strreplace(char needle, const char *haystack,
+ const char *replace, const char *expected)
+{
+ char *new = strreplace_chars(needle, haystack, replace);
+ int ret = strcmp(new, expected);
+
+ free(new);
+ return ret == 0;
+}
+
+/* Maximum data length tested by test_blake2s() */
+#define MAX_DATA_LEN 512
+
+/*
+ * Hash length tested by test_blake2s(). BLAKE2s supports variable-length
+ * hashes. However, the only user of BLAKE2s in 'perf' uses 20-byte hashes,
+ * matching the length of the ELF build ID field. So that's the length we test.
+ */
+#define HASH_LEN 20
+
+/* Test the implementation of the BLAKE2s hash algorithm. */
+static int test_blake2s(void)
+{
+ u8 data[MAX_DATA_LEN];
+ u8 hash[HASH_LEN];
+ u8 hash2[HASH_LEN];
+ struct blake2s_ctx main_ctx;
+ /*
+ * This value was generated by the following Python code:
+ *
+ * import hashlib
+ *
+ * data = bytes(i % 256 for i in range(513))
+ * h = hashlib.blake2s(digest_size=20)
+ * for i in range(513):
+ * h.update(hashlib.blake2s(data=data[:i], digest_size=20).digest())
+ * print(h.hexdigest())
+ */
+ static const u8 expected_hash_of_hashes[20] = {
+ 0xef, 0x9b, 0x13, 0x98, 0x78, 0x8e, 0x74, 0x59, 0x9c, 0xd5,
+ 0x0c, 0xf0, 0x33, 0x97, 0x79, 0x3d, 0x3e, 0xd0, 0x95, 0xa6
+ };
+ size_t i;
+
+ /* Generate MAX_DATA_LEN bytes of data. */
+ for (i = 0; i < MAX_DATA_LEN; i++)
+ data[i] = i;
+
+ blake2s_init(&main_ctx, sizeof(hash));
+ for (i = 0; i <= MAX_DATA_LEN; i++) {
+ struct blake2s_ctx ctx;
+
+ /* Compute the BLAKE2s hash of 'i' data bytes. */
+ blake2s_init(&ctx, HASH_LEN);
+ blake2s_update(&ctx, data, i);
+ blake2s_final(&ctx, hash);
+
+ /* Verify that multiple updates produce the same result. */
+ blake2s_init(&ctx, HASH_LEN);
+ blake2s_update(&ctx, data, i / 2);
+ blake2s_update(&ctx, &data[i / 2], i - (i / 2));
+ blake2s_final(&ctx, hash2);
+ TEST_ASSERT_VAL("inconsistent BLAKE2s hashes",
+ memcmp(hash, hash2, HASH_LEN) == 0);
+
+ /*
+ * Pass the hash to another BLAKE2s context, so that we
+ * incrementally compute the hash of all the hashes.
+ */
+ blake2s_update(&main_ctx, hash, HASH_LEN);
+ }
+
+ /* Verify the hash of all the hashes. */
+ blake2s_final(&main_ctx, hash);
+ TEST_ASSERT_VAL("wrong BLAKE2s hashes",
+ memcmp(hash, expected_hash_of_hashes, HASH_LEN) == 0);
+ return 0;
+}
+
+static int test__blake2s_case(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
+{
+ return test_blake2s();
+}
+
+static int test__strreplace(struct test_suite *t __maybe_unused, int subtest __maybe_unused)
+{
+ TEST_ASSERT_VAL("empty string", test_strreplace(' ', "", "123", ""));
+ TEST_ASSERT_VAL("no match", test_strreplace('5', "123", "4", "123"));
+ TEST_ASSERT_VAL("replace 1", test_strreplace('3', "123", "4", "124"));
+ TEST_ASSERT_VAL("replace 2", test_strreplace('a', "abcabc", "ef", "efbcefbc"));
+ TEST_ASSERT_VAL("replace long", test_strreplace('a', "abcabc", "longlong",
+ "longlongbclonglongbc"));
+
+ return 0;
+}
+
+static struct test_case tests__util[] = {
+ TEST_CASE("String replacement", strreplace),
+ TEST_CASE("BLAKE2s hash", blake2s_case),
+ { .name = NULL, }
+};
+
+struct test_suite suite__util = {
+ .desc = "util",
+ .test_cases = tests__util,
+};