* [PATCH] squashfs: fix NULL pointer dereference in bio_alloc_clone failure path
@ 2025-05-15 12:01 caius.zone
2025-05-15 12:41 ` Matthew Wilcox
0 siblings, 1 reply; 2+ messages in thread
From: caius.zone @ 2025-05-15 12:01 UTC (permalink / raw)
To: phillip; +Cc: linux-fsdevel, linux-kernel, Caius Zone
From: Caius Zone <zone@ai-sast.com>
bio_alloc_clone() may return NULL under memory pressure. The current code
does not check its return value, which may lead to a NULL pointer dereference
in bio_chain() or other bio operations.
Add a NULL check and return -ENOMEM if allocation fails.
Signed-off-by: Caius Zone <zone@ai-sast.com>
---
fs/squashfs/block.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/fs/squashfs/block.c b/fs/squashfs/block.c
index 2dc730800f44..b00a71f8933c 100644
--- a/fs/squashfs/block.c
+++ b/fs/squashfs/block.c
@@ -115,6 +115,9 @@ static int squashfs_bio_read_cached(struct bio *fullbio,
struct bio *new = bio_alloc_clone(bdev, fullbio,
GFP_NOIO, &fs_bio_set);
+ if (!new)
+ return -ENOMEM;
+
if (bio) {
bio_trim(bio, start_idx * PAGE_SECTORS,
(end_idx - start_idx) * PAGE_SECTORS);
--
2.25.1
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] squashfs: fix NULL pointer dereference in bio_alloc_clone failure path
2025-05-15 12:01 [PATCH] squashfs: fix NULL pointer dereference in bio_alloc_clone failure path caius.zone
@ 2025-05-15 12:41 ` Matthew Wilcox
0 siblings, 0 replies; 2+ messages in thread
From: Matthew Wilcox @ 2025-05-15 12:41 UTC (permalink / raw)
To: caius.zone; +Cc: phillip, linux-fsdevel, linux-kernel, Caius Zone
On Thu, May 15, 2025 at 08:01:54PM +0800, caius.zone@icloud.com wrote:
> diff --git a/fs/squashfs/block.c b/fs/squashfs/block.c
> index 2dc730800f44..b00a71f8933c 100644
> --- a/fs/squashfs/block.c
> +++ b/fs/squashfs/block.c
> @@ -115,6 +115,9 @@ static int squashfs_bio_read_cached(struct bio *fullbio,
> struct bio *new = bio_alloc_clone(bdev, fullbio,
> GFP_NOIO, &fs_bio_set);
>
> + if (!new)
> + return -ENOMEM;
> +
> if (bio) {
> bio_trim(bio, start_idx * PAGE_SECTORS,
> (end_idx - start_idx) * PAGE_SECTORS);
This is definitely wrong. The check for 'new' being NULL should be
after the submit_bio() otherwise we'll leak the previous bio.
But then we need to not call bio_chain() ... so I think this ends up
looking like:
@@ -115,12 +115,15 @@ static int squashfs_bio_read_cached(struct bio *fullbio,
struct bio *new = bio_alloc_clone(bdev, fullbio,
GFP_NOIO, &fs_bio_set);
+ if (new)
+ bio_chain(bio, new);
if (bio) {
bio_trim(bio, start_idx * PAGE_SECTORS,
(end_idx - start_idx) * PAGE_SECTORS);
- bio_chain(bio, new);
submit_bio(bio);
}
+ if (!new)
+ return -ENOMEM;
bio = new;
start_idx = idx;
... but that might not be right either. We might need to break out of
the loop, rather than returning and do the submit_bio_wait() followed by
the bio_put().
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-05-15 12:41 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-15 12:01 [PATCH] squashfs: fix NULL pointer dereference in bio_alloc_clone failure path caius.zone
2025-05-15 12:41 ` Matthew Wilcox
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®