Skip to contentSkip to conversation

ntfs: fix missing kstrdup() error check in ntfs_write_volume_label()

PATCH Superseded Zhan Xusheng 3 participants 5 messages May 6 – May 9

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

PATCH 1/1 ntfs: fix missing kstrdup() error check in ntfs_write_volume_label()

ntfs_write_volume_label() does not check the return value of kstrdup(). If the allocation fails, vol->volume_label is set to NULL while the function returns success. A subsequent FS_IOC_GETFSLABEL then returns an empty string even though the on-disk label was updated correctly.

Fix by allocating the new label before freeing the old one and propagating -ENOMEM on failure. Also move mark_inode_dirty_sync() into the success path so that it is not called when no metadata was actually modified.

Fixes 6251f0b0de7d ("ntfs: update super block operations")
---
 fs/ntfs/super.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
fs/ntfs/super.c +8 -4
index 22dc7865eca7..629fac580f95 100644
@@ -425,7 +425,7 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
 		return uname_len;
 	}
 
-	if (uname_len  > NTFS_MAX_LABEL_LEN) {
+	if (uname_len > NTFS_MAX_LABEL_LEN) {
 		ntfs_error(vol->sb,
 			   "Volume label is too long (max %d characters).",
 			   NTFS_MAX_LABEL_LEN);
@@ -437,7 +437,7 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
 	ctx = ntfs_attr_get_search_ctx(vol_ni, NULL);
 	if (!ctx) {
 		ret = -ENOMEM;
-		goto  out;
+		goto out;
 	}
 
 	if (!ntfs_attr_lookup(AT_VOLUME_NAME, NULL, 0, 0, 0, NULL, 0,
@@ -450,11 +450,15 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
 out:
 	mutex_unlock(&vol_ni->mrec_lock);
 	kvfree(uname);
-	mark_inode_dirty_sync(vol->vol_ino);
 
 	if (ret >= 0) {
+		char *new_label = kstrdup(label, GFP_KERNEL);
+
+		if (!new_label)
+			return -ENOMEM;
 		kfree(vol->volume_label);
-		vol->volume_label = kstrdup(label, GFP_KERNEL);
+		vol->volume_label = new_label;
+		mark_inode_dirty_sync(vol->vol_ino);
 		ret = 0;
 	}
 	return ret;
Signature · 1 line
2.43.0
maintainer
#1 lore raw

2026년 5월 6일 (수) 오후 4:11, Zhan Xusheng <[email protected]>님이 작성:

Quoted 41 lines

ntfs_write_volume_label() does not check the return value of kstrdup(). If the allocation fails, vol->volume_label is set to NULL while the function returns success. A subsequent FS_IOC_GETFSLABEL then returns an empty string even though the on-disk label was updated correctly.

Fix by allocating the new label before freeing the old one and propagating -ENOMEM on failure. Also move mark_inode_dirty_sync() into the success path so that it is not called when no metadata was actually modified.

Fixes 6251f0b0de7d ("ntfs: update super block operations")
---
 fs/ntfs/super.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
fs/ntfs/super.c +2 -2
index 22dc7865eca7..629fac580f95 100644
@@ -425,7 +425,7 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
                return uname_len;
        }
 
-       if (uname_len  > NTFS_MAX_LABEL_LEN) {
+       if (uname_len > NTFS_MAX_LABEL_LEN) {
                ntfs_error(vol->sb,
                           "Volume label is too long (max %d characters).",
                           NTFS_MAX_LABEL_LEN);
@@ -437,7 +437,7 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
        ctx = ntfs_attr_get_search_ctx(vol_ni, NULL);
        if (!ctx) {
                ret = -ENOMEM;
-               goto  out;
+               goto out;
        }
 
        if (!ntfs_attr_lookup(AT_VOLUME_NAME, NULL, 0, 0, 0, NULL, 0,

fs/ntfs/super.c int ntfs_write_volume_label(struct ntfs_volume *vol, char *label) · line 455

Quoted 7 lines
fs/ntfs/super.c +1 -1
@@ -450,11 +450,15 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
 out:
        mutex_unlock(&vol_ni->mrec_lock);
        kvfree(uname);
-       mark_inode_dirty_sync(vol->vol_ino);
 
        if (ret >= 0) {
+               char *new_label = kstrdup(label, GFP_KERNEL);

How about the moving kstrdup() call to before mutex_lock(&vol_ni->mrec_lock)? If we return a failure here, it could cause the in-memory and on-disk labels to become
inconsistent.

Quoted 13 lines
fs/ntfs/super.c +5 -2
+
+               if (!new_label)
+                       return -ENOMEM;
                kfree(vol->volume_label);
-               vol->volume_label = kstrdup(label, GFP_KERNEL);
+               vol->volume_label = new_label;
+               mark_inode_dirty_sync(vol->vol_ino);
                ret = 0;
        }
        return ret;
--

2.43.0

Signature · 2 lines
Thanks,
Hyunchul
#2 lore raw

PATCH v2 1/1 ntfs: fix missing kstrdup() error check in ntfs_write_volume_label()

ntfs_write_volume_label() does not check the return value of kstrdup(). If the allocation fails, vol->volume_label is set to NULL while the function returns success. A subsequent FS_IOC_GETFSLABEL then returns an empty string even though the on-disk label was updated correctly.

Fix by allocating the new label before taking vol_ni->mrec_lock and updating any on-disk metadata, so an -ENOMEM from kstrdup() leaves both the in-memory and on-disk labels untouched and consistent. On success the preallocated copy replaces the old vol->volume_label. Also move mark_inode_dirty_sync() into the success path so that it is not called when no metadata was actually modified.

Fixes 6251f0b0de7d ("ntfs: update super block operations")
---
 fs/ntfs/super.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
fs/ntfs/super.c +18 -4
index 22dc7865eca7..ba99c497ee38 100644
@@ -413,6 +413,7 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
 {
 	struct ntfs_inode *vol_ni = NTFS_I(vol->vol_ino);
 	struct ntfs_attr_search_ctx *ctx;
+	char *new_label;
 	__le16 *uname;
 	int uname_len, ret;
 
@@ -425,7 +426,7 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
 		return uname_len;
 	}
 
-	if (uname_len  > NTFS_MAX_LABEL_LEN) {
+	if (uname_len > NTFS_MAX_LABEL_LEN) {
 		ntfs_error(vol->sb,
 			   "Volume label is too long (max %d characters).",
 			   NTFS_MAX_LABEL_LEN);
@@ -433,11 +434,22 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
 		return -EINVAL;
 	}
 
+	/*
+	 * Allocate the in-memory label copy up front. If kstrdup() fails we
+	 * bail out before touching on-disk metadata, so the in-memory label
+	 * and the on-disk label stay in sync.
+	 */
+	new_label = kstrdup(label, GFP_KERNEL);
+	if (!new_label) {
+		kvfree(uname);
+		return -ENOMEM;
+	}
+
 	mutex_lock(&vol_ni->mrec_lock);
 	ctx = ntfs_attr_get_search_ctx(vol_ni, NULL);
 	if (!ctx) {
 		ret = -ENOMEM;
-		goto  out;
+		goto out;
 	}
 
 	if (!ntfs_attr_lookup(AT_VOLUME_NAME, NULL, 0, 0, 0, NULL, 0,
@@ -450,12 +462,14 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
 out:
 	mutex_unlock(&vol_ni->mrec_lock);
 	kvfree(uname);
-	mark_inode_dirty_sync(vol->vol_ino);
 
 	if (ret >= 0) {
 		kfree(vol->volume_label);
-		vol->volume_label = kstrdup(label, GFP_KERNEL);
+		vol->volume_label = new_label;
+		mark_inode_dirty_sync(vol->vol_ino);
 		ret = 0;
+	} else {
+		kfree(new_label);
 	}
 	return ret;
 }
Signature · 1 line
2.43.0
maintainer Reviewed-by on v2 1/1
#3 lore raw

2026년 5월 8일 (금) 오후 4:29, Zhan Xusheng <[email protected]>님이 작성:

Quoted 14 lines

ntfs_write_volume_label() does not check the return value of kstrdup(). If the allocation fails, vol->volume_label is set to NULL while the function returns success. A subsequent FS_IOC_GETFSLABEL then returns an empty string even though the on-disk label was updated correctly.

Fix by allocating the new label before taking vol_ni->mrec_lock and updating any on-disk metadata, so an -ENOMEM from kstrdup() leaves both the in-memory and on-disk labels untouched and consistent. On success the preallocated copy replaces the old vol->volume_label. Also move mark_inode_dirty_sync() into the success path so that it is not called when no metadata was actually modified.

Quoted 3 lines
Fixes 6251f0b0de7d ("ntfs: update super block operations")

Looks good to me.

Quoted 69 lines
---
 fs/ntfs/super.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
fs/ntfs/super.c +18 -5
index 22dc7865eca7..ba99c497ee38 100644
@@ -413,6 +413,7 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
 {
        struct ntfs_inode *vol_ni = NTFS_I(vol->vol_ino);
        struct ntfs_attr_search_ctx *ctx;
+       char *new_label;
        __le16 *uname;
        int uname_len, ret;
 
@@ -425,7 +426,7 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
                return uname_len;
        }
 
-       if (uname_len  > NTFS_MAX_LABEL_LEN) {
+       if (uname_len > NTFS_MAX_LABEL_LEN) {
                ntfs_error(vol->sb,
                           "Volume label is too long (max %d characters).",
                           NTFS_MAX_LABEL_LEN);
@@ -433,11 +434,22 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
                return -EINVAL;
        }
 
+       /*
+        * Allocate the in-memory label copy up front. If kstrdup() fails we
+        * bail out before touching on-disk metadata, so the in-memory label
+        * and the on-disk label stay in sync.
+        */
+       new_label = kstrdup(label, GFP_KERNEL);
+       if (!new_label) {
+               kvfree(uname);
+               return -ENOMEM;
+       }
+
        mutex_lock(&vol_ni->mrec_lock);
        ctx = ntfs_attr_get_search_ctx(vol_ni, NULL);
        if (!ctx) {
                ret = -ENOMEM;
-               goto  out;
+               goto out;
        }
 
        if (!ntfs_attr_lookup(AT_VOLUME_NAME, NULL, 0, 0, 0, NULL, 0,
@@ -450,12 +462,14 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
 out:
        mutex_unlock(&vol_ni->mrec_lock);
        kvfree(uname);
-       mark_inode_dirty_sync(vol->vol_ino);
 
        if (ret >= 0) {
                kfree(vol->volume_label);
-               vol->volume_label = kstrdup(label, GFP_KERNEL);
+               vol->volume_label = new_label;
+               mark_inode_dirty_sync(vol->vol_ino);
                ret = 0;
+       } else {
+               kfree(new_label);
        }
        return ret;
 }
--

2.43.0

Signature · 2 lines
Thanks,
Hyunchul
maintainer Applied
#4 lore raw
In reply to Zhan Xusheng
Quoted 14 lines · Zhan Xusheng

ntfs_write_volume_label() does not check the return value of kstrdup(). If the allocation fails, vol->volume_label is set to NULL while the function returns success. A subsequent FS_IOC_GETFSLABEL then returns an empty string even though the on-disk label was updated correctly.

Fix by allocating the new label before taking vol_ni->mrec_lock and updating any on-disk metadata, so an -ENOMEM from kstrdup() leaves both the in-memory and on-disk labels untouched and consistent. On success the preallocated copy replaces the old vol->volume_label. Also move mark_inode_dirty_sync() into the success path so that it is not called when no metadata was actually modified.

Quoted 3 lines
Fixes 6251f0b0de7d ("ntfs: update super block operations")

Applied it to #ntfs-next.
Thanks!