Skip to contentSkip to conversation

ntfs: do not dereference a null ctx on error

PATCH Applied Danila Chernetsov 6 participants 8 messages Apr 7, 2023 – May 6, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ntfs: do not dereference a null ctx on error

In ntfs_mft_data_extend_allocation_nolock(), if an error condition occurs prior to 'ctx' being set to a non-NULL value, avoid dereferencing the NULL 'ctx' pointer in error handling.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 fs/ntfs/mft.c | 38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)
fs/ntfs/mft.c +21 -17
index 48030899dc6e..e1126ce6f8ec 100644
@@ -1955,36 +1955,40 @@ static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
 				"attribute.%s", es);
 		NVolSetErrors(vol);
 	}
-	a = ctx->attr;
+	
 	if (ntfs_rl_truncate_nolock(vol, &mft_ni->runlist, old_last_vcn)) {
 		ntfs_error(vol->sb, "Failed to truncate mft data attribute "
 				"runlist.%s", es);
 		NVolSetErrors(vol);
 	}
-	if (mp_rebuilt && !IS_ERR(ctx->mrec)) {
-		if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
+	if (ctx) {
+		a = ctx->attr;
+		if (mp_rebuilt && !IS_ERR(ctx->mrec)) {
+			if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
 				a->data.non_resident.mapping_pairs_offset),
 				old_alen - le16_to_cpu(
-				a->data.non_resident.mapping_pairs_offset),
+					a->data.non_resident.mapping_pairs_offset),
 				rl2, ll, -1, NULL)) {
-			ntfs_error(vol->sb, "Failed to restore mapping pairs "
+				ntfs_error(vol->sb, "Failed to restore mapping pairs "
 					"array.%s", es);
-			NVolSetErrors(vol);
-		}
-		if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
-			ntfs_error(vol->sb, "Failed to restore attribute "
+				NVolSetErrors(vol);
+			}
+			if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
+				ntfs_error(vol->sb, "Failed to restore attribute "
 					"record.%s", es);
-			NVolSetErrors(vol);
+				NVolSetErrors(vol);
+			}
+			flush_dcache_mft_record_page(ctx->ntfs_ino);
+			mark_mft_record_dirty(ctx->ntfs_ino);
 		}
