git: 5f4a02c1d889 - main - soelim: bound include recursion to 64 levels

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 07:40:27 UTC
The branch main has been updated by bapt:

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

commit 5f4a02c1d8890c2a42b65967d7479aac3ec508e8
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2026-11-18 06:17:00 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2026-11-18 07:40:20 +0000

    soelim: bound include recursion to 64 levels
---
 usr.bin/soelim/soelim.c             | 18 +++++++++++++++---
 usr.bin/soelim/tests/soelim_test.sh |  8 ++++++++
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/usr.bin/soelim/soelim.c b/usr.bin/soelim/soelim.c
index 51f3203c067f..fe363b9e8827 100644
--- a/usr.bin/soelim/soelim.c
+++ b/usr.bin/soelim/soelim.c
@@ -37,7 +37,11 @@
 
 #define C_OPTION 0x1
 
+/* Maximum number of nested .so files to process. */
+#define MAX_INCLUDE_DEPTH 64
+
 static StringList *includes;
+static int include_depth;
 
 static void
 usage(void)
@@ -90,6 +94,7 @@ soelim_file(FILE *f, const char *name, int flag)
 	if (f == NULL)
 		return (1);
 
+	include_depth++;
 	ret = 0;
 	while ((linelen = getline(&line, &linecap, f)) > 0) {
 		if (strncmp(line, ".so", 3) != 0) {
@@ -117,20 +122,27 @@ soelim_file(FILE *f, const char *name, int flag)
 			printf("%s", line);
 			continue;
 		}
+		if (include_depth >= MAX_INCLUDE_DEPTH) {
+			warnx("%s: too many nested includes", walk);
+			ret = 1;
+			break;
+		}
 		if (soelim_file(soelim_fopen(walk), walk, flag) == 1) {
-			free(line);
-			return (1);
+			ret = 1;
+			break;
 		}
 		if (*cp != '\0')
 			printf("%s", cp);
 	}
+	include_depth--;
 
 	if (ferror(f)) {
 		warn("%s", name);
 		ret = 1;
 	}
 	free(line);
-	fclose(f);
+	if (f != stdin)
+		fclose(f);
 
 	return (ret);
 }
diff --git a/usr.bin/soelim/tests/soelim_test.sh b/usr.bin/soelim/tests/soelim_test.sh
index 5624a991ca35..bf126d35260b 100755
--- a/usr.bin/soelim/tests/soelim_test.sh
+++ b/usr.bin/soelim/tests/soelim_test.sh
@@ -126,6 +126,14 @@ errors_body()
 		-e ignore \
 		-s exit:1 \
 		sh -c 'soelim basic.in > /dev/full'
+
+	# Self-inclusion is bounded.
+	printf '.so self\n' > self
+	atf_check \
+		-o empty \
+		-e match:"too many nested includes" \
+		-s exit:1 \
+		soelim self
 }
 
 atf_init_test_cases()