Skip to content

Commit ec81aec

Browse files
Amerigo Wangtorvalds
authored andcommitted
hfs: fix a potential buffer overflow
A specially-crafted Hierarchical File System (HFS) filesystem could cause a buffer overflow to occur in a process's kernel stack during a memcpy() call within the hfs_bnode_read() function (at fs/hfs/bnode.c:24). The attacker can provide the source buffer and length, and the destination buffer is a local variable of a fixed length. This local variable (passed as "&entry" from fs/hfs/dir.c:112 and allocated on line 60) is stored in the stack frame of hfs_bnode_read()'s caller, which is hfs_readdir(). Because the hfs_readdir() function executes upon any attempt to read a directory on the filesystem, it gets called whenever a user attempts to inspect any filesystem contents. [[email protected]: modify this patch and fix coding style problems] Signed-off-by: WANG Cong <[email protected]> Cc: Eugene Teo <[email protected]> Cc: Roman Zippel <[email protected]> Cc: Al Viro <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Alexey Dobriyan <[email protected]> Cc: Dave Anderson <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 4b731d5 commit ec81aec

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

fs/hfs/catalog.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ int hfs_cat_move(u32 cnid, struct inode *src_dir, struct qstr *src_name,
289289
err = hfs_brec_find(&src_fd);
290290
if (err)
291291
goto out;
292+
if (src_fd.entrylength > sizeof(entry) || src_fd.entrylength < 0) {
293+
err = -EIO;
294+
goto out;
295+
}
292296

293297
hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
294298
src_fd.entrylength);

fs/hfs/dir.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ static int hfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
7979
filp->f_pos++;
8080
/* fall through */
8181
case 1:
82+
if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
83+
err = -EIO;
84+
goto out;
85+
}
86+
8287
hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
8388
if (entry.type != HFS_CDR_THD) {
8489
printk(KERN_ERR "hfs: bad catalog folder thread\n");
@@ -109,6 +114,12 @@ static int hfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
109114
err = -EIO;
110115
goto out;
111116
}
117+
118+
if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
119+
err = -EIO;
120+
goto out;
121+
}
122+
112123
hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
113124
type = entry.type;
114125
len = hfs_mac2asc(sb, strbuf, &fd.key->cat.CName);

fs/hfs/super.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,13 @@ static int hfs_fill_super(struct super_block *sb, void *data, int silent)
409409
/* try to get the root inode */
410410
hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
411411
res = hfs_cat_find_brec(sb, HFS_ROOT_CNID, &fd);
412-
if (!res)
412+
if (!res) {
413+
if (fd.entrylength > sizeof(rec) || fd.entrylength < 0) {
414+
res = -EIO;
415+
goto bail;
416+
}
413417
hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
418+
}
414419
if (res) {
415420
hfs_find_exit(&fd);
416421
goto bail_no_root;

0 commit comments

Comments
 (0)