Skip to contentSkip to conversation

ocfs2: add bounds checking to ocfs2_search_dirblock()

PATCH Superseded lei lu 2 participants 3 messages May 26, 2024 – May 27, 2024 2 messages not in the archive OCFS2

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

lei lu

author
#0 lore raw

PATCH 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 | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
fs/ocfs2/dir.c +11 -1
index d620d4c53c6f..385576d86983 100644
@@ -358,6 +358,17 @@ static inline int ocfs2_search_dirblock(struct buffer_head *bh,
 
 		de = (struct ocfs2_dir_entry *) de_buf;
 
+		if (unlikely(de_buf + OCFS2_DIR_MEMBER_LEN > dlimit)) {
+			ret = -1;
+			goto bail;
+		}
+		de_len = le16_to_cpu(de->rec_len);
+		if (unlikely(de_buf + de_len > dlimit) ||
+		    unlikely(de_len < OCFS2_DIR_REC_LEN(de->name_len))) {
+			ret = -1;
+			goto bail;
+		}
+
 		if (de_buf + namelen <= dlimit &&
 		    ocfs2_match(namelen, name, de)) {
 			/* found a match - just to be sure, do a full check */
@@ -371,7 +382,6 @@ static inline int ocfs2_search_dirblock(struct buffer_head *bh,
 		}
 
 		/* prevent looping on a bad block */
-		de_len = le16_to_cpu(de->rec_len);
 		if (de_len <= 0) {
 			ret = -1;
 			goto bail;
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 13 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 | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
fs/ocfs2/dir.c +0 -0
index d620d4c53c6f..385576d86983 100644

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

Quoted 7 lines
fs/ocfs2/dir.c +4 -0
@@ -358,6 +358,17 @@ static inline int ocfs2_search_dirblock(struct buffer_head *bh,
 
 		de = (struct ocfs2_dir_entry *) de_buf;
 
+		if (unlikely(de_buf + OCFS2_DIR_MEMBER_LEN > dlimit)) {
+			ret = -1;
+			goto bail;
+		}

It seems that this check can be moved to:

while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN) {
	...
}

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

Quoted 2 lines
fs/ocfs2/dir.c +2 -0
+		de_len = le16_to_cpu(de->rec_len);
+		if (unlikely(de_buf + de_len > dlimit) ||

dlimit = de_buf + bytes;
So here you mean check de_len < bytes?

Or should we check more precisely, like in ocfs2_check_dir_entry():
(de - bh->b_data) + de_len > bytes - OCFS2_DIR_REC_LEN(de->name_len)

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

Quoted 1 line
fs/ocfs2/dir.c +1 -0
+		    unlikely(de_len < OCFS2_DIR_REC_LEN(de->name_len))) {

This is already done in ocfs2_check_dir_entry().

Quoted 15 lines
fs/ocfs2/dir.c +4 -1
+			ret = -1;
+			goto bail;
+		}
+
 		if (de_buf + namelen <= dlimit &&
 		    ocfs2_match(namelen, name, de)) {
 			/* found a match - just to be sure, do a full check */
@@ -371,7 +382,6 @@ static inline int ocfs2_search_dirblock(struct buffer_head *bh,
 		}
 
 		/* prevent looping on a bad block */
-		de_len = le16_to_cpu(de->rec_len);
 		if (de_len <= 0) {
 			ret = -1;
 			goto bail;

A message that is not in the archive (sent off-list or not yet received): CAEBF3_b6uSbo5s6dk=Vvt+mYzSjzzx0RMnaHQcc+YdWco6Dpzw@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): CAEBF3_anUzNk6gwLG0d=_5QyWeStvkmp_nT9Z_k9fFdjvJ-wiQ@mail.gmail.com

maintainer
#4 lore raw
Quoted 9 lines · lei lu

code in ocfs2_search_dirblock:
if (de_buf + namelen <= dlimit &&

    ocfs2_match(namelen, name, de)) {

I'm not sure what "de_buf + namelen < dlimit" means. I think "de_buf + namelen" maybe "<= dlimit", but "((struct ocfs2_dir_entry *) de_buf)->name
+ namelen" maybe "> dlimit". Or maybe we should check "de->name + namelen <= dlimit" here?

Yes, I think you are right.
de->name + namelen is a more precisely check.

Thanks,
Joseph

Quoted 94 lines
Quoted 91 lines · lei lu <[email protected]> 于2024年5月27日周一 11:11写道:
Quoted 4 lines

It seems that this check can be moved to:

while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN) {
       ...
}

Right, it also work.

Quoted 5 lines

dlimit = de_buf + bytes;
So here you mean check de_len < bytes?

Or should we check more precisely, like in ocfs2_check_dir_entry():
(de - bh->b_data) + de_len > bytes - OCFS2_DIR_REC_LEN(de->name_len)

"de_buf + OCFS2_DIR_MEMBER_LEN > dlimit" checks the fixed members don't stray beyond valid memory region, and "de_buf + de_len > dlimit" for the whole de. You should mean "((char *) de - bh->b_data) + de_len > dlimit" which is similar to "((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)" in ocfs2_check_dir_entry. But dlimit which is end of the de list may not reach the block end.

Quoted 1 line

This is already done in ocfs2_check_dir_entry().

Right, We need to check "de_len < OCFS2_DIR_REC_LEN(de->name_len)" before "ocfs2_match(namelen, name, de)", because if de->name_len is too large, I think maybe trigger anther OOB in ocfs2_match. Or we should call ocfs2_check_dir_entry earlier to do a full check for de.

Quoted 61 lines · Joseph Qi <[email protected]> 于2024年5月27日周一 10:33写道:

fs/ocfs2/dir.c static inline int ocfs2_search_dirblock(struct

Quoted 13 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 | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
fs/ocfs2/dir.c +0 -0
index d620d4c53c6f..385576d86983 100644
@@ -358,6 +358,17 @@ static inline int ocfs2_search_dirblock(struct

buffer_head *bh,

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

Quoted 7 lines
fs/ocfs2/dir.c +4 -0
              de = (struct ocfs2_dir_entry *) de_buf;
 
+             if (unlikely(de_buf + OCFS2_DIR_MEMBER_LEN > dlimit)) {
+                     ret = -1;
+                     goto bail;
+             }

It seems that this check can be moved to:

while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN) {
        ...
}

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

Quoted 2 lines
fs/ocfs2/dir.c +2 -0
+             de_len = le16_to_cpu(de->rec_len);
+             if (unlikely(de_buf + de_len > dlimit) ||

dlimit = de_buf + bytes;
So here you mean check de_len < bytes?

Or should we check more precisely, like in ocfs2_check_dir_entry():
(de - bh->b_data) + de_len > bytes - OCFS2_DIR_REC_LEN(de->name_len)

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

Quoted 1 line
fs/ocfs2/dir.c +1 -0
+                 unlikely(de_len < OCFS2_DIR_REC_LEN(de->name_len))) {

This is already done in ocfs2_check_dir_entry().

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

Quoted 7 lines
fs/ocfs2/dir.c +4 -0
+                     ret = -1;
+                     goto bail;
+             }
+
              if (de_buf + namelen <= dlimit &&
                  ocfs2_match(namelen, name, de)) {
                      /* found a match - just to be sure, do a full

check */

fs/ocfs2/dir.c static inline int ocfs2_search_dirblock(struct

Quoted 1 line
fs/ocfs2/dir.c +0 -0
@@ -371,7 +382,6 @@ static inline int ocfs2_search_dirblock(struct

buffer_head *bh,

Quoted 7 lines
fs/ocfs2/dir.c +0 -1
              }
 
              /* prevent looping on a bad block */
-             de_len = le16_to_cpu(de->rec_len);
              if (de_len <= 0) {
                      ret = -1;
                      goto bail;