mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Charles Pellegrini <c4ffein.work@gmail.com>
To: akpm@linux-foundation.org, egorenar-dev@posteo.net,
	robert.jarzmik@free.fr, t-pratham@ti.com, david@davidgow.net,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 4/5] lib: scatterlist: guard sg_split_phys()/sg_split_mapped() against zero-nents splits
Date: Wed, 10 Jun 2026 15:39:35 -0700 (PDT)	[thread overview]
Message-ID: <178113124324.90620.1158417223990002175@gmail.com> (raw)
In-Reply-To: <178113124323.90620.6403136846887207198@gmail.com>

If a caller passes a split_sizes[] array whose trailing entry is 0 and
the preceding splits exactly consume the input scatterlist,
sg_calculate_split() returns success with the trailing split's nents
left at 0. sg_split() then allocates that split's output with

    kmalloc_objs(struct scatterlist, 0, gfp_mask)

which returns ZERO_SIZE_PTR. Both copiers finish each split with a fixup of
the last written output entry -- sg_split_phys() does

    out_sg[-1].length = split->length_last_sg;
    sg_mark_end(out_sg - 1);

and sg_split_mapped() does

    sg_dma_len(--out_sg) = split->length_last_sg;

For a zero-nents split out_sg has not advanced, so that fixup dereferences
ZERO_SIZE_PTR: an out-of-bounds write. KASAN reports it as a slab
out-of-bounds write; a userspace ASAN harness reproduces it as a heap
overflow.

Skip empty splits at the top of the per-split loop in both copiers:

    if (!split->nents)
        continue;

No in-tree caller passes such a split_sizes[] array, so this is latent; it
was surfaced by an automated review of the v1 posting. The fix tolerates
and skips the degenerate split rather than rejecting it with -EINVAL in
sg_calculate_split(); if a stricter contract is preferred, the rejecting
variant is a trivial respin.

Fixes: f8bcbe62acd0 ("lib: scatterlist: add sg splitting function")
Link: https://lore.kernel.org/all/20260601175549.b4a10c07dd9e3b867f66da0c@linux-foundation.org/
Assisted-by: Claude:claude-opus-4-8 hegel-c
Signed-off-by: Charles Pellegrini <c4ffein.work@gmail.com>
---
 lib/sg_split.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/lib/sg_split.c b/lib/sg_split.c
index ab574dd26b03..e90a1bfbdb56 100644
--- a/lib/sg_split.c
+++ b/lib/sg_split.c
@@ -81,6 +81,8 @@ static void sg_split_phys(struct sg_splitter *splitters, const int nb_splits)
 	struct sg_splitter *split;
 
 	for (i = 0, split = splitters; i < nb_splits; i++, split++) {
+		if (!split->nents)
+			continue;
 		in_sg = split->in_sg0;
 		out_sg = split->out_sg;
 		for (j = 0; j < split->nents; j++, out_sg++) {
@@ -108,6 +110,8 @@ static void sg_split_mapped(struct sg_splitter *splitters, const int nb_splits)
 	struct sg_splitter *split;
 
 	for (i = 0, split = splitters; i < nb_splits; i++, split++) {
+		if (!split->nents)
+			continue;
 		in_sg = split->in_sg0;
 		out_sg = split->out_sg;
 		for (j = 0; j < split->nents; j++, out_sg++) {
-- 
2.47.3


      parent reply	other threads:[~2026-06-10 22:39 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-31 23:42 [PATCH 0/2] lib: scatterlist: fix sg_split() partial-coverage geometry + add KUnit tests Charles Pellegrini
2026-05-31 23:42 ` [PATCH 1/2] lib: scatterlist: fix sg_calculate_split() nb_splits overshoot on partial coverage Charles Pellegrini
2026-05-31 23:42 ` [PATCH 2/2] lib: scatterlist: add KUnit tests for sg_split() Charles Pellegrini
2026-06-02  0:55 ` [PATCH 0/2] lib: scatterlist: fix sg_split() partial-coverage geometry + add KUnit tests Andrew Morton
2026-06-10 22:35   ` c4ffein.work
2026-06-10 22:39 ` [PATCH v2 0/5] lib: scatterlist: fix sg_split() partial-coverage geometry, two latent corruption bugs, and " Charles Pellegrini
2026-06-10 22:39   ` [PATCH v2 1/5] lib: scatterlist: fix sg_calculate_split() nb_splits overshoot on partial coverage Charles Pellegrini
2026-06-10 22:39   ` [PATCH v2 2/5] lib: scatterlist: fix sg_split_phys() ->length clobber on !NEED_SG_DMA_LENGTH Charles Pellegrini
2026-06-10 22:39   ` [PATCH v2 3/5] lib: scatterlist: add KUnit tests for sg_split() Charles Pellegrini
2026-06-10 22:39   ` [PATCH v2 5/5] lib: scatterlist: add zero-nents regression test " Charles Pellegrini
2026-06-10 22:39   ` Charles Pellegrini [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=178113124324.90620.1158417223990002175@gmail.com \
    --to=c4ffein.work@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@davidgow.net \
    --cc=egorenar-dev@posteo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robert.jarzmik@free.fr \
    --cc=t-pratham@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome