From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795836975; b=upL4IR5N5x1DFN4TzVaUleQcOJRKOKzazO1/aG8xccEn5V5henImgX8nVvVahHC0etb9 r4SiXPa+Uv+bjf8UZzOBepuf+AM48BfnHB9Vpm7doZCTv6De46Mg4q0BKjWR0nK+1Xuep VCEExP67TsPXPLyAkxz7siIUqsgX8mG0+/FRNZHTPLDJX+MvxkHbhv5WM78D6NLGIU3R7 Q5/F6DE04Ot+eUxV+BOeZSORQ6xl2+j6BKp/kc54fDcbwSBCpl9fozDd+u72us9L9EtSL D+EcLqRF2cbOtTBFmYeUA+PWwunyat1RI1ZqYPQIjLt5B1d8pK6UbI619gHfl6U+NKQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795836975; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=zpen4d1kkqo7HxjfJn3mlnlDWjSkeh8lYu4nl7L7jCU=; b=yEaV7zJGvzxSU1rDLqa/7ztQSisETrChY4J/k+vn+Ohz1rLk7eGrdoLdyk6k5OE+6UMf ptIcztSvxj1Cyxoi7MkXh3pb7AbD8tIZTd8iECBPMTgvvFJIzLRET/OWlpWDjyRfBjiqZ g7vW8hwxc1gNvGb+kGdIyAlpWKkBqBE52iFL4Xr69bQwTC4BotBNryQVUQIrRCgALGeBe 6EvCQBWK26vwjgUAPMdY1nFYR3hDtSbtgrSgfoTgnCttTyt3q6r9hMYXGWtP6iMsLVAg3 t6a/5DdukrkjCC+D41etp7O4Q24MnEMaCKJJy0TW1KsOvVw9y9OZX6AsV3pxHOXgKLw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=grJoWkjR; arc=none smtp.remote-ip=74.125.82.49 Received: from mail-dl1-f49.google.com (mail-dl1-f49.google.com [74.125.82.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB4D7606A31F for ; Sat, 28 Nov 2026 03:36:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EB4D7606A31F 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.a=rsa-sha256 header.s=20260707 header.b=grJoWkjR Received: by mail-dl1-f49.google.com with SMTP id a92af1059eb24-170135e43c7so13527354c88.0 for ; Fri, 27 Nov 2026 19:36:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795836974; x=1796441774; 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:content-type; bh=zpen4d1kkqo7HxjfJn3mlnlDWjSkeh8lYu4nl7L7jCU=; b=grJoWkjROdHFwhnP7DYe2eHRpWDyKsyWAmEBV4wgSBiy/r6+1wUx4M9gHG4uG+KDlY 62vdweFhtIafSHfPS8Z4TZIHcNK/GHkoGW0fvOS/oqln/2czexKSGtN7rGTtqAMW4XRX 8bpTFjwECr2h+cE+ZkaNuAgA4KQmTJCLAv4w311b8XHsmtfuMLfTDu8ly3xrrXoSIxeZ 4GVckzkrRBaOI/f5T8+ht8TpHDISkCQiK6BGj3/AmN2V522lrQK8y2QJ1JgEV58ucfxH UlEdm5Tc/VXol4FGpLOb74YHBDO4xdEBDrQjY3p/NW4O3cRpuMB7OKc2D/4Nz9TcynqG 8WkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795836974; x=1796441774; 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:content-type; bh=zpen4d1kkqo7HxjfJn3mlnlDWjSkeh8lYu4nl7L7jCU=; b=lfCComjFOmxWMnCPZ4iV9c6jnDXqVjjroEZmsnP98NrdSgRwxGD6ah8WPnEVgpVOXh 0J5cyXjlDrNZEWxgHhIitz7ksXNbOrW6I4aD0NEd/teR75yEXGPFLmk08JkgeJDPP+wk +coCqmfTSvVUWvMGLrfvPNM/bUdIvt4gSN7itv+HJS6Xi6Bk+wIUJD2PIQahzomNe2Tm E1TW/MVCbXoO+aQiCE6AURbKe/RRT0ZgfUam1dDdayuElUu2ECYLyIQelNl00OEp9MFI +85eKeDFuWRlt6fpLzYpBoKnZZX1/6+Q2dih8Qiyoqdw5M9dQlvBWex+KUC+OqkWG8OP PJHg== X-Gm-Message-State: APuU4Z/e3I6tLxZOeESEpgIQC5r6JtrcpAJv56hTvPm9SK9Mnh/P/RTa /6gcjdAjxI81HvaYiTMZ/MpPKaNEXutlyEs0BQglwtHmnhkh1KiaHuMwjwlM9ubm X-Gm-Gg: AZFdTZcE/5BrLGWGL1OOyzq3rvAoqtiy2B3O9r3+3+kH+eGSfnX7IWomYSYWEKCxIrZ 488Hy+LunbSFp2q0a8t/FF2/TEyNiZQ68zItP4Sn5uIvbCnsJ0LOG5ln3a28GoGr7SveXr+xX6t zTnAYlF6jkY81geknWdxM7W4mskqNqXKucqP/KdFprPHXFqV8HpEtK3iB9RKGb9dBilDKUQ4Ozv L8QUTpEmzNo+Hg59ummVZSrylRhg03Y51ub3kSv6Az2/yxTZwjwgIbPW4io/c25at7op0Dnp48U NuY37tx7JpDOFwnfas75L7pHtSE2M2qXVLJk3zWxQroxiZhrYGkMhsU5urmhPUpnGK6XUot/kJb Dx82OYBkcIPY8QIFb+a8Oaisn9R8WR8v/b1AxYuqEhIS5sJQhCn1QSBdWZKn6rzKRo5rPqKwRWT Su0g3kzpd+CGA6ffwH/2GkllqZtmHyONmFKQchJhcnHQbvlCGN6x72FxLMvHoEuvXDx2iI/EESQ 11M+dkIWw3znvnmlmR2XuCw/CumIXLISklke7f5u0ZAA/MfBiewv81HzFOubmRADh/gniOLHjtI WgWDPEWEGRwruUTuOuxVtn8ku2ZwQ4Do7m7mQ2dZmg4zhcmlwBByNJIE0BFwvkdWF/XQwST/epf o78GTtutsifx6cqIM7YXNL9ea41TrJB2lhnuvcEIERlGXQnsnew== X-Received: by 2002:a05:701b:4308:b0:165:aced:1fde with SMTP id a92af1059eb24-1b56f87a910mr17234011c88.0.1795836973825; Fri, 27 Nov 2026 19:36:13 -0800 (PST) Received: from localhost.localdomain ([2601:646:4300:d042:5070:1d3d:1d30:e241]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b55682d301sm62036381c88.10.2026.11.27.19.36.13 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 27 Nov 2026 19:36:13 -0800 (PST) From: Ozgun Kultekin To: colyli@fygo.io, kent.overstreet@linux.dev Cc: linux-bcache@vger.kernel.org, Ozgun Kultekin , stable@vger.kernel.org Subject: [PATCH] bcache: don't read more than the buffer holds for a btree node or the uuids Date: Fri, 27 Nov 2026 19:36:09 -0800 Message-ID: <20261128033609.42213-1-ozgunkultekin@gmail.com> X-Mailer: git-send-email 2.56.0 X-Mailing-List: linux-bcache@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit bch_btree_node_read() sets the bio size to KEY_SIZE(&b->key) << 9 and maps it onto the node buffer, which is PAGE_SIZE << b->keys.page_order bytes. page_order is ilog2(KEY_SIZE / PAGE_SECTORS), so it rounds down, and nothing checks that the key fits in the buffer. For example a 1023 sector btree pointer in a 1024 sector bucket passes __bch_btree_ptr_invalid(), gets a 256 KiB buffer, and the read runs past the end of it. __ptr_invalid() also skips the bucket_size check for pointers whose device index is >= MAX_CACHES_PER_SET, while bch_submit_bbio() always reads from c->cache. Such a key can be up to 65535 sectors, which is larger than both the node buffer and the bvecs of the bbio. uuid_io() has the same shape: it reads KEY_SIZE(k) << 9 bytes of jset->uuid_bucket into c->uuids, which is one meta bucket. The btree root and uuid keys are read from the journal, so a corrupted on-disk key makes these reads overrun their buffers before any node checksum is checked. Reject a btree pointer larger than a bucket regardless of its device index, fail a node read whose key is larger than the node buffer or the bbio, and reject a uuid pointer larger than a meta bucket. Keys that bcache writes itself are btree_pages * PAGE_SECTORS and meta_bucket_pages() * PAGE_SECTORS sectors on device 0, so they are not affected. Fixes: cafe56359144 ("bcache: A block layer cache") Cc: stable@vger.kernel.org Signed-off-by: Ozgun Kultekin --- drivers/md/bcache/btree.c | 13 +++++++++++++ drivers/md/bcache/extents.c | 7 +++++++ drivers/md/bcache/super.c | 4 +++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c index 27a129d47d0a..bbff98661494 100644 --- a/drivers/md/bcache/btree.c +++ b/drivers/md/bcache/btree.c @@ -248,6 +248,19 @@ static void bch_btree_node_read(struct btree *b) trace_bcache_btree_read(b); + /* + * KEY_SIZE() comes from disk. Don't read more than the node buffer + * holds, or than a bbio has bvecs for. + */ + if (KEY_SIZE(&b->key) > PAGE_SECTORS << b->keys.page_order || + KEY_SIZE(&b->key) > meta_bucket_pages(&b->c->cache->sb) * PAGE_SECTORS) { + set_btree_node_io_error(b); + bch_cache_set_error(b->c, "btree node too big at bucket %zu: %llu sectors", + PTR_BUCKET_NR(b->c, &b->key, 0), + KEY_SIZE(&b->key)); + return; + } + closure_init_stack(&cl); bio = bch_bbio_alloc(b->c); diff --git a/drivers/md/bcache/extents.c b/drivers/md/bcache/extents.c index d626ffcbecb9..9ddc90049623 100644 --- a/drivers/md/bcache/extents.c +++ b/drivers/md/bcache/extents.c @@ -153,6 +153,13 @@ bool __bch_btree_ptr_invalid(struct cache_set *c, const struct bkey *k) if (!KEY_PTRS(k) || !KEY_SIZE(k) || KEY_DIRTY(k)) goto bad; + /* + * __ptr_invalid() skips pointers to a device index >= MAX_CACHES_PER_SET, + * but btree nodes and uuids are always read from c->cache. + */ + if (KEY_SIZE(k) > c->cache->sb.bucket_size) + goto bad; + if (__ptr_invalid(c, k)) goto bad; diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c index 97d9adb0bf96..fd35e4b76fca 100644 --- a/drivers/md/bcache/super.c +++ b/drivers/md/bcache/super.c @@ -458,7 +458,9 @@ static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl) { struct bkey *k = &j->uuid_bucket; - if (__bch_btree_ptr_invalid(c, k)) + /* c->uuids and the bbio hold one meta bucket */ + if (__bch_btree_ptr_invalid(c, k) || + KEY_SIZE(k) > meta_bucket_pages(&c->cache->sb) * PAGE_SECTORS) return "bad uuid pointer"; bkey_copy(&c->uuid_bucket, k); -- 2.39.5