Skip to contentSkip to conversation

libext2fs: Provide functions to safely access name_len and file_type

PATCH RESEND Applied Jan Kara 2 participants 2 messages May 28, 2013 – Jun 10, 2013

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

PATCH RESEND 1/1 libext2fs: Provide functions to safely access name_len and file_type

Accessing name_len (and file_type) in ext4_dir_entry structure is somewhat problematic because on big endian architecture we need to now whether we are really dealing with ext4_dir_entry (which has u16 name_len which needs byte swapping) or ext4_dir_entry_2 (which has u8 name_len which must not be byte swapped).

Currently the code is somewhat surprising and name_len is always treated as u16 and byte swapped (flag EXT2_DIRBLOCK_V2_STRUCT isn't ever used) and then masking of name_len is used to access real name_len or file_type. Doing things this way in applications using libext2fs is unexpected to say the least (more natural is to type struct ext4_dir_entry * to struct ext4_dir_entry_2 * but that gives wrong results on big endian architectures. So provide helper functions that give endian-safe access to these fields. Also convert users in e2fsprogs to use these functions.

---
I didn't get any reply last time so I'm resending the patch. Ted?

 debugfs/debugfs.c         |  4 +--
 debugfs/dump.c            |  2 +-
 debugfs/filefrag.c        |  2 +-
 debugfs/htree.c           |  6 ++--
 debugfs/ls.c              |  5 +--
 debugfs/ncheck.c          |  8 +++--
 e2fsck/message.c          |  6 ++--
 e2fsck/pass1.c            |  4 +--
 e2fsck/pass2.c            | 48 +++++++++++++++-------------
 e2fsck/pass3.c            |  7 ++---
 e2fsck/rehash.c           | 80 ++++++++++++++++++++++++++---------------------
 lib/ext2fs/dir_iterate.c  |  6 ++--
 lib/ext2fs/ext2_fs.h      |  8 +++++
 lib/ext2fs/ext2fs.h       | 20 ++++++++++++
 lib/ext2fs/get_pathname.c | 11 +++----
 lib/ext2fs/link.c         |  9 +++---
 lib/ext2fs/lookup.c       |  4 +--
 lib/ext2fs/newdir.c       |  8 +++--
 lib/ext2fs/unlink.c       |  4 +--
 misc/tune2fs.c            |  2 +-
 resize/resize2fs.c        |  2 +-
 21 files changed, 145 insertions(+), 101 deletions(-)
debugfs/debugfs.c +2 -2
index 969dbe0..dcf16e2 100644
@@ -1911,9 +1911,9 @@ static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
 
 	if (dirent->inode == 0)
 		return 0;
-	if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
+	if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
 		return 0;
-	if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
+	if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
 	    (dirent->name[1] == '.')) {
 		rds->parent = dirent->inode;
 		return 0;
debugfs/dump.c +1 -1
index 9409ab6..c75b9f1 100644
@@ -308,7 +308,7 @@ static int rdump_dirent(struct ext2_dir_entry *dirent,
 	const char *dumproot = private;
 	struct ext2_inode inode;
 
-	thislen = dirent->name_len & 0xFF;
+	thislen = ext2fs_dirent_name_len(dirent);
 	strncpy(name, dirent->name, thislen);
 	name[thislen] = 0;
 
debugfs/filefrag.c +1 -1
index 7f28bc0..e82d133 100644
@@ -183,7 +183,7 @@ static int filefrag_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
 	if (entry == DIRENT_DELETED_FILE)
 		return 0;
 
-	thislen = dirent->name_len & 0xFF;
+	thislen = ext2fs_dirent_name_len(dirent);
 	strncpy(name, dirent->name, thislen);
 	name[thislen] = '\0';
 	ino = dirent->inode;
debugfs/htree.c +3 -3
index d94dbea..4f0118d 100644
@@ -79,15 +79,15 @@ static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
 				(unsigned long) blk);
 			return;
 		}
+		thislen = ext2fs_dirent_name_len(dirent);
 		if (((offset + rec_len) > fs->blocksize) ||
 		    (rec_len < 8) ||
 		    ((rec_len % 4) != 0) ||
-		    ((((unsigned) dirent->name_len & 0xFF)+8) > rec_len)) {
+		    (thislen + 8 > rec_len)) {
 			fprintf(pager, "Corrupted directory block (%llu)!\n",
 				blk);
 			break;
 		}
