source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Security fix: Validate the name of the file to show before
@ 2014-07-19 11:35 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2014-07-19 11:35 UTC (permalink / raw)
  To: source

Log Message:
-----------
Security fix:
Validate the name of the file to show before opening it.
Only allow relative filenames starting with "man" or "cat"
and containing neither "/.." nor "../".          

While here, correct the condition discarding an initial "./".

Vulnerability found by Sebastien Marie <semarie-openbsd at latrappe dot fr>.
Many thanks for sending a patch; however, i did not use it but made the
checks even stricter.

Modified Files:
--------------
    mdocml:
        cgi.c

Revision Data
-------------
Index: cgi.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/cgi.c,v
retrieving revision 1.75
retrieving revision 1.76
diff -Lcgi.c -Lcgi.c -u -p -r1.75 -r1.76
--- cgi.c
+++ cgi.c
@@ -466,6 +466,17 @@ resp_searchform(const struct req *req)
 	puts("<!-- End search form. //-->");
 }
 
+static int
+validate_filename(const char *file)
+{
+
+	if ('.' == file[0] && '/' == file[1])
+		file += 2;
+
+	return ( ! (strstr(file, "../") || strstr(file, "/..") ||
+	    (strncmp(file, "man", 3) && strncmp(file, "cat", 3))));
+}
+
 static void
 pg_index(const struct req *req)
 {
@@ -523,6 +534,15 @@ pg_searchres(const struct req *req, stru
 	int		 prio, priouse;
 	char		 sec;
 
+	for (i = 0; i < sz; i++) {
+		if (validate_filename(r[i].file))
+			continue;
+		fprintf(stderr, "invalid filename %s in %s database\n",
+		    r[i].file, req->q.manpath);
+		pg_error_internal();
+		return;
+	}
+
 	if (1 == sz) {
 		/*
 		 * If we have just one result, then jump there now
@@ -777,7 +797,8 @@ format(const struct req *req, const char
 static void
 resp_show(const struct req *req, const char *file)
 {
-	if ('.' == file[0] || '/' == file[1])
+
+	if ('.' == file[0] && '/' == file[1])
 		file += 2;
 
 	if ('c' == *file)
@@ -807,6 +828,12 @@ pg_show(const struct req *req, const cha
 	if (-1 == chdir(path)) {
 		pg_error_badrequest(
 		    "You specified an invalid manpath.");
+		return;
+	}
+
+	if ( ! validate_filename(sub)) {
+		pg_error_badrequest(
+		    "You specified an invalid manual file.");
 		return;
 	}
 
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-07-19 11:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-19 11:35 mdocml: Security fix: Validate the name of the file to show before schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).