Skip to contentSkip to conversation

hfsplus: make hidden directory name independent from nls

PATCH New Viacheslav Dubeyko 1 participant 3 messages Nov 4

Subsystems (3)
lore
Share & tools
lore

Viacheslav Dubeyko

author maintainer
#0 lore raw

cover hfsplus: make hidden directory name independent from nls

HFS+ keeps the indirect node files of hard links in the hidden directory of the root folder. On disk, its name is four U+0000 (NUL) characters followed by "HFS+ Private Data". Currently, the catalog key of this directory is built from the UTF-8 string HFSP_HIDDENDIR_NAME by means of the nls table of the mount. hfsplus_fill_super() works around it by temporarily replacing sbi->nls with utf8 table, but hfsplus_reconfigure() creates the hidden directory on remount read-write with the mount's nls table. As a result, with nls=cp437, for example, the hidden directory is created with garbled name that neither macOS nor the next mount recognizes.

At first, define the on-disk name of the hidden directory as constant UTF-16 string hfsplus_hidden_dir_name_utf16, introduces hfsplus_create_cat_uni() that creates catalog records for already converted name, and removes the temporary nls replacement from hfsplus_fill_super(). The length of the name is calculated from named constants, and the UTF-8 and UTF-16 representations of the name are distinguished by the HFSP_HIDDENDIR_NAME_UTF8* and HFSP_HIDDENDIR_NAME_UTF16_LEN macros.

Secondly, add KUnit test that checks that hfsplus_asc2uni() converts HFSP_HIDDENDIR_NAME_UTF8 with utf8 nls table into exactly hfsplus_hidden_dir_name_utf16, with decomposition enabled and disabled.

The hfsplus_unicode KUnit suite passes (35 tests) under UML.

Viacheslav Dubeyko (2):
hfsplus: make hidden directory name independent from mount's nls
hfsplus: add KUnit check for hidden directory name

 fs/hfsplus/catalog.c       | 83 ++++++++++++++++++++++++++++++--------
 fs/hfsplus/hfsplus_fs.h    |  6 +++
 fs/hfsplus/super.c         | 68 +++++++++++++++++--------------
 fs/hfsplus/unicode_test.c  | 55 +++++++++++++++++++++++++
 include/linux/hfs_common.h | 20 ++++++++-
 5 files changed, 182 insertions(+), 50 deletions(-)

base-commit: 71d5a8a05c83b61bce1884905c192eed34ed3069

Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 1/2 hfsplus: make hidden directory name independent from mount's nls

The catalog key of the hidden directory is built from HFSP_HIDDENDIR_NAME, that is UTF-8 string, by means of the nls table of the mount. hfsplus_fill_super() works around it by temporarily replacing sbi->nls with utf8 table. However, hfsplus_reconfigure() creates the hidden directory on remount read-write with the mount's nls table. As a result, for example, with nls=cp437 the hidden directory is created with garbled name that neither macOS nor the next mount recognizes, and yet another hidden directory is created on the next read-write mount. Temporary replacement of sbi->nls is not an option in hfsplus_reconfigure() because lookups are running concurrently and use sbi->nls.

Define the hidden directory name as constant Unicode string and introduce hfsplus_create_cat_uni() that creates catalog records for already converted name. Use hfsplus_cat_build_key_uni() for hidden directory lookup and remove the temporary nls replacement from hfsplus_fill_super(). The rollback in hfsplus_create_hidden_dir() calls hfsplus_delete_cat() with NULL name, so the name is taken from the thread record.

cc John Paul Adrian Glaubitz <[email protected]>
cc Yangtao Li <[email protected]>
---
 fs/hfsplus/catalog.c       | 83 ++++++++++++++++++++++++++++++--------
 fs/hfsplus/hfsplus_fs.h    |  5 +++
 fs/hfsplus/super.c         | 66 ++++++++++++++++--------------
 include/linux/hfs_common.h | 20 ++++++++-
 4 files changed, 124 insertions(+), 50 deletions(-)
fs/hfsplus/catalog.c +66 -17
index fe11c4b6dd99..5e53a978d420 100644
@@ -40,8 +40,8 @@ int hfsplus_cat_bin_cmp_key(const hfsplus_btree_key *k1,
 }
 
 /* Generates key for catalog file/folders record. */
