From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 8C19730170F for ; Sat, 6 Jun 2026 06:40:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780728060; cv=none; b=rLQnZcTGPsKMA9g2ytfMfHdPBMKuEURIpaEaRvLWrsPtMEYURy1ItZpnBenvXI+XtEqcNHhoPt/uyodhBbb8ew9jrIQP5IV+z4MdjimUMLf7L8cBk3/QGMn9j4g6KXyAEAuHsqoWKrw7jKxLxiXa6ROZRHkiWwqsl0LGR6VrPh4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780728060; c=relaxed/simple; bh=FTCFPDxznd2VnSWF1/IP5a2tyREzKaS26U4h2kBWQpA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pCb+CQmusOo/v9C+l4jBAtaWR18cdAKKOVeTfIdpOVegHEAVUZU6drVpqbeby44HLw7Rqs/OkQHf4AS+8O+vAv/IfCPjkXwRnD8JDvakyCVZpTnovVR2dwzjuJAqRvq7FD5D7UWB40i5y6ZdjCg75Tw3CoANAVp/hGLdVL0S750= 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=r8AOHvlc; arc=none smtp.client-ip=209.85.214.180 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="r8AOHvlc" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2c0bd02d97eso31049625ad.2 for ; Fri, 05 Jun 2026 23:40:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780728059; x=1781332859; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fi2g1ORMQplpFEWEapSXzP8JusgWR8+hu6m3SYXtI+o=; b=r8AOHvlcs/HPKcqJU6gVJ0F7EYptg22a6QHcIo7FsKOWOnfN1mz/f6sPsPDYs+lOHp Uo5nNhbs+fsBMcbRUraEoF08PyWS/pPKsky7iJJq87seiPIYNYtHk8hKlcWuOtqDWhm7 xkf145wI18VurHgHHDaLccvLnvrUS7N1fUc39emOd3BL5LDTj7maeClCaQdKTm9ODNb2 FE94GdFxSBRdxWzNm1BgWdBEMRzyQtJ18Me7iaNl7qMJMsftarR2RNFnex5KeQv4lEdH 1g4YuTQeGPBjpC26ZVqvyBE3jnBY5/3h0+vC74XQC9tsPgG8zlHuAlpf/dRBSPK9/XAC i5yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780728059; x=1781332859; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fi2g1ORMQplpFEWEapSXzP8JusgWR8+hu6m3SYXtI+o=; b=kDGRQ3QQkDoKv5qS6XO2X5LKncXgNgqbFFUha6qv2MiZ41icXJe4sO4WRLyBIhscFL kxn/hwzdHp4uzNDg1oI7UHIJ5QyTNjos+3QMOw4OCAEqucETYGvrMpvf5falU2e6YDOE 2MWhuEb/3UUcYs26GS6/yzSZVTeu7ETIW9nG9w/D4JWjFPmecuVCnx1m0X+nbCBigOA/ S6GrwCzDSXrMoPrL+p4HkeVXviTBFzQtQ1bVA9HkfR64biaBFf+zt3ntMGcYz8mN2aaU 1OKnrAlu1GbIGS4CY/77/6vZXGkkniefqlhwuxrxZcZnIBfQknwSRiBAHjHuD9+FQ/NV ddTA== X-Forwarded-Encrypted: i=1; AFNElJ/OGxpIqywAfve101atY+NMFec1EMpD3ndPpHClRkNzXQ0zK+Lx6IpmeajjOv187WhslbKTQY12Z7Q0Vko=@vger.kernel.org X-Gm-Message-State: AOJu0YwtT9i4R1G4/ey5oaw3JgnU/D1rr9ktIAiX1CdYvdVcJERcVJUf oKCLstv7Ya/Hz2YZgNT4s0yfjHC+bJOt2CfejUjqXzEWCeLjbehJATOt X-Gm-Gg: Acq92OHevvqOS+cGzfG2liXEKkN7bszRFrj3O6gLjzZxADgSs/nV/Xg6P8GX3ftr39z ejhKxb17fBOfVTthBr/lzeiMHWQ1F+ESRmlTACcU1EI9vUH1T859e7J/7RzJDd6svjOkhaEiFMs W6LrXNDx33mBE9qoJt7otNyB8qbp6VHWyYBLRJjEZQ9ORipoHr9JBksyfhWkTD6faXJbq5qdiNa N/IrR3udmZBqvRNRgPgv+nsanCa7wk+qj7F0MYkoRSGKqg3NI/ySqktLNQ28HTClQyYfSjMb7Rn a41St7wElcUP46VE8jbjYY3k+8PC7UL+hiwd096Y7n9i3nLmn3E7QbrYwaEh6uVb9ZI7+ZL6mTs Itib+ctCj8nW6MTIvPHKcDKpiDfliUahfkJLNtMRwoynIWMcu4OmXurlUamGQY4lN+W11ERN0KN xyjuzYPM+pZUIHezTnSZ+boLx29yyiLWt3r3iR8VAUzrqRLp0ywI/BLJSYqQQ0Pi2PpVPErtBWv OQD X-Received: by 2002:a17:903:1252:b0:2bd:eeb6:ff28 with SMTP id d9443c01a7336-2c1e820dae7mr74635345ad.12.1780728058696; Fri, 05 Jun 2026 23:40:58 -0700 (PDT) Received: from localhost.localdomain ([2601:600:947f:47d0:d807:4407:d4e8:8895]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2c16629cfb4sm117159815ad.59.2026.06.05.23.40.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jun 2026 23:40:58 -0700 (PDT) From: Hem Parekh To: Luis de Bethencourt , Salah Triki Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, fs-audit Subject: [PATCH] befs: validate b+tree node geometry to prevent out-of-bounds reads Date: Fri, 5 Jun 2026 23:40:55 -0700 Message-ID: <20260606064055.5977-1-hemparekh1596@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260606063706.5844-1-hemparekh1596@gmail.com> References: <20260606063706.5844-1-hemparekh1596@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: fs-audit A befs b+tree node is parsed inside a single block-sized buffer_head returned by befs_read_datastream() (block_size is one of 1024/2048/4096/ 8192 and is capped at PAGE_SIZE). befs_bt_read_node() copies the on-disk all_key_count and all_key_length fields (each an unsigned 16-bit value, 0..65535) out of the node header but never bounds-checks them against the size of that buffer, and the b+tree node_size is likewise never compared to block_size. befs_bt_keylen_index(), befs_bt_valarray() and befs_bt_get_key() then derive the keylen-index and value-array pointers directly from those untrusted counts: off = ALIGN(sizeof(befs_btree_nodehead) + all_key_length, 8); keylen_index = od_node + off; /* may exceed the block */ valarray = keylen_index + all_key_count * sizeof(fs16); With all_key_length close to block_size, or all_key_count large, these pointers land past the end of the node buffer. Indexing them in befs_bt_get_key() (keylen_index[index - 1], keylen_index[index]) and the value reads in befs_btree_read()/befs_find_key() are then out-of-bounds reads of page-cache memory. The path is reachable from an unprivileged readdir()/lookup() on a mounted, crafted befs image. befs_bt_get_key() additionally accepts index == all_key_count (index > all_key_count), reading one element past the keylen array even for an otherwise valid node; tighten the bound to >=. Reject any node whose keylen-index + value arrays would extend past the block that backs it, once, in befs_bt_read_node(). Signed-off-by: fs-audit --- fs/befs/btree.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/fs/befs/btree.c b/fs/befs/btree.c index aa24f1d..33a13da 100644 --- a/fs/befs/btree.c +++ b/fs/befs/btree.c @@ -219,6 +219,21 @@ befs_bt_read_node(struct super_block *sb, const befs_data_stream *ds, node->head.all_key_length = fs16_to_cpu(sb, node->od_node->all_key_length); + /* + * od_node points into a single block_size buffer_head. Reject + * nodes whose (untrusted) key counts/lengths would push the + * keylen-index or value arrays past the end of that block, which + * would otherwise cause out-of-bounds reads in befs_bt_get_key() + * and befs_bt_valarray(). + */ + if (ALIGN(sizeof(befs_btree_nodehead) + node->head.all_key_length, 8) + + (size_t)node->head.all_key_count * (sizeof(fs16) + sizeof(fs64)) > + BEFS_SB(sb)->block_size) { + befs_error(sb, "corrupt b+tree node: keys (%u/%u) exceed block size", + node->head.all_key_count, node->head.all_key_length); + return BEFS_ERR; + } + befs_debug(sb, "<--- %s", __func__); return BEFS_OK; } @@ -678,7 +693,7 @@ befs_bt_get_key(struct super_block *sb, struct befs_btree_node *node, char *keystart; fs16 *keylen_index; - if (index < 0 || index > node->head.all_key_count) { + if (index < 0 || index >= node->head.all_key_count) { *keylen = 0; return NULL; } -- 2.34.1