From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) (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 1A12D224D6 for ; Sun, 7 Jun 2026 08:59:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.66 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780822796; cv=none; b=eRSx8V6/T4LNy/G9YiAHOOwJY10AvH+SfmLxk5Pcntdw8832MpwlVb/jevS0goQNl4+ZocMbk0/ctsaCcjXe4//4V7MH7MXl6+bsK85htidQXwwkK2eHMDCTz1x2mRUSJ8QnImyxgApery2fncSnJQ9Y/MbmIaOfhdHc+hBFdi0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780822796; c=relaxed/simple; bh=51hCHviJV9dx2kHJUEdIxPea9gknSXJj2/BlEWlzHPA=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:Subject:From:To: References:In-Reply-To; b=DC8VlkwoxqbuA1UjwGwznBJZlnZWfQdTlxnUe/yjIfm6gw2hbyXjlrVa+hnb2v7eaP898op3/ZX4tcetCD+X+Rp+p5OJXGgcu8Qm1Zy01PWM+NfZDIIPr0g4/yLk71/CDYrv8tScB08Kq0ZSulVBzucDRrsEQoKIXWFsozneL+E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IrE0gVMt; arc=none smtp.client-ip=209.85.128.66 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IrE0gVMt" Received: by mail-wm1-f66.google.com with SMTP id 5b1f17b1804b1-490ae94a89eso27122885e9.1 for ; Sun, 07 Jun 2026 01:59:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780822792; x=1781427592; darn=vger.kernel.org; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=hd8eJQE2e7HGUVx3+ey2vy9S5cDeuFg6IlJZJVllcIw=; b=IrE0gVMtulCp3oN+57lGho/0OvEyOcm17i4vJeiNhE9ynyhO2q26zdmuxh/tu2IPeY SCHz/B3zBNOHZIkgY0hf4t9GCZgIHZnh+czAQ3UymI0m/m5SrMI+IaXYo3C+FFotApdD ZoBU7FONBHEYzKjsZzOCu+HhfZCWHqlxs0YMuWu5n2ZHJSYjhfi3P2INkkNPICzgV9mm 1JvcWhCl3jN+yQg/CRAt2HQ5nbZIG7RGU6X8I3KnOdKg0w+7noDqvtjfwW0rNH1G8w9I P6q34vJZnO9m/lWgHwm/Lj2qNI8+0FvT9DF5FrOTafbujNCMuDZIyjSOkFU2lOHgj6FA NiwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780822792; x=1781427592; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=hd8eJQE2e7HGUVx3+ey2vy9S5cDeuFg6IlJZJVllcIw=; b=qHcJc1/7Egz0cENPAWPLdl0J2bRfQq9y1I+eF+Lhlblhv1sZycx1JWp54EYRubzXQF GozZuoYgMNcay99F5ods8wzXfSgjvZ5i9Lyl6LyjDkZ7UrUXKlFe2UVuNPdwKt5QYDMz VCMT72azMwGNS82eFEaHLTTQPecN/ahoocIwgAi5kBdd0PXNpJfCBMGlkh2uTsDxRXny OFDOCv+oAkgZTKO3s1SOodv8dbmlzqCHpl1QCGrRzpoyUtGJ0m0uPAn0jfQIXzJaO6O9 8L1+WXAK+v8U3sXcLTVNV/4SXDIrl3bfTJbEAZ9RoXycrFenhM+z/pfePtmCSwDLiuvF SWmQ== X-Forwarded-Encrypted: i=1; AFNElJ/+tJfxcK02vkS4oXbMmA91jL4vTx1Bs/RZsKEPgLByGuYrKvn1g56xIoAJsRES4miHeBgyvEqGsPkzGFI=@vger.kernel.org X-Gm-Message-State: AOJu0YzLs6m9bmwM2PyNJd9tiNwh9vNI++Sye/W5y8QdiznZgarIuDUD H2pZjb5t0qfWiE1U7eVcIj5AkrwWdpgm3jB9GLC/m83yPj2od4T4dI8H X-Gm-Gg: Acq92OGBJgJbdcZw53QvgGgGOut5m/zz3ATM1eAk1OjSm8678r/kZ+uoSExpLCVsZRW Z9qVLUI19AQbBGfQ2Ei0B28ddUyzkyucOBlaZk+4fHwDAq/RhwqXR+9noVB7NbRmSAXxHfdpM3f hiSg/raryoaap1N4k6+Z75+E+VsdYXUKmaJhYEbJWMp9cloNtEabJcpFRyDAr+vGKKamGtQz/Gc WS+YPzaVCzPeyqQQxgYDJdm9ixPaIM0fF6BMZrrGKfiRGt34MDwK9PRNpd5wJb+fnGw0OA908YE ZTe2PG0CtThrOnzkICfkPQMhJ5z8Hxt/Ds6DTBi5uP1wExNYWn4qlCMdVKKDXvx+ObQFSYSstkd AGKX/pYb3mD4xO9cWqsXmz4pZWuB9OxTrPfHyN4KPE82KUNdq3CymGYOnsxg74Df7pimWh1731X TYJonUp+ktwzJyYeu5LhA5t1xKwrb8WEef5iVqhypJFmgxbfp9GUzhkt3L8JuJg788I/tVVWQFG uQMjVVzLGq2CChn9bkROKLKfnchdCcnOzFqHufzscMSVKzK9oQyiXC55ZgkGD9QfuAEzGlzcaCl X-Received: by 2002:a05:600c:628c:b0:48f:e26a:1744 with SMTP id 5b1f17b1804b1-490c25a87f5mr166153635e9.9.1780822792343; Sun, 07 Jun 2026 01:59:52 -0700 (PDT) Received: from localhost (nat-icclus-192-26-29-3.epfl.ch. [192.26.29.3]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4601f35fd33sm45756094f8f.35.2026.06.07.01.59.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jun 2026 01:59:51 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Sun, 07 Jun 2026 10:59:51 +0200 Message-Id: Cc: , , , , , Subject: Re: [PATCH bpf] bpf: Validate BTF repeated field counts before expansion From: "Kumar Kartikeya Dwivedi" To: "Paul Moses" , , , , , , , X-Mailer: aerc 0.21.0 References: <20260605234301.1109063-1-p@1g4.org> In-Reply-To: <20260605234301.1109063-1-p@1g4.org> On Sat Jun 6, 2026 at 1:43 AM CEST, Paul Moses wrote: > btf_parse_struct_metas() walks user-supplied BTF during BPF_BTF_LOAD, > and btf_repeat_fields() expands repeatable fields from array elements > into the fixed BTF_FIELDS_MAX scratch array used by btf_parse_fields(). > > The remaining-capacity check performs the expanded field count calculatio= n > in u32. A malformed BTF can wrap that calculation, causing the check to > pass even when the expanded field count exceeds the scratch array > capacity. The following memcpy() can then write past the end of the > array. > > Use checked addition and multiplication before copying repeated fields > and reject impossible counts. > > Fixes: 797d73ee232d ("bpf: Check the remaining info_cnt before repeating = btf fields") > Cc: stable@vger.kernel.org > Signed-off-by: Paul Moses > --- Do you have an example where this actually occurred in practice? > kernel/bpf/btf.c | 9 ++++----- > 1 file changed, 4 insertions(+), 5 deletions(-) > > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c > index a62d78581207..510aa32847da 100644 > --- a/kernel/bpf/btf.c > +++ b/kernel/bpf/btf.c > @@ -3668,7 +3668,7 @@ static int btf_get_field_type(const struct btf *btf= , const struct btf_type *var_ > static int btf_repeat_fields(struct btf_field_info *info, int info_cnt, > u32 field_cnt, u32 repeat_cnt, u32 elem_size) > { > - u32 i, j; > + u32 i, j, total_cnt, total_repeats; > u32 cur; > > /* Ensure not repeating fields that should not be repeated. */ > @@ -3686,10 +3686,9 @@ static int btf_repeat_fields(struct btf_field_info= *info, int info_cnt, > } > } > > - /* The type of struct size or variable size is u32, > - * so the multiplication will not overflow. > - */ > - if (field_cnt * (repeat_cnt + 1) > info_cnt) > + if (check_add_overflow(repeat_cnt, 1, &total_repeats) || > + check_mul_overflow(field_cnt, total_repeats, &total_cnt) || > + total_cnt > (u32)info_cnt) > return -E2BIG; > > cur =3D field_cnt;