From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (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 DDC482FF672 for ; Sat, 6 Jun 2026 06:37:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780727847; cv=none; b=EEyhWQsftXZOG32wFGXqbnIg7t/0E/n2oQSiHRIwmHkEUYTXPHu+J7mMoA9YGsob/vbD9PbVmVlSTUlQHOHCayBQsKkSUeOkB/BfW8jXlrjItSmqOLFFWzI3zmERv9u7o15mwv59hWmWSkM6ZI2obCdFf4ed21yYUDS1w3jdX5A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780727847; c=relaxed/simple; bh=FTCFPDxznd2VnSWF1/IP5a2tyREzKaS26U4h2kBWQpA=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Ppn4qc7QEAXvktCrgs1wAohygoc0sgfMqeER62FrNdLAtOYXOxsbi5ii18jDWS0zVKZ7SuWNpVWZbXAxip4bJT3ORz+uLOKoLF/rJX0ZhNgkELSLf+CaU+MZyodqxRVoZ9NmUmkoZKf6Js2YG6GiOi1bOFF2a08+SmIM/0L58N0= 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=Ezsj8UDR; arc=none smtp.client-ip=209.85.216.43 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="Ezsj8UDR" Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-36babe2c4bdso1708309a91.1 for ; Fri, 05 Jun 2026 23:37:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780727845; x=1781332645; 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=fi2g1ORMQplpFEWEapSXzP8JusgWR8+hu6m3SYXtI+o=; b=Ezsj8UDRqMh63tr8f1+ClACQF1JVjGhAleLJIqNK9v9hy5qPd/hgaXUqBko4mhwVpW FtyrKnqg4+VLzhKX7w1uJxUZ5TCQx2QRBILT6tr8i0f4hQcG0S/ZGK7QIXe81u1BL3PJ uVzN0VJ1fiP/hKN3tqUFIn/cLLny+vz8kiCNViYDqlLYrf75qsW0+c76iNSYuVv7RN3X Iwh8C4y7LDRrxshxXX/Ic8CDYGNq4l+ipBkjg6u6adsWR0ndrF3wbXVLKzKWJVXRHGdk n88HtMfTshX8B4gOTZVPbPZnGuEAA/eejcKCDWEFJa2lIAHhqCL8eFaHKUkZ8d87wXh8 lBoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780727845; x=1781332645; 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=fi2g1ORMQplpFEWEapSXzP8JusgWR8+hu6m3SYXtI+o=; b=o+ce7GL0AL+kJhLEGj8arXPmlbF7ZFOI5h370ab73RB+ioD2r2fGsg1n3uQ2Wa7YvE /LpEJ1dl0wYrLTE7deu52kcyOxveA87O7e1EOgKYDRffR5vJVK1CEYnPdRlbfXVI+6QY JQnZefNp85nJL7MnXdRXpxWl2utvLw8zLDd0ZaB5ya5JbB8qxGKK3EBuaSMv6GSSWrhE sw2h/050PTZYi49ENuAvIsgXenm/Ib+nlno1E6Dmj9LReuK1S3Dh7Jb3GrCH1scbsXKR xreFSdE0JY2e2sWqqJGOETBNMGBnQjHC/aCXzSQnYZCx3UHUdiM5FkfzYlxiEKTMwK8r ll4Q== X-Forwarded-Encrypted: i=1; AFNElJ9dz9+WV2B+HwZwjGA3lRawUHeaDjofDgDbxtuHFUeHiZ7kJ/2NQP/DjyCVTQsbURk6wHi5vrmORuKqqrM=@vger.kernel.org X-Gm-Message-State: AOJu0YymQTsrxU81LSv8Cwtn7413g+ujAnAuk32mDq21ITzs/vhfJDLQ YeHWl+5y/e1dMVUD66xr1ty+tTSD2Plmg45BNOiyL02PDZ+CrlF4Equr X-Gm-Gg: Acq92OGmmNqUzCYpaVs6XecuMkD1VOTPjAKYC/wR/uKKN9wN3olgiC98aoUF70Bsbge kzZLcFI1kyDErDYQdCpCQhxzKHiJlN1+zfRNCtGoxaKyy/8Lv+xGR5Uu5mKRJ0dVU1wu1cc6VRj 5IYN2epGvmSi8MgZ1jjM13bRl8tKmR4gbWAr/oPnZJ3WZkUefYKb9RLxhNYqKOoxt1xaZxJLpsa rvO68rNB7rswWF+748oE/A4OynbDP1QEs2IGU0jenU9Bqv7wcW0775cQjP1Trchj23WDZ79uXeU U3WL8Y7o3kELp+idtBrDggD7fExqsI7jPfzW+NJfsiCcij0RyKsqFs8/64A+beCmXoOldejL0hR 4iHLePDC/dsCwdfkvNnp+2Yg4NLPdZquLWTD3b717mqC7VMOwc23Qvsrhku/s6u4ws8Ta5MMz2I 6BZbUagXfyOHfF+iA9DvOZ7dyw4blybPsf/QMGzmGdcB+Ng8Ru6UUkvXdoJVN6KYSyD2/9iN1nD 87K X-Received: by 2002:a17:902:fe0c:b0:2be:22cc:e227 with SMTP id d9443c01a7336-2c1e78e0291mr55587825ad.4.1780727845062; Fri, 05 Jun 2026 23:37:25 -0700 (PDT) Received: from localhost.localdomain ([2601:600:947f:47d0:d807:4407:d4e8:8895]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2c1664ad138sm116025135ad.82.2026.06.05.23.37.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jun 2026 23:37:24 -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:37:06 -0700 Message-ID: <20260606063706.5844-1-hemparekh1596@gmail.com> X-Mailer: git-send-email 2.50.1 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