git: cbd7e7fa5327 - main - stand: ufs directory corruption sanity checks

From: Ryan Libby <rlibby_at_FreeBSD.org>
Date: Mon, 09 Nov 2026 17:54:03 UTC
The branch main has been updated by rlibby:

URL: https://cgit.FreeBSD.org/src/commit/?id=cbd7e7fa5327fe91d5a6649b92f8f2e2fb383e87

commit cbd7e7fa5327fe91d5a6649b92f8f2e2fb383e87
Author:     Ryan Libby <rlibby@FreeBSD.org>
AuthorDate: 2026-11-09 17:15:54 +0000
Commit:     Ryan Libby <rlibby@FreeBSD.org>
CommitDate: 2026-11-09 17:15:54 +0000

    stand: ufs directory corruption sanity checks
    
    Add sanity checks for corrupt UFS directory blocks.  Don't allow bad
    data to cause buffer overflows or infinite loops.
    
    Reviewed by:    dab, imp (previous version)
    Differential Revision:  https://reviews.freebsd.org/D59378
---
 stand/libsa/ufs.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/stand/libsa/ufs.c b/stand/libsa/ufs.c
index 868e8d47dbbd..67d414b217c9 100644
--- a/stand/libsa/ufs.c
+++ b/stand/libsa/ufs.c
@@ -78,7 +78,6 @@
 #include <ufs/ufs/dir.h>
 #include <ufs/ffs/fs.h>
 #include "stand.h"
-#include "string.h"
 
 static int	ufs_open(const char *path, struct open_file *f);
 static int	ufs_write(struct open_file *f, const void *buf, size_t size,
@@ -444,8 +443,9 @@ buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
 }
 
 /*
- * Search a directory for a name and return its
- * i_number.
+ * Search a directory for a name and return its i_number.
+ * Light corruption sanity checks avoid buffer overruns and infinite
+ * loops, but we avoid being very strict.
  */
 static int
 search_directory(char *name, struct open_file *f, ino_t *inumber_p)
@@ -469,6 +469,8 @@ search_directory(char *name, struct open_file *f, ino_t *inumber_p)
 		dp = (struct direct *)buf;
 		edp = (struct direct *)(buf + buf_size);
 		while (dp < edp) {
+			if ((uintptr_t)&dp->d_name > (uintptr_t)edp)
+				return (EIO);
 			if (dp->d_ino == (ino_t)0)
 				goto next;
 #if BYTE_ORDER == LITTLE_ENDIAN
@@ -477,13 +479,17 @@ search_directory(char *name, struct open_file *f, ino_t *inumber_p)
 			else
 #endif
 				namlen = dp->d_namlen;
+			if ((uintptr_t)&dp->d_name[namlen] > (uintptr_t)edp)
+				goto next;
 			if (namlen == length &&
-			    !strcmp(name, dp->d_name)) {
+			    bcmp(dp->d_name, name, length) == 0) {
 				/* found entry */
 				*inumber_p = dp->d_ino;
 				return (0);
 			}
 		next:
+			if (dp->d_reclen == 0)
+				return (EIO);
 			dp = (struct direct *)((char *)dp + dp->d_reclen);
 		}
 		fp->f_seekp += buf_size;