From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 34A61376A1B for ; Wed, 8 Jul 2026 02:58:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783479506; cv=none; b=cjEjsOaNp7HjS1VEeO1jM0HJREpZOi9L92s+x8tW00+6UZTP9MbS775upGzq0VWZS9RtDBynUzSKyEdW5iaImuqsuaZ9n3VZJxkVN+wdQgYkRPEsrb+V5ghW2aiMSVjzWbWKS3EKH3TUeCIE4Buks47MUZ5oIfwjs94rVAf4Eq0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783479506; c=relaxed/simple; bh=s5FTQdbOnM7OXuG5ACRl496Z55u8vQXvSfGirL8UMqE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=YT8YsY/iHFk7WQx8mckq6RhBYEzHL0gymZbbntb2IWDsuymfd2Q8NoY7BKBBVvI04DbYDrK9n4t057+Si6gIce5sFM+Sb1gLf6kzObNMCKTyWvpQMyYsu/JC7a0TWgMBYkGtjsU5nTo2u44k4vnOy2ddbXwujd0g64qTrHaCunA= 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=L0FNx5Kn; arc=none smtp.client-ip=209.85.214.170 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="L0FNx5Kn" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2cc84e77e78so1924815ad.2 for ; Tue, 07 Jul 2026 19:58:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783479505; x=1784084305; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=gu0Oj5H+ScjgHzDAloVQBDSsZh3OVcVLQKoErfepCiE=; b=L0FNx5KnKYTSrWIQ16mvH5yeson68xFFn3PUA0Tw+i+wgw+CYH9idSzd7RkHispmeJ mgxNlWfdQR8NeIqzUlw4wYwr+cskZA4s93+eBcVRAnv/U87JBhyTemPGucW0e4or2U4o PL19AJapj34AaSyWA/uiMfwhmscdlNCoBSjv2KrV/Bo39LUrhBTktcUtPz4ktJwwusm/ TU+Hf9VO5oZ6FOMd55caS54Xe7CUVt6iJeVeeHqxjak4mRaBqv1UA9sM1R7Y9UCq6zuY 5jVycxd9mmonIfopusPXagL6ESFQ2eukjoBwapWy5IpzaSt/R5C7La5YqeqwfEe+59if Pp0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783479505; x=1784084305; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=gu0Oj5H+ScjgHzDAloVQBDSsZh3OVcVLQKoErfepCiE=; b=EnGMXlZnghAkrmtMLhYgkKv9vCd64nmonQ6M4Zy08NeBGN5XhUxnDUQ/reens9aX36 Kb/iFrBRSWU53MB//JIINP40YJ+pWS55u8HhXnIdv7n4gzOxHYu3KAd68lNoN/GbsHF8 owhrssMvPmX7vmH6eaVLxFJOK/6W4OnNpwr5GSFdDNPVd6Snx91M5bMAeQNtChLri5md U5mxEQJZIBEUsNym18GxcPyYxJF1b4jjaiiehx5GoFSs3MzPrDSB1i1+MZdt/0APppMu buGUS0E1g14uGdqQ7xEcaUVOWcxzMLLoaxmgi0OijpRoeNgWDDtGlEt36/ngBa+vOUA3 UmmQ== X-Forwarded-Encrypted: i=1; AHgh+Rpd+Azej/2YsqbRpM5eU4LWQ4RpPIGsJMuMJPLslNc8m2IEw25nDlgr5iQpR3NXCsMQzYG7jFRdI86rYps=@vger.kernel.org X-Gm-Message-State: AOJu0YyyBgwJ7Ta3eW1M/oFOeBijTAvdwfPfMBT/PRJxnrfvzWBrzbG2 D02ckp4MtUQjeijy8i88jKR8VwYk1KIMvu5zRuy7Zddvgw6ROuUKOB+j X-Gm-Gg: AfdE7cnJJhx9OFbk6D7hWVTuU5MV7JKX2Ym5R5QOkw+9kjzVjXzmZdc5+Jz/7NetzK3 0XWsZ/cvtKSc/MjsdLhAxeHDONYvK4YMSp9F49vEfs4KgiOlu9adAwf6VnSC9iRvUMydb0UQhJd 6WmndO+DfNtjYlB5WSfqqWeNj1emxhChMx/4WyQlOiKGsj59dU6aSK+PCUcDqcv4gdooDjmM8BK cHqXehWSHfqUihFFijXoDYQ1VM/oX/Y6TiLkHAhgQSaeNVIy7e+d7qx6oxzZUY5tjZPYgGFyQko Y2t90GwAU/otfum/MKvVLhhvlNUEKz6hvcxjnbrPBLE5Xj5oHt6xU11CG/jGMxUZenSPeotjFz0 ODJ1qOIwwrvdT8uf2mTfZUDsnLt0Vo7cGknGSsUbVrLkmgLOrUNddbw0T5LQgJYjpj6qD5lkIX/ R0H5GKu7/TPgnr X-Received: by 2002:a17:903:15c8:b0:2c9:97a8:8c13 with SMTP id d9443c01a7336-2ccea582aedmr5730825ad.38.1783479504583; Tue, 07 Jul 2026 19:58:24 -0700 (PDT) Received: from lgs.. ([152.32.188.52]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2ccc9bf8d80sm20568755ad.25.2026.07.07.19.58.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jul 2026 19:58:23 -0700 (PDT) From: Guangshuo Li To: Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Varun R Mallya , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Guangshuo Li Subject: [PATCH] libbpf: Limit bitfield dump reads to checked bytes Date: Wed, 8 Jul 2026 10:58:07 +0800 Message-ID: <20260708025807.702010-1-lgs201920130244@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit btf_dump_get_bitfield_value() bounds-checks the number of bytes covered by the bitfield, but the value-building loops read the full backing type size. For narrow bitfields this can read past the checked range. Checking the full backing type size is too strict for packed bitfields. For example, a packed structure can contain an 8-bit field backed by an int type whose BTF size is 4, while the actual object bytes covering the field are only 1 byte. Keep the bounds check based on the bytes covered by the field, and make the value-building loops consume the same checked byte range. Preserve the full-size read for the non-bitfield path used by unaligned enum values. Fixes: 5714ca8cba5e ("libbpf: Fix OOB read in btf_dump_get_bitfield_value") Signed-off-by: Guangshuo Li --- tools/lib/bpf/btf_dump.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c index cc1ba65bb6c5..5d185f7961ff 100644 --- a/tools/lib/bpf/btf_dump.c +++ b/tools/lib/bpf/btf_dump.c @@ -1766,17 +1766,21 @@ static int btf_dump_get_bitfield_value(struct btf_dump *d, __u64 num = 0; int i; - /* Calculate how many bytes cover the bitfield */ - start_bit = bits_offset % 8; - nr_bytes = (start_bit + bit_sz + 7) / 8; + if (bit_sz) { + /* Calculate how many bytes cover the bitfield */ + start_bit = bits_offset % 8; + nr_bytes = (start_bit + bit_sz + 7) / 8; + } else { + nr_bytes = t->size; + } /* Bound check */ if (data + nr_bytes > d->typed_dump->data_end) return -E2BIG; /* Maximum supported bitfield size is 64 bits */ - if (t->size > 8) { - pr_warn("unexpected bitfield size %d\n", t->size); + if (nr_bytes < 1 || nr_bytes > 8) { + pr_warn("unexpected bitfield size %d\n", nr_bytes); return -EINVAL; } @@ -1784,13 +1788,13 @@ static int btf_dump_get_bitfield_value(struct btf_dump *d, * stored in num, then we left/right shift num to eliminate irrelevant bits. */ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - for (i = t->size - 1; i >= 0; i--) + for (i = nr_bytes - 1; i >= 0; i--) num = num * 256 + bytes[i]; - nr_copy_bits = bit_sz + bits_offset; + nr_copy_bits = bit_sz ? bit_sz + bits_offset : nr_bytes * 8; #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - for (i = 0; i < t->size; i++) + for (i = 0; i < nr_bytes; i++) num = num * 256 + bytes[i]; - nr_copy_bits = t->size * 8 - bits_offset; + nr_copy_bits = nr_bytes * 8 - bits_offset; #else # error "Unrecognized __BYTE_ORDER__" #endif -- 2.43.0