From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796227145; b=3+9gy0z1tWaNZJVKdiHVv1wBNGqPu0n7Go2ZK5eYgjRfMbDh/KWC2ofIujILGOwMb4ul BVm378zVvw08xB6+db54DJVk4NHuXZZzuwJ5n1SDqqT592/5mCI8lAoWkkCP/tnB19noe N2pkCLfsl2k9KcghqqzkxykB5Qz0fBdSodJBUaNTz4FVVi35fx7/NWTjtsWpg3Lzm277b 6FrO7iLGWG6HOk4OyJnlxiTgym2BZDXoz4lckW/ltAOkIzDJGEC7MN1O1fxYlK4GH8Wnd OxxPoEsS0FA7VltW8g/bilOip0tryBnaS4V49Z1E15Vt9LJwkrxbDLDd2kUPfwviV2Q== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796227145; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=gWrXNnT/vAxYk8cnhMHClKn9Swp14w+Po+K6UXcdsow=; b=k25i6LRwKnHa4RnUQsf1YELkeuLT0ozWifWci0IsoIdPh2dZ3WwHSMCba5SSmBiThZLU 01v79C0Xi63oQJgrUTwTaId2wExzWl3/fYUpM9EoXFe8fuliQX+Zw3UHF+v5xwLGLGhuF czQpFy4qb/GAjEdSnBQDzXSAo17qqYutqWYCI8Vm/aw76nJxTwJD843KFpO2rgXCJobng 5u+TtckExCA1XyRKnK63cELprPHiGyPOb4dpmU4TcY+jvLL8qf+8qT7AynkfP+YmjwdYe GskBGnuzRVM3I1bT7SNDnDtDJiLkfaXA90lxdeiNo9M2YF5P12xB7C76d4w0WOcw3Mw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=X7OG26BO; arc=pass header.oldest-pass=0 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 0B937606A31A; Wed, 2 Dec 2026 15:59:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=X7OG26BO Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796227144; b=jmdSVDXN6V3hqD8BTzYO70doPMkFgwickF3uS2k1f/9v7N33vavrwFxeZO419JfzZYJo 6Q0p59wAWKVrt3FlJEhBzF4yPV9473L4E2XoyGcrKt70u3ZQO4RSjAhPURrDhVY/DTm/Z aDDUd4QCS0J95uzWufkF6zmr3onx43B7H5v0lBZosiyN0+AoQFIirjUuo6lmQprRmnLs5 vhfkzsetrExSIP/t/hTrQ8OSXGBq23nVxMdh18Qk2BZebbv+qm3PryW2ewY8W7a8gFjLz iWOi/rQPycJZ9IyoLqEP7+Irpi0Gw1AliQPmeMxAFdxi4uOhyx+VcXCrSXr7fQpa7CQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796227144; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=gWrXNnT/vAxYk8cnhMHClKn9Swp14w+Po+K6UXcdsow=; b=p3hhh+IdhPX+x68cq21hYxy0R7EMm8OS4os4JhLEpEtqDNd5oAhufZOS1xoSxWqiAP4d 1x/zRE04s/wZqh2bpHo1eKaYREcYgm6rGPevZgrc5+8lPDTHIlhRCwXwIIl99lavcGvG2 SqT10mnlc/hK23LvV9CVap9EY4uJ9K9si1BpdEwFzzMEJy0+HHQwi4Gp3p6wwRmtnKvUt v+4X1FF137HsI90EKCIzlTq1Ts1iJXJOB841lawiX8ibhG0m1zSeLlJ6oPa+Z3iBtQSFP fIO66I6Ru/pVU1M8dwbTxxlcYmNiurvAtfyyTCP35xuPOUHBWWE04nM+K2eTbALt8+w== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id A7AA61F00893; Wed, 2 Dec 2026 15:58:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796227144; bh=gWrXNnT/vAxYk8cnhMHClKn9Swp14w+Po+K6UXcdsow=; h=From:Date:Subject:To:Cc:Reply-To; b=X7OG26BOohdfhPtFSYHPAO/nVOkSzOKN43iFH2AbJU9+9tSrsgGs/FhLDyoNd7Mp6 XsNy1aSKuNXtHnR7vLul4bMnXPH6R2CfI2HMVeySIsw4D6cPRuA70RkxzKdafHg9Co TXk5E0wK8QXsPLYP4uabsnoUygC3w6PhBPQqOE2nfGn0jEcYph9PT+fGCpOwzVGtnF ZjRXC5XfJf+lBO1Do/vB/x617/12c65FkhxMjf9zfv8X1JCmp8h5l3mqz2F2A9x3Il bDRtiXnr4djKLvYNu/Ml0sLnQsGFxNawuI5diY8TJ1k7sp5btxFBjl6hkAAJt2iklf Im7u7k/5RGyaA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 3706E4F4C1; Wed, 2 Dec 2026 15:58:58 +0000 (UTC) From: Anh Nguyen Hoang via B4 Relay Date: Wed, 02 Dec 2026 09:58:58 -0600 Subject: [PATCH] perf trace: Fix heap overflow when -e expands a syscall alias X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261202-kbug-l947-v1-1-9c324b56cea5@gmail.com> X-B4-Tracking: v=1; b=H4sIAEFAEGsC/6tWKk4tykwtVrJSqFYqSi3LLM7MzwNyDHUUlJIzE vPSU3UzU4B8JSMDIzNDIKGbnVSarptjaWKum2JpZmqWZJFkYpKcrARUX1CUmpZZATYrOhbCLy5 NykpNLgEZoFRbCwCywpgIbQAAAA== X-Change-ID: 20261202-kbug-l947-d9656b8b44cc To: Arnaldo Carvalho de Melo , Namhyung Kim Cc: Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Anh Nguyen Hoang X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796227138; l=3736; i=anhnh.4work@gmail.com; s=20261127; h=from:subject:message-id; bh=nG/s400s5BBHOS2NRLmDhz7DyaZL4VY+Hsx13dKFWy4=; b=me9y3XTDfxDoexLN88jA3tiyDmtn1miinEy7iELFSTjCKcUrKrCvP/XmVZYf6P6f1nJhFxZHx wRMaughVT2tDay5cbmpTOlHTpUTbziSNBy8P/H8n7AnTrNv9a7GYRm+ X-Developer-Key: i=anhnh.4work@gmail.com; a=ed25519; pk=9NJ/0SKqaFZmt1/I5XY11TZSNY2ys6RWB3gvqzrLHmI= X-Endpoint-Received: by B4 Relay for anhnh.4work@gmail.com/20261127 with auth_id=1662 X-Original-From: Anh Nguyen Hoang Reply-To: anhnh.4work@gmail.com Content-Transfer-Encoding: 7bit From: Anh Nguyen Hoang trace__parse_events_option() splits the -e argument into a list of syscalls and a list of other events. Both lists are allocated with strlen(str) + 1 bytes, which is enough only as long as every name copied into them is a piece of the argument. That stopped being true when the syscall alias lookup was added: a name that is not in the syscall table but matches an alias in syscall_fmts[] is replaced with the canonical name, which can be longer than what the user typed: "pread" becomes "preadv", "umount" becomes "umount2" and "fstatat" becomes "newfstatat". The strcpy() and sprintf() calls then write past the end of the allocation. Short overruns land in the allocator's padding and go unnoticed, so the visible effect depends on the length of the argument. On x86_64 with a normal perf build: $ perf trace -e open,close,read,fstatat true double free or corruption (!prev) Aborted With perf built with -fsanitize=address the overflow is reported directly, e.g. for "perf trace -e fstatat true": ERROR: AddressSanitizer: heap-buffer-overflow WRITE of size 11 [...] in trace__parse_events_option Build the lists from the resolved names instead of using a buffer sized from the argument: strdup() the first name and asprintf() each further one onto the list. With the change perf trace with the -e list above traces and prints the newfstatat() calls, and the sanitizer no longer reports this overflow. Found by an automated code-checking tool. Fixes: f932184e282f ("perf trace: Consider syscall aliases too") Signed-off-by: Anh Nguyen Hoang --- Built with make -C tools/perf on x86_64 (this tree and linux-next 20261201), default flags and -fsanitize=address; no new warnings. Before: ASan reports the overflow for -e fstatat, umount or pread and most lists holding them; the default build aborts for some list lengths, also as root in a QEMU guest. After: no report in this function, no such abort; the guest traces newfstatat(), umount2() and preadv(). Not fixed here: a separate, older overflow in trace__validate_ev_qualifier() when a glob is followed by more names, e.g. -e 'newfstatat,*stat*'. Testing: - checkpatch.pl --strict: no errors or warnings. This patch was drafted with AI assistance (Claude, claude-fable-5-2). --- tools/perf/builtin-trace.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index c08f2c3a8128..d5604a9ee206 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -5579,7 +5579,7 @@ static int trace__parse_events_option(const struct option *opt, const char *str, struct trace *trace = (struct trace *)opt->value; const char *s; char *strd, *sep = NULL, *lists[2] = { NULL, NULL, }; - int len = strlen(str) + 1, err = -1, list, idx; + int err = -1, list, idx; char *strace_groups_dir = system_path(STRACE_GROUPS_DIR); char group_name[PATH_MAX]; const struct syscall_fmt *fmt; @@ -5619,12 +5619,16 @@ static int trace__parse_events_option(const struct option *opt, const char *str, } do_concat: if (lists[list]) { - sprintf(lists[list] + strlen(lists[list]), ",%s", s); + char *tmp; + + if (asprintf(&tmp, "%s,%s", lists[list], s) < 0) + goto out; + free(lists[list]); + lists[list] = tmp; } else { - lists[list] = malloc(len); + lists[list] = strdup(s); if (lists[list] == NULL) goto out; - strcpy(lists[list], s); } if (!sep) --- base-commit: 419442c00c667d33e27db4fef9733fa966e6a149 change-id: 20261202-kbug-l947-d9656b8b44cc Best regards, -- Anh Nguyen Hoang