From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795685890; b=PYgyuY7cKc0wdpmmllGmB6idsuB7dMTbba4tf9OI96+Cm4WOixuWMV2PdSnivjO8ewmN gB2CbFuBk7a0hTqmxEPS7GkZpQ8rXQ+97uxpJvekyWKQSNQBu57nsVHUAZn66OrGh2cf+ EddxIRi562P9RJbXxL95YZ/PuhhYw7FOyugsAXwea+/p2HmZnWyQJkoIAZ6mumdHbyes7 xR0N676CuVNaFYHPOoooNtNiPGQskJEPeQTTq2C/ZFnhcWN6rLfDAgCZacDAf9rdrY1tq EO2GNZhHyW5cL5gJ8cmDSdCYy9axs/g4pzdWDthtJGOsUtFWqt3J3FLqaHWoehzqGkQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795685890; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted: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=rsNqaj+bXc/v5pLCFTeTs+S9z83Xe3DxebqgKuXb2Ic=; b=tG/I9l+epiIOo31iBbCEGMVWsg2Fr/M8w+ZErrhHIMjQcDOzCd/pNecK1VoQAG1Y9w2A s0ER8GvX8Tf5rqT4+6miIwsLbJiQazB3o8GHBM67cSP1WhdGQami5qLN/M43/T1GpHzz3 6Tz0QSWKxLP7SOqeL7WoC6LEtTmQaK/PK396uhPwtzNeyGOBudTIIF3oXYS9uEVYlA1N3 /0XxQHmaLX+lYYswQR/5jBTRK/D55769rpnRADAgIMAPrclNy72h2rl0DM6xI28CCXSKm TqCuxy5mMLKC++3faNyDPX4j3fXT+lYQRwsjBxqW2CGX+m76pLvSCnxGF2r6T5EHXlg== 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=fSw66Vwg; arc=none smtp.remote-ip=74.125.82.51 Received: from mail-dl1-f51.google.com (mail-dl1-f51.google.com [74.125.82.51]) (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 D8953606A68F for ; Thu, 26 Nov 2026 09:38:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D8953606A68F 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=fSw66Vwg Received: by mail-dl1-f51.google.com with SMTP id a92af1059eb24-1b3b4944a0eso5159626c88.0 for ; Thu, 26 Nov 2026 01:38:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795685888; x=1796290688; 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=rsNqaj+bXc/v5pLCFTeTs+S9z83Xe3DxebqgKuXb2Ic=; b=fSw66Vwge0QjmaCrydvktO8QXKG8LEluTa1Zb5fBq+5ZOsYGUtfBQssVIInvAtBD5b 88Qry4se30oJNGuIyCaEyPctUhFLaA6dENbTmM1wna5wk/YOUEkNVAq/7pOk24x4VTqO cWsRFFBIFwDlMT1rdpA7CbvF7NJ8jtU1AWJ0UoJDmOq7NWRUpeQUIcihjzJj9n3fimcE l8xVUlwqGYGDIJybc5iyPIr+CFqz4TB6f4RAiJjAYxdgTQQgBzVTHUxYmyWqGwyXGs+3 iLxaR52q4O/AtS4im9iHZgzxPkEW2yUWSmAOJfTYhjqfnmU4Lgg7/siTjDT0PS5oHVly Lgyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795685888; x=1796290688; 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=rsNqaj+bXc/v5pLCFTeTs+S9z83Xe3DxebqgKuXb2Ic=; b=L9VPkwyuJKTpECIV29rE7JBA3xc2EMu92s2crzEiR/jaG0ckQ1NeuXM0T9Ywd7LJym T/WHfNWFWDXurgUkY7QurW7V3sJlCtJnhAu+VEyqDM65PRqHWvD0+TVV/1/XLqonuwJ+ jYFwQzYuVH/bl0kosp4/wn2hEgNaSXXH7/ykzBLQ+5YSs//LMXHpDy5IwOKAXKDm46ld xqdN29ukUl7zEV62kz4A49r1tOdbpgkahx1luw3ThRUjgER+/su2Gq0hC3Ob5Lekskre P2AMmEF4jyrEWkgsHXAXuHhsjGkIB8PeRP0KYCIlxE9hY1YF0+unyBPzUjyHjm72us3o xIIw== X-Forwarded-Encrypted: i=1; AL+hErHo1XFaTqTM12GiS0FSQI5dXegwWJrDZKVMUvjIj7igdtlA8PmTdGrEOS5A4pLvgiUAhv6/h+Sbh/gNuAo=@vger.kernel.org X-Gm-Message-State: APuU4Z/mC/E5CQ7FuuAPuwKlQ7tN/em0RsgF3TLrt92hz1V7GSkOpcAv Xd0PMgJL+b98BNpTDObx+8ukHmJG6LXxbNNyIZ55YsGD0/+5kle4qkap X-Gm-Gg: AZFdTZc4W4L3OMfC+jHiZyiBNZhYLKdd2D6WU81v9E+K+MmkHcVFrfAO2HSPsu2J6gq tPYP0UtpXwJwmRfE4Ir/IQkziGnp0RVnkOlstn0Mip+53qS0R5orlpun4lk07hlBuHXWNYLDsyS 936ewa21TyuTQjxsxyfV9i30xW+mM8W0Bvh7eitEmVDh8Qf+FdgUldCS/MVw/OEKX3Ot2tsRAVD L339hcXmqya80eDm9c/RLPJ8sgi3txnl6JjdiwLbREUoIwwFDTeOqvola/Ia2/Dq9qirfdXwCq9 heR9BYdUiPL4SwJpxtBiNWEgoeeFkWb4qJet76tAStJjVCVLsrwde5Hxaql+v4VOOKN+L3SHu9f 4CFHxbhNorKZ/tZmlwg6F+3dnt7dJesMVcY3qD/i1SeYk9vtNNP2Mul0QCTt1i4wNHD//W1yqo5 hSdhyuWln0LXvCljudfyvNdoPmGHH1q0F6MnVyv+vPSEAWsvFnx6QkrtlNwUinrADZyvssF6A13 H8bKo4uXQqXnBW63YnePU1kBvxqmqwzBijH+OoJtyfUhwYQ4cZTVAjKLl1Uy5l08nuhIWHBCFDc USFO/RTgaEaWuwSWzLTSZnt6XDgK1XzoKaYgr5oApa7Cxbw3DimrqU1IiN1wQjvnG02dEeaqRt1 tkzdxs5FHXhBR3A== X-Received: by 2002:a05:701b:4502:20b0:152:9e88:daaa with SMTP id a92af1059eb24-1b55690040amr20145598c88.36.1795685887798; Thu, 26 Nov 2026 01:38:07 -0800 (PST) Received: from hyeonjun-ThinkPad-X13-Gen-1.. ([124.197.199.8]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5568592adsm46845363c88.15.2026.11.26.01.38.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Nov 2026 01:38:07 -0800 (PST) From: Hyeonjun Hong To: Coly Li , Kent Overstreet Cc: Jens Axboe , linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org, Hyeonjun Hong , Seojin An , GEONWOO HAN , stable@vger.kernel.org Subject: [PATCH] bcache: fix out-of-bounds write in bch_data_verify() Date: Thu, 26 Nov 2026 18:36:55 +0900 Message-Id: <20261126093655.393608-1-hyeonjunhong330@gmail.com> X-Mailer: git-send-email 2.34.1 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_data_verify() sizes its check bio from bio_segments() of the original read bio, which is an unsigned int. bio_kmalloc() takes an unsigned short, so the count is truncated before bio_kmalloc() compares it against BIO_MAX_INLINE_VECS. A read bio with 65536 segments is truncated to zero, passes the limit check and gets a bio with no bio_vec space. bch_bio_map() then fills in bio_vecs from bi_size without looking at bi_max_vecs and writes far beyond the allocation. A single read bio of this size reaches bcache through an io_uring registered buffer, which the block layer attaches to the bio without splitting. Reading 256 MiB from a cached device with verify enabled on a KASAN/KFENCE kernel gives: BUG: KFENCE: out-of-bounds write in bch_bio_map+0x132/0x420 Out-of-bounds write at 0x00000000cb5c6057 (132B right of kfence-#252): bch_bio_map+0x132/0x420 bch_data_verify+0x3d2/0xc40 cached_dev_read_done+0x6d4/0x890 process_one_work+0xafd/0x1ee0 kfence-#252: 0x000000008ee01c90-0x000000001f8e8fd6, size=120, cache=kmalloc-128 allocated by task 12 on cpu 0 at 63.371786s (0.000577s ago): bio_kmalloc+0x45/0x80 bch_data_verify+0x2be/0xc40 followed by a general protection fault and panic. A 255 MiB read is not affected because its truncated count still exceeds BIO_MAX_INLINE_VECS and bio_kmalloc() returns NULL. The verify path needs CONFIG_BCACHE_DEBUG and the verify attribute to be set through sysfs. Check the segment count before it is narrowed and skip verification of bios that are too large, which is what already happens for every other count above BIO_MAX_INLINE_VECS. AI tools assisted the source audit, reproducer construction and patch drafting. Fixes: 0f2e6ab851ae ("block: turn the nr_iovecs argument to bio_alloc* into an unsigned short") Reported-by: Seojin An Reported-by: GEONWOO HAN Cc: stable@vger.kernel.org Signed-off-by: Hyeonjun Hong --- drivers/md/bcache/debug.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/md/bcache/debug.c b/drivers/md/bcache/debug.c index 2691a5da3368..fa665b4628b1 100644 --- a/drivers/md/bcache/debug.c +++ b/drivers/md/bcache/debug.c @@ -112,6 +112,14 @@ void bch_data_verify(struct cached_dev *dc, struct bio *bio) struct bio_vec bv, cbv; struct bvec_iter iter, citer = { 0 }; + /* + * bio_kmalloc() takes an unsigned short, so a larger segment count + * would be truncated before its own limit check. Skip verification + * of bios which cannot be represented. + */ + if (nr_segs > BIO_MAX_INLINE_VECS) + return; + check = bio_kmalloc(nr_segs, GFP_NOIO); if (!check) return;