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

Buffer overflow with string_view format string that isn't null terminated #977

Closed
msimonsson opened this issue Dec 12, 2018 · 4 comments
Closed

Comments

@msimonsson
Copy link

Hi,

It looks like the format strings are assumed to be null-terminated.
Here is a simple example that will result in a stack-overflow:

const char c[] = {'{', 'y'};
const std::string_view f{c, 2};
const auto s = fmt::format(f, 42);

I found this when fuzzing with libFuzzer.

@vitaut
Copy link
Contributor

vitaut commented Dec 12, 2018

Which {fmt} version? I get an exception with the message "argument not found" as expected.

@msimonsson
Copy link
Author

Which {fmt} version?

master

You have to compile with sanitizers, I should have mentioned that:
https://clang.llvm.org/docs/AddressSanitizer.html

Here is the code I used to fuzz the library, it will give a heap-buffer-overflow within seconds:

#include "fmt/format.hh"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
    try
    {
        const std::string_view f{reinterpret_cast<const char*>(data), size};  
        fmt::format(f, 42, 328.238123, "abc");
    }
    catch (const fmt::format_error&)
    {
        // Ignore
    }

    return 0;
}

vitaut added a commit that referenced this issue Dec 12, 2018
vitaut added a commit that referenced this issue Dec 12, 2018
@vitaut
Copy link
Contributor

vitaut commented Dec 12, 2018

Fixed in de71db6, thanks for catching this!

@vitaut vitaut closed this as completed Dec 12, 2018
@vitaut
Copy link
Contributor

vitaut commented Dec 12, 2018

The dereferenced value was never used BTW, but it was still technically a UB I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants