Skip to contentSkip to conversation

fs: add NULL check in drop_buffers() to prevent null-ptr-deref

PATCH In review Deepakkumar Karn 3 participants 11 messages Dec 8, 2025 – Dec 11, 2025 VFS

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fs: add NULL check in drop_buffers() to prevent null-ptr-deref

drop_buffers() dereferences the buffer_head pointer returned by folio_buffers() without checking for NULL. This leads to a null pointer dereference when called from try_to_free_buffers() on a folio with no buffers attached. This happens when filemap_release_folio() is called on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but without release_folio address_space operation defined. In such case, folio_needs_release() returns true because of AS_RELEASE_ALWAYS flag, the folio has no private buffer data, causing the try_to_free_buffers() with a folio that has no buffers.

Adding NULL check for the buffer_head pointer and return false early if no buffers are attached to the folio.

Reported-by [email protected]
Fixes 6439476311a6 ("fs: Convert drop_buffers() to use a folio")
---
 fs/buffer.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/buffer.c +4 -0
index 838c0c571022..fa5de0cdf540 100644
@@ -2893,6 +2893,10 @@ drop_buffers(struct folio *folio, struct buffer_head **buffers_to_free)
 	struct buffer_head *head = folio_buffers(folio);
 	struct buffer_head *bh;
 