-		flush_dcache_mft_record_page(ctx->ntfs_ino);
-		mark_mft_record_dirty(ctx->ntfs_ino);
-	} else if (IS_ERR(ctx->mrec)) {
-		ntfs_error(vol->sb, "Failed to restore attribute search "
+		else if (IS_ERR(ctx->mrec)) {
+			ntfs_error(vol->sb, "Failed to restore attribute search "
 				"context.%s", es);
-		NVolSetErrors(vol);
+			NVolSetErrors(vol);
+		}
+		if (ctx)
+			ntfs_attr_put_search_ctx(ctx);
 	}
-	if (ctx)
-		ntfs_attr_put_search_ctx(ctx);
 	if (!IS_ERR(mrec))
 		unmap_mft_record(mft_ni);
 	up_write(&mft_ni->runlist.lock);
Signature · 1 line
2.25.1
#1 lore raw

ntfs orphan? (was Re: [PATCH] ntfs: do not dereference a null ctx on error)

Quoted 5 lines · Danila Chernetsov

In ntfs_mft_data_extend_allocation_nolock(), if an error condition occurs prior to 'ctx' being set to a non-NULL value, avoid dereferencing the NULL 'ctx' pointer in error handling.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Duplicate of 10-years-old outstanding patch at [1].

I'm not speaking of the patch itself but rather on unfortunate state of ntfs subsystem. It seems like the maintainer is MIA (has not responding to patch submissions for a long time). Some trivial patches, however, are merged through mm tree.

Konstantin (from newer ntfs3 subsystem), Andrew, would you like to take a look on this orphaned subsystem (and help reviewing)? I'd like to send MAINTAINERS update if it turns out to be the case.

Thanks.

[1]: https://lore.kernel.org/all/[email protected]/

Signature · 1 line
An old man doll... just what I always wanted! - Clara
maintainer
#2 lore raw

Re: ntfs orphan? (was Re: [PATCH] ntfs: do not dereference a null ctx on error)

Quoted 8 lines · On Wed, 3 May 2023 11:16:02 +0700 Bagas Sanjaya <[email protected]> wrote:
Quoted 5 lines · Danila Chernetsov

In ntfs_mft_data_extend_allocation_nolock(), if an error condition occurs prior to 'ctx' being set to a non-NULL value, avoid dereferencing the NULL 'ctx' pointer in error handling.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Duplicate of 10-years-old outstanding patch at [1].

Well, the patches are actually quite different. Is Danila's longer one better?

Quoted 9 lines

I'm not speaking of the patch itself but rather on unfortunate state of ntfs subsystem. It seems like the maintainer is MIA (has not responding to patch submissions for a long time). Some trivial patches, however, are merged through mm tree.

Konstantin (from newer ntfs3 subsystem), Andrew, would you like to take a look on this orphaned subsystem (and help reviewing)? I'd like to send MAINTAINERS update if it turns out to be the case.

Sure, I can join [email protected] and hendle things which come along.

Or Christian may want to do that?

Quoted 2 lines

[1]: https://lore.kernel.org/all/[email protected]/

#3 lore raw

Re: ntfs orphan? (was Re: [PATCH] ntfs: do not dereference a null ctx on error)

Hi,

On 3 May 2023, at 22:06, Andrew Morton <[email protected]> wrote:
On Wed, 3 May 2023 11:16:02 +0700 Bagas Sanjaya <[email protected]> wrote:
On Fri, Apr 07, 2023 at 07:44:33PM +0000, Danila Chernetsov wrote:
In ntfs_mft_data_extend_allocation_nolock(), if an error condition occurs prior to 'ctx' being set to a non-NULL value, avoid dereferencing the NULL 'ctx' pointer in error handling.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Duplicate of 10-years-old outstanding patch at [1].

Well, the patches are actually quite different. Is Danila's longer one better?

Yes, I think the new, longer one is much nicer. Please take that one. Oh just noticed the next email in my inbox - you already have, thank you.

I'm not speaking of the patch itself but rather on unfortunate state of ntfs subsystem. It seems like the maintainer is MIA (has not responding to patch submissions for a long time). Some trivial patches, however, are merged through mm tree.

Konstantin (from newer ntfs3 subsystem), Andrew, would you like to take a look on this orphaned subsystem (and help reviewing)? I'd like to send MAINTAINERS update if it turns out to be the case.

Sure, I can join [email protected] and hendle things which come along.

Or Christian may want to do that?

Sorry, I am not totally MIA - I do handle the odd patch but I do miss many. Sorry to say I have been so busy it is very easy for things to slip through my attention or to be read and then forgotten and my inbox is currently at 79213 messages so the old patch is probably somewhere in there just chances of me ever getting to the bottom of my inbox and dealing with all those old messages is close to zero given I am losing ground constantly rather than winning it. I am trying just not succeeding very well...

Best regards,

Anton

[1]: https://lore.kernel.org/all/[email protected]/

--
Anton Altaparmakov <anton at tuxera.com> (replace at with @) Lead in File System Development, Tuxera Inc., http://www.tuxera.com/
Linux NTFS maintainer

#4 lore raw

Re: ntfs orphan? (was Re: [PATCH] ntfs: do not dereference a null ctx on error)

In reply to Andrew Morton
Quoted 24 lines · Andrew Morton
Quoted 8 lines · On Wed, 3 May 2023 11:16:02 +0700 Bagas Sanjaya <[email protected]> wrote:
Quoted 5 lines · Danila Chernetsov

In ntfs_mft_data_extend_allocation_nolock(), if an error condition occurs prior to 'ctx' being set to a non-NULL value, avoid dereferencing the NULL 'ctx' pointer in error handling.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Duplicate of 10-years-old outstanding patch at [1].

Well, the patches are actually quite different. Is Danila's longer one better?

Quoted 9 lines

I'm not speaking of the patch itself but rather on unfortunate state of ntfs subsystem. It seems like the maintainer is MIA (has not responding to patch submissions for a long time). Some trivial patches, however, are merged through mm tree.

Konstantin (from newer ntfs3 subsystem), Andrew, would you like to take a look on this orphaned subsystem (and help reviewing)? I'd like to send MAINTAINERS update if it turns out to be the case.

Quoted 4 lines

Sure, I can join [email protected] and hendle things which come along.

Or Christian may want to do that?

Thanks for asking. I would be able to pick up those patches into fs.misc. I got an off-list ping about someone also wanting to help with review apparently.

maintainer
#5 lore raw

Re: ntfs orphan? (was Re: [PATCH] ntfs: do not dereference a null ctx on error)

In reply to Andrew Morton

2023-05-04 6:06 GMT+09:00, Andrew Morton <[email protected]>:

Quoted 10 lines
Quoted 7 lines · On Wed, 3 May 2023 11:16:02 +0700 Bagas Sanjaya <[email protected]> wrote:
Quoted 6 lines · Danila Chernetsov

In ntfs_mft_data_extend_allocation_nolock(), if an error condition occurs
prior to 'ctx' being set to a non-NULL value, avoid dereferencing the NULL
'ctx' pointer in error handling.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Duplicate of 10-years-old outstanding patch at [1].

Well, the patches are actually quite different. Is Danila's longer one better?

Danila's patch is better than old one. old patch doesn't free allocated cluster in error handling. And Christian will apply this to his queue if you drop this patch.

Quoted 22 lines
Quoted 10 lines

I'm not speaking of the patch itself but rather on unfortunate state of ntfs subsystem. It seems like the maintainer is MIA (has not responding to patch submissions for a long time). Some trivial patches, however, are merged through mm tree.

Konstantin (from newer ntfs3 subsystem), Andrew, would you like to take a look on this orphaned subsystem (and help reviewing)? I'd like to send MAINTAINERS update if it turns out to be the case.

Sure, I can join [email protected] and hendle things which come along.

Or Christian may want to do that?

Quoted 3 lines

[1]:
https://lore.kernel.org/all/[email protected]/

maintainer
#6 lore raw
In reply to Danila Chernetsov

2023-04-08 4:44 GMT+09:00, Danila Chernetsov <[email protected]>:

Quoted 58 lines

In ntfs_mft_data_extend_allocation_nolock(), if an error condition occurs prior to 'ctx' being set to a non-NULL value, avoid dereferencing the NULL 'ctx' pointer in error handling.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 fs/ntfs/mft.c | 38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)
fs/ntfs/mft.c +15 -11
index 48030899dc6e..e1126ce6f8ec 100644
@@ -1955,36 +1955,40 @@ static int
 ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
 				"attribute.%s", es);
 		NVolSetErrors(vol);
 	}
