Skip to contentSkip to conversation

ocfs2: add bounds checking to ocfs2_search_dirblock()

PATCH In review lei lu 2 participants 5 messages May 28, 2024 – May 29, 2024 OCFS2

lore
Share & tools
lore

lei lu

author
#0 lore raw

PATCH v2 1/1 ocfs2: add bounds checking to ocfs2_search_dirblock()

Add a check to make sure all members of the ocfs2_dir_entry don't stray beyond valid memory region.

---
 fs/ocfs2/dir.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
fs/ocfs2/dir.c +8 -7
index d620d4c53c6f..56e2f8c62106 100644
@@ -352,19 +352,20 @@ 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 &&
+		/* do a full check */
+		if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+			ret = -1;
+			goto bail;
+		}
+
+		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)) {
-				ret = -1;
-				goto bail;
-			}
 			*res_dir = de;
 			ret = 1;
 			goto bail;
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 19 lines · lei lu

Add a check to make sure all members of the ocfs2_dir_entry don't stray beyond valid memory region.

---
 fs/ocfs2/dir.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
fs/ocfs2/dir.c +1 -1
index d620d4c53c6f..56e2f8c62106 100644
@@ -352,19 +352,20 @@ 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 365

Quoted 10 lines
fs/ocfs2/dir.c +5 -1
 		/* do minimal checking `by hand' */
 
 		de = (struct ocfs2_dir_entry *) de_buf;
 
-		if (de_buf + namelen <= dlimit &&
+		/* do a full check */
+		if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+			ret = -1;
+			goto bail;
+		}

What's the difference by moving full check above?
As I commented in v1, we may add a more check in ocfs2_check_dir_entry().

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

Quoted 2 lines
fs/ocfs2/dir.c +2 -0
+
+		if (de->name + namelen <= dlimit &&

As discussed in v1, this change looks reasonable.

Thanks,
Joseph

Quoted 9 lines
fs/ocfs2/dir.c +0 -5
 		    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)) {
-				ret = -1;
-				goto bail;
-			}
 			*res_dir = de;
 			ret = 1;
 			goto bail;
author
#2 lore raw
Quoted 27 lines · Joseph Qi <[email protected]> 于2024年5月29日周三 09:33写道:
Quoted 23 lines · lei lu

Add a check to make sure all members of the ocfs2_dir_entry don't stray beyond valid memory region.

---
 fs/ocfs2/dir.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
fs/ocfs2/dir.c +1 -1
index d620d4c53c6f..56e2f8c62106 100644
@@ -352,19 +352,20 @@ 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 365

Quoted 6 lines
fs/ocfs2/dir.c +5 -1
-             if (de_buf + namelen <= dlimit &&
+             /* do a full check */
+             if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+                     ret = -1;
+                     goto bail;
+             }

What's the difference by moving full check above?
As I commented in v1, we may add a more check in ocfs2_check_dir_entry().

Thanks for your time.

If no match is found, there is no check for de. I'm not sure if there will be any OOB elsewhere when "de->rec_len < OCFS2_DIR_REC_LEN(de->name_len)".

I'm not sure what "(de - bh->b_data) + de_len > bytes - OCFS2_DIR_REC_LEN(de->name_len)" means in V1, bh->b_data is the start offset of ocfs2_dinode. bytes is just the size of the de list. Maybe we should check "((char *) de - bh->b_data) + rlen > sizeof(struct ocfs2_dinode) + bytes" which is same as "((char *) de - bh->b_data) + rlen > dlimit".

Quoted 18 lines

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

Quoted 3 lines
fs/ocfs2/dir.c +2 -0
+
+             if (de->name + namelen <= dlimit &&

As discussed in v1, this change looks reasonable.

Thanks,
Joseph

Quoted 9 lines
fs/ocfs2/dir.c +0 -5
                  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)) {
-                             ret = -1;
-                             goto bail;
-                     }
                      *res_dir = de;
                      ret = 1;
                      goto bail;
maintainer
#3 lore raw
Quoted 44 lines · lei lu
Quoted 27 lines · Joseph Qi <[email protected]> 于2024年5月29日周三 09:33写道:
Quoted 23 lines · lei lu

