Skip to contentSkip to conversation

ocfs2: add bounds checking to ocfs2_check_dir_entry()

PATCH Superseded lei lu 3 participants 6 messages Jun 6, 2024 – Jun 18, 2024 OCFS2

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

lei lu

author
#0 lore raw

PATCH 1/1 ocfs2: add bounds checking to ocfs2_check_dir_entry()

This adds sanity checks for ocfs2_dir_entry to make sure all members of ocfs2_dir_entry don't stray beyond valid memory region.

---
 fs/ocfs2/dir.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)
fs/ocfs2/dir.c +20 -12
index d620d4c53c6f..85966385d134 100644
@@ -294,13 +294,14 @@ static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
  * bh passed here can be an inode block or a dir data block, depending
  * on the inode inline data flag.
  */
-static int ocfs2_check_dir_entry(struct inode * dir,
-				 struct ocfs2_dir_entry * de,
-				 struct buffer_head * bh,
+static int ocfs2_check_dir_entry(struct inode *dir,
+				 struct ocfs2_dir_entry *de,
+				 struct buffer_head *bh, char *buf, unsigned int size,
 				 unsigned long offset)
 {
 	const char *error_msg = NULL;
 	const int rlen = le16_to_cpu(de->rec_len);
+	const unsigned long next_offset = ((char *) de - buf) + rlen;
 
 	if (unlikely(rlen < OCFS2_DIR_REC_LEN(1)))
 		error_msg = "rec_len is smaller than minimal";
@@ -308,6 +309,11 @@ static int ocfs2_check_dir_entry(struct inode * dir,
 		error_msg = "rec_len % 4 != 0";
 	else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len)))
 		error_msg = "rec_len is too small for name_len";
+	else if (unlikely(next_offset > size))
+		error_msg = "directory entry overrun";
+	else if (unlikely(next_offset > size - OCFS2_DIR_REC_LEN(1)) &&
+		 next_offset != size)
+		error_msg = "directory entry too close to end";
 	else if (unlikely(
 		 ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize))
 		error_msg = "directory entry across blocks";
@@ -352,16 +358,16 @@ static inline int ocfs2_search_dirblock(struct buffer_head *bh,
 	de_buf = first_de;
 	dlimit = de_buf + bytes;
 
-	while (de_buf < dlimit) {
+	while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN) {
 		/* this code is executed quadratically often */
 		/* do minimal checking `by hand' */
 
 		de = (struct ocfs2_dir_entry *) de_buf;
 
-		if (de_buf + namelen <= dlimit &&
+		if (de->name + namelen <= dlimit &&
 		    ocfs2_match(namelen, name, de)) {
 			/* found a match - just to be sure, do a full check */
-			if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+			if (!ocfs2_check_dir_entry(dir, de, bh, de_buf, bytes offset)) {
 				ret = -1;
 				goto bail;
 			}
@@ -1138,7 +1144,7 @@ static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
 	pde = NULL;
 	de = (struct ocfs2_dir_entry *) first_de;
 	while (i < bytes) {
-		if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
+		if (!ocfs2_check_dir_entry(dir, de, bh, first_de, bytes, i)) {
 			status = -EIO;
 			mlog_errno(status);
 			goto bail;
@@ -1635,7 +1641,7 @@ int __ocfs2_add_entry(handle_t *handle,
 		/* These checks should've already been passed by the
 		 * prepare function, but I guess we can leave them
 		 * here anyway. */
-		if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
+		if (!ocfs2_check_dir_entry(dir, de, insert_bh, data_start, size, offset)) {
 			retval = -ENOENT;
 			goto bail;
 		}
@@ -1774,7 +1780,8 @@ static int ocfs2_dir_foreach_blk_id(struct inode *inode,
 		}
 
 		de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos);
-		if (!ocfs2_check_dir_entry(inode, de, di_bh, ctx->pos)) {
+		if (!ocfs2_check_dir_entry(inode, de, di_bh, (char *)data->id_data,
+					   i_size_read(inode), ctx->pos)) {
 			/* On error, skip the f_pos to the end. */
 			ctx->pos = i_size_read(inode);
 			break;
@@ -1867,7 +1874,8 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,
 		while (ctx->pos < i_size_read(inode)
 		       && offset < sb->s_blocksize) {
 			de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
-			if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
+			if (!ocfs2_check_dir_entry(inode, de, bh, bh->b_data,
+						   i_size_read(inode), offset)) {
 				/* On error, skip the f_pos to the
 				   next block. */
 				ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1;
@@ -3359,7 +3367,7 @@ static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
 	while (de_buf < limit) {
 		de = (struct ocfs2_dir_entry *)de_buf;
 
-		if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
+		if (!ocfs2_check_dir_entry(dir, de, di_bh, de_buf, i_size_read(dir), offset)) {
 			ret = -ENOENT;
 			goto out;
 		}
@@ -3441,7 +3449,7 @@ static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
 			/* move to next block */
 			de = (struct ocfs2_dir_entry *) bh->b_data;
 		}
-		if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+		if (!ocfs2_check_dir_entry(dir, de, bh, bh->b_data, sb->s_blocksize, offset)) {
 			status = -ENOENT;
 			goto bail;
 		}
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 13 lines · lei lu

This adds sanity checks for ocfs2_dir_entry to make sure all members of ocfs2_dir_entry don't stray beyond valid memory region.

---
 fs/ocfs2/dir.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)
fs/ocfs2/dir.c +0 -0
index d620d4c53c6f..85966385d134 100644

fs/ocfs2/dir.c static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len, · line 299

Quoted 9 lines
fs/ocfs2/dir.c +3 -3
@@ -294,13 +294,14 @@ static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
  * bh passed here can be an inode block or a dir data block, depending
  * on the inode inline data flag.
  */
-static int ocfs2_check_dir_entry(struct inode * dir,
-				 struct ocfs2_dir_entry * de,
-				 struct buffer_head * bh,
+static int ocfs2_check_dir_entry(struct inode *dir,
+				 struct ocfs2_dir_entry *de,
+				 struct buffer_head *bh, char *buf, unsigned int size,

Don't see why we have to pass 'buf' here.
Seems it can be obtained from bh directly.

Quoted 9 lines
fs/ocfs2/dir.c +1 -0
 				 unsigned long offset)
 {
 	const char *error_msg = NULL;
 	const int rlen = le16_to_cpu(de->rec_len);
+	const unsigned long next_offset = ((char *) de - buf) + rlen;
 
 	if (unlikely(rlen < OCFS2_DIR_REC_LEN(1)))
 		error_msg = "rec_len is smaller than minimal";

fs/ocfs2/dir.c static int ocfs2_check_dir_entry(struct inode * dir, · line 315

Quoted 7 lines
fs/ocfs2/dir.c +4 -0
@@ -308,6 +309,11 @@ static int ocfs2_check_dir_entry(struct inode * dir,
 		error_msg = "rec_len % 4 != 0";
 	else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len)))
 		error_msg = "rec_len is too small for name_len";
+	else if (unlikely(next_offset > size))
+		error_msg = "directory entry overrun";
+	else if (unlikely(next_offset > size - OCFS2_DIR_REC_LEN(1)) &&
+		 next_offset != size)

Seems we also have to consider meta ecc.

Quoted 11 lines
fs/ocfs2/dir.c +2 -1
+		error_msg = "directory entry too close to end";
 	else if (unlikely(
 		 ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize))
 		error_msg = "directory entry across blocks";
@@ -352,16 +358,16 @@ static inline int ocfs2_search_dirblock(struct buffer_head *bh,
 	de_buf = first_de;
 	dlimit = de_buf + bytes;
 
-	while (de_buf < dlimit) {
+	while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN) {
 		/* this code is executed quadratically often */

fs/ocfs2/dir.c static inline int ocfs2_search_dirblock(struct buffer_head *bh, · line 370

Quoted 10 lines
fs/ocfs2/dir.c +2 -2
 		/* do minimal checking `by hand' */
 
 		de = (struct ocfs2_dir_entry *) de_buf;
 
-		if (de_buf + namelen <= dlimit &&
+		if (de->name + namelen <= dlimit &&
 		    ocfs2_match(namelen, name, de)) {
 			/* found a match - just to be sure, do a full check */
-			if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+			if (!ocfs2_check_dir_entry(dir, de, bh, de_buf, bytes offset)) {

It looks buggy, even cannot compile.
Have you compiled and then verified with your POC?

Thanks,
Joseph

Quoted 59 lines
fs/ocfs2/dir.c +8 -6
 				ret = -1;
 				goto bail;
 			}
@@ -1138,7 +1144,7 @@ static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
 	pde = NULL;
 	de = (struct ocfs2_dir_entry *) first_de;
 	while (i < bytes) {
-		if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
+		if (!ocfs2_check_dir_entry(dir, de, bh, first_de, bytes, i)) {
 			status = -EIO;
 			mlog_errno(status);
 			goto bail;
@@ -1635,7 +1641,7 @@ int __ocfs2_add_entry(handle_t *handle,
 		/* These checks should've already been passed by the
 		 * prepare function, but I guess we can leave them
 		 * here anyway. */
-		if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
+		if (!ocfs2_check_dir_entry(dir, de, insert_bh, data_start, size, offset)) {
 			retval = -ENOENT;
 			goto bail;
 		}
@@ -1774,7 +1780,8 @@ static int ocfs2_dir_foreach_blk_id(struct inode *inode,
 		}
 
 		de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos);
-		if (!ocfs2_check_dir_entry(inode, de, di_bh, ctx->pos)) {
+		if (!ocfs2_check_dir_entry(inode, de, di_bh, (char *)data->id_data,
+					   i_size_read(inode), ctx->pos)) {
 			/* On error, skip the f_pos to the end. */
 			ctx->pos = i_size_read(inode);
 			break;
@@ -1867,7 +1874,8 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,
 		while (ctx->pos < i_size_read(inode)
 		       && offset < sb->s_blocksize) {
 			de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
-			if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
+			if (!ocfs2_check_dir_entry(inode, de, bh, bh->b_data,
+						   i_size_read(inode), offset)) {
 				/* On error, skip the f_pos to the
 				   next block. */
 				ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1;
@@ -3359,7 +3367,7 @@ static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
 	while (de_buf < limit) {
 		de = (struct ocfs2_dir_entry *)de_buf;
 
-		if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
+		if (!ocfs2_check_dir_entry(dir, de, di_bh, de_buf, i_size_read(dir), offset)) {
 			ret = -ENOENT;
 			goto out;
 		}
@@ -3441,7 +3449,7 @@ static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
 			/* move to next block */
 			de = (struct ocfs2_dir_entry *) bh->b_data;
 		}
-		if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+		if (!ocfs2_check_dir_entry(dir, de, bh, bh->b_data, sb->s_blocksize, offset)) {
 			status = -ENOENT;
 			goto bail;
 		}
author
#2 lore raw
Quoted 21 lines · Joseph Qi
Quoted 17 lines · lei lu

This adds sanity checks for ocfs2_dir_entry to make sure all members of ocfs2_dir_entry don't stray beyond valid memory region.

---
 fs/ocfs2/dir.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)
fs/ocfs2/dir.c +0 -1
index d620d4c53c6f..85966385d134 100644
@@ -294,13 +294,14 @@ static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
  * bh passed here can be an inode block or a dir data block, depending
  * on the inode inline data flag.
  */
-static int ocfs2_check_dir_entry(struct inode * dir,
Quoted 8 lines

fs/ocfs2/dir.c static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len, · line 299

Quoted 5 lines
fs/ocfs2/dir.c +3 -2
-                              struct ocfs2_dir_entry * de,
-                              struct buffer_head * bh,
+static int ocfs2_check_dir_entry(struct inode *dir,
+                              struct ocfs2_dir_entry *de,
+                              struct buffer_head *bh, char *buf, unsigned int size,

Don't see why we have to pass 'buf' here.
Seems it can be obtained from bh directly.

We can not simply obtained de from bh->b_data. Because in ocfs2_search_dirblock, the beginning of bh->b_data is ocfs2_dinode (found in caller ocfs2_find_entry_id). But in ocfs2_find_dir_space_el, we can see "de = (struct ocfs2_dir_entry *) bh->b_data;", which means the beginning of bh->b_data is de directly. I'm not sure what's causing this difference.

Quoted 7 lines
Quoted 7 lines
fs/ocfs2/dir.c +1 -0
                               unsigned long offset)
 {
      const char *error_msg = NULL;
      const int rlen = le16_to_cpu(de->rec_len);
+     const unsigned long next_offset = ((char *) de - buf) + rlen;
 
Quoted 12 lines

fs/ocfs2/dir.c static int ocfs2_check_dir_entry(struct inode * dir, · line 315

Quoted 10 lines
fs/ocfs2/dir.c +4 -0
      if (unlikely(rlen < OCFS2_DIR_REC_LEN(1)))
              error_msg = "rec_len is smaller than minimal";
@@ -308,6 +309,11 @@ static int ocfs2_check_dir_entry(struct inode * dir,
              error_msg = "rec_len % 4 != 0";
      else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len)))
              error_msg = "rec_len is too small for name_len";
+     else if (unlikely(next_offset > size))
+             error_msg = "directory entry overrun";
+     else if (unlikely(next_offset > size - OCFS2_DIR_REC_LEN(1)) &&
+              next_offset != size)

Seems we also have to consider meta ecc.

Sorry, I'm not very familiar with this.

Quoted 16 lines
Quoted 16 lines
fs/ocfs2/dir.c +2 -1
+             error_msg = "directory entry too close to end";
      else if (unlikely(
               ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize))
              error_msg = "directory entry across blocks";
@@ -352,16 +358,16 @@ static inline int ocfs2_search_dirblock(struct buffer_head *bh,
      de_buf = first_de;
      dlimit = de_buf + bytes;
 
-     while (de_buf < dlimit) {
+     while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN) {
              /* this code is executed quadratically often */
              /* do minimal checking `by hand' */
 
              de = (struct ocfs2_dir_entry *) de_buf;
 
Quoted 9 lines

fs/ocfs2/dir.c static inline int ocfs2_search_dirblock(struct buffer_head *bh, · line 370

Quoted 6 lines
fs/ocfs2/dir.c +2 -2
-             if (de_buf + namelen <= dlimit &&
+             if (de->name + namelen <= dlimit &&
                  ocfs2_match(namelen, name, de)) {
                      /* found a match - just to be sure, do a full check */
-                     if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+                     if (!ocfs2_check_dir_entry(dir, de, bh, de_buf, bytes offset)) {

It looks buggy, even cannot compile.
Have you compiled and then verified with your POC?

I have compiled and tested it. And I got an output as below:

root@syzkaller:~# mount -o loop ocfs2-test.img /mnt/ocfs2-test
[  116.777184] loop0: detected capacity change from 0 to 40960
[  116.843546] (mount,232,0):ocfs2_init_global_system_inodes:461
ERROR: status = -22
[  116.843946] (mount,232,0):ocfs2_init_global_system_inodes:462
ERROR: Unable to load system inode ?
[  116.844001] (mount,232,0):ocfs2_init_global_system_inodes:472
ERROR: status = -22
[  116.844821] (mount,232,0):ocfs2_initialize_super:2251 ERROR: status = -22
[  116.845590] (mount,232,0):ocfs2_fill_super:1176 ERROR: status = -22
mount: /mnt/ocfs2-test: wrong fs type, bad option, bad superblock on
/dev/loop0, missing codepage or.

The ocfs2_check_dir_entry was not called because "de->name + namelen <= dlimit && ocfs2_match(namelen, name, de)" was not meet. I think "while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN)" has taken effect, and we got an error elsewhere.

Thanks,
LL

Quoted 63 lines

Thanks,
Joseph

Quoted 59 lines
fs/ocfs2/dir.c +8 -6
                              ret = -1;
                              goto bail;
                      }
@@ -1138,7 +1144,7 @@ static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
      pde = NULL;
      de = (struct ocfs2_dir_entry *) first_de;
      while (i < bytes) {
-             if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
+             if (!ocfs2_check_dir_entry(dir, de, bh, first_de, bytes, i)) {
                      status = -EIO;
                      mlog_errno(status);
                      goto bail;
@@ -1635,7 +1641,7 @@ int __ocfs2_add_entry(handle_t *handle,
              /* These checks should've already been passed by the
               * prepare function, but I guess we can leave them
               * here anyway. */
-             if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
+             if (!ocfs2_check_dir_entry(dir, de, insert_bh, data_start, size, offset)) {
                      retval = -ENOENT;
                      goto bail;
              }
@@ -1774,7 +1780,8 @@ static int ocfs2_dir_foreach_blk_id(struct inode *inode,
              }
 
              de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos);
-             if (!ocfs2_check_dir_entry(inode, de, di_bh, ctx->pos)) {
+             if (!ocfs2_check_dir_entry(inode, de, di_bh, (char *)data->id_data,
+                                        i_size_read(inode), ctx->pos)) {
                      /* On error, skip the f_pos to the end. */
                      ctx->pos = i_size_read(inode);
                      break;
@@ -1867,7 +1874,8 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,
              while (ctx->pos < i_size_read(inode)
                     && offset < sb->s_blocksize) {
                      de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
-                     if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
+                     if (!ocfs2_check_dir_entry(inode, de, bh, bh->b_data,
+                                                i_size_read(inode), offset)) {
                              /* On error, skip the f_pos to the
                                 next block. */
                              ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1;
@@ -3359,7 +3367,7 @@ static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
      while (de_buf < limit) {
              de = (struct ocfs2_dir_entry *)de_buf;
 
-             if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
+             if (!ocfs2_check_dir_entry(dir, de, di_bh, de_buf, i_size_read(dir), offset)) {
                      ret = -ENOENT;
                      goto out;
              }
@@ -3441,7 +3449,7 @@ static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
                      /* move to next block */
                      de = (struct ocfs2_dir_entry *) bh->b_data;
              }
-             if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+             if (!ocfs2_check_dir_entry(dir, de, bh, bh->b_data, sb->s_blocksize, offset)) {
                      status = -ENOENT;
                      goto bail;
              }
reviewer
#3 lore raw
Quoted 100 lines · lei lu
Quoted 21 lines · Joseph Qi
Quoted 17 lines · lei lu

This adds sanity checks for ocfs2_dir_entry to make sure all members of ocfs2_dir_entry don't stray beyond valid memory region.

---
  fs/ocfs2/dir.c | 32 ++++++++++++++++++++------------
  1 file changed, 20 insertions(+), 12 deletions(-)
fs/ocfs2/dir.c +0 -1
index d620d4c53c6f..85966385d134 100644
@@ -294,13 +294,14 @@ static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
   * bh passed here can be an inode block or a dir data block, depending
   * on the inode inline data flag.
   */
-static int ocfs2_check_dir_entry(struct inode * dir,
Quoted 8 lines

fs/ocfs2/dir.c static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len, · line 299

Quoted 5 lines
fs/ocfs2/dir.c +3 -2
-                              struct ocfs2_dir_entry * de,
-                              struct buffer_head * bh,
+static int ocfs2_check_dir_entry(struct inode *dir,
+                              struct ocfs2_dir_entry *de,
+                              struct buffer_head *bh, char *buf, unsigned int size,

Don't see why we have to pass 'buf' here.
Seems it can be obtained from bh directly.

We can not simply obtained de from bh->b_data. Because in ocfs2_search_dirblock, the beginning of bh->b_data is ocfs2_dinode (found in caller ocfs2_find_entry_id). But in ocfs2_find_dir_space_el, we can see "de = (struct ocfs2_dir_entry *) bh->b_data;", which means the beginning of bh->b_data is de directly. I'm not sure what's causing this difference.

Quoted 7 lines
Quoted 7 lines
fs/ocfs2/dir.c +1 -0
                                unsigned long offset)
  {
       const char *error_msg = NULL;
       const int rlen = le16_to_cpu(de->rec_len);
+     const unsigned long next_offset = ((char *) de - buf) + rlen;
 
Quoted 12 lines

fs/ocfs2/dir.c static int ocfs2_check_dir_entry(struct inode * dir, · line 315

Quoted 10 lines
fs/ocfs2/dir.c +4 -0
       if (unlikely(rlen < OCFS2_DIR_REC_LEN(1)))
               error_msg = "rec_len is smaller than minimal";
@@ -308,6 +309,11 @@ static int ocfs2_check_dir_entry(struct inode * dir,
               error_msg = "rec_len % 4 != 0";
       else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len)))
               error_msg = "rec_len is too small for name_len";
+     else if (unlikely(next_offset > size))
+             error_msg = "directory entry overrun";
+     else if (unlikely(next_offset > size - OCFS2_DIR_REC_LEN(1)) &&
+              next_offset != size)

Seems we also have to consider meta ecc.

Sorry, I'm not very familiar with this.

Quoted 16 lines
Quoted 16 lines
fs/ocfs2/dir.c +2 -1
+             error_msg = "directory entry too close to end";
       else if (unlikely(
                ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize))
               error_msg = "directory entry across blocks";
@@ -352,16 +358,16 @@ static inline int ocfs2_search_dirblock(struct buffer_head *bh,
       de_buf = first_de;
       dlimit = de_buf + bytes;
 
-     while (de_buf < dlimit) {
+     while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN) {
               /* this code is executed quadratically often */
               /* do minimal checking `by hand' */
 
               de = (struct ocfs2_dir_entry *) de_buf;
 
Quoted 9 lines

fs/ocfs2/dir.c static inline int ocfs2_search_dirblock(struct buffer_head *bh, · line 370

Quoted 6 lines
fs/ocfs2/dir.c +2 -2
-             if (de_buf + namelen <= dlimit &&
+             if (de->name + namelen <= dlimit &&
                   ocfs2_match(namelen, name, de)) {
                       /* found a match - just to be sure, do a full check */
-                     if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+                     if (!ocfs2_check_dir_entry(dir, de, bh, de_buf, bytes offset)) {

It looks buggy, even cannot compile.
Have you compiled and then verified with your POC?

I have compiled and tested it. And I got an output as below:

root@syzkaller:~# mount -o loop ocfs2-test.img /mnt/ocfs2-test
[  116.777184] loop0: detected capacity change from 0 to 40960
[  116.843546] (mount,232,0):ocfs2_init_global_system_inodes:461
ERROR: status = -22
[  116.843946] (mount,232,0):ocfs2_init_global_system_inodes:462
ERROR: Unable to load system inode ?
[  116.844001] (mount,232,0):ocfs2_init_global_system_inodes:472
ERROR: status = -22
[  116.844821] (mount,232,0):ocfs2_initialize_super:2251 ERROR: status = -22
[  116.845590] (mount,232,0):ocfs2_fill_super:1176 ERROR: status = -22
mount: /mnt/ocfs2-test: wrong fs type, bad option, bad superblock on
/dev/loop0, missing codepage or.
Quoted 5 lines

The ocfs2_check_dir_entry was not called because "de->name + namelen <= dlimit && ocfs2_match(namelen, name, de)" was not meet. I think "while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN)" has taken effect, and we got an error elsewhere.

Hi,

I am not agree with the above statement. Based on your test, (applied this patch), there must be another issue preventing the mount from succeeding. "mount -o loop" command shouldn't cause an error in this case.

-Heming

author
#4 lore raw
Quoted 108 lines · Heming Zhao
Quoted 100 lines · lei lu
Quoted 21 lines · Joseph Qi
Quoted 17 lines · lei lu

This adds sanity checks for ocfs2_dir_entry to make sure all members of ocfs2_dir_entry don't stray beyond valid memory region.

---
  fs/ocfs2/dir.c | 32 ++++++++++++++++++++------------
  1 file changed, 20 insertions(+), 12 deletions(-)
fs/ocfs2/dir.c +0 -1
index d620d4c53c6f..85966385d134 100644
@@ -294,13 +294,14 @@ static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
   * bh passed here can be an inode block or a dir data block, depending
   * on the inode inline data flag.
   */
-static int ocfs2_check_dir_entry(struct inode * dir,
Quoted 8 lines

fs/ocfs2/dir.c static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len, · line 299

Quoted 5 lines
fs/ocfs2/dir.c +3 -2
-                              struct ocfs2_dir_entry * de,
-                              struct buffer_head * bh,
+static int ocfs2_check_dir_entry(struct inode *dir,
+                              struct ocfs2_dir_entry *de,
+                              struct buffer_head *bh, char *buf, unsigned int size,

Don't see why we have to pass 'buf' here.
Seems it can be obtained from bh directly.

We can not simply obtained de from bh->b_data. Because in ocfs2_search_dirblock, the beginning of bh->b_data is ocfs2_dinode (found in caller ocfs2_find_entry_id). But in ocfs2_find_dir_space_el, we can see "de = (struct ocfs2_dir_entry *) bh->b_data;", which means the beginning of bh->b_data is de directly. I'm not sure what's causing this difference.

Quoted 7 lines
Quoted 7 lines
fs/ocfs2/dir.c +1 -0
                                unsigned long offset)
  {
       const char *error_msg = NULL;
       const int rlen = le16_to_cpu(de->rec_len);
+     const unsigned long next_offset = ((char *) de - buf) + rlen;
 
Quoted 12 lines

fs/ocfs2/dir.c static int ocfs2_check_dir_entry(struct inode * dir, · line 315

Quoted 10 lines
fs/ocfs2/dir.c +4 -0
       if (unlikely(rlen < OCFS2_DIR_REC_LEN(1)))
               error_msg = "rec_len is smaller than minimal";
@@ -308,6 +309,11 @@ static int ocfs2_check_dir_entry(struct inode * dir,
               error_msg = "rec_len % 4 != 0";
       else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len)))
               error_msg = "rec_len is too small for name_len";
+     else if (unlikely(next_offset > size))
+             error_msg = "directory entry overrun";
+     else if (unlikely(next_offset > size - OCFS2_DIR_REC_LEN(1)) &&
+              next_offset != size)

Seems we also have to consider meta ecc.

Sorry, I'm not very familiar with this.

Quoted 16 lines
Quoted 16 lines
fs/ocfs2/dir.c +2 -1
+             error_msg = "directory entry too close to end";
       else if (unlikely(
                ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize))
               error_msg = "directory entry across blocks";
@@ -352,16 +358,16 @@ static inline int ocfs2_search_dirblock(struct buffer_head *bh,
       de_buf = first_de;
       dlimit = de_buf + bytes;
 
-     while (de_buf < dlimit) {
+     while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN) {
               /* this code is executed quadratically often */
               /* do minimal checking `by hand' */
 
               de = (struct ocfs2_dir_entry *) de_buf;
 
Quoted 9 lines

fs/ocfs2/dir.c static inline int ocfs2_search_dirblock(struct buffer_head *bh, · line 370

Quoted 6 lines
fs/ocfs2/dir.c +2 -2
-             if (de_buf + namelen <= dlimit &&
+             if (de->name + namelen <= dlimit &&
                   ocfs2_match(namelen, name, de)) {
                       /* found a match - just to be sure, do a full check */
-                     if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+                     if (!ocfs2_check_dir_entry(dir, de, bh, de_buf, bytes offset)) {

It looks buggy, even cannot compile.
Have you compiled and then verified with your POC?

I have compiled and tested it. And I got an output as below:

root@syzkaller:~# mount -o loop ocfs2-test.img /mnt/ocfs2-test
[  116.777184] loop0: detected capacity change from 0 to 40960
[  116.843546] (mount,232,0):ocfs2_init_global_system_inodes:461
ERROR: status = -22
[  116.843946] (mount,232,0):ocfs2_init_global_system_inodes:462
ERROR: Unable to load system inode ?
[  116.844001] (mount,232,0):ocfs2_init_global_system_inodes:472
ERROR: status = -22
[  116.844821] (mount,232,0):ocfs2_initialize_super:2251 ERROR: status = -22
[  116.845590] (mount,232,0):ocfs2_fill_super:1176 ERROR: status = -22
mount: /mnt/ocfs2-test: wrong fs type, bad option, bad superblock on
/dev/loop0, missing codepage or.
Quoted 5 lines

The ocfs2_check_dir_entry was not called because "de->name + namelen <= dlimit && ocfs2_match(namelen, name, de)" was not meet. I think "while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN)" has taken effect, and we got an error elsewhere.

Quoted 5 lines

Hi,

I am not agree with the above statement. Based on your test, (applied this patch), there must be another issue preventing the mount from succeeding. "mount -o loop" command shouldn't cause an error in this case.

Hi,

Here is a security report about this issue.

In ocfs2_search_dirblock, it traverses de (ocfs2_dir_entry) depends on de->rec_len. And it just check start address of de < dlimit. So if I leave 1 byte of space in the current block, In the next traversal, this space will be considered as de and access de->rec_len in "de_len = le16_to_cpu(de->rec_len);".

PoC as below:
1. I found when I mount an image. The parameters for the first call to ocfs2_search_dirblock is as follows:

    bytes: 0x338
    first_de: xx
    offset: 0x0
    namelen: 0x12
    name: global_inode_alloc <--
    dir: xx
    bh: xx
2. I found the corresponding ocfs2_dinode on the disk:
    ocfs2_dinode start at 0x2800
    ...
    ocfs2_dinode.i_size: 0x338 (824)
    ...
    ocfs2_dir_entry[0] 0x28c8
        ocfs2_dir_entry.inode: 0xa
        ocfs2_dir_entry.rec_len: 0x10
        ocfs2_dir_entry.name_len: 0x1
        ocfs2_dir_entry.file_type: 0x2
        ocfs2_dir_entry.name[255]: .
    ocfs2_dir_entry[1] 0x28d8
        ocfs2_dir_entry.inode: 0xa
        ocfs2_dir_entry.rec_len: 0x10
        ocfs2_dir_entry.name_len: 0x2
        ocfs2_dir_entry.file_type: 0x2
        ocfs2_dir_entry.name[255]: ..
    ocfs2_dir_entry[2] 0x28e8
        ocfs2_dir_entry.inode: 0xb
        ocfs2_dir_entry.rec_len: 0x18
        ocfs2_dir_entry.name_len: 0xa
        ocfs2_dir_entry.file_type: 0x1
        ocfs2_dir_entry.name[255]: bad_blocks
    ocfs2_dir_entry[3] 0x2900
        ocfs2_dir_entry.inode: 0xc
        ocfs2_dir_entry.rec_len: 0x20
        ocfs2_dir_entry.name_len: 0x12
        ocfs2_dir_entry.file_type: 0x1
        ocfs2_dir_entry.name[255]: global_inode_alloc
        ...
3. I change some values:
    ocfs2_dinode start at 0x2800
    ...
    ocfs2_dinode.i_size: 0xf38 <-- 0xf38 + sizeof(ocfs2_dinode) +
sizeof(ocfs2_inline_data) = 0x1000
    ...
    ocfs2_dir_entry[0] 0x28c8
        ocfs2_dir_entry.inode: 0xa
        ocfs2_dir_entry.rec_len: 0x10
        ocfs2_dir_entry.name_len: 0x1
        ocfs2_dir_entry.file_type: 0x2
        ocfs2_dir_entry.name[255]: .
    ocfs2_dir_entry[1] 0x28d8
        ocfs2_dir_entry.inode: 0xa
        ocfs2_dir_entry.rec_len: 0x10
        ocfs2_dir_entry.name_len: 0x2
        ocfs2_dir_entry.file_type: 0x2
        ocfs2_dir_entry.name[255]: ..
    ocfs2_dir_entry[2] 0x28e8
        ocfs2_dir_entry.inode: 0xb
        ocfs2_dir_entry.rec_len: 0xf17 <-- 0x2800+0x1000-0x28e8-1=0xf17
        ocfs2_dir_entry.name_len: 0xa
        ocfs2_dir_entry.file_type: 0x1
        ocfs2_dir_entry.name[255]: bad_blocks
    ocfs2_dir_entry[3] 0x2900
        ocfs2_dir_entry.inode: 0xc
        ocfs2_dir_entry.rec_len: 0x20
        ocfs2_dir_entry.name_len: 0x12
        ocfs2_dir_entry.file_type: 0x1
        ocfs2_dir_entry.name[255]: global_inode_alloc
        ...

I create a PoC on the corresponding ocfs2_dinode. It seems to be searching for global_inode_alloc, and we couldn't find it (Before patch, it triggered out-of-bound read, After patch, it also skip it because of ocfs2_dir_entry[2].rec_len). So we got an error "ocfs2_init_global_system_inodes:462 ERROR: Unable to load system inode ?".

Quoted 2 lines

-Heming

author
#5 lore raw
In reply to Joseph Qi
Quoted 64 lines · Joseph Qi
Quoted 13 lines · lei lu

This adds sanity checks for ocfs2_dir_entry to make sure all members of ocfs2_dir_entry don't stray beyond valid memory region.

---
 fs/ocfs2/dir.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)
fs/ocfs2/dir.c +0 -0
index d620d4c53c6f..85966385d134 100644

fs/ocfs2/dir.c static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len, · line 299

Quoted 9 lines
fs/ocfs2/dir.c +3 -3
@@ -294,13 +294,14 @@ static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
  * bh passed here can be an inode block or a dir data block, depending
  * on the inode inline data flag.
  */
-static int ocfs2_check_dir_entry(struct inode * dir,
-                              struct ocfs2_dir_entry * de,
-                              struct buffer_head * bh,
+static int ocfs2_check_dir_entry(struct inode *dir,
+                              struct ocfs2_dir_entry *de,
+                              struct buffer_head *bh, char *buf, unsigned int size,

Don't see why we have to pass 'buf' here.
Seems it can be obtained from bh directly.

Quoted 9 lines
fs/ocfs2/dir.c +1 -0
                               unsigned long offset)
 {
      const char *error_msg = NULL;
      const int rlen = le16_to_cpu(de->rec_len);
+     const unsigned long next_offset = ((char *) de - buf) + rlen;
 
      if (unlikely(rlen < OCFS2_DIR_REC_LEN(1)))
              error_msg = "rec_len is smaller than minimal";

fs/ocfs2/dir.c static int ocfs2_check_dir_entry(struct inode * dir, · line 315

Quoted 7 lines
fs/ocfs2/dir.c +4 -0
@@ -308,6 +309,11 @@ static int ocfs2_check_dir_entry(struct inode * dir,
              error_msg = "rec_len % 4 != 0";
      else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len)))
              error_msg = "rec_len is too small for name_len";
+     else if (unlikely(next_offset > size))
+             error_msg = "directory entry overrun";
+     else if (unlikely(next_offset > size - OCFS2_DIR_REC_LEN(1)) &&
+              next_offset != size)

Seems we also have to consider meta ecc.

Quoted 15 lines
fs/ocfs2/dir.c +2 -1
+             error_msg = "directory entry too close to end";
      else if (unlikely(
               ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize))
              error_msg = "directory entry across blocks";
@@ -352,16 +358,16 @@ static inline int ocfs2_search_dirblock(struct buffer_head *bh,
      de_buf = first_de;
      dlimit = de_buf + bytes;
 
-     while (de_buf < dlimit) {
+     while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN) {
              /* this code is executed quadratically often */
              /* do minimal checking `by hand' */
 
              de = (struct ocfs2_dir_entry *) de_buf;
 
Quoted 9 lines

fs/ocfs2/dir.c static inline int ocfs2_search_dirblock(struct buffer_head *bh, · line 370

Quoted 6 lines
fs/ocfs2/dir.c +2 -2
-             if (de_buf + namelen <= dlimit &&
+             if (de->name + namelen <= dlimit &&
                  ocfs2_match(namelen, name, de)) {
                      /* found a match - just to be sure, do a full check */
-                     if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+                     if (!ocfs2_check_dir_entry(dir, de, bh, de_buf, bytes offset)) {

It looks buggy, even cannot compile.
Have you compiled and then verified with your POC?

Oh, I see, I am missing a comma here. Sorry for that.

Quoted 63 lines

Thanks,
Joseph

Quoted 59 lines
fs/ocfs2/dir.c +8 -6
                              ret = -1;
                              goto bail;
                      }
@@ -1138,7 +1144,7 @@ static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
      pde = NULL;
      de = (struct ocfs2_dir_entry *) first_de;
      while (i < bytes) {
-             if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
+             if (!ocfs2_check_dir_entry(dir, de, bh, first_de, bytes, i)) {
                      status = -EIO;
                      mlog_errno(status);
                      goto bail;
@@ -1635,7 +1641,7 @@ int __ocfs2_add_entry(handle_t *handle,
              /* These checks should've already been passed by the
               * prepare function, but I guess we can leave them
               * here anyway. */
-             if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
+             if (!ocfs2_check_dir_entry(dir, de, insert_bh, data_start, size, offset)) {
                      retval = -ENOENT;
                      goto bail;
              }
@@ -1774,7 +1780,8 @@ static int ocfs2_dir_foreach_blk_id(struct inode *inode,
              }
 
              de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos);
-             if (!ocfs2_check_dir_entry(inode, de, di_bh, ctx->pos)) {
+             if (!ocfs2_check_dir_entry(inode, de, di_bh, (char *)data->id_data,
+                                        i_size_read(inode), ctx->pos)) {
                      /* On error, skip the f_pos to the end. */
                      ctx->pos = i_size_read(inode);
                      break;
@@ -1867,7 +1874,8 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,
              while (ctx->pos < i_size_read(inode)
                     && offset < sb->s_blocksize) {
                      de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
-                     if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
+                     if (!ocfs2_check_dir_entry(inode, de, bh, bh->b_data,
+                                                i_size_read(inode), offset)) {
                              /* On error, skip the f_pos to the
                                 next block. */
                              ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1;
@@ -3359,7 +3367,7 @@ static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
      while (de_buf < limit) {
              de = (struct ocfs2_dir_entry *)de_buf;
 
-             if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
+             if (!ocfs2_check_dir_entry(dir, de, di_bh, de_buf, i_size_read(dir), offset)) {
                      ret = -ENOENT;
                      goto out;
              }
@@ -3441,7 +3449,7 @@ static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
                      /* move to next block */
                      de = (struct ocfs2_dir_entry *) bh->b_data;
              }
-             if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+             if (!ocfs2_check_dir_entry(dir, de, bh, bh->b_data, sb->s_blocksize, offset)) {
                      status = -ENOENT;
                      goto bail;
              }