Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

lib/scatterlist: fix length calculations in extract_kvec_to_sg

Patch series "Fix bugs in extract_iter_to_sg()", v3.

Fix bugs in the kvec and user variants of extract_iter_to_sg. This series
is growing due to useful remarks made by sashiko.dev.

The main bugs are:
- The length for an sglist entry when extracting from
a kvec can exceed the number of bytes in the page. This
is obviously not intended.
- When extracting a user buffer the sglist is temporarily
used as a scratch buffer for extracted page pointers.
If the sglist already contains some elements this scratch
buffer could overlap with existing entries in the sglist.

The series adds test cases to the kunit_iov_iter test that demonstrate all
of these bugs. Additionally, there is a memory leak fix for the test
itself.

The bugs were orignally introduced into kernel v6.3 where the function
lived in fs/netfs/iterator.c. It was later moved to lib/scatterlist.c in
v6.5. Thus the actual fix is only marked for backports to v6.5+.


This patch (of 5):

When extracting from a kvec to a scatterlist, do not cross page
boundaries. The required length was already calculated but not used as
intended.

Adjust the copied length if the loop runs out of sglist entries without
extracting everything.

While there, return immediately from extract_iter_to_sg if there are no
sglist entries at all.

A subsequent commit will add kunit test cases that demonstrate that the
patch is necessary.

Link: https://lkml.kernel.org/r/20260326214905.818170-1-lk@c--e.de
Link: https://lkml.kernel.org/r/20260326214905.818170-2-lk@c--e.de
Fixes: 018584697533 ("netfs: Add a function to extract an iterator into a scatterlist")
Signed-off-by: Christian A. Ehrhardt <lk@c--e.de>
Cc: David Gow <davidgow@google.com>
Cc: David Howells <dhowells@redhat.com>
Cc: Kees Cook <kees@kernel.org>
Cc: Petr Mladek <pmladek@suse.com>
Cc: <stable@vger.kernel.org> [v6.5+]
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Christian A. Ehrhardt and committed by
Andrew Morton
07b7d66e 86bda539

+3 -2
+3 -2
lib/scatterlist.c
··· 1247 1247 else 1248 1248 page = virt_to_page((void *)kaddr); 1249 1249 1250 - sg_set_page(sg, page, len, off); 1250 + sg_set_page(sg, page, seg, off); 1251 1251 sgtable->nents++; 1252 1252 sg++; 1253 1253 sg_max--; ··· 1256 1256 kaddr += PAGE_SIZE; 1257 1257 off = 0; 1258 1258 } while (len > 0 && sg_max > 0); 1259 + ret -= len; 1259 1260 1260 1261 if (maxsize <= 0 || sg_max == 0) 1261 1262 break; ··· 1410 1409 struct sg_table *sgtable, unsigned int sg_max, 1411 1410 iov_iter_extraction_t extraction_flags) 1412 1411 { 1413 - if (maxsize == 0) 1412 + if (maxsize == 0 || sg_max == 0) 1414 1413 return 0; 1415 1414 1416 1415 switch (iov_iter_type(iter)) {