Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out get_wwwroot_parent and test it. #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ devel/test.out.stdout
devel/test.out.stderr
devel/test_make_safe_uri
devel/test_password_equal
devel/test_get_wwwroot_parent
devel/tmp.httpd.tests
60 changes: 35 additions & 25 deletions darkhttpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2916,6 +2916,38 @@ static void pidfile_create(void) {
}
/* [<-] end of pidfile helpers. */

/* In the --single-file case, wwwroot is a path to a file.
* Return the parent dir of the wwwroot and mutate the wwwroot
* to just the filename. Caller must free the return value. */
char* get_wwwroot_parent() {
off_t ofs;
size_t len = strlen(wwwroot) + 1;
Comment on lines +2922 to +2924
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last things I would change:
static char* get_wwwroot_parent(void) {
and something I forgot again
const size_t len = strlen(wwwroot) + 1;

While being on the topic of pedantic changes, I noticed a few things in darkhttpd.c, none of them matter much, so I don't know if I want to make a PR, but if you want I can make it.

if (len == 1) return xstrdup(".");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len can't be 1. It's the length of wwwroot including NULL terminator.
We could remove + 1, but then memmove(wwwroot, &wwwroot[ofs], len + 1 - ofs); could be a bit more confusing.
I don't mind either way.

I am dumb, just do len == 2

char *path = xstrdup(wwwroot);
for (ofs = strlen(wwwroot);
(ofs >= 0) && (wwwroot[ofs] != '/');
ofs--)
;
/* wwwroot file is not in current directory */
if (ofs >= 0) {
path[ofs + 1] = '\0';
memmove(wwwroot, &wwwroot[ofs], len - ofs);
} else {
path[0] = '.';
path[1] = '\0';
}
return path;
}

static void xchroot(const char *path) {
if (chdir(path) == -1)
err(1, "chdir(%s)", path);
if (chroot(path) == -1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be if (chroot(".") == -1)

This is my bad. I tested with absolute paths, so I didn't notice that we already are in the directory we want to chroot to. I think we can leave path in the error messages.

err(1, "chroot(%s)", path);
printf("chrooted to `%s'\n", path);
}

/* chroot() into wwwroot. */
static void change_root(void) {
#ifdef HAVE_NON_ROOT_CHROOT
/* We run this even as root, which should never be a bad thing. */
Expand All @@ -2927,33 +2959,11 @@ static void change_root(void) {

tzset(); /* read /etc/localtime before we chroot */
if (want_single_file) {
off_t ofs;
size_t len = strlen(wwwroot) + 1;
char *path = xstrdup(wwwroot);
for (ofs = strlen(wwwroot);
(ofs >= 0) && (wwwroot[ofs] != '/');
ofs--)
;
/* wwwroot file is not in current directory */
if (ofs >= 0) {
path[ofs + 1] = '\0';
if (chdir(path) == -1)
err(1, "chdir(%s)", path);
memmove(wwwroot, &wwwroot[ofs], len - ofs);
} else {
path[0] = '.';
path[1] = '\0';
}
if (chroot(path) == -1)
err(1, "chroot(%s)", path);
printf("chrooted to `%s'\n", path);
char *path = get_wwwroot_parent();
xchroot(path);
free(path);
} else {
if (chdir(wwwroot) == -1)
err(1, "chdir(%s)", wwwroot);
if (chroot(wwwroot) == -1)
err(1, "chroot(%s)", wwwroot);
printf("chrooted to `%s'\n", wwwroot);
xchroot(wwwroot);
wwwroot[0] = '\0'; /* empty string */
}
}
Expand Down
10 changes: 8 additions & 2 deletions devel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ clean:
test.pyc \
test_make_safe_uri \
test_password_equal \
a.out darkhttpd.gcda darkhttpd.gcno \
fuzz_darkhttpd.o fuzz_llvm_make_safe_uri fuzz_socket
test_get_wwwroot_parent \
a.out \
darkhttpd.gcda \
darkhttpd.gcno \
fuzz_darkhttpd.o \
fuzz_llvm_make_safe_uri \
fuzz_socket \
fuzz_get_wwwroot_parent
rm -rf tmp.fuzz tmp.httpd.tests
20 changes: 20 additions & 0 deletions devel/fuzz_get_wwwroot_parent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#define main darkhttpd_main
#include "../darkhttpd.c"
#undef main

char* sdup(const uint8_t *data, size_t size) {
char *buf = malloc(size + 1);
memcpy(buf, data, size);
buf[size] = 0;
return buf;
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
wwwroot = sdup(data, size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It test for data = NULL. This is the same problem we have with test("", ".", "");. We can account for wwwroot being NULL in get_wwwroot_parent(), but like I said, I don't think it is worth it.

I couldn't find anything to disallow the fuzzer from testing a string or a string length. So I added if (size == 0) return 0; to run it.

char *path = get_wwwroot_parent();
free(path);
free(wwwroot);
return 0;
}

/* vim:set ts=2 sw=2 sts=2 expandtab tw=78: */
6 changes: 6 additions & 0 deletions devel/fuzz_get_wwwroot_parent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash -e
set -x
clang -g -O -fsanitize=fuzzer,address \
fuzz_get_wwwroot_parent.c -o fuzz_get_wwwroot_parent
mkdir -p fuzz_get_wwwroot_parent_testcases
./fuzz_get_wwwroot_parent -only_ascii=1 $* fuzz_get_wwwroot_parent_testcases/
10 changes: 9 additions & 1 deletion devel/run-tests
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ if ./test_password_equal | egrep '^FAIL:'; then
exit 1
fi

echo "===> test_get_wwwroot_parent"
$CC -g -O2 -fsanitize=address -fsanitize=undefined \
test_get_wwwroot_parent.c -o test_get_wwwroot_parent | exit 1
if ./test_get_wwwroot_parent | egrep '^FAIL:'; then
echo test_get_wwwroot_parent failed >&2
exit 1
fi

# Check that the code builds with various defines.
echo "===> building without -DDEBUG"
$CC -O2 -Wall ../darkhttpd.c || exit 1
Expand Down Expand Up @@ -239,7 +247,7 @@ $CC -g -O2 -fprofile-arcs -ftest-coverage -fsanitize=address \
exit 1
}
echo "===> generating report"
gcov darkhttpd
gcov a-darkhttpd
chmod 755 $DIR/forbidden
chmod 755 $DIR/unreadable
rm -rf $DIR
Expand Down
29 changes: 29 additions & 0 deletions devel/test_get_wwwroot_parent.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#define main _main_disabled_
#include "../darkhttpd.c"
#undef main

static void test(const char* input_wwwroot, const char* expected_path,
const char* expected_wwwroot) {
wwwroot = xstrdup(input_wwwroot);
char* path = get_wwwroot_parent();
int pass = (strcmp(path, expected_path) == 0) &&
(strcmp(wwwroot, expected_wwwroot) == 0);
printf("%s: \"%s\" -> \"%s\" : \"%s\"", pass ? "PASS" : "FAIL", input_wwwroot,
path, wwwroot);
if (!pass) {
printf(" (expected \"%s\" : \"%s\")", expected_path, expected_wwwroot);
}
printf("\n");
free(path);
free(wwwroot);
}

int main(void) {
test("", ".", "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a case that can't occur in darkhttpd. wwwroot can't be NULL at this point and

path[0] = '.';
path[1] = '\0';

in get_wwwroot_parent() would write to unallocated memory. So this test is not necessary I think.

These tests would be good too I think. Just to cover all bases.

test("x", ".", "x"); // 1 char
test(".dir/file", ".dir/", "/file"); // hidden dir
test(".file", ".", ".file"); // hidden file
test("/file", "/", "/file"); // file in root

test(".", ".", ".");
test("dir/file", "dir/", "/file");
test("./a/b/c", "./a/b/", "/c");
return 0;
}

/* vim:set tabstop=4 shiftwidth=4 expandtab tw=78: */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might it be worth setting these settings in the .editorconfig?

Loading