From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.127.231.46 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796052448; b=sXCxPhSuedVNBfcFWN7Pb/uaurKIuh32QQbMzb+KuPazzhEDL/EycOGFSEa2FBZt/DPw DdcyiBZNaIOe1FGK3besIvrS8GNKk5YPMu+4tYPvh8dnQKC1vAKMdIskqV19ay2xGkJrp KBlKLQbEArVJ7JkGRsh0lWCwE4gdrWSuysv3W7JmzAaMb4+yv9HVHrJRZfI0Dcr6RaiAR uIFnZbyCMmX3pKv6oXzORUmKODRsuF1vkXRoYUvH9q7KvIX+CuoGL5jvDWwnI/ZFLgxSD I8z3nh3eNaL5pg5HNS2JjAV43CeNRjlkEMI3AQt2tn8Ph111Dqj6g/YwP9zYZ7bJPkg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796052448; h=DMARC-Filter:DKIM-Signature:Subject:Date:Message-Id:Content-Type: References:To:From:Mime-Version:Content-Disposition:Cc:In-Reply-To: X-Original-From:X-Lms-Return-Path:Received:Content-Transfer-Encoding; bh=V1cgUrVuJdfgMIaUEZpIvgUaPXMveSIwUb1YNA2stII=; b=tZgZiwuitDDXAwGzv25nKyaTnpp9WyHyV310zr7wHF3UyNv/kAtNDC77aqfAakoG4auN nawxNNUs7p78kSildvH+lP3Kp65cFoAIMvx2MduVn1wjAr7kPp2VYgZVnmv1XMtembVV0 znRrv9EVfVUtTRZDvnk7NY35cuZk4fsXVcPOVFdg85cHFEM9A/fDOYl5D2oUV9X8p0ny1 fNdObGw9yXLQmD7fL7hGV/C+2684OgjWyyE3gTFFb3fa/vajGUY7NkJK90lDE8WowOmWu 24M8QFf0xZVJ1/yQPjzpJARBpbCPjeC0b+gtvZowrdsO8V+vepSkPv/e7bIp7C5coNg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=fygo.io; spf=pass smtp.mailfrom=fygo.io; dkim=pass header.d=fygo-io.20200929.dkim.larksuite.com header.i=@fygo-io.20200929.dkim.larksuite.com header.a=rsa-sha256 header.s=s1 header.b=blMuwCE2; arc=none smtp.remote-ip=209.127.231.46 Received: from va-2-46.ptr.blmpb.com (va-2-46.ptr.blmpb.com [209.127.231.46]) (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 AC25F606A31C for ; Mon, 30 Nov 2026 15:27:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AC25F606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fygo.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fygo.io Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=fygo-io.20200929.dkim.larksuite.com header.i=@fygo-io.20200929.dkim.larksuite.com header.a=rsa-sha256 header.s=s1 header.b=blMuwCE2 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=s1; d=fygo-io.20200929.dkim.larksuite.com; t=1796052440; h=from:subject:mime-version:from:date:message-id:subject:to:cc: reply-to:content-type:mime-version:in-reply-to:message-id; bh=V1cgUrVuJdfgMIaUEZpIvgUaPXMveSIwUb1YNA2stII=; b=blMuwCE29CkuKyYjxP6g43ZvS/sfhOg/xRWnemfRkQIj7usDxXQgBBY0ULGXyufI7XVnHG ZsyAGjTVisLglLVuXNufFcP1Zb/nMqml+OD5wlhouy+oFnuiuDRWcmXptPHcsdvceegj0X 5cZAI2VRiOkJ4CRYIiV7VqV1FOGzY5Jm6mAfzbtehHOrtoTIJDyCD38VEro3QAIh7oOTSS umYtZPD1B5PdY0IiD2KeHyRLALeVg61VIo4ulwCLWKQDdVNiK2Vn3skVNWF9sHRaBukQRY co2iD3J7+AHRtyMaQEo+c7jXcstOEtW+gJmYuvsjvv3AU242umvDEOMURpdXKQ== Subject: Re: [PATCH] bcache: don't read more than the buffer holds for a btree node or the uuids Date: Mon, 30 Nov 2026 23:27:16 +0800 Message-Id: Content-Type: text/plain; charset=UTF-8 References: <20261128033609.42213-1-ozgunkultekin@gmail.com> To: "Ozgun Kultekin" From: "Coly Li" 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-Disposition: inline Cc: , , In-Reply-To: <20261128033609.42213-1-ozgunkultekin@gmail.com> X-Original-From: Coly Li X-Lms-Return-Path: Received: from studio.local ([120.245.64.92]) by smtp.larksuite.com with ESMTPS; Mon, 30 Nov 2026 15:27:18 +0000 Content-Transfer-Encoding: 7bit Hi Ozgun, On Fri, Nov 27, 2026 at 07:36:09PM +0800, Ozgun Kultekin wrote: > 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. > My question is, how can the above condition happens? I mean "a 1023 sector btree pointer in a 1024 sector bucket". Firstly you need to provide the detail code location where such circomstances can happen in bcache code. Otherwide I don't take this as a valid defact. > __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. > Same here, before solve a problem, I want to see how the problem may take place by the current code or reasonable configuration. Some situations will be treated as invalid by me, e.g. deliberatedly compose a hash collision in journal checksum with a valid KEY value. We cannot avoid such deliberated on-media hash colission, and won't fix the addressed condition of this patch in kernel code like bcache. > 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. > So before you fix this 'problem', I want to see how such issue may take place in real live. Thanks. Coly Li > 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