Skip to contentSkip to conversation

ufs: validate maximum fast symlink size from superblock

PATCH Superseded Duane Griffin 3 participants 9 messages Dec 31, 2008 – Jan 4, 2009

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 ufs: validate maximum fast symlink size from superblock

The maximum fast symlink size is set in the superblock of certain types of UFS filesystem. Before using it we need to check that it isn't longer than the available space we have in the inode.

---
 fs/ufs/super.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
fs/ufs/super.c +11 -0
index e65212d..a959cdc 100644
@@ -636,6 +636,7 @@ static int ufs_fill_super(struct super_block *sb, void *data, int silent)
 	unsigned block_size, super_block_size;
 	unsigned flags;
 	unsigned super_block_offset;
+	unsigned maxsymlen;
 	int ret = -EINVAL;
 
 	uspi = NULL;
@@ -1069,6 +1070,16 @@ magic_found:
 		uspi->s_maxsymlinklen =
 		    fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
 
+	if (uspi->fs_magic == UFS2_MAGIC)
+		maxsymlen = 2 * 4 * (UFS_NDADDR + UFS_NINDIR);
+	else
+		maxsymlen = 4 * (UFS_NDADDR + UFS_NINDIR);
+	if (uspi->s_maxsymlinklen > maxsymlen) {
+		printk(KERN_WARNING "ufs_read_super: excessive maximum fast "
+		       "symlink size (%u)\n", uspi->s_maxsymlinklen);
+		uspi->s_maxsymlinklen = maxsymlen;
+	}
+
 	inode = ufs_iget(sb, UFS_ROOTINO);
 	if (IS_ERR(inode)) {
 		ret = PTR_ERR(inode);
Signature · 1 line
1.6.0.6
#1 lore raw

PATCH 2/4 ufs: don't truncate longer ufs2 fast symlinks

ufs2 fast symlinks can be twice as long as ufs ones, however the code was using the ufs size in various places. Fix that so ufs2 symlinks over 60 characters aren't truncated.

---
 fs/ufs/inode.c |    4 ++--
 fs/ufs/ufs.h   |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/ufs/inode.c +2 -2
index 39f8778..c1ea916 100644
@@ -708,7 +708,7 @@ static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
 			ufsi->i_u1.u2_i_data[i] =
 				ufs2_inode->ui_u2.ui_addr.ui_db[i];
 	} else {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
+		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4 * 2; i++)
 			ufsi->i_u1.i_symlink[i] = ufs2_inode->ui_u2.ui_symlink[i];
 	}
 	return 0;
@@ -853,7 +853,7 @@ static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
 		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
 			ufs_inode->ui_u2.ui_addr.ui_db[i] = ufsi->i_u1.u2_i_data[i];
 	} else {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
+		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4 * 2; i++)
 			ufs_inode->ui_u2.ui_symlink[i] = ufsi->i_u1.i_symlink[i];
  	}
 
fs/ufs/ufs.h +1 -1
index 11c0351..69b3427 100644
@@ -23,7 +23,7 @@ struct ufs_sb_info {
 struct ufs_inode_info {
 	union {
 		__fs32	i_data[15];
-		__u8	i_symlink[4*15];
+		__u8	i_symlink[2 * 4 * 15];
 		__fs64	u2_i_data[15];
 	} i_u1;
 	__u32	i_flags;
Signature · 1 line
1.6.0.6
#2 lore raw

PATCH 3/4 ufs: ensure fast symlinks are NUL-terminated

Ensure fast symlink targets are NUL-terminated, even if corrupted on-disk.

---
 fs/ufs/inode.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
fs/ufs/inode.c +4 -2
index c1ea916..c2edfb6 100644
@@ -658,8 +658,9 @@ static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
 		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
 			ufsi->i_u1.i_data[i] = ufs_inode->ui_u2.ui_addr.ui_db[i];
 	} else {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
+		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4 - 1; i++)
 			ufsi->i_u1.i_symlink[i] = ufs_inode->ui_u2.ui_symlink[i];
+		ufsi->i_u1.i_symlink[(UFS_NDADDR + UFS_NINDIR) * 4 - 1] = 0;
 	}
 	return 0;
 }
@@ -708,8 +709,9 @@ static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
 			ufsi->i_u1.u2_i_data[i] =
 				ufs2_inode->ui_u2.ui_addr.ui_db[i];
 	} else {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4 * 2; i++)
+		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4 * 2 - 1; i++)
 			ufsi->i_u1.i_symlink[i] = ufs2_inode->ui_u2.ui_symlink[i];
+		ufsi->i_u1.i_symlink[(UFS_NDADDR + UFS_NINDIR) * 4 * 2 - 1] = 0;
 	}
 	return 0;
 }
Signature · 1 line
1.6.0.6
#3 lore raw

PATCH 4/4 ufs: copy symlink data into the correct union member

Copy symlink data into the union member it is accessed through. Although this shouldn't make a difference to behaviour it makes the code easier to follow and grep through. It may also prevent problems if the struct/union definitions change in the future.

---
 fs/ufs/namei.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