+	/* In cases of folio without buffer_head*/
+	if (!head)
+		return false;
+
 	bh = head;
 	do {
 		if (buffer_busy(bh))
Signature · 1 line
2.52.0
#1 lore raw
Quoted 6 lines · Deepakkumar Karn

drop_buffers() dereferences the buffer_head pointer returned by folio_buffers() without checking for NULL. This leads to a null pointer dereference when called from try_to_free_buffers() on a folio with no buffers attached. This happens when filemap_release_folio() is called on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but without release_folio address_space operation defined. In such case,

What user is that? All the users of AS_RELEASE_ALWAYS in 6.18 appear to supply a ->release_folio. Is this some new thing in 6.19?

--D

Quoted 34 lines

folio_needs_release() returns true because of AS_RELEASE_ALWAYS flag, the folio has no private buffer data, causing the try_to_free_buffers() with a folio that has no buffers.

Adding NULL check for the buffer_head pointer and return false early if no buffers are attached to the folio.

Reported-by [email protected]
Fixes 6439476311a6 ("fs: Convert drop_buffers() to use a folio")
---
 fs/buffer.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/buffer.c +4 -0
index 838c0c571022..fa5de0cdf540 100644
@@ -2893,6 +2893,10 @@ drop_buffers(struct folio *folio, struct buffer_head **buffers_to_free)
 	struct buffer_head *head = folio_buffers(folio);
 	struct buffer_head *bh;
 
+	/* In cases of folio without buffer_head*/
+	if (!head)
+		return false;
+
 	bh = head;
 	do {
 		if (buffer_busy(bh))
Signature · 1 line
2.52.0
#2 lore raw
Quoted 9 lines · Darrick J. Wong
Quoted 6 lines

drop_buffers() dereferences the buffer_head pointer returned by folio_buffers() without checking for NULL. This leads to a null pointer dereference when called from try_to_free_buffers() on a folio with no buffers attached. This happens when filemap_release_folio() is called on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but without release_folio address_space operation defined. In such case,

What user is that? All the users of AS_RELEASE_ALWAYS in 6.18 appear to supply a ->release_folio. Is this some new thing in 6.19?

AFS directories SET AS_RELEASE_ALWAYS but have not .release_folio.

reviewer
#3 lore raw
Quoted 8 lines · Deepakkumar Karn
Quoted 6 lines · Darrick J. Wong
Quoted 6 lines

drop_buffers() dereferences the buffer_head pointer returned by folio_buffers() without checking for NULL. This leads to a null pointer dereference when called from try_to_free_buffers() on a folio with no buffers attached. This happens when filemap_release_folio() is called on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but without release_folio address_space operation defined. In such case,

Quoted 4 lines
Quoted 2 lines

What user is that? All the users of AS_RELEASE_ALWAYS in 6.18 appear to supply a ->release_folio. Is this some new thing in 6.19?

AFS directories SET AS_RELEASE_ALWAYS but have not .release_folio.

AFAICS AFS sets AS_RELEASE_ALWAYS only for symlinks but not for directories? Anyway I agree AFS symlinks will have AS_RELEASE_ALWAYS but no .release_folio callback. And this looks like a bug in AFS because AFAICT there's no point in setting AS_RELEASE_ALWAYS when you don't have .release_folio callback. Added relevant people to CC.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw
Quoted 15 lines · Jan Kara
Quoted 8 lines · Deepakkumar Karn
Quoted 6 lines · Darrick J. Wong
Quoted 6 lines

drop_buffers() dereferences the buffer_head pointer returned by folio_buffers() without checking for NULL. This leads to a null pointer dereference when called from try_to_free_buffers() on a folio with no buffers attached. This happens when filemap_release_folio() is called on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but without release_folio address_space operation defined. In such case,

Quoted 4 lines
Quoted 2 lines

What user is that? All the users of AS_RELEASE_ALWAYS in 6.18 appear to supply a ->release_folio. Is this some new thing in 6.19?

AFS directories SET AS_RELEASE_ALWAYS but have not .release_folio.

Quoted 7 lines

AFAICS AFS sets AS_RELEASE_ALWAYS only for symlinks but not for directories? Anyway I agree AFS symlinks will have AS_RELEASE_ALWAYS but no .release_folio callback. And this looks like a bug in AFS because AFAICT there's no point in setting AS_RELEASE_ALWAYS when you don't have .release_folio callback. Added relevant people to CC.

Honza

Thank you for your response Jan. As you suggested, the bug is in AFS. Can we include this current defensive check in drop_buffers() and I can submit another patch to handle that bug of AFS we discussed?

Regards,
Deepakkumar Karn

reviewer
#5 lore raw
Quoted 22 lines · Deepak Karn
Quoted 21 lines · Jan Kara
Quoted 8 lines · Deepakkumar Karn
Quoted 6 lines · Darrick J. Wong
Quoted 6 lines

drop_buffers() dereferences the buffer_head pointer returned by folio_buffers() without checking for NULL. This leads to a null pointer dereference when called from try_to_free_buffers() on a folio with no buffers attached. This happens when filemap_release_folio() is called on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but without release_folio address_space operation defined. In such case,

Quoted 4 lines
Quoted 2 lines

What user is that? All the users of AS_RELEASE_ALWAYS in 6.18 appear to supply a ->release_folio. Is this some new thing in 6.19?

AFS directories SET AS_RELEASE_ALWAYS but have not .release_folio.

AFAICS AFS sets AS_RELEASE_ALWAYS only for symlinks but not for directories? Anyway I agree AFS symlinks will have AS_RELEASE_ALWAYS but no .release_folio callback. And this looks like a bug in AFS because AFAICT there's no point in setting AS_RELEASE_ALWAYS when you don't have .release_folio callback. Added relevant people to CC.

Quoted 5 lines
Quoted 1 line

Honza

Thank you for your response Jan. As you suggested, the bug is in AFS. Can we include this current defensive check in drop_buffers() and I can submit another patch to handle that bug of AFS we discussed?

I'm not strongly opposed to that (although try_to_free_buffers() would seem like a tad bit better place) but overall I don't think it's a great idea as it would hide bugs. But perhaps with WARN_ON_ONCE() (to catch sloppy programming) it would be a sensible hardening.

Also I think mapping_set_release_always() should assert that mapping->a_ops->release_folio is non-NULL to catch the problem early (once you fix the AFS bug).

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#6 lore raw
Quoted 26 lines · Jan Kara
Quoted 24 lines · Deepak Karn
Quoted 22 lines · Jan Kara
Quoted 8 lines · Deepakkumar Karn
Quoted 6 lines · Darrick J. Wong
Quoted 6 lines

drop_buffers() dereferences the buffer_head pointer returned by folio_buffers() without checking for NULL. This leads to a null pointer dereference when called from try_to_free_buffers() on a folio with no buffers attached. This happens when filemap_release_folio() is called on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but without release_folio address_space operation defined. In such case,

Quoted 4 lines
Quoted 2 lines

What user is that? All the users of AS_RELEASE_ALWAYS in 6.18 appear to supply a ->release_folio. Is this some new thing in 6.19?

AFS directories SET AS_RELEASE_ALWAYS but have not .release_folio.

AFAICS AFS sets AS_RELEASE_ALWAYS only for symlinks but not for directories? Anyway I agree AFS symlinks will have AS_RELEASE_ALWAYS but no .release_folio callback. And this looks like a bug in AFS because AFAICT there's no point in setting AS_RELEASE_ALWAYS when you don't have .release_folio callback. Added relevant people to CC.

Honza

Quoted 9 lines
Quoted 3 lines

Thank you for your response Jan. As you suggested, the bug is in AFS. Can we include this current defensive check in drop_buffers() and I can submit another patch to handle that bug of AFS we discussed?

I'm not strongly opposed to that (although try_to_free_buffers() would seem like a tad bit better place) but overall I don't think it's a great idea as it would hide bugs. But perhaps with WARN_ON_ONCE() (to catch sloppy programming) it would be a sensible hardening.

Thanks Jan for your response. As suggested, adding WARN_ON_ONCE() will be more sensible.
I just wanted to clarify my understanding, you are suggesting adding WARN_ON_ONCE()
in try_to_free_buffers() as this highlights the issue and also solves the concern. If my
understanding is wrong please let me know, I will share the updated patch.

Quoted 4 lines

Also I think mapping_set_release_always() should assert that mapping->a_ops->release_folio is non-NULL to catch the problem early (once you fix the AFS bug).

I will address this in another patch for AFS.

Regards,
Deepakkumar Karn

reviewer
#7 lore raw
Quoted 37 lines · Deepak Karn
Quoted 26 lines · Jan Kara
Quoted 24 lines · Deepak Karn
Quoted 22 lines · Jan Kara
Quoted 8 lines · Deepakkumar Karn
Quoted 6 lines · Darrick J. Wong
Quoted 6 lines

drop_buffers() dereferences the buffer_head pointer returned by folio_buffers() without checking for NULL. This leads to a null pointer dereference when called from try_to_free_buffers() on a folio with no buffers attached. This happens when filemap_release_folio() is called on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but without release_folio address_space operation defined. In such case,

Quoted 4 lines
Quoted 2 lines

What user is that? All the users of AS_RELEASE_ALWAYS in 6.18 appear to supply a ->release_folio. Is this some new thing in 6.19?

AFS directories SET AS_RELEASE_ALWAYS but have not .release_folio.

AFAICS AFS sets AS_RELEASE_ALWAYS only for symlinks but not for directories? Anyway I agree AFS symlinks will have AS_RELEASE_ALWAYS but no .release_folio callback. And this looks like a bug in AFS because AFAICT there's no point in setting AS_RELEASE_ALWAYS when you don't have .release_folio callback. Added relevant people to CC.

Honza

Quoted 9 lines
Quoted 3 lines

Thank you for your response Jan. As you suggested, the bug is in AFS. Can we include this current defensive check in drop_buffers() and I can submit another patch to handle that bug of AFS we discussed?

I'm not strongly opposed to that (although try_to_free_buffers() would seem like a tad bit better place) but overall I don't think it's a great idea as it would hide bugs. But perhaps with WARN_ON_ONCE() (to catch sloppy programming) it would be a sensible hardening.

Quoted 6 lines

Thanks Jan for your response. As suggested, adding WARN_ON_ONCE() will be more sensible.
I just wanted to clarify my understanding, you are suggesting adding WARN_ON_ONCE() in try_to_free_buffers() as this highlights the issue and also solves the concern. If my understanding is wrong please let me know, I will share the updated patch.

Yes, I meant something like:

	if (WARN_ON_ONCE(!folio_buffers(folio)))
		return true;

in try_to_free_buffers().

Quoted 6 lines
Quoted 4 lines

Also I think mapping_set_release_always() should assert that mapping->a_ops->release_folio is non-NULL to catch the problem early (once you fix the AFS bug).

I will address this in another patch for AFS.

Thanks. As a separate patch please :)

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#8 lore raw
Quoted 46 lines · Jan Kara
Quoted 37 lines · Deepak Karn
Quoted 26 lines · Jan Kara
Quoted 24 lines · Deepak Karn
Quoted 22 lines · Jan Kara
Quoted 8 lines · Deepakkumar Karn
Quoted 6 lines · Darrick J. Wong
Quoted 6 lines

drop_buffers() dereferences the buffer_head pointer returned by folio_buffers() without checking for NULL. This leads to a null pointer dereference when called from try_to_free_buffers() on a folio with no buffers attached. This happens when filemap_release_folio() is called on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but without release_folio address_space operation defined. In such case,

Quoted 4 lines
Quoted 2 lines

What user is that? All the users of AS_RELEASE_ALWAYS in 6.18 appear to supply a ->release_folio. Is this some new thing in 6.19?

AFS directories SET AS_RELEASE_ALWAYS but have not .release_folio.

AFAICS AFS sets AS_RELEASE_ALWAYS only for symlinks but not for directories? Anyway I agree AFS symlinks will have AS_RELEASE_ALWAYS but no .release_folio callback. And this looks like a bug in AFS because AFAICT there's no point in setting AS_RELEASE_ALWAYS when you don't have .release_folio callback. Added relevant people to CC.

Honza

Quoted 9 lines
Quoted 3 lines

Thank you for your response Jan. As you suggested, the bug is in AFS. Can we include this current defensive check in drop_buffers() and I can submit another patch to handle that bug of AFS we discussed?

I'm not strongly opposed to that (although try_to_free_buffers() would seem like a tad bit better place) but overall I don't think it's a great idea as it would hide bugs. But perhaps with WARN_ON_ONCE() (to catch sloppy programming) it would be a sensible hardening.

Quoted 6 lines

Thanks Jan for your response. As suggested, adding WARN_ON_ONCE() will be more sensible.
I just wanted to clarify my understanding, you are suggesting adding WARN_ON_ONCE() in try_to_free_buffers() as this highlights the issue and also solves the concern. If my understanding is wrong please let me know, I will share the updated patch.

Quoted 7 lines

Yes, I meant something like:

        if (WARN_ON_ONCE(!folio_buffers(folio)))
                return true;

in try_to_free_buffers().

Yes this is what I am also going for, just a minor change that I was going to return false.

Reason:
From the flow it should return false whenever failed to free buffers / operation
couldn't complete. Do you think otherwise.

Quoted 10 lines
Quoted 6 lines
Quoted 4 lines

Also I think mapping_set_release_always() should assert that mapping->a_ops->release_folio is non-NULL to catch the problem early (once you fix the AFS bug).

I will address this in another patch for AFS.

Thanks. As a separate patch please :)