-	a = ctx->attr;
+	
 	if (ntfs_rl_truncate_nolock(vol, &mft_ni->runlist, old_last_vcn)) {
 		ntfs_error(vol->sb, "Failed to truncate mft data attribute "
 				"runlist.%s", es);
 		NVolSetErrors(vol);
 	}
-	if (mp_rebuilt && !IS_ERR(ctx->mrec)) {
-		if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
+	if (ctx) {
+		a = ctx->attr;
+		if (mp_rebuilt && !IS_ERR(ctx->mrec)) {
+			if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
 				a->data.non_resident.mapping_pairs_offset),
 				old_alen - le16_to_cpu(
-				a->data.non_resident.mapping_pairs_offset),
+					a->data.non_resident.mapping_pairs_offset),
 				rl2, ll, -1, NULL)) {
-			ntfs_error(vol->sb, "Failed to restore mapping pairs "
+				ntfs_error(vol->sb, "Failed to restore mapping pairs "
 					"array.%s", es);
-			NVolSetErrors(vol);
-		}
-		if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
-			ntfs_error(vol->sb, "Failed to restore attribute "
+				NVolSetErrors(vol);
+			}
+			if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
+				ntfs_error(vol->sb, "Failed to restore attribute "
 					"record.%s", es);
-			NVolSetErrors(vol);
+				NVolSetErrors(vol);
+			}
+			flush_dcache_mft_record_page(ctx->ntfs_ino);
+			mark_mft_record_dirty(ctx->ntfs_ino);
 		}
-		flush_dcache_mft_record_page(ctx->ntfs_ino);

fs/ntfs/mft.c static int · line 1990

