From ee1e45d3fec65f3886a2d3ba9f91a19b32954a9e Mon Sep 17 00:00:00 2001 From: Gennaro Prota Date: Tue, 24 Nov 2026 17:10:02 +0100 Subject: [PATCH] Fix an out-of-bounds read in the XML archive signature check `basic_xml_grammar<>::init()` compared the signature in the header of an XML archive with `BOOST_ARCHIVE_SIGNATURE()` using the three-iterator `std::equal()`, which reads as many characters of the expected signature as the archive provides. So a longer signature made it read past the end of the expected one, and any prefix of it, such as "serialization", was accepted. We now compare the lengths first, as "fix address sanitizer exception when loading corrupted XML archive. GitHub issue #257" did for the tag names in `load_end()`. Note that the test can only catch the accepted prefix: the read past the end goes unnoticed without a sanitizer, and CI runs none. Fixes #392. --- src/basic_xml_grammar.ipp | 7 ++-- test/Jamfile.v2 | 1 + test/test_xml_signature_check.cpp | 60 +++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 test/test_xml_signature_check.cpp diff --git a/src/basic_xml_grammar.ipp b/src/basic_xml_grammar.ipp index ee9eced64..61cd5a2a9 100644 --- a/src/basic_xml_grammar.ipp +++ b/src/basic_xml_grammar.ipp @@ -22,7 +22,7 @@ #endif #include // errno -#include // strerror(errno) +#include // strerror(errno), strlen // spirit stuff #include @@ -461,10 +461,13 @@ void basic_xml_grammar::init(IStream & is){ boost::serialization::throw_exception( xml_archive_exception(xml_archive_exception::xml_archive_parsing_error) ); - if(! std::equal(rv.class_name.begin(), rv.class_name.end(), BOOST_ARCHIVE_SIGNATURE())) + const char * const signature = BOOST_ARCHIVE_SIGNATURE(); + if(rv.class_name.size() != std::strlen(signature) + || ! std::equal(rv.class_name.begin(), rv.class_name.end(), signature)){ boost::serialization::throw_exception( archive_exception(archive_exception::invalid_signature) ); + } } template diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index ec76185fe..b934441a0 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -181,6 +181,7 @@ if ! $(BOOST_ARCHIVE_LIST) { [ test-bsl-run test_xml_escape_boundary : : ../build//boost_wserialization : [ requires std_wstreambuf ] ] [ test-bsl-run test_xml_trailing_whitespace ] [ test-bsl-run test_xml_missing_nvp ] + [ test-bsl-run test_xml_signature_check ] [ test-bsl-run test_mult_archive_types : : : [ requires std_wstreambuf ] ] [ test-bsl-run test_iterators : : : [ requires std_wstreambuf ] ] [ test-bsl-run test_iterators_base64 ] diff --git a/test/test_xml_signature_check.cpp b/test/test_xml_signature_check.cpp new file mode 100644 index 000000000..4001a59b0 --- /dev/null +++ b/test/test_xml_signature_check.cpp @@ -0,0 +1,60 @@ +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// test_xml_signature_check.cpp + +// Copyright 2026 Gennaro Prota. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// An XML input archive must accept only the exact signature in the header. +// The check used to compare just as many characters as the archive gave, so +// it accepted a prefix of the signature, and read past the end of it for a +// longer one. + +// Reported by jhtrace in +// https://github.com/boostorg/serialization/issues/392. Thanks! + +#include +#include + +#include +#include +#include + +#include "test_tools.hpp" + +// The header of a valid archive, with the signature replaced. +std::string header_with_signature(const std::string & signature){ + std::ostringstream os; + { + boost::archive::xml_oarchive oa(os); + } + std::string header = os.str(); + const std::string attribute = "signature=\""; + const std::size_t first = header.find(attribute) + attribute.size(); + const std::size_t last = header.find('"', first); + header.replace(first, last - first, signature); + return header; +} + +bool is_accepted(const std::string & signature){ + bool accepted = true; + std::istringstream is(header_with_signature(signature)); + try{ + boost::archive::xml_iarchive ia(is); + } + catch(const boost::archive::archive_exception & e){ + BOOST_CHECK( + e.code == boost::archive::archive_exception::invalid_signature + ); + accepted = false; + } + return accepted; +} + +int test_main(int /* argc */, char * /* argv */ []){ + BOOST_CHECK(is_accepted("serialization::archive")); + BOOST_CHECK(! is_accepted("serialization::archive_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); + BOOST_CHECK(! is_accepted("serialization")); + return EXIT_SUCCESS; +}