Skip to contentSkip to conversation

fscrypt: Add FSTR() helper macro and convert initializers

PATCH In review Thorsten Blum 4 participants 9 messages Nov 18 – Nov 19

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

cover fscrypt: Add FSTR() helper macro and convert initializers

Similar to QSTR(), add FSTR() to initialize struct fscrypt_str from a NUL-terminated string without specifying its length.

Use the helper for 8 initializers in btrfs, F2FS, and UBIFS, replacing hard-coded lengths and explicit strlen() calls.

Thorsten Blum (4):
fscrypt: Add FSTR() helper macro
btrfs: Use FSTR() for the "default" name
f2fs: Use FSTR() in f2fs_do_make_empty_dir()
ubifs: Use FSTR() in ubifs_xattr_{set,get,remove}()

 fs/btrfs/inode.c        | 3 +--
 fs/btrfs/ioctl.c        | 3 +--
 fs/btrfs/super.c        | 3 +--
 fs/f2fs/dir.c           | 7 ++-----
 fs/ubifs/xattr.c        | 6 +++---
 include/linux/fscrypt.h | 1 +
 6 files changed, 9 insertions(+), 14 deletions(-)
#1 lore raw

PATCH 1/4 fscrypt: Add FSTR() helper macro

Similar to QSTR(), add FSTR() to initialize struct fscrypt_str from a NUL-terminated string without specifying its length.

---
 include/linux/fscrypt.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/fscrypt.h +1 -0
index 28f2108e29f3..84d9147fda2f 100644
@@ -50,6 +50,7 @@ struct fscrypt_name {
 };
 
 #define FSTR_INIT(n, l)		{ .name = n, .len = l }
+#define FSTR(n)			((struct fscrypt_str)FSTR_INIT(n, strlen(n)))
 #define FSTR_TO_QSTR(f)		QSTR_INIT((f)->name, (f)->len)
 #define fname_name(p)		((p)->disk_name.name)
 #define fname_len(p)		((p)->disk_name.len)
#2 lore raw

PATCH 2/4 btrfs: Use FSTR() for the "default" name

In reply to Thorsten Blum

Replace FSTR_INIT() with FSTR() and drop the hard-coded length argument.

Use FSTR() directly and remove the temporary name variables.

---
 fs/btrfs/inode.c | 3 +--
 fs/btrfs/ioctl.c | 3 +--
 fs/btrfs/super.c | 3 +--
 3 files changed, 3 insertions(+), 6 deletions(-)
fs/btrfs/inode.c +1 -2
index abaa33471456..1ffdcdd16bb5 100644
@@ -4640,7 +4640,6 @@ static noinline int may_destroy_subvol(struct btrfs_root *root)
 	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_dir_item *di;
 	struct btrfs_key key;
-	struct fscrypt_str name = FSTR_INIT("default", 7);
 	u64 dir_id;
 	int ret;
 
@@ -4651,7 +4650,7 @@ static noinline int may_destroy_subvol(struct btrfs_root *root)
 	/* Make sure this root isn't set as the default subvol */
 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
 	di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
-				   dir_id, &name, 0);
+				   dir_id, &FSTR("default"), 0);
 	if (!IS_ERR_OR_NULL(di)) {
 		btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
 		if (key.objectid == btrfs_root_id(root)) {
fs/btrfs/ioctl.c +1 -2
index 6ba883db107b..6c371621f014 100644
@@ -2825,7 +2825,6 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
 	struct btrfs_trans_handle *trans;
 	struct btrfs_path *path = NULL;
 	struct btrfs_disk_key disk_key;
-	struct fscrypt_str name = FSTR_INIT("default", 7);
 	u64 objectid = 0;
 	u64 dir_id;
 	int ret;
@@ -2869,7 +2868,7 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
 
 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
 	di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
-				   dir_id, &name, 1);
+				   dir_id, &FSTR("default"), 1);
 	if (IS_ERR_OR_NULL(di)) {
 		btrfs_release_path(path);
 		btrfs_end_transaction(trans);
fs/btrfs/super.c +1 -2
index b424cc836bd4..0b8b808872c7 100644
@@ -911,7 +911,6 @@ static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objec
 	struct btrfs_dir_item *di;
 	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key location;
-	struct fscrypt_str name = FSTR_INIT("default", 7);
 	u64 dir_id;
 
 	path = btrfs_alloc_path();
@@ -924,7 +923,7 @@ static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objec
 	 * to mount.
 	 */
 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
-	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0);
+	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &FSTR("default"), 0);
 	if (IS_ERR(di)) {
 		return PTR_ERR(di);
 	}
#4 lore raw

PATCH 3/4 f2fs: Use FSTR() in f2fs_do_make_empty_dir()

In reply to Thorsten Blum

Replace FSTR_INIT() with FSTR() and drop the hard-coded length argument.

Use FSTR() directly and remove the temporary variables.

---
 fs/f2fs/dir.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/f2fs/dir.c +2 -5
index 97f7fef6b068..469c4cc7a3b7 100644
@@ -515,14 +515,11 @@ static void init_dent_inode(struct inode *dir, struct inode *inode,
 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
 					struct f2fs_dentry_ptr *d)
 {
-	struct fscrypt_str dot = FSTR_INIT(".", 1);
-	struct fscrypt_str dotdot = FSTR_INIT("..", 2);
-
 	/* update dirent of "." */
-	f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
+	f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &FSTR("."), 0, 0);
 
 	/* update dirent of ".." */
-	f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
+	f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &FSTR(".."), 0, 1);
 }
 
 static int make_empty_dir(struct inode *inode,
#5 lore raw

PATCH 4/4 ubifs: Use FSTR() in ubifs_xattr_{set,get,remove}()

In reply to Thorsten Blum

Replace FSTR_INIT() with FSTR() and drop the explicit length argument.

---
 fs/ubifs/xattr.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/ubifs/xattr.c +3 -3
index 3b0e8a270f59..036eb7e2be0b 100644
@@ -252,7 +252,7 @@ int ubifs_xattr_set(struct inode *host, const char *name, const void *value,
 {
 	struct inode *inode;
 	struct ubifs_info *c = host->i_sb->s_fs_info;
-	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
+	struct fscrypt_name nm = { .disk_name = FSTR((char *)name) };
 	struct ubifs_dent_node *xent;
 	union ubifs_key key;
 	int err;
@@ -315,7 +315,7 @@ ssize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf,
 {
 	struct inode *inode;
 	struct ubifs_info *c = host->i_sb->s_fs_info;
-	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
+	struct fscrypt_name nm = { .disk_name = FSTR((char *)name) };
 	struct ubifs_inode *ui;
 	struct ubifs_dent_node *xent;
 	union ubifs_key key;
@@ -557,7 +557,7 @@ static int ubifs_xattr_remove(struct inode *host, const char *name)
 {
 	struct inode *inode;
 	struct ubifs_info *c = host->i_sb->s_fs_info;
-	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
+	struct fscrypt_name nm = { .disk_name = FSTR((char *)name) };
 	struct ubifs_dent_node *xent;
 	union ubifs_key key;
 	int err;
#6 lore raw

Re: [PATCH 4/4] ubifs: Use FSTR() in ubifs_xattr_{set,get,remove}()

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] ubifs: missing read-only and error…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] ubifs: missing read-only and error state checks in fileattr and xattr setters [1]
--

commit b8fcd8b365f77753007ac6808d8a6b998a34c773
Author: Thorsten Blum <[email protected]>

ubifs: Use FSTR() in ubifs_xattr_{set,get,remove}()

This commit replaces FSTR_INIT(..., strlen(name)) with the FSTR() macro in ubifs_xattr_set(), ubifs_xattr_get(), and ubifs_xattr_remove().

Quoted 13 lines
fs/ubifs/xattr.c +1 -1
index 3b0e8a270f594..036eb7e2be0b2 100644
@@ -252,7 +252,7 @@ int ubifs_xattr_set(struct inode *host, const char *name, const void *value,
 {
 	struct inode *inode;
 	struct ubifs_info *c = host->i_sb->s_fs_info;
-	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
+	struct fscrypt_name nm = { .disk_name = FSTR((char *)name) };
 	struct ubifs_dent_node *xent;
 	union ubifs_key key;
 	int err;

[ ... ]

fs/ubifs/xattr.c static int ubifs_xattr_remove(struct inode *host, const char *name) · line 563

Quoted 9 lines
fs/ubifs/xattr.c +1 -1
@@ -557,7 +557,7 @@ static int ubifs_xattr_remove(struct inode *host, const char *name)
 {
 	struct inode *inode;
 	struct ubifs_info *c = host->i_sb->s_fs_info;
-	struct fscrypt_name nm = { .disk_name = FSTR_INIT((char *)name, strlen(name))};
+	struct fscrypt_name nm = { .disk_name = FSTR((char *)name) };
 	struct ubifs_dent_node *xent;
 	union ubifs_key key;
 	int err;

[Severity: High]
This problem wasn't introduced by this patch, but can ubifs_xattr_set(), ubifs_xattr_remove(), and setflags() (via ubifs_fileattr_set()) mutate state on a read-only or errored UBIFS filesystem [1]?

A stacked filesystem such as ecryptfs can call vfs_setxattr(), vfs_removexattr(), or vfs_fileattr_set() on a lower UBIFS mount without acquiring write access on the lower mount, and those VFS helpers dispatch into UBIFS without rejecting read-only or emergency read-only lower superblocks.

ubifs_xattr_set() and ubifs_xattr_remove() then call create_xattr(), change_xattr(), and remove_xattr(), which mutate in-memory host and xattr inode state before reaching reserve_space(), where ubifs_assert(c, !c->ro_media && !c->ro_mount) fires, c->ro_error is set, and the xattr inode is marked bad. Similarly, setflags() in fs/ubifs/ioctl.c allocates dirty inode budget via ubifs_budget_space() and dirties the inode without checking c->ro_mount, c->ro_media, or c->ro_error, leaking c->bi.dd_growth on a read-only mount and tripping ubifs_assert(c, c->bi.dd_growth == 0) at unmount.

[1] https://sashiko.dev/#/bug/linux-5bb2275d-76e5-4d6a-b814-a7ac921778bf

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#7 lore raw

Re: [PATCH 4/4] ubifs: Use FSTR() in ubifs_xattr_{set,get,remove}()

In reply to Thorsten Blum
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] ubifs: missing read-only and error…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] ubifs: missing read-only and error state checks in fileattr and xattr setters [1]
--

This problem wasn't introduced by this patch, but can ubifs_xattr_set(), ubifs_xattr_remove(), and setflags() (via ubifs_fileattr_set()) mutate state on a read-only or errored UBIFS filesystem [1]?

A stacked filesystem such as ecryptfs can call vfs_setxattr(), vfs_removexattr(), or vfs_fileattr_set() on a lower UBIFS mount without acquiring write access on the lower mount, and those VFS helpers dispatch into UBIFS without rejecting read-only or emergency read-only lower superblocks.

ubifs_xattr_set() and ubifs_xattr_remove() then call create_xattr(), change_xattr(), and remove_xattr(), which mutate in-memory host and xattr inode state before reaching reserve_space(), where ubifs_assert(c, !c->ro_media && !c->ro_mount) fires, c->ro_error is set, and the xattr inode is marked bad. Similarly, setflags() in fs/ubifs/ioctl.c allocates dirty inode budget via ubifs_budget_space() and dirties the inode without checking c->ro_mount, c->ro_media, or c->ro_error, leaking c->bi.dd_growth on a read-only mount and tripping ubifs_assert(c, c->bi.dd_growth == 0) at unmount.

[1] https://sashiko.dev/#/bug/linux-5bb2275d-76e5-4d6a-b814-a7ac921778bf

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
maintainer
#8 lore raw
In reply to Thorsten Blum
Quoted 5 lines · Thorsten Blum

Similar to QSTR(), add FSTR() to initialize struct fscrypt_str from a NUL-terminated string without specifying its length.

Use the helper for 8 initializers in btrfs, F2FS, and UBIFS, replacing hard-coded lengths and explicit strlen() calls.

It would be worth stating explicitly that the new strlen() calls are constant-folded in most configurations (if they indeed are).

- Eric