Quoted 10 lines
fs/ntfs/mft.c +5 -4
-		mark_mft_record_dirty(ctx->ntfs_ino);
-	} else if (IS_ERR(ctx->mrec)) {
-		ntfs_error(vol->sb, "Failed to restore attribute search "
+		else if (IS_ERR(ctx->mrec)) {
+			ntfs_error(vol->sb, "Failed to restore attribute search "
 				"context.%s", es);
-		NVolSetErrors(vol);
+			NVolSetErrors(vol);
+		}
+		if (ctx)

I think that this check is not needed.

Quoted 11 lines
fs/ntfs/mft.c +1 -2
+			ntfs_attr_put_search_ctx(ctx);
 	}
-	if (ctx)
-		ntfs_attr_put_search_ctx(ctx);
 	if (!IS_ERR(mrec))
 		unmap_mft_record(mft_ni);

up_write(&mft_ni->runlist.lock);
--
2.25.1

maintainer
#7 lore raw
In reply to Danila Chernetsov

2023-04-08 4:44 GMT+09:00, Danila Chernetsov <[email protected]>:

Quoted 3 lines

In ntfs_mft_data_extend_allocation_nolock(), if an error condition occurs prior to 'ctx' being set to a non-NULL value, avoid dereferencing the NULL 'ctx' pointer in error handling.

Please check the warnings from checkpatch.pl.

ERROR: trailing whitespace
#107: FILE: fs/ntfs/mft.c:1958:
+^I$

ERROR: "(foo*)" should be "(foo *)"
#118: FILE: fs/ntfs/mft.c:1967:
+ if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(

ERROR: else should follow close brace '}'
#146: FILE: fs/ntfs/mft.c:1984:
}
+ else if (IS_ERR(ctx->mrec)) {

Quoted 76 lines

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 fs/ntfs/mft.c | 38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)
fs/ntfs/mft.c +21 -17
index 48030899dc6e..e1126ce6f8ec 100644
@@ -1955,36 +1955,40 @@ static int
 ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
 				"attribute.%s", es);
 		NVolSetErrors(vol);
 	}
-	a = ctx->attr;
+	
 	if (ntfs_rl_truncate_nolock(vol, &mft_ni->runlist, old_last_vcn)) {
 		ntfs_error(vol->sb, "Failed to truncate mft data attribute "
 				"runlist.%s", es);
 		NVolSetErrors(vol);
 	}
-	if (mp_rebuilt && !IS_ERR(ctx->mrec)) {
-		if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
+	if (ctx) {
+		a = ctx->attr;
+		if (mp_rebuilt && !IS_ERR(ctx->mrec)) {
+			if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
 				a->data.non_resident.mapping_pairs_offset),
 				old_alen - le16_to_cpu(
-				a->data.non_resident.mapping_pairs_offset),
+					a->data.non_resident.mapping_pairs_offset),
 				rl2, ll, -1, NULL)) {
-			ntfs_error(vol->sb, "Failed to restore mapping pairs "
+				ntfs_error(vol->sb, "Failed to restore mapping pairs "
 					"array.%s", es);
-			NVolSetErrors(vol);
-		}
-		if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
-			ntfs_error(vol->sb, "Failed to restore attribute "
+				NVolSetErrors(vol);
+			}
+			if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
+				ntfs_error(vol->sb, "Failed to restore attribute "
 					"record.%s", es);
-			NVolSetErrors(vol);
+				NVolSetErrors(vol);
+			}
+			flush_dcache_mft_record_page(ctx->ntfs_ino);
+			mark_mft_record_dirty(ctx->ntfs_ino);
 		}
-		flush_dcache_mft_record_page(ctx->ntfs_ino);
-		mark_mft_record_dirty(ctx->ntfs_ino);
-	} else if (IS_ERR(ctx->mrec)) {
-		ntfs_error(vol->sb, "Failed to restore attribute search "
+		else if (IS_ERR(ctx->mrec)) {
+			ntfs_error(vol->sb, "Failed to restore attribute search "
 				"context.%s", es);
-		NVolSetErrors(vol);
+			NVolSetErrors(vol);
+		}
+		if (ctx)
+			ntfs_attr_put_search_ctx(ctx);
 	}
-	if (ctx)
-		ntfs_attr_put_search_ctx(ctx);
 	if (!IS_ERR(mrec))
 		unmap_mft_record(mft_ni);

up_write(&mft_ni->runlist.lock);
--
2.25.1