[PATCH 2/2] mcstrans: bound Include nesting depth

Stephen Smalley <[email protected]>
Newsgroups org.kernel.vger.selinux
Message-ID <[email protected]>
process_trans() handles Include directives by calling
read_translations() on each matched path, which in turn calls
process_trans() line by line. A configuration file that Includes
itself (directly or via a cycle) recurses until the daemon runs out of
file descriptors or stack. Cap the nesting depth at 32.

Signed-off-by: Stephen Smalley <[email protected]>
---
 mcstrans/src/mcstrans.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mcstrans/src/mcstrans.c b/mcstrans/src/mcstrans.c
index b5c8d54a..e79aa3d5 100644
--- a/mcstrans/src/mcstrans.c
+++ b/mcstrans/src/mcstrans.c
@@ -911,16 +911,23 @@ static int process_trans(char *buffer)
 
 int read_translations(const char *filename)
 {
+	static unsigned int depth;
 	size_t size = 0;
 	char *buffer = NULL;
 	int rval = 0;
 
+	if (depth >= 32) {
+		syslog(LOG_ERR, "%s: Include nesting too deep", filename);
+		return -1;
+	}
+
 	FILE *cfg = fopen(filename, "r");
 	if (!cfg) {
 		syslog(LOG_ERR, "%s file open failed", filename);
 		return -1;
 	}
 
+	depth++;
 	__fsetlocking(cfg, FSETLOCKING_BYCALLER);
 	while (getline(&buffer, &size, cfg) > 0) {
 		if (process_trans(buffer) < 0) {
@@ -931,6 +938,7 @@ int read_translations(const char *filename)
 	}
 	free(buffer);
 	fclose(cfg);
+	depth--;
 	return rval;
 }
 
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.