fs/ufs/namei.c +1 -1
index e3a9b1f..3884730 100644
@@ -147,7 +147,7 @@ static int ufs_symlink (struct inode * dir, struct dentry * dentry,
 	} else {
 		/* fast symlink */
 		inode->i_op = &ufs_fast_symlink_inode_operations;
-		memcpy((char*)&UFS_I(inode)->i_u1.i_data,symname,l);
+		memcpy((char *) &UFS_I(inode)->i_u1.i_symlink, symname, l);
 		inode->i_size = l-1;
 	}
 	mark_inode_dirty(inode);
Signature · 1 line
1.6.0.6
#4 lore raw

Re: [PATCH 4/4] ufs: copy symlink data into the correct union member

Quoted 15 lines · Duane Griffin

Copy symlink data into the union member it is accessed through. Although this shouldn't make a difference to behaviour it makes the code easier to follow and grep through. It may also prevent problems if the struct/union definitions change in the future.

---
 fs/ufs/namei.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
fs/ufs/namei.c +0 -0
index e3a9b1f..3884730 100644

fs/ufs/namei.c static int ufs_symlink (struct inode * dir, struct dentry * dentry, · line 150

Quoted 5 lines
fs/ufs/namei.c +1 -1
@@ -147,7 +147,7 @@ static int ufs_symlink (struct inode * dir, struct dentry * dentry,
 	} else {
 		/* fast symlink */
 		inode->i_op = &ufs_fast_symlink_inode_operations;
-		memcpy((char*)&UFS_I(inode)->i_u1.i_data,symname,l);
+		memcpy((char *) &UFS_I(inode)->i_u1.i_symlink, symname, l);

Just what are these cast and & doing there? i_symlink is an array of u8, for fsck sake; taking its address is a pointless obfuscation. And memcpy() is just as fine with u8 * as it is with char *...

#5 lore raw

Re: [PATCH 4/4] ufs: copy symlink data into the correct union member

Quoted 11 lines · Al Viro
Quoted 10 lines · Duane Griffin

Copy symlink data into the union member it is accessed through. Although this shouldn't make a difference to behaviour it makes the code easier to follow and grep through. It may also prevent problems if the struct/union definitions change in the future.

---
 fs/ufs/namei.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Quoted 14 lines

fs/ufs/namei.c static int ufs_symlink (struct inode * dir, struct dentry * dentry, · line 150

Quoted 10 lines
fs/ufs/namei.c +1 -1
index e3a9b1f..3884730 100644
@@ -147,7 +147,7 @@ static int ufs_symlink (struct inode * dir, struct dentry * dentry,
 	} else {
 		/* fast symlink */
 		inode->i_op = &ufs_fast_symlink_inode_operations;
-		memcpy((char*)&UFS_I(inode)->i_u1.i_data,symname,l);
+		memcpy((char *) &UFS_I(inode)->i_u1.i_symlink, symname, l);

Just what are these cast and & doing there? i_symlink is an array of u8, for fsck sake; taking its address is a pointless obfuscation. And memcpy() is just as fine with u8 * as it is with char *...

Ahem. Good points. I neglected to clean-up properly, sorry. Will redo and resend once we've got agreement on the other issues.

Cheers,
Duane.

Signature · 1 line
"I never could learn to drink that blood and call it wine" - Bob Dylan
#6 lore raw

Re: [PATCH 2/4] ufs: don't truncate longer ufs2 fast symlinks

In reply to Duane Griffin

fs/ufs/inode.c static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode) · line 712

Quoted 4 lines · Duane Griffin
fs/ufs/inode.c +1 -1
 	} else {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
+		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4 * 2; i++)
 			ufsi->i_u1.i_symlink[i] = ufs2_inode->ui_u2.ui_symlink[i];

a) that probably ought to be a memcpy()
b) max len is superblock parameter and you've even validated it. Why don't you use it?

fs/ufs/inode.c static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode) · line 857

Quoted 3 lines
fs/ufs/inode.c +1 -1
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
+		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4 * 2; i++)
 			ufs_inode->ui_u2.ui_symlink[i] = ufsi->i_u1.i_symlink[i];

Ditto.

#7 lore raw

Re: [PATCH 2/4] ufs: don't truncate longer ufs2 fast symlinks

Quoted 7 lines · Al Viro

Review

Quoted 4 lines · Duane Griffin
(quoted diff) +1 -1
 	} else {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
+		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4 * 2; i++)
 			ufsi->i_u1.i_symlink[i] = ufs2_inode->ui_u2.ui_symlink[i];

a) that probably ought to be a memcpy()

OK, see below. I've changed the other close-by loops as well, for consistency. Note however that they were copying an entire structure (consisting of two arrays of the same type) whilst seeming to copy only the first member. So the patch looks like it is changing behaviour, but actually should not. Please let me know if you prefer this; I'll resend the series if so.

Quoted 2 lines

b) max len is superblock parameter and you've even validated it. Why don't you use it?