Honza

Yes.

Regards,
Deepak Karn

reviewer
#9 lore raw
Quoted 55 lines · Deepak Karn
Quoted 46 lines · Jan Kara
Quoted 37 lines · Deepak Karn
Quoted 26 lines · Jan Kara
Quoted 24 lines · Deepak Karn
Quoted 22 lines · Jan Kara
Quoted 8 lines · Deepakkumar Karn
Quoted 6 lines · Darrick J. Wong
Quoted 6 lines

drop_buffers() dereferences the buffer_head pointer returned by folio_buffers() without checking for NULL. This leads to a null pointer dereference when called from try_to_free_buffers() on a folio with no buffers attached. This happens when filemap_release_folio() is called on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but without release_folio address_space operation defined. In such case,

Quoted 4 lines
Quoted 2 lines

What user is that? All the users of AS_RELEASE_ALWAYS in 6.18 appear to supply a ->release_folio. Is this some new thing in 6.19?

AFS directories SET AS_RELEASE_ALWAYS but have not .release_folio.

AFAICS AFS sets AS_RELEASE_ALWAYS only for symlinks but not for directories? Anyway I agree AFS symlinks will have AS_RELEASE_ALWAYS but no .release_folio callback. And this looks like a bug in AFS because AFAICT there's no point in setting AS_RELEASE_ALWAYS when you don't have .release_folio callback. Added relevant people to CC.

