Skip to contentSkip to conversation

ntfs: Bug fixes for attrib.c

PATCH Applied Ethan Tidmore 3 participants 8 messages Feb 26 – Feb 27

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

cover ntfs: Bug fixes for attrib.c

Here are three bug fixes found with Smatch.

Ethan Tidmore (3):
ntfs: Place check before dereference
ntfs: Add missing error code
ntfs: Fix possible deadlock

 fs/ntfs/attrib.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/3 ntfs: Place check before dereference

The variable ni has the possiblity of being null and is checked for it but, only after it was dereferenced in a log message.

Put check before dereference.

Detected by Smatch:
fs/ntfs/attrib.c:2115 ntfs_resident_attr_record_add() warn:
variable dereferenced before check 'ni' (see line 2111)

fs/ntfs/attrib.c:2237 ntfs_non_resident_attr_record_add() warn:
variable dereferenced before check 'ni' (see line 2232)

---
 fs/ntfs/attrib.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
fs/ntfs/attrib.c +6 -6
index e8285264f619..e260540eb7c5 100644
@@ -2108,13 +2108,13 @@ int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
 	int err, offset;
 	struct ntfs_inode *base_ni;
 
+	if (!ni || (!name && name_len))
+		return -EINVAL;
+
 	ntfs_debug("Entering for inode 0x%llx, attr 0x%x, flags 0x%x.\n",
 			(long long) ni->mft_no, (unsigned int) le32_to_cpu(type),
 			(unsigned int) le16_to_cpu(flags));
 
