-
Notifications
You must be signed in to change notification settings - Fork 87
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
if (len == 1) return xstrdup("."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I am dumb, just do |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be 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. */ | ||
|
@@ -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 */ | ||
} | ||
} | ||
|
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 I couldn't find anything to disallow the fuzzer from testing a string or a string length. So I added |
||
char *path = get_wwwroot_parent(); | ||
free(path); | ||
free(wwwroot); | ||
return 0; | ||
} | ||
|
||
/* vim:set ts=2 sw=2 sts=2 expandtab tw=78: */ |
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/ |
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("", ".", ""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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(".", ".", "."); | ||
test("dir/file", "dir/", "/file"); | ||
test("./a/b/c", "./a/b/", "/c"); | ||
return 0; | ||
} | ||
|
||
/* vim:set tabstop=4 shiftwidth=4 expandtab tw=78: */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might it be worth setting these settings in the .editorconfig? |
There was a problem hiding this comment.
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.