It is only validated to an upper bound. If it is incorrectly set to zero, say, this way at least the link names will show up. True, we copy a bit of extra data around, but since it isn't exactly a fast path I figured it was worth it for the extra reliability.

Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +1 -1
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
+		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4 * 2; i++)
 			ufs_inode->ui_u2.ui_symlink[i] = ufsi->i_u1.i_symlink[i];

Ditto.

---

fs/ufs/inode.c +16 -21
index 39f8778..ac8b324 100644
@@ -622,7 +622,6 @@ static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
 	struct ufs_inode_info *ufsi = UFS_I(inode);
 	struct super_block *sb = inode->i_sb;
 	mode_t mode;
-	unsigned i;
 
 	/*
 	 * Copy data to the in-core inode.
@@ -655,11 +654,11 @@ static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
 
 	
 	if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
-			ufsi->i_u1.i_data[i] = ufs_inode->ui_u2.ui_addr.ui_db[i];
+		memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr,
+		       sizeof(ufs_inode->ui_u2.ui_addr));
 	} else {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
-			ufsi->i_u1.i_symlink[i] = ufs_inode->ui_u2.ui_symlink[i];
+		memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink,
+		       sizeof(ufs_inode->ui_u2.ui_symlink));
 	}
 	return 0;
 }
@@ -669,7 +668,6 @@ static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
 	struct ufs_inode_info *ufsi = UFS_I(inode);
 	struct super_block *sb = inode->i_sb;
 	mode_t mode;
-	unsigned i;
 
 	UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
 	/*
@@ -704,12 +702,11 @@ static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
 	*/
 
 	if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
-			ufsi->i_u1.u2_i_data[i] =
-				ufs2_inode->ui_u2.ui_addr.ui_db[i];
+		memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr,
+		       sizeof(ufs2_inode->ui_u2.ui_addr));
 	} else {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
-			ufsi->i_u1.i_symlink[i] = ufs2_inode->ui_u2.ui_symlink[i];
+		memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink,
+		       sizeof(ufs2_inode->ui_u2.ui_symlink));
 	}
 	return 0;
 }
@@ -781,7 +778,6 @@ static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode)
 {
 	struct super_block *sb = inode->i_sb;
  	struct ufs_inode_info *ufsi = UFS_I(inode);
- 	unsigned i;
 
 	ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
 	ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
@@ -809,12 +805,12 @@ static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode)
 		/* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
 		ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
 	} else if (inode->i_blocks) {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
-			ufs_inode->ui_u2.ui_addr.ui_db[i] = ufsi->i_u1.i_data[i];
+		memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data,
+		       sizeof(ufs_inode->ui_u2.ui_addr));
 	}
 	else {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
-			ufs_inode->ui_u2.ui_symlink[i] = ufsi->i_u1.i_symlink[i];
+		memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
+		       sizeof(ufs_inode->ui_u2.ui_symlink));
 	}
 
 	if (!inode->i_nlink)
@@ -825,7 +821,6 @@ static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
 {
 	struct super_block *sb = inode->i_sb;
  	struct ufs_inode_info *ufsi = UFS_I(inode);
- 	unsigned i;
 
 	UFSD("ENTER\n");
 	ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
@@ -850,11 +845,11 @@ static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
 		/* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
 		ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0];
 	} else if (inode->i_blocks) {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
-			ufs_inode->ui_u2.ui_addr.ui_db[i] = ufsi->i_u1.u2_i_data[i];
+		memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data,
+		       sizeof(ufs_inode->ui_u2.ui_addr));
 	} else {
-		for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
-			ufs_inode->ui_u2.ui_symlink[i] = ufsi->i_u1.i_symlink[i];
+		memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
+		       sizeof(ufs_inode->ui_u2.ui_symlink));
  	}
 
 	if (!inode->i_nlink)
fs/ufs/ufs.h +1 -1
index 11c0351..69b3427 100644
@@ -23,7 +23,7 @@ struct ufs_sb_info {
 struct ufs_inode_info {
 	union {
 		__fs32	i_data[15];
-		__u8	i_symlink[4*15];
+		__u8	i_symlink[2 * 4 * 15];
 		__fs64	u2_i_data[15];
 	} i_u1;
 	__u32	i_flags;
Signature · 1 line
"I never could learn to drink that blood and call it wine" - Bob Dylan
#8 lore raw
In reply to Duane Griffin
Quoted 10 lines · Duane Griffin

The maximum fast symlink size is set in the superblock of certain types of UFS filesystem. Before using it we need to check that it isn't longer than the available space we have in the inode.

---
 fs/ufs/super.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)

fs/ufs/super.c magic_found: · line 1082

Quoted 5 lines
fs/ufs/super.c +5 -0
+		printk(KERN_WARNING "ufs_read_super: excessive maximum fast "
+		       "symlink size (%u)\n", uspi->s_maxsymlinklen);
+		uspi->s_maxsymlinklen = maxsymlen;
+	}
+

Better and logically to use ufs_warning here, because of it show device id, in case person has serveral ufs formated partitions and mount happened during system boot time.

Signature · 1 line
/Evgeniy