Honza

Quoted 9 lines
Quoted 3 lines

Thank you for your response Jan. As you suggested, the bug is in AFS. Can we include this current defensive check in drop_buffers() and I can submit another patch to handle that bug of AFS we discussed?

I'm not strongly opposed to that (although try_to_free_buffers() would seem like a tad bit better place) but overall I don't think it's a great idea as it would hide bugs. But perhaps with WARN_ON_ONCE() (to catch sloppy programming) it would be a sensible hardening.

Quoted 6 lines

Thanks Jan for your response. As suggested, adding WARN_ON_ONCE() will be more sensible.
I just wanted to clarify my understanding, you are suggesting adding WARN_ON_ONCE() in try_to_free_buffers() as this highlights the issue and also solves the concern. If my understanding is wrong please let me know, I will share the updated patch.

Quoted 7 lines

Yes, I meant something like:

        if (WARN_ON_ONCE(!folio_buffers(folio)))
                return true;

in try_to_free_buffers().

Quoted 6 lines

Yes this is what I am also going for, just a minor change that I was going to return false.

Reason:
From the flow it should return false whenever failed to free buffers / operation couldn't complete. Do you think otherwise.

Well, it's rather we should return false when the page cannot be freed because we were unable to free the buffers. When folio_buffers() are NULL, the page *can* be freed so we should be returning true in that case.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#10 lore raw
Quoted 60 lines · Jan Kara
Quoted 58 lines · Deepak Karn
Quoted 46 lines · Jan Kara
Quoted 37 lines · Deepak Karn
Quoted 26 lines · Jan Kara
Quoted 24 lines · Deepak Karn
Quoted 22 lines · Jan Kara
Quoted 8 lines · Deepakkumar Karn
Quoted 6 lines · Darrick J. Wong
Quoted 6 lines

