server_mount_fuzzer.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <random>
  5. #include <string>
  6. #include <vector>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include <utime.h>
  10. #include "fuzzed_stream.h"
  11. // Fixture layout, with <token> unknown to the fuzzer:
  12. // root/mnt/{index.html,a.txt,data.bin,sub/index.html} served at /mnt
  13. // root/mnt/link-in.txt -> a.txt (stays inside, may be served)
  14. // root/mnt/link-out.txt -> ../outside.txt (escapes, must be refused)
  15. // root/mnt/link-sibling.txt -> ../mnt_secret/secret.txt (shares the "mnt"
  16. // prefix, must be refused)
  17. // The response must never contain <token>.
  18. namespace {
  19. FuzzableServer g_server;
  20. std::string g_root;
  21. std::string g_token;
  22. std::vector<std::string> g_paths; // In creation order
  23. void check(bool ok, const char *what) {
  24. if (!ok) {
  25. fprintf(stderr, "fixture setup failed: %s\n", what);
  26. abort();
  27. }
  28. }
  29. void make_dir(const std::string &path) {
  30. check(mkdir(path.c_str(), 0755) == 0, path.c_str());
  31. g_paths.push_back(path);
  32. }
  33. void make_file(const std::string &path, const std::string &content) {
  34. {
  35. std::ofstream out(path, std::ios::binary);
  36. out << content;
  37. check(out.good(), path.c_str());
  38. }
  39. // Fixed, so that conditional requests behave the same in every run
  40. utimbuf times = {1700000000, 1700000000};
  41. check(utime(path.c_str(), &times) == 0, path.c_str());
  42. g_paths.push_back(path);
  43. }
  44. void make_link(const std::string &target, const std::string &path) {
  45. check(symlink(target.c_str(), path.c_str()) == 0, path.c_str());
  46. g_paths.push_back(path);
  47. }
  48. std::string serve(const uint8_t *data, size_t size) {
  49. FuzzedStream stream{data, size};
  50. g_server.ProcessFuzzedRequest(stream);
  51. return stream.response();
  52. }
  53. void check_served(const char *path) {
  54. auto req = std::string("GET ") + path + " HTTP/1.1\r\nHost: x\r\n\r\n";
  55. auto res = serve(reinterpret_cast<const uint8_t *>(req.data()), req.size());
  56. // Without this, a fuzzer that serves nothing would pass its check forever
  57. check(res.find("abcdef") != std::string::npos, path);
  58. }
  59. void remove_fixture() {
  60. for (auto it = g_paths.rbegin(); it != g_paths.rend(); ++it) {
  61. if (unlink(it->c_str()) != 0) { rmdir(it->c_str()); }
  62. }
  63. rmdir(g_root.c_str());
  64. }
  65. } // namespace
  66. extern "C" int LLVMFuzzerInitialize(int * /*argc*/, char *** /*argv*/) {
  67. std::random_device rd;
  68. char token[33];
  69. snprintf(token, sizeof(token), "%08x%08x%08x%08x", rd(), rd(), rd(), rd());
  70. g_token = token;
  71. auto tmpdir = getenv("TMPDIR");
  72. auto tmpl = std::string(tmpdir && *tmpdir ? tmpdir : "/tmp") +
  73. "/httplib-mount-XXXXXX";
  74. check(mkdtemp(&tmpl[0]) != nullptr, tmpl.c_str());
  75. g_root = tmpl;
  76. atexit(remove_fixture);
  77. auto mnt = g_root + "/mnt";
  78. make_dir(mnt);
  79. make_dir(mnt + "/sub");
  80. make_dir(g_root + "/mnt_secret");
  81. make_file(mnt + "/index.html", "<html>index</html>");
  82. make_file(mnt + "/a.txt", "abcdef");
  83. make_file(mnt + "/data.bin", std::string("\x01\x02\x03\x04\x05\x06", 6));
  84. make_file(mnt + "/sub/index.html", "<html>sub</html>");
  85. make_file(g_root + "/outside.txt", g_token);
  86. make_file(g_root + "/mnt_secret/secret.txt", g_token);
  87. make_link("a.txt", mnt + "/link-in.txt");
  88. make_link("../outside.txt", mnt + "/link-out.txt");
  89. make_link("../mnt_secret/secret.txt", mnt + "/link-sibling.txt");
  90. g_server.MarkAsRunning();
  91. check(g_server.set_mount_point("/mnt", mnt), "set_mount_point");
  92. check_served("/mnt/a.txt");
  93. check_served("/mnt/link-in.txt");
  94. return 0;
  95. }
  96. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  97. if (serve(data, size).find(g_token) != std::string::npos) { abort(); }
  98. return 0;
  99. }