-int hfsplus_cat_build_key(struct super_block *sb,
-		hfsplus_btree_key *key, u32 parent, const struct qstr *str)
+int hfsplus_cat_build_key(struct super_block *sb, hfsplus_btree_key *key,
+			  u32 parent, const struct qstr *str)
 {
 	int len, err;
 
@@ -58,15 +58,15 @@ int hfsplus_cat_build_key(struct super_block *sb,
 
 /* Generates key for catalog thread record. */
 void hfsplus_cat_build_key_with_cnid(struct super_block *sb,
-			hfsplus_btree_key *key, u32 parent)
+				     hfsplus_btree_key *key, u32 parent)
 {
 	key->cat.parent = cpu_to_be32(parent);
 	key->cat.name.length = 0;
 	key->key_len = cpu_to_be16(6);
 }
 
-static void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
-				      struct hfsplus_unistr *name)
+void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
+			       const struct hfsplus_unistr *name)
 {
 	int ustrlen;
 
@@ -103,7 +103,7 @@ void hfsplus_cat_set_perms(struct inode *inode, struct hfsplus_perm *perms)
 }
 
 static int hfsplus_cat_build_record(hfsplus_cat_entry *entry,
-		u32 cnid, struct inode *inode)
+				    u32 cnid, struct inode *inode)
 {
 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
 
@@ -187,7 +187,23 @@ static int hfsplus_fill_cat_thread(struct super_block *sb,
 	if (unlikely(err < 0))
 		return err;
 
-	return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
+	return hfsplus_cat_thread_size(&entry->thread);
+}
+
+static int hfsplus_fill_cat_thread_uni(hfsplus_cat_entry *entry, int type,
+				       u32 parentid,
+				       const struct hfsplus_unistr *name)
+{
+	u16 len = be16_to_cpu(name->length);
+
+	entry->type = cpu_to_be16(type);
+	entry->thread.reserved = 0;
+	entry->thread.parentID = cpu_to_be32(parentid);
+	entry->thread.nodeName.length = name->length;
+	memcpy(entry->thread.nodeName.unicode, name->unicode,
+	       len * sizeof(hfsplus_unichr));
+
+	return hfsplus_cat_thread_size(&entry->thread);
 }
 
 /* Try to get a catalog entry for given catalog id */
@@ -250,17 +266,25 @@ static void hfsplus_subfolders_dec(struct inode *dir)
 	}
 }
 
-int hfsplus_create_cat(u32 cnid, struct inode *dir,
-		const struct qstr *str, struct inode *inode)
+/*
+ * Create catalog records for the name given either as @str (it is converted
+ * by means of the mount's nls table) or as already converted @name.
+ */
+static int __hfsplus_create_cat(u32 cnid, struct inode *dir,
+				const struct qstr *str,
+				const struct hfsplus_unistr *name,
+				struct inode *inode)
 {
 	struct super_block *sb = dir->i_sb;
 	struct hfs_find_data fd;
 	hfsplus_cat_entry entry;
+	int type;
 	int entry_size;
 	int err;
 
 	hfs_dbg("name %s, cnid %u, i_nlink %d\n",
-		str->name, cnid, inode->i_nlink);
+		str ? (const char *)str->name : "<unicode>",
+		cnid, inode->i_nlink);
 	err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
 	if (err)
 		return err;
@@ -274,10 +298,14 @@ int hfsplus_create_cat(u32 cnid, struct inode *dir,
 		goto err2;
 
 	hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
-	entry_size = hfsplus_fill_cat_thread(sb, &entry,
-		S_ISDIR(inode->i_mode) ?
-			HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
-		dir->i_ino, str);
+	type = S_ISDIR(inode->i_mode) ?
+			HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD;
+	if (name)
+		entry_size = hfsplus_fill_cat_thread_uni(&entry, type,
+							 dir->i_ino, name);
+	else
+		entry_size = hfsplus_fill_cat_thread(sb, &entry, type,
+						     dir->i_ino, str);
 	if (unlikely(entry_size < 0)) {
 		err = entry_size;
 		goto err2;
@@ -293,9 +321,13 @@ int hfsplus_create_cat(u32 cnid, struct inode *dir,
 	if (err)
 		goto err2;
 
-	err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
-	if (unlikely(err))
-		goto err1;
+	if (name)
+		hfsplus_cat_build_key_uni(fd.search_key, dir->i_ino, name);
+	else {
+		err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
+		if (unlikely(err))
+			goto err1;
+	}
 
 	entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
 	err = hfs_brec_find(&fd, hfs_find_rec_by_key);
@@ -328,6 +360,23 @@ int hfsplus_create_cat(u32 cnid, struct inode *dir,
 	return err;
 }
 
+int hfsplus_create_cat(u32 cnid, struct inode *dir,
+			const struct qstr *str, struct inode *inode)
+{
+	return __hfsplus_create_cat(cnid, dir, str, NULL, inode);
+}
+
+/*
+ * Create catalog records for the name that is independent from
+ * the mount's nls table (for example, the hidden directory name).
+ */
+int hfsplus_create_cat_uni(u32 cnid, struct inode *dir,
+			   const struct hfsplus_unistr *name,
+			   struct inode *inode)
+{
+	return __hfsplus_create_cat(cnid, dir, NULL, name, inode);
+}
+
 int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str)
 {
 	struct super_block *sb = dir->i_sb;
fs/hfsplus/hfsplus_fs.h +5 -0
index 6f240f87f8ff..e2d3f488142b 100644
@@ -418,11 +418,16 @@ int hfsplus_cat_build_key(struct super_block *sb, hfsplus_btree_key *key,
 			   u32 parent, const struct qstr *str);
 void hfsplus_cat_build_key_with_cnid(struct super_block *sb,
 				     hfsplus_btree_key *key, u32 parent);
+void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
+			       const struct hfsplus_unistr *name);
 void hfsplus_cat_set_perms(struct inode *inode, struct hfsplus_perm *perms);
 int hfsplus_find_cat(struct super_block *sb, u32 cnid,
 		     struct hfs_find_data *fd);
 int hfsplus_create_cat(u32 cnid, struct inode *dir, const struct qstr *str,
 		       struct inode *inode);
+int hfsplus_create_cat_uni(u32 cnid, struct inode *dir,
+			   const struct hfsplus_unistr *name,
+			   struct inode *inode);
 int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str);
 int hfsplus_rename_cat(u32 cnid, struct inode *src_dir, const struct qstr *src_name,
 		       struct inode *dst_dir, const struct qstr *dst_name);
fs/hfsplus/super.c +35 -31

158 lines not on this page · raw message

include/linux/hfs_common.h +18 -2
index d6a615e74b26..8a7bc9763eb9 100644
@@ -57,8 +57,24 @@
 
 #define HFSP_MOUNT_VERSION		0x482b4c78	/* 'H+Lx' */
 
-#define HFSP_HIDDENDIR_NAME \
-	"\xe2\x90\x80\xe2\x90\x80\xe2\x90\x80\xe2\x90\x80HFS+ Private Data"
+/*
+ * The name of the hidden directory is HFSP_HIDDENDIR_NUL_COUNT U+0000
+ * characters followed by HFSP_HIDDENDIR_SUFFIX. On disk, it is stored
+ * in UTF-16 and has HFSP_HIDDENDIR_NAME_UTF16_LEN characters. In Linux,
+ * it is HFSP_HIDDENDIR_NAME_UTF8, where every U+0000 is represented by
+ * U+2400 (SYMBOL FOR NULL); it has HFSP_HIDDENDIR_NAME_UTF8_LEN bytes.
+ */
+#define HFSP_HIDDENDIR_NUL_COUNT	4
+#define HFSP_HIDDENDIR_SUFFIX		"HFS+ Private Data"
+#define HFSP_HIDDENDIR_SUFFIX_LEN	(sizeof(HFSP_HIDDENDIR_SUFFIX) - 1)
+#define HFSP_HIDDENDIR_NAME_UTF16_LEN \
+	(HFSP_HIDDENDIR_NUL_COUNT + HFSP_HIDDENDIR_SUFFIX_LEN)
+#define HFSP_SYMBOL_FOR_NULL_UTF8	"\xe2\x90\x80"
+#define HFSP_HIDDENDIR_NAME_UTF8 \
+	(HFSP_SYMBOL_FOR_NULL_UTF8 HFSP_SYMBOL_FOR_NULL_UTF8 \
+	 HFSP_SYMBOL_FOR_NULL_UTF8 HFSP_SYMBOL_FOR_NULL_UTF8 \
+	 HFSP_HIDDENDIR_SUFFIX)
+#define HFSP_HIDDENDIR_NAME_UTF8_LEN	(sizeof(HFSP_HIDDENDIR_NAME_UTF8) - 1)
 
 /* various FIXED size parameters */
 #define HFS_SECTOR_SIZE			512	/* size of an HFS sector */
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/2 hfsplus: add KUnit check for hidden directory name

In reply to Viacheslav Dubeyko

The hidden directory is created and looked up by means of precomputed UTF-16 name hfsplus_hidden_dir_name_utf16, while its Linux representation HFSP_HIDDENDIR_NAME_UTF8 is still used elsewhere. Add regression test that checks that hfsplus_asc2uni() converts HFSP_HIDDENDIR_NAME_UTF8 with utf8 nls table into exactly the same UTF-16 string, with decomposition enabled and disabled. In particular, it checks that every U+2400 (SYMBOL FOR NULL) of HFSP_HIDDENDIR_NAME_UTF8 is stored as U+0000 (NUL), as macOS expects, and that the length of the converted name is HFSP_HIDDENDIR_NAME_UTF16_LEN.

cc John Paul Adrian Glaubitz <[email protected]>
cc Yangtao Li <[email protected]>
---
 fs/hfsplus/hfsplus_fs.h   |  1 +
 fs/hfsplus/super.c        |  4 ++-
 fs/hfsplus/unicode_test.c | 55 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 1 deletion(-)
fs/hfsplus/hfsplus_fs.h +1 -0
index e2d3f488142b..dd88053cc9b3 100644
@@ -487,6 +487,7 @@ int hfs_part_find(struct super_block *sb, sector_t *part_start,
 		  sector_t *part_size);
 
 /* super.c */
+extern const struct hfsplus_unistr hfsplus_hidden_dir_name_utf16;
 struct inode *hfsplus_iget(struct super_block *sb, unsigned long ino);
 void hfsplus_mark_mdb_dirty(struct super_block *sb);
 void hfsplus_prepare_volume_header_for_commit(struct hfsplus_vh *vhdr);
fs/hfsplus/super.c +3 -1
index 57858d8685ff..4e878641fb59 100644
@@ -18,6 +18,7 @@
 #include <linux/slab.h>
 #include <linux/vfs.h>
 #include <linux/nls.h>
+#include <kunit/visibility.h>
 
 static struct inode *hfsplus_alloc_inode(struct super_block *sb);
 static void hfsplus_free_inode(struct inode *inode);
@@ -406,7 +407,7 @@ static int hfsplus_statfs(struct dentry *dentry, struct kstatfs *buf)
  * by U+2400 (SYMBOL FOR NULL). The catalog key of the hidden directory
  * must not depend on the nls table of the mount.
  */
-static const struct hfsplus_unistr hfsplus_hidden_dir_name_utf16 = {
+const struct hfsplus_unistr hfsplus_hidden_dir_name_utf16 = {
 	.length = cpu_to_be16(HFSP_HIDDENDIR_NAME_UTF16_LEN),
 	.unicode = {
 		/* unicode[0 .. HFSP_HIDDENDIR_NUL_COUNT - 1] are U+0000 */
@@ -420,6 +421,7 @@ static const struct hfsplus_unistr hfsplus_hidden_dir_name_utf16 = {
 		cpu_to_be16('a'),
 	},
 };
+EXPORT_SYMBOL_IF_KUNIT(hfsplus_hidden_dir_name_utf16);
 
 /*
  * Create the hidden directory that keeps the indirect node files
fs/hfsplus/unicode_test.c +55 -0

67 lines not on this page · raw message

Signature · 1 line
2.43.0