Re: [PATCH] ntfs: fix missing kstrdup() error check in ntfs_write_volume_label()
From: Hyunchul Lee
Date: Wed May 06 2026 - 20:37:32 EST
2026년 5월 6일 (수) 오후 4:11, Zhan Xusheng <zhanxusheng1024@xxxxxxxxx>님이 작성:
>
> 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")
> Signed-off-by: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
> ---
> fs/ntfs/super.c | 12 ++++++++----
> 1 file changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/fs/ntfs/super.c b/fs/ntfs/super.c
> index 22dc7865eca7..629fac580f95 100644
> --- a/fs/ntfs/super.c
> +++ b/fs/ntfs/super.c
> @@ -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);
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.
> +
> + 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
>
--
Thanks,
Hyunchul