diff --git a/module/os/freebsd/zfs/zfs_vnops_os.c b/module/os/freebsd/zfs/zfs_vnops_os.c index be773190e02a..93ec05806516 100644 --- a/module/os/freebsd/zfs/zfs_vnops_os.c +++ b/module/os/freebsd/zfs/zfs_vnops_os.c @@ -5190,25 +5190,28 @@ zfs_check_attrname(const char *name) } static char * -zfs_attrname_maybe_rename(char *name) +zfs_dup_attrname_maybe_rename(const char *name) { - char *ret = name; + char *ret; + size_t len = strlen(name); if (zfs_xattr_compat) return (ret); if (ZFS_XA_NS_PREFIX_FORBIDDEN(name)) { - ret = kmem_alloc(strlen(name) + 6, KM_SLEEP); - snprintf(ret, strlen(name), "user.%s", name); + ret = kmem_alloc(len + 6, KM_SLEEP); + snprintf(ret, len + 5, "user.%s", name); } + + ret = kmem_alloc(len + 1, KM_SLEEP); + memcpy(ret, name, len); return (ret); } static void -zfs_attrname_maybe_free(char *name, char *orig) +zfs_dup_attrname_free(char *name, const char *orig) { - if (name != orig) - kmem_free(name, strlen(name)); + kmem_free(name, strlen(name)); } /* @@ -5413,16 +5416,16 @@ zfs_getextattr(struct vop_getextattr_args *ap) if (error != 0) return (SET_ERROR(error)); - maybe_renamed = zfs_attrname_maybe_rename(origname); + maybe_renamed = zfs_dup_attrname_maybe_rename(origname); error = zfs_check_attrname(maybe_renamed); if (error != 0) { - zfs_attrname_maybe_free(maybe_renamed, origname); + zfs_dup_attrname_free(maybe_renamed, origname); return (error); } if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) { - zfs_attrname_maybe_free(maybe_renamed, origname); + zfs_dup_attrname_free(maybe_renamed, origname); return (error); } error = ENOENT; @@ -5442,7 +5445,7 @@ zfs_getextattr(struct vop_getextattr_args *ap) zfs_exit(zfsvfs, FTAG); if (error == ENOENT) error = SET_ERROR(ENOATTR); - zfs_attrname_maybe_free(maybe_renamed, origname); + zfs_dup_attrname_free(maybe_renamed, origname); return (error); } @@ -5564,14 +5567,14 @@ zfs_deleteextattr(struct vop_deleteextattr_args *ap) if (error != 0) return (SET_ERROR(error)); - maybe_renamed = zfs_attrname_maybe_rename(origname); + maybe_renamed = zfs_dup_attrname_maybe_rename(origname); error = zfs_check_attrname(maybe_renamed); if (error != 0) { - zfs_attrname_maybe_free(maybe_renamed, origname); + zfs_dup_attrname_free(maybe_renamed, origname); return (error); } if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) { - zfs_attrname_maybe_free(maybe_renamed, origname); + zfs_dup_attrname_free(maybe_renamed, origname); return (error); } rw_enter(&zp->z_xattr_lock, RW_WRITER); @@ -5591,7 +5594,7 @@ zfs_deleteextattr(struct vop_deleteextattr_args *ap) zfs_exit(zfsvfs, FTAG); if (error == ENOENT) error = SET_ERROR(ENOATTR); - zfs_attrname_maybe_free(maybe_renamed, origname); + zfs_dup_attrname_free(maybe_renamed, origname); return (error); } @@ -5761,15 +5764,15 @@ zfs_setextattr(struct vop_setextattr_args *ap) if (error != 0) return (SET_ERROR(error)); - maybe_renamed = zfs_attrname_maybe_rename(origname); + maybe_renamed = zfs_dup_attrname_maybe_rename(origname); error = zfs_check_attrname(maybe_renamed); if (error != 0) { - zfs_attrname_maybe_free(maybe_renamed, origname); + zfs_dup_attrname_free(maybe_renamed, origname); return (error); } if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) { - zfs_attrname_maybe_free(maybe_renamed, origname); + zfs_dup_attrname_free(maybe_renamed, origname); return (error); } rw_enter(&zp->z_xattr_lock, RW_WRITER); @@ -5778,7 +5781,7 @@ zfs_setextattr(struct vop_setextattr_args *ap) rw_exit(&zp->z_xattr_lock); zfs_exit(zfsvfs, FTAG); - zfs_attrname_maybe_free(maybe_renamed, origname); + zfs_dup_attrname_free(maybe_renamed, origname); return (error); }