-	if (!ni || (!name && name_len))
-		return -EINVAL;
-
 	err = ntfs_attr_can_be_resident(ni->vol, type);
 	if (err) {
 		if (err == -EPERM)
@@ -2229,14 +2229,14 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
 	struct ntfs_inode *base_ni;
 	int err, offset;
 
+	if (!ni || dataruns_size <= 0 || (!name && name_len))
+		return -EINVAL;
+
 	ntfs_debug("Entering for inode 0x%llx, attr 0x%x, lowest_vcn %lld, dataruns_size %d, flags 0x%x.\n",
 			(long long) ni->mft_no, (unsigned int) le32_to_cpu(type),
 			(long long) lowest_vcn, dataruns_size,
 			(unsigned int) le16_to_cpu(flags));
 
-	if (!ni || dataruns_size <= 0 || (!name && name_len))
-		return -EINVAL;
-
 	err = ntfs_attr_can_be_non_resident(ni->vol, type);
 	if (err) {
 		if (err == -EPERM)
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] ntfs: Place check before dereference

Quoted 5 lines · Ethan Tidmore

The variable ni has the possiblity of being null and is checked for it but, only after it was dereferenced in a log message.

Put check before dereference.

Quoted 8 lines

Detected by Smatch:
fs/ntfs/attrib.c:2115 ntfs_resident_attr_record_add() warn:
variable dereferenced before check 'ni' (see line 2111)

fs/ntfs/attrib.c:2237 ntfs_non_resident_attr_record_add() warn:
variable dereferenced before check 'ni' (see line 2232)

Looks good to me. Thank for the patch

Quoted 46 lines
---
 fs/ntfs/attrib.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
fs/ntfs/attrib.c +6 -6
index e8285264f619..e260540eb7c5 100644
@@ -2108,13 +2108,13 @@ int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
 	int err, offset;
 	struct ntfs_inode *base_ni;
 
+	if (!ni || (!name && name_len))
+		return -EINVAL;
+
 	ntfs_debug("Entering for inode 0x%llx, attr 0x%x, flags 0x%x.\n",
 			(long long) ni->mft_no, (unsigned int) le32_to_cpu(type),
 			(unsigned int) le16_to_cpu(flags));
 
-	if (!ni || (!name && name_len))
-		return -EINVAL;
-
 	err = ntfs_attr_can_be_resident(ni->vol, type);
 	if (err) {
 		if (err == -EPERM)
@@ -2229,14 +2229,14 @@ static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type,
 	struct ntfs_inode *base_ni;
 	int err, offset;
 
+	if (!ni || dataruns_size <= 0 || (!name && name_len))
+		return -EINVAL;
+
 	ntfs_debug("Entering for inode 0x%llx, attr 0x%x, lowest_vcn %lld, dataruns_size %d, flags 0x%x.\n",
 			(long long) ni->mft_no, (unsigned int) le32_to_cpu(type),
 			(long long) lowest_vcn, dataruns_size,
 			(unsigned int) le16_to_cpu(flags));
 
-	if (!ni || dataruns_size <= 0 || (!name && name_len))
-		return -EINVAL;
-
 	err = ntfs_attr_can_be_non_resident(ni->vol, type);
 	if (err) {
 		if (err == -EPERM)
Signature · 1 line
2.53.0
Signature · 2 lines
Thanks,
Hyunchul
#3 lore raw

PATCH 2/3 ntfs: Add missing error code

In reply to Ethan Tidmore

If ntfs_attr_iget() fails no error code is assigned to be returned.

Detected by Smatch:
fs/ntfs/attrib.c:2665 ntfs_attr_add() warn:
missing error code 'err'

---
 fs/ntfs/attrib.c | 1 +
 1 file changed, 1 insertion(+)
fs/ntfs/attrib.c +1 -0
index e260540eb7c5..71ad870eceac 100644
@@ -2661,6 +2661,7 @@ int ntfs_attr_add(struct ntfs_inode *ni, __le32 type,
 	/* Open new attribute and resize it. */
 	attr_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);
 	if (IS_ERR(attr_vi)) {
+		err = PTR_ERR(attr_vi);
 		ntfs_error(sb, "Failed to open just added attribute");
 		goto rm_attr_err_out;
 	}
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] ntfs: Add missing error code

Quoted 7 lines · Ethan Tidmore

If ntfs_attr_iget() fails no error code is assigned to be returned.

Detected by Smatch:
fs/ntfs/attrib.c:2665 ntfs_attr_add() warn:
missing error code 'err'

Looks good to me. Thank for the patch

Quoted 19 lines
---
 fs/ntfs/attrib.c | 1 +
 1 file changed, 1 insertion(+)
fs/ntfs/attrib.c +1 -0
index e260540eb7c5..71ad870eceac 100644
@@ -2661,6 +2661,7 @@ int ntfs_attr_add(struct ntfs_inode *ni, __le32 type,
 	/* Open new attribute and resize it. */
 	attr_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len);
 	if (IS_ERR(attr_vi)) {
+		err = PTR_ERR(attr_vi);
 		ntfs_error(sb, "Failed to open just added attribute");
 		goto rm_attr_err_out;
 	}
Signature · 1 line
2.53.0
Signature · 2 lines
Thanks,
Hyunchul
#5 lore raw

PATCH 3/3 ntfs: Fix possible deadlock

In reply to Ethan Tidmore

In the error path for ntfs_attr_map_whole_runlist() the lock is not released.

Add release for lock.

Detected by Smatch:
fs/ntfs/attrib.c:5197 ntfs_non_resident_attr_collapse_range() warn:
inconsistent returns '&ni->runlist.lock'.

Fixes 495e90fa33482 ("ntfs: update attrib operations")
---
 fs/ntfs/attrib.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/ntfs/attrib.c +3 -1
index 71ad870eceac..2af45df2aab1 100644
@@ -5124,8 +5124,10 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
 
 	down_write(&ni->runlist.lock);
 	ret = ntfs_attr_map_whole_runlist(ni);
-	if (ret)
+	if (ret) {
+		up_write(&ni->runlist.lock);
 		return ret;
+	}
 
 	len = min(len, end_vcn - start_vcn);
 	for (rl = ni->runlist.rl, dst_cnt = 0; rl && rl->length; rl++)
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] ntfs: Fix possible deadlock

Quoted 11 lines · Ethan Tidmore

In the error path for ntfs_attr_map_whole_runlist() the lock is not released.

Add release for lock.

Detected by Smatch:
fs/ntfs/attrib.c:5197 ntfs_non_resident_attr_collapse_range() warn:
inconsistent returns '&ni->runlist.lock'.

Fixes 495e90fa33482 ("ntfs: update attrib operations")

Looks good to me. Thank for the patch

Quoted 23 lines
---
 fs/ntfs/attrib.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/ntfs/attrib.c +3 -1
index 71ad870eceac..2af45df2aab1 100644
@@ -5124,8 +5124,10 @@ int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn,
 
 	down_write(&ni->runlist.lock);
 	ret = ntfs_attr_map_whole_runlist(ni);
-	if (ret)
+	if (ret) {
+		up_write(&ni->runlist.lock);
 		return ret;
+	}
 
 	len = min(len, end_vcn - start_vcn);
 	for (rl = ni->runlist.rl, dst_cnt = 0; rl && rl->length; rl++)
Signature · 1 line
2.53.0
Signature · 2 lines
Thanks,
Hyunchul
maintainer Applied
#7 lore raw
In reply to Ethan Tidmore
Quoted 7 lines · Ethan Tidmore

Here are three bug fixes found with Smatch.

Ethan Tidmore (3):
ntfs: Place check before dereference
ntfs: Add missing error code
ntfs: Fix possible deadlock

Applied them to #ntfs-next.
Thanks!