-		thislen = dirent->name_len & 0xFF;
 		strncpy(name, dirent->name, thislen);
 		name[thislen] = '\0';
 		errcode = ext2fs_dirhash(hash_alg, name,
@@ -428,7 +428,7 @@ static int search_dir_block(ext2_filsys fs, blk64_t *blocknr,
 			return BLOCK_ABORT;
 		}
 		if (dirent->inode &&
-		    p->len == (dirent->name_len & 0xFF) &&
+		    p->len == ext2fs_dirent_name_len(dirent) &&
 		    strncmp(p->search_name, dirent->name,
 			    p->len) == 0) {
 			printf("Entry found at logical block %lld, "
debugfs/ls.c +3 -2
index 032d12d..69c7897 100644
@@ -61,7 +61,7 @@ static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
 	struct list_dir_struct *ls = (struct list_dir_struct *) private;
 	struct ext2_dir_entry_tail *t = (struct ext2_dir_entry_tail *) dirent;
 
-	thislen = dirent->name_len & 0xFF;
+	thislen = ext2fs_dirent_name_len(dirent);
 	strncpy(name, dirent->name, thislen);
 	name[thislen] = '\0';
 	ino = dirent->inode;
@@ -105,7 +105,8 @@ static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
 				t->det_checksum);
 			return 0;
 		}
-		fprintf(ls->f, "(%d)  %5d  %5d   ", dirent->name_len >> 8,
+		fprintf(ls->f, "(%d)  %5d  %5d   ",
+			ext2fs_dirent_file_type(dirent),
 			inode_uid(inode), inode_gid(inode));
 		if (LINUX_S_ISDIR(inode.i_mode))
 			fprintf(ls->f, "%5d", inode.i_size);
debugfs/ncheck.c +5 -3
index 58f3a50..5d9b5d2 100644
@@ -45,7 +45,7 @@ static int ncheck_proc(struct ext2_dir_entry *dirent,
 	struct inode_walk_struct *iw = (struct inode_walk_struct *) private;
 	struct ext2_inode inode;
 	errcode_t	retval;
-	int		filetype = dirent->name_len >> 8;
+	int		filetype = ext2fs_dirent_file_type(dirent);
 	int		i;
 
 	iw->position++;
@@ -66,11 +66,13 @@ static int ncheck_proc(struct ext2_dir_entry *dirent,
 			if (iw->parent)
 				printf("%u\t%s/%.*s", iw->iarray[i],
 				       iw->parent,
-				       (dirent->name_len & 0xFF), dirent->name);
+				       ext2fs_dirent_name_len(dirent),
+				       dirent->name);
 			else
 				printf("%u\t<%u>/%.*s", iw->iarray[i],
 				       iw->dir,
-				       (dirent->name_len & 0xFF), dirent->name);
+				       ext2fs_dirent_name_len(dirent),
+				       dirent->name);
 			if (iw->check_dirent && filetype) {
 				if (!debugfs_read_inode(dirent->inode, &inode,
 							"ncheck") &&
e2fsck/message.c +3 -3
index b99473d..72a56ca 100644
@@ -373,7 +373,7 @@ static _INLINE_ void expand_dirent_expression(FILE *f, ext2_filsys fs, char ch,
 		fprintf(f, "%u", dirent->inode);
 		break;
 	case 'n':
-		len = dirent->name_len & 0xFF;
+		len = ext2fs_dirent_name_len(dirent);
 		if ((ext2fs_get_rec_len(fs, dirent, &rec_len) == 0) &&
 		    (len > rec_len))
 			len = rec_len;
@@ -384,10 +384,10 @@ static _INLINE_ void expand_dirent_expression(FILE *f, ext2_filsys fs, char ch,
 		fprintf(f, "%u", rec_len);
 		break;
 	case 'l':
-		fprintf(f, "%u", dirent->name_len & 0xFF);
+		fprintf(f, "%u", ext2fs_dirent_name_len(dirent));
 		break;
 	case 't':
-		fprintf(f, "%u", dirent->name_len >> 8);
+		fprintf(f, "%u", ext2fs_dirent_file_type(dirent));
 		break;
 	default:
 	no_dirent:
e2fsck/pass1.c +2 -2
index 1b410be..df9457e 100644
@@ -482,7 +482,7 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
 	retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
 	if (retval)
 		return;
-	if (((dirent->name_len & 0xFF) != 1) ||
+	if ((ext2fs_dirent_name_len(dirent) != 1) ||
 	    (dirent->name[0] != '.') ||
 	    (dirent->inode != pctx->ino) ||
 	    (rec_len < 12) ||
@@ -494,7 +494,7 @@ static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
 	retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
 	if (retval)
 		return;
-	if (((dirent->name_len & 0xFF) != 2) ||
+	if ((ext2fs_dirent_name_len(dirent) != 2) ||
 	    (dirent->name[0] != '.') ||
 	    (dirent->name[1] != '.') ||
 	    (rec_len < 12) ||
e2fsck/pass2.c +26 -22

169 lines not on this page · raw message

e2fsck/pass3.c +3 -4
index a379e9b..193ec1c 100644
@@ -612,7 +612,7 @@ static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
 	errcode_t	retval;
 	struct problem_context pctx;
 
-	if ((dirent->name_len & 0xFF) != 2)
+	if (ext2fs_dirent_name_len(dirent) != 2)
 		return 0;
 	if (strncmp(dirent->name, "..", 2))
 		return 0;
@@ -632,10 +632,9 @@ static int fix_dotdot_proc(struct ext2_dir_entry *dirent,
 	dirent->inode = fp->parent;
 	if (fp->ctx->fs->super->s_feature_incompat &
 	    EXT2_FEATURE_INCOMPAT_FILETYPE)
-		dirent->name_len = (dirent->name_len & 0xFF) |
-			(EXT2_FT_DIR << 8);
+		ext2fs_dirent_set_file_type(dirent, EXT2_FT_DIR);
 	else
-		dirent->name_len = dirent->name_len & 0xFF;
+		ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
 
 	fp->done++;
 	return DIRENT_ABORT | DIRENT_CHANGED;
e2fsck/rehash.c +44 -36

201 lines not on this page · raw message

lib/ext2fs/dir_iterate.c +3 -3
index 1a4bf5c..8be0ac2 100644
@@ -83,7 +83,7 @@ static int ext2fs_validate_entry(ext2_filsys fs, char *buf,
 		offset += rec_len;
 		if ((rec_len < 8) ||
 		    ((rec_len % 4) != 0) ||
-		    ((((unsigned) dirent->name_len & 0xFF)+8) > rec_len))
+		    ((ext2fs_dirent_name_len(dirent)+8) > rec_len))
 			return 0;
 	}
 	return (offset == final_offset);
@@ -215,7 +215,7 @@ int ext2fs_process_dir_block(ext2_filsys fs,
 		if (((offset + rec_len) > fs->blocksize) ||
 		    (rec_len < 8) ||
 		    ((rec_len % 4) != 0) ||
-		    ((((unsigned) dirent->name_len & 0xFF)+8) > rec_len)) {
+		    ((ext2fs_dirent_name_len(dirent)+8) > rec_len)) {
 			ctx->errcode = EXT2_ET_DIR_CORRUPTED;
 			return BLOCK_ABORT;
 		}
@@ -253,7 +253,7 @@ next:
 			next_real_entry += rec_len;
 
  		if (ctx->flags & DIRENT_FLAG_INCLUDE_REMOVED) {
-			size = ((dirent->name_len & 0xFF) + 11) & ~3;
+			size = (ext2fs_dirent_name_len(dirent) + 11) & ~3;
 
 			if (rec_len != size)  {
 				unsigned int final_offset;
lib/ext2fs/ext2_fs.h +8 -0
index 0c0bbcb..6a28d55 100644
@@ -803,6 +803,14 @@ struct ext2_dir_entry {
  * stored in intel byte order, and the name_len field could never be
  * bigger than 255 chars, it's safe to reclaim the extra byte for the
  * file_type field.
+ *
+ * This structure is deprecated due to endianity issues. Please use struct
+ * ext2_dir_entry and accessor functions
+ *   ext2fs_dirent_name_len
+ *   ext2fs_dirent_set_name_len
+ *   ext2fs_dirent_file_type
+ *   ext2fs_dirent_set_file_type
+ * to get and set name_len and file_type fields.
  */
 struct ext2_dir_entry_2 {
 	__u32	inode;			/* Inode number */
lib/ext2fs/ext2fs.h +20 -0
index 6923f34..48bc8bb 100644
@@ -1805,6 +1805,26 @@ _INLINE_ __u64 ext2fs_div64_ceil(__u64 a, __u64 b)
 	return ((a - 1) / b) + 1;
 }
 
+_INLINE_ int ext2fs_dirent_name_len(const struct ext2_dir_entry *entry)
+{
+	return entry->name_len & 0xff;
+}
+
+_INLINE_ void ext2fs_dirent_set_name_len(struct ext2_dir_entry *entry, int len)
+{
+	entry->name_len = (entry->name_len & 0xff00) | (len & 0xff);
+}
+
+_INLINE_ int ext2fs_dirent_file_type(const struct ext2_dir_entry *entry)
+{
+	return entry->name_len >> 8;
+}
+
+_INLINE_ void ext2fs_dirent_set_file_type(struct ext2_dir_entry *entry, int type)
+{
+	entry->name_len = (entry->name_len & 0xff) | (type << 8);
+}
+
 #undef _INLINE_
 #endif
 
lib/ext2fs/get_pathname.c +5 -6
index 52aea62..4c9c765 100644
@@ -49,21 +49,20 @@ static int get_pathname_proc(struct ext2_dir_entry *dirent,
 {
 	struct get_pathname_struct	*gp;
 	errcode_t			retval;
+	int name_len = ext2fs_dirent_name_len(dirent);
 
 	gp = (struct get_pathname_struct *) priv_data;
 
-	if (((dirent->name_len & 0xFF) == 2) &&
-	    !strncmp(dirent->name, "..", 2))
+	if ((name_len == 2) && !strncmp(dirent->name, "..", 2))
 		gp->parent = dirent->inode;
 	if (dirent->inode == gp->search_ino) {
-		retval = ext2fs_get_mem((dirent->name_len & 0xFF) + 1,
-					&gp->name);
+		retval = ext2fs_get_mem(name_len + 1, &gp->name);
 		if (retval) {
 			gp->errcode = retval;
 			return DIRENT_ABORT;
 		}
-		strncpy(gp->name, dirent->name, (dirent->name_len & 0xFF));
-		gp->name[dirent->name_len & 0xFF] = '\0';
+		strncpy(gp->name, dirent->name, name_len);
+		gp->name[name_len] = '\0';
 		return DIRENT_ABORT;
 	}
 	return 0;
lib/ext2fs/link.c +5 -4
index 2dec5dc..f715067 100644
@@ -73,7 +73,7 @@ static int link_proc(struct ext2_dir_entry *dirent,
 	 * truncate it and return.
 	 */
 	if (dirent->inode) {
-		min_rec_len = EXT2_DIR_REC_LEN(dirent->name_len & 0xFF);
+		min_rec_len = EXT2_DIR_REC_LEN(ext2fs_dirent_name_len(dirent));
 		if (curr_rec_len < (min_rec_len + rec_len))
 			return ret;
 		rec_len = curr_rec_len - min_rec_len;
@@ -83,7 +83,8 @@ static int link_proc(struct ext2_dir_entry *dirent,
 		next = (struct ext2_dir_entry *) (buf + offset +
 						  dirent->rec_len);
 		next->inode = 0;
-		next->name_len = 0;
+		ext2fs_dirent_set_name_len(next, 0);
+		ext2fs_dirent_set_file_type(next, 0);
 		ls->err = ext2fs_set_rec_len(ls->fs, rec_len, next);
 		if (ls->err)
 			return DIRENT_ABORT;
@@ -97,10 +98,10 @@ static int link_proc(struct ext2_dir_entry *dirent,
 	if (curr_rec_len < rec_len)
 		return ret;
 	dirent->inode = ls->inode;
-	dirent->name_len = ls->namelen;
+	ext2fs_dirent_set_name_len(dirent, ls->namelen);
 	strncpy(dirent->name, ls->name, ls->namelen);
 	if (ls->sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
-		dirent->name_len |= (ls->flags & 0x7) << 8;
+		ext2fs_dirent_set_file_type(dirent, ls->flags & 0x7);
 
 	ls->done++;
 	return DIRENT_ABORT|DIRENT_CHANGED;
lib/ext2fs/lookup.c +2 -2
index 0e66e71..c1d802c 100644
@@ -37,9 +37,9 @@ static int lookup_proc(struct ext2_dir_entry *dirent,
 {
 	struct lookup_struct *ls = (struct lookup_struct *) priv_data;
 
-	if (ls->len != (dirent->name_len & 0xFF))
+	if (ls->len != ext2fs_dirent_name_len(dirent))
 		return 0;
-	if (strncmp(ls->name, dirent->name, (dirent->name_len & 0xFF)))
+	if (strncmp(ls->name, dirent->name, ext2fs_dirent_name_len(dirent)))
 		return 0;
 	*ls->inode = dirent->inode;
 	ls->found++;
lib/ext2fs/newdir.c +5 -3
index 2cd541d..d134bdf 100644
@@ -56,12 +56,13 @@ errcode_t ext2fs_new_dir_block(ext2_filsys fs, ext2_ino_t dir_ino,
 	if (dir_ino) {
 		if (fs->super->s_feature_incompat &
 		    EXT2_FEATURE_INCOMPAT_FILETYPE)
-			filetype = EXT2_FT_DIR << 8;
+			filetype = EXT2_FT_DIR;
 		/*
 		 * Set up entry for '.'
 		 */
 		dir->inode = dir_ino;
-		dir->name_len = 1 | filetype;
+		ext2fs_dirent_set_name_len(dir, 1);
+		ext2fs_dirent_set_file_type(dir, filetype);
 		dir->name[0] = '.';
 		rec_len = (fs->blocksize - csum_size) - EXT2_DIR_REC_LEN(1);
 		dir->rec_len = EXT2_DIR_REC_LEN(1);
@@ -74,7 +75,8 @@ errcode_t ext2fs_new_dir_block(ext2_filsys fs, ext2_ino_t dir_ino,
 		if (retval)
 			return retval;
 		dir->inode = parent_ino;
-		dir->name_len = 2 | filetype;
+		ext2fs_dirent_set_name_len(dir, 2);
+		ext2fs_dirent_set_file_type(dir, filetype);
 		dir->name[0] = '.';
 		dir->name[1] = '.';
 
lib/ext2fs/unlink.c +2 -2
index d2d31cc..8ab27ee 100644
@@ -44,9 +44,9 @@ static int unlink_proc(struct ext2_dir_entry *dirent,
 	ls->prev = dirent;
 
 	if (ls->name) {
-		if ((dirent->name_len & 0xFF) != ls->namelen)
+		if (ext2fs_dirent_name_len(dirent) != ls->namelen)
 			return 0;
-		if (strncmp(ls->name, dirent->name, dirent->name_len & 0xFF))
+		if (strncmp(ls->name, dirent->name, ext2fs_dirent_name_len(dirent)))
 			return 0;
 	}
 	if (ls->inode) {
misc/tune2fs.c +1 -1
index 7d6520e..c38711d 100644
@@ -515,7 +515,7 @@ static int rewrite_dir_block(ext2_filsys fs,
 		ctx->errcode = ext2fs_get_rec_len(fs, last_de, &rec_len);
 		if (ctx->errcode)
 			return BLOCK_ABORT;
-		name_size = last_de->name_len & 0xFF;
+		name_size = ext2fs_dirent_name_len(last_de);
 
 		if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
 				EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
resize/resize2fs.c +1 -1
index e40a85c..aa2364c 100644
@@ -1591,7 +1591,7 @@ static int check_and_change_inodes(ext2_ino_t dir,
 #ifdef RESIZE2FS_DEBUG
 	if (is->rfs->flags & RESIZE_DEBUG_INODEMAP)
 		printf("Inode translate (dir=%u, name=%.*s, %u->%u)\n",
-		       dir, dirent->name_len&0xFF, dirent->name,
+		       dir, ext2fs_dirent_name_len(dirent), dirent->name,
 		       dirent->inode, new_inode);
 #endif
 
Signature · 1 line
1.8.1.4
#1 lore raw
Quoted 16 lines · Jan Kara

Accessing name_len (and file_type) in ext4_dir_entry structure is somewhat problematic because on big endian architecture we need to now whether we are really dealing with ext4_dir_entry (which has u16 name_len which needs byte swapping) or ext4_dir_entry_2 (which has u8 name_len which must not be byte swapped).

Currently the code is somewhat surprising and name_len is always treated as u16 and byte swapped (flag EXT2_DIRBLOCK_V2_STRUCT isn't ever used) and then masking of name_len is used to access real name_len or file_type. Doing things this way in applications using libext2fs is unexpected to say the least (more natural is to type struct ext4_dir_entry * to struct ext4_dir_entry_2 * but that gives wrong results on big endian architectures. So provide helper functions that give endian-safe access to these fields. Also convert users in e2fsprogs to use these functions.

Quoted 1 line

Applied, thanks.

- Ted