From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (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 59B4947607F for ; Tue, 21 Jul 2026 23:53:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.69 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784677991; cv=none; b=YXP3U2vZkvVBIajYHt/RSDmar0OLIfZSpie+0OJzp8Xg3JrbPAQqprngEmcWNjsOPJPfNeMzCzwyp78go9hBPBjLRB5S47aiWBV2z5+upltzhzIbUvcbh14ig7GOgKJ1cs7RPKjMJ3vn4QvbYBjFeMUuTd3R4A55zSgTEn/J5e8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784677991; c=relaxed/simple; bh=/YeOSfAgO5rkMvf9uJr3Wv7ZqimEhbokrDMzqormkcQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mbVU+d3qcBlEievIPq0PIm+xWaTJdf10Bfqg0uTh1EPSZ+gsCRZ5KbujPfFO5SKGjoNA7/w5pKjBgafUNmL2BhVjBsxSGa46xVM01aqTjr2ECB/ZTvz871Guntd7OKL5WdvKiDUqHVNKjIzYYujXJ/dUqeqA5bGa28F5USNH9ZE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=e4jJzwVO; arc=none smtp.client-ip=209.85.216.69 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--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="e4jJzwVO" Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-38e22137fb3so10711600a91.0 for ; Tue, 21 Jul 2026 16:53:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784677990; x=1785282790; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=CVX6X+hj30Y8z8Y2WWGsOx1d6huMMqhYwvfBgRyMVvE=; b=e4jJzwVOkgtsTZ3H6Azp+vvyEi9kYVuHUfYAVxBKWUus5WvoT6vjCntIddApbBUUbE beKpPPeVHx0+CuO6zHNR2e+qEir98gATqMHUgKGqs9gV1IfvonCxZYJ+UUqsrqPnvjwp 6qyPODAnw1zavH/3I/OsJNpT866EGFsUMmI2s9H0kLc6foD9fzDVGMHOdIbzGiUC5I5p StMMU6CycppeOk2PS++GDjZyf3YYe36KuR46u+5rSZdgIp4lnaqH1KQBoEvA0F8sM/4k TO5mvPOb5YpI7rRZuRJo0cBnYRGklTP67w2oox0tVQoEryilOSwGPGy47wexe54FFmCM +esg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784677990; x=1785282790; h=content-type: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:content-type; bh=CVX6X+hj30Y8z8Y2WWGsOx1d6huMMqhYwvfBgRyMVvE=; b=VinYZ0f+KSBFP2M50pXFtUNVymBlV/fC6VzKdDf47Z2Rf7+tGKJYSJ/ui6tsXqprof z8lFmoLMY8RKXNUhc9N2qORqvYXyD8QDmoHFPFvwrMw/rDDHcgGiX1QTZHLw2Z785FwQ 9mPb57ih1IrTowwJxFOO+iXmpVL09SCQpDYEwjSvNvR9za7J+k8T+kB+YYuAWzTkNdnt XCQuj2nn29vAN2UjM5PPPcK4C/I+WjopWrfIZ/mNojWztVynI8OyosXcI6d1JUahqhFO zO3LjW1wxl1367RyxprKJngD2va3b4w5LWHEtExh7WCH8rv0c0aSh4pPav/uWSV9c9w3 W11Q== X-Forwarded-Encrypted: i=1; AHgh+RoFBlRk59gizIc3O/+s2qiYAS0a3z/BN/LFCnL0UuPNQrNLxv3nl5WqvphIjbg9biKnHMLlxV3jIa2rmo8=@vger.kernel.org X-Gm-Message-State: AOJu0Yzs0N/1ptCifWeY3TG6Sxwm8HwArjGqkc09ZZBldB+HXv4qW7pV RCWxIGTxVNX4YcdnTS/hyHrm8wSA4RKkcxXAzdZ8/YVp2oIWyoDnMwGLAi6TUn8223/Nj3tdWZ6 YQRCKQE3PEg== X-Received: from dloo22-n2.prod.google.com ([2002:a05:7023:a56:20b0:13c:e1bd:718c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4b86:b0:38e:8050:e63a with SMTP id 98e67ed59e1d1-38e8050e7b8mr9552295a91.6.1784677989362; Tue, 21 Jul 2026 16:53:09 -0700 (PDT) Date: Tue, 21 Jul 2026 16:52:54 -0700 In-Reply-To: <20260721235254.294053-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260721230952.267754-1-irogers@google.com> <20260721235254.294053-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.229.g6434b31f56-goog Message-ID: <20260721235254.294053-6-irogers@google.com> Subject: [PATCH v7 5/5] perf synthetic-events: Fix bounds and union member access in mmap2 build_id synthesis From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, ravi.bangoria@amd.com, swapnil.sapkal@amd.com Content-Type: text/plain; charset="UTF-8" Modify bounds and union member access in mmap2 build_id synthesis. Bound max_filename_len against the minimum of filename array capacity and the outer union stack layout minus sample ID trailers. This prevents both -E2BIG overruns and _FORTIFY_SOURCE array bounds aborts on strlcpy even if the enclosing union expands. Assisted-by: Antigravity:gemini-3.5-flash Signed-off-by: Ian Rogers --- tools/perf/util/synthetic-events.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c index 5bbf024ad606..75a32ae8ef62 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -2457,13 +2457,18 @@ int perf_event__synthesize_mmap2_build_id(const struct perf_tool *tool, size_t filename_len = strlen(filename); size_t ev_len; u64 sample_type = sample->evsel ? sample->evsel->core.attr.sample_type : 0; - void *array; + void *array = &ev; int ret; + size_t max_filename_len; - if (filename_len >= sizeof(ev.mmap2.filename)) - return -EINVAL; + max_filename_len = min(sizeof(ev.mmap2.filename) - 1, + sizeof(ev) - (MAX_ID_HDR_ENTRIES * sizeof(__u64)) - + offsetof(struct perf_record_mmap2, filename) - 1); - ev_len = sizeof(ev.mmap2) - sizeof(ev.mmap2.filename) + filename_len + 1; + if (filename_len > max_filename_len) + filename_len = max_filename_len; + + ev_len = offsetof(struct perf_record_mmap2, filename) + filename_len + 1; ev_len = PERF_ALIGN(ev_len, sizeof(u64)); if (ev_len + MAX_ID_HDR_ENTRIES * sizeof(__u64) > sizeof(ev)) @@ -2483,16 +2488,15 @@ int perf_event__synthesize_mmap2_build_id(const struct perf_tool *tool, ev.mmap2.build_id_size = bid->size; if (ev.mmap2.build_id_size > sizeof(ev.mmap2.build_id)) - ev.build_id.size = sizeof(ev.mmap2.build_id); + ev.mmap2.build_id_size = sizeof(ev.mmap2.build_id); memcpy(ev.mmap2.build_id, bid->data, ev.mmap2.build_id_size); ev.mmap2.prot = prot; ev.mmap2.flags = flags; - memcpy(ev.mmap2.filename, filename, min(strlen(filename), sizeof(ev.mmap.filename))); + strlcpy(ev.mmap2.filename, filename, filename_len + 1); - array = &ev; - array += ev.header.size; + array = (void *)((char *)&ev + ev.header.size); ret = perf_event__synthesize_id_sample(array, sample_type, sample); if (ret < 0) return ret; -- 2.55.0.229.g6434b31f56-goog