Skip to content

Commit

Permalink
Fuzzer for base::nix::GetFileMimeType()
Browse files Browse the repository at this point in the history
Change-Id: I0160bba4f9a5e9feb99b02a9de052f946fd98066
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4982470
Reviewed-by: Thomas Anderson <thomasanderson@chromium.org>
Commit-Queue: Joel Hockey <joelhockey@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1216720}
  • Loading branch information
Joel Hockey authored and Chromium LUCI CQ committed Oct 29, 2023
1 parent e6151d5 commit 264c759
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4987,6 +4987,13 @@ fuzzer_test("file_path_fuzzer") {
deps = [ "//base" ]
}

if (is_linux || is_chromeos) {
fuzzer_test("mime_util_xdg_fuzzer") {
sources = [ "nix/mime_util_xdg_fuzzer.cc" ]
deps = [ "//base" ]
}
}

fuzzer_test("string_number_conversions_fuzzer") {
sources = [ "strings/string_number_conversions_fuzzer.cc" ]
deps = [ "//base" ]
Expand Down
38 changes: 38 additions & 0 deletions base/nix/mime_util_xdg_fuzzer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdlib.h>
#include <string>

#include "base/containers/span.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/nix/mime_util_xdg.h"

// Entry point for LibFuzzer.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
base::ScopedTempDir temp_dir;
if (!temp_dir.CreateUniqueTempDir()) {
// Not a fuzzer error, so we return 0.
LOG(ERROR) << "Failed to create temp dir";
return 0;
}

// The parser reads file $XDG_DATA_DIRS/mime/mime.cache.
setenv("XDG_DATA_DIRS", temp_dir.GetPath().value().c_str(), 1);
base::FilePath mime_dir = temp_dir.GetPath().Append("mime");
base::FilePath mime_cache = mime_dir.Append("mime.cache");
if (!base::CreateDirectory(mime_dir) ||
!base::WriteFile(mime_cache, base::make_span(data, size))) {
LOG(ERROR) << "Failed to create " << mime_cache;
// Not a fuzzer error, so we return 0.
return 0;
}

base::FilePath dummy_path("foo.txt");
std::string type = base::nix::GetFileMimeType(dummy_path);
return 0;
}

0 comments on commit 264c759

Please sign in to comment.