summaryrefslogtreecommitdiffstats
path: root/cgi.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2014-07-19 11:35:12 +0000
committerIngo Schwarze <schwarze@openbsd.org>2014-07-19 11:35:12 +0000
commita96928e9475061fac88468b762b3335bb8af06f2 (patch)
treefb4e77a526e6a9f2edf71c295c86ae2ad621afa8 /cgi.c
parentd06d27331700307ee6ca6040c2bd638cafbeef2e (diff)
downloadmandoc-a96928e9475061fac88468b762b3335bb8af06f2.tar.gz
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.
Diffstat (limited to 'cgi.c')
-rw-r--r--cgi.c29
1 files changed, 28 insertions, 1 deletions
diff --git a/cgi.c b/cgi.c
index 03426986..1e962ebe 100644
--- a/cgi.c
+++ b/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, struct manpage *r, size_t sz)
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 *file)
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)
@@ -810,6 +831,12 @@ pg_show(const struct req *req, const char *path)
return;
}
+ if ( ! validate_filename(sub)) {
+ pg_error_badrequest(
+ "You specified an invalid manual file.");
+ return;
+ }
+
resp_begin_html(200, NULL);
resp_searchform(req);
resp_show(req, sub);