drop_buffers() dereferences the buffer_head pointer returned by folio_buffers() without checking for NULL. This leads to a null pointer dereference when called from try_to_free_buffers() on a folio with no buffers attached. This happens when filemap_release_folio() is called on a folio belonging to a mapping with AS_RELEASE_ALWAYS set but without release_folio address_space operation defined. In such case,

Quoted 4 lines
Quoted 2 lines

What user is that? All the users of AS_RELEASE_ALWAYS in 6.18 appear to supply a ->release_folio. Is this some new thing in 6.19?

AFS directories SET AS_RELEASE_ALWAYS but have not .release_folio.

AFAICS AFS sets AS_RELEASE_ALWAYS only for symlinks but not for directories? Anyway I agree AFS symlinks will have AS_RELEASE_ALWAYS but no .release_folio callback. And this looks like a bug in AFS because AFAICT there's no point in setting AS_RELEASE_ALWAYS when you don't have .release_folio callback. Added relevant people to CC.

Honza

Quoted 9 lines
Quoted 3 lines

Thank you for your response Jan. As you suggested, the bug is in AFS. Can we include this current defensive check in drop_buffers() and I can submit another patch to handle that bug of AFS we discussed?

I'm not strongly opposed to that (although try_to_free_buffers() would seem like a tad bit better place) but overall I don't think it's a great idea as it would hide bugs. But perhaps with WARN_ON_ONCE() (to catch sloppy programming) it would be a sensible hardening.

Quoted 6 lines

Thanks Jan for your response. As suggested, adding WARN_ON_ONCE() will be more sensible.
I just wanted to clarify my understanding, you are suggesting adding WARN_ON_ONCE() in try_to_free_buffers() as this highlights the issue and also solves the concern. If my understanding is wrong please let me know, I will share the updated patch.

Quoted 7 lines

Yes, I meant something like:

        if (WARN_ON_ONCE(!folio_buffers(folio)))
                return true;

in try_to_free_buffers().

Yes this is what I am also going for, just a minor change that I was going to return false.

Quoted 8 lines
Quoted 3 lines

Reason:
From the flow it should return false whenever failed to free buffers / operation couldn't complete. Do you think otherwise.

Well, it's rather we should return false when the page cannot be freed because we were unable to free the buffers. When folio_buffers() are NULL, the page *can* be freed so we should be returning true in that case.

Thank you Jan for your response and clarifying this. I will submit an updated patch.

Regards,
Deepakkumar Karn