History

stream_file: allow to open only our fd in case of fuzzing

This ensures that we don't open some other fd, for example when loading playlist. Also filters out loading any local files. This also allows to remove custom filtering from fuzzer itself.

Kacper Michajłow committed 1 year ago

Commit 1d352f81 parent 9661a384 files · +13 −37Browse files

Unified · Split

4 files
  1. fuzzers/fuzzer_load.c +6 −1
  2. fuzzers/fuzzer_loadfile_direct.c +0 −20
  3. player/loadfile.c +0 −12
  4. stream/stream_file.c +7 −4
fuzzers/fuzzer_load.c+6−1
@@ -45,6 +45,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) · 44 unmodified lines
4545 int fd = memfd_create("fuzz_mpv_load", MFD_CLOEXEC | MFD_ALLOW_SEALING);
4646 if (fd == -1)
4747 exit(1);
48+ if (dup3(fd, 42, O_CLOEXEC) != 42 || close(fd))
49+ exit(1);
50+ fd = 42;
4851 ssize_t written = 0;
4952 while (written < size) {
5053 ssize_t result = write(fd, data + written, size - written);
@@ -85,7 +88,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) · 34 unmodified lines
8588 #endif
8689
8790 mpv_terminate_destroy(ctx);
88− close(fd);
91+
92+ if (close(fd))
93+ exit(1);
8994
9095 return 0;
9196 }
fuzzers/fuzzer_loadfile_direct.c+0−20
@@ -38,26 +38,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) · 37 unmodified lines
3838 return 0;
3939 #endif
4040
41−#if !defined(MPV_PROTO) || defined(MPV_PROTO_FILE)
42− const uint8_t *data_check = data;
43− size_t size_check = size;
44− size_t prefix_size = sizeof("file://") - 1;
45− if (str_startswith(data, size - 1, "file://", prefix_size)) {
46− data_check += prefix_size;
47− size_check -= prefix_size;
48− }
49− // Exclude some common paths that are not useful for testing.
50− // Exclude -
51− if (size_check == 2 && !strncmp(data_check, "-", 1))
52− return 0;
53− // Exclude relative paths
54− if (str_startswith(data_check, size_check - 1, ".", 1))
55− return 0;
56− // Exclude absolute paths
57− if (str_startswith(data_check, size_check - 1, "/", 1))
58− return 0;
59−#endif
60−
6141 mpv_handle *ctx = mpv_create();
6242 if (!ctx)
6343 exit(1);
player/loadfile.c+0−12
@@ -1182,18 +1182,6 @@ static void start_open(struct MPContext *mpctx, char *url, int url_flags, · 1181 unmodified lines
11821182 mpctx->open_for_prefetch = for_prefetch && mpctx->opts->demuxer_thread;
11831183 mpctx->demuxer_changed = false;
11841184
1185−#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1186− // Don't allow to open local paths or stdin during fuzzing
1187− bstr open_url = bstr0(mpctx->open_url);
1188− if (bstr_startswith0(open_url, "/") ||
1189− bstr_startswith0(open_url, ".") ||
1190− bstr_equals0(open_url, "-"))
1191− {
1192− cancel_open(mpctx);
1193− return;
1194− }
1195−#endif
1196−
11971185 if (mp_thread_create(&mpctx->open_thread, open_demux_thread, mpctx)) {
11981186 cancel_open(mpctx);
11991187 return;
stream/stream_file.c+7−4
@@ -309,10 +309,6 @@ static int open_f(stream_t *stream, const struct stream_open_args *args) · 308 unmodified lines
309309 MP_ERR(stream, "Invalid FD: %d\n", p->fd);
310310 return STREAM_ERROR;
311311 }
312−#endif
313−#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
314− if (p->fd == STDIN_FILENO || p->fd == STDOUT_FILENO || p->fd == STDERR_FILENO)
315− return STREAM_ERROR;
316312 #endif
317313 if (is_fdclose)
318314 p->close = true;
@@ -344,6 +340,13 @@ static int open_f(stream_t *stream, const struct stream_open_args *args) · 25 unmodified lines
344340 p->close = true;
345341 }
346342
343+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
344+ if (p->fd != 42) {
345+ s_close(stream);
346+ return STREAM_ERROR;
347+ }
348+#endif
349+
347350 struct stat st;
348351 bool is_sock_or_fifo = false;
349352 if (fstat(p->fd, &st) == 0) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t