Add a check to make sure all members of the ocfs2_dir_entry don't stray beyond valid memory region.

---
 fs/ocfs2/dir.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
fs/ocfs2/dir.c +1 -1
index d620d4c53c6f..56e2f8c62106 100644
@@ -352,19 +352,20 @@ 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 365

Quoted 6 lines
fs/ocfs2/dir.c +5 -1
-             if (de_buf + namelen <= dlimit &&
+             /* do a full check */
+             if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+                     ret = -1;
+                     goto bail;
+             }

What's the difference by moving full check above?
As I commented in v1, we may add a more check in ocfs2_check_dir_entry().

Thanks for your time.

If no match is found, there is no check for de. I'm not sure if there will be any OOB elsewhere when "de->rec_len < OCFS2_DIR_REC_LEN(de->name_len)".

Quoted 7 lines

I'm not sure what "(de - bh->b_data) + de_len > bytes - OCFS2_DIR_REC_LEN(de->name_len)" means in V1, bh->b_data is the start offset of ocfs2_dinode. bytes is just the size of the de list. Maybe we should check "((char *) de - bh->b_data) + rlen > sizeof(struct ocfs2_dinode) + bytes" which is same as "((char *) de - bh->b_data) + rlen > dlimit".

You can refer ext4_check_dir_entry() for details.

Quoted 18 lines
Quoted 18 lines

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

Quoted 3 lines
fs/ocfs2/dir.c +2 -0
+
+             if (de->name + namelen <= dlimit &&

As discussed in v1, this change looks reasonable.

Thanks,
Joseph

Quoted 9 lines
fs/ocfs2/dir.c +0 -5
                  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)) {
-                             ret = -1;
-                             goto bail;
-                     }
                      *res_dir = de;
                      ret = 1;
                      goto bail;
author
#4 lore raw

Thanks, I apologize for not being very familiar. Could you take some time to write a patch for this issue.

Thanks,
LL

Quoted 76 lines · Joseph Qi <[email protected]> 于2024年5月29日周三 14:15写道:
Quoted 44 lines · lei lu
Quoted 27 lines · Joseph Qi <[email protected]> 于2024年5月29日周三 09:33写道:
Quoted 23 lines · lei lu

Add a check to make sure all members of the ocfs2_dir_entry don't stray beyond valid memory region.

---
 fs/ocfs2/dir.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
fs/ocfs2/dir.c +1 -1
index d620d4c53c6f..56e2f8c62106 100644
@@ -352,19 +352,20 @@ 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 365

Quoted 6 lines
fs/ocfs2/dir.c +5 -1
-             if (de_buf + namelen <= dlimit &&
+             /* do a full check */
+             if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
+                     ret = -1;
+                     goto bail;
+             }

What's the difference by moving full check above?
As I commented in v1, we may add a more check in ocfs2_check_dir_entry().

Thanks for your time.

If no match is found, there is no check for de. I'm not sure if there will be any OOB elsewhere when "de->rec_len < OCFS2_DIR_REC_LEN(de->name_len)".

Quoted 7 lines

I'm not sure what "(de - bh->b_data) + de_len > bytes - OCFS2_DIR_REC_LEN(de->name_len)" means in V1, bh->b_data is the start offset of ocfs2_dinode. bytes is just the size of the de list. Maybe we should check "((char *) de - bh->b_data) + rlen > sizeof(struct ocfs2_dinode) + bytes" which is same as "((char *) de - bh->b_data) + rlen > dlimit".

You can refer ext4_check_dir_entry() for details.

Quoted 18 lines
Quoted 18 lines

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

Quoted 3 lines
fs/ocfs2/dir.c +2 -0
+
+             if (de->name + namelen <= dlimit &&

As discussed in v1, this change looks reasonable.

Thanks,
Joseph

Quoted 9 lines
fs/ocfs2/dir.c +0 -5
                  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)) {
-                             ret = -1;
-                             goto bail;
-                     }
                      *res_dir = de;
                      ret = 1;
                      goto bail;