Backport security fix https://github.com/rakshasa/libtorrent/pull/1094.patch From 34c11db37137f66730d9b3f928854eaaed37ba4b Mon Sep 17 00:00:00 2001 From: rakshasa <475125+rakshasa@users.noreply.github.com> Date: Thu, 26 Nov 2026 11:39:05 +0000 Subject: [PATCH] Restrict metadata placeholders to trusted sessions Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- a/configure +++ b/configure @@ -13702,9 +13702,9 @@ printf "%s\n" "#define PEER_NAME \"-lt101C-\"" >>confdefs.h printf "%s\n" "#define PEER_VERSION \"lt\\x10\\x1C\"" >>confdefs.h -LIBTORRENT_CURRENT=56 +LIBTORRENT_CURRENT=57 LIBTORRENT_REVISION=0 -LIBTORRENT_AGE=0 +LIBTORRENT_AGE=1 LIBTORRENT_INTERFACE_VERSION_INFO=$LIBTORRENT_CURRENT:$LIBTORRENT_REVISION:$LIBTORRENT_AGE LIBTORRENT_INTERFACE_VERSION_NO=$LIBTORRENT_CURRENT.$LIBTORRENT_AGE.$LIBTORRENT_REVISION --- a/src/download/download_constructor.cc +++ b/src/download/download_constructor.cc @@ -62,9 +62,12 @@ any_of_shared_prefix(const std::vector& split_list) { } // namespace void -DownloadConstructor::initialize(Object& b) { - if (!b.has_key_map("info") && b.has_key_string("magnet-uri")) +DownloadConstructor::initialize(Object& b, bool is_session) { + if (!b.has_key_map("info") && b.has_key_string("magnet-uri")) { parse_magnet_uri(b, b.get_key_string("magnet-uri")); + } else if (!is_session && b.get_key("info").has_key("meta_download")) { + throw input_error("External torrent has an internal meta-download marker."); + } if (b.has_key_value("creation date")) m_download->info()->set_creation_date(b.get_key_value("creation date")); @@ -125,6 +128,9 @@ DownloadConstructor::parse_info(const Object& b) { if (b.get_key_string("pieces").length() != HashString::size_data) throw input_error("Meta-download has invalid piece data."); + if (m_download->info()->name_sanitized() != utils::transform_to_hex_str(b.get_key_string("pieces")) + ".meta") + throw input_error("Meta-download has invalid name."); + chunkSize = 1; parse_single_file(b, chunkSize); --- a/src/download/download_constructor.h +++ b/src/download/download_constructor.h @@ -13,7 +13,7 @@ class Path; class DownloadConstructor { public: - void initialize(Object& b); + void initialize(Object& b, bool is_session); void parse_tracker(const Object& b); --- a/src/torrent/torrent.cc +++ b/src/torrent/torrent.cc @@ -130,14 +130,14 @@ Throttle* up_throttle_global() { return manager->upload_throttle(); } const Rate* down_rate() { return manager->download_throttle()->rate(); } const Rate* up_rate() { return manager->upload_throttle()->rate(); } -Download -download_add(Object* object, uint32_t tracker_key) { +static Download +download_add_impl(Object* object, uint32_t tracker_key, bool is_session) { auto download = std::make_unique(); DownloadConstructor ctor; ctor.set_download(download.get()); - ctor.initialize(*object); + ctor.initialize(*object, is_session); std::string infoHash; if (download->info()->is_meta_download()) @@ -180,6 +180,16 @@ download_add(Object* object, uint32_t tracker_key) { return Download(download.release()); } +Download +download_add(Object* object, uint32_t tracker_key) { + return download_add_impl(object, tracker_key, false); +} + +Download +download_add_session(Object* object, uint32_t tracker_key) { + return download_add_impl(object, tracker_key, true); +} + void download_remove(Download d) { manager->cleanup_download(d.ptr()); --- a/src/torrent/torrent.h +++ b/src/torrent/torrent.h @@ -38,6 +38,9 @@ using DList = std::list; // // Might consider redesigning that... Download download_add(Object* s, uint32_t tracker_key) LIBTORRENT_EXPORT; + +// Only use for session data loaded from a trusted source. +Download download_add_session(Object* s, uint32_t tracker_key) LIBTORRENT_EXPORT; void download_remove(Download d) LIBTORRENT_EXPORT; // Add all downloads to dlist. The client is responsible for clearing --- a/test/torrent/test_torrent.cc +++ b/test/torrent/test_torrent.cc @@ -45,6 +45,16 @@ create_magnet(const std::string& uri) { return object; } +torrent::Object* +create_meta_download(const std::string& name, const std::string& hash) { + auto object = new torrent::Object(torrent::Object::create_map()); + auto& info = object->insert_key("info", torrent::Object::create_map()); + info.insert_key("meta_download", int64_t{1}); + info.insert_key("name", name); + info.insert_key("pieces", hash); + return object; +} + // Removes the download when the scope ends, so a failed assertion does not leave it registered. struct scoped_download { ~scoped_download() { @@ -109,6 +119,61 @@ test_torrent::test_cleanup_removes_downloads() { torrent::initialize(); } +void +test_torrent::test_meta_download_trust_and_name() { + auto config = torrent::runtime::client_config(); + const bool previous_utf8 = config->file_name_allow_legacy_utf8(); + auto restore = torrent::utils::scope_guard([=]() { + config->set_file_name_allow_legacy_utf8(previous_utf8); + }); + + const std::string hash(20, '\x55'); + const std::string name = std::string(40, '5') + ".meta"; + + { + std::unique_ptr object(create_meta_download(name, hash)); + object->insert_key("magnet-uri", "magnet:?xt=urn:btih:" + std::string(40, '5')); + CPPUNIT_ASSERT_THROW(torrent::download_add(object.get(), 0), torrent::input_error); + } + + { + std::unique_ptr object(create_metafile("ordinary", 1)); + object->get_key("info").insert_key("meta_download", int64_t{0}); + CPPUNIT_ASSERT_THROW(torrent::download_add(object.get(), 0), torrent::input_error); + } + + for (const auto& invalid : {std::string(40, '5') + ".torrent", std::string(40, '6') + ".meta", + std::string(40, '5'), std::string("5.meta"), name + ".torrent"}) { + std::unique_ptr object(create_meta_download(invalid, hash)); + CPPUNIT_ASSERT_THROW_MESSAGE(invalid, torrent::download_add_session(object.get(), 0), torrent::input_error); + } + + for (bool allow_utf8 : {false, true}) { + config->set_file_name_allow_legacy_utf8(allow_utf8); + std::unique_ptr object(create_meta_download(name, hash)); + object->get_key("info").insert_key("name.utf-8", std::string(40, '5') + ".torrent"); + + if (allow_utf8) { + CPPUNIT_ASSERT_THROW(torrent::download_add_session(object.get(), 0), torrent::input_error); + } else { + scoped_download added{torrent::download_add_session(object.get(), 0)}; + object.release(); + CPPUNIT_ASSERT_EQUAL(name, added.download.file_list()->front()->path()->front().str()); + } + } + + scoped_download added{torrent::download_add(create_magnet("magnet:?xt=urn:btih:" + std::string(40, '5')), 0)}; + auto reloaded = std::make_unique(*added.download.bencode()); + torrent::download_remove(added.download); + added.download = torrent::Download(); + + CPPUNIT_ASSERT_THROW(torrent::download_add(reloaded.get(), 0), torrent::input_error); + added.download = torrent::download_add_session(reloaded.get(), 0); + reloaded.release(); + CPPUNIT_ASSERT(added.download.info()->is_meta_download()); + CPPUNIT_ASSERT_EQUAL(hash, added.download.info()->hash().str()); +} + void test_torrent::test_download_add_zero_info_hash() { const std::string zero_magnets[] = { @@ -123,13 +188,9 @@ test_torrent::test_download_add_zero_info_hash() { CPPUNIT_ASSERT_THROW_MESSAGE(uri, torrent::download_add(object.get(), 0), torrent::input_error); } - std::unique_ptr meta_download(new torrent::Object(torrent::Object::create_map())); - auto& info = meta_download->insert_key("info", torrent::Object::create_map()); - info.insert_key("meta_download", int64_t{1}); - info.insert_key("name", std::string("zero.meta")); - info.insert_key("pieces", std::string(20, '\0')); + std::unique_ptr meta_download(create_meta_download(std::string(40, '0') + ".meta", std::string(20, '\0'))); - CPPUNIT_ASSERT_THROW(torrent::download_add(meta_download.get(), 0), torrent::input_error); + CPPUNIT_ASSERT_THROW(torrent::download_add_session(meta_download.get(), 0), torrent::input_error); // Only an all-zero hash is refused. const std::pair valid_magnets[] = { --- a/test/torrent/test_torrent.h +++ b/test/torrent/test_torrent.h @@ -3,6 +3,7 @@ class test_torrent : public test_fixture { CPPUNIT_TEST_SUITE(test_torrent); CPPUNIT_TEST(test_cleanup_removes_downloads); + CPPUNIT_TEST(test_meta_download_trust_and_name); CPPUNIT_TEST(test_download_add_zero_info_hash); CPPUNIT_TEST(test_download_find_unknown_hash); CPPUNIT_TEST(test_download_find_wrong_size); @@ -16,6 +17,7 @@ class test_torrent : public test_fixture { void tearDown() override; void test_cleanup_removes_downloads(); + void test_meta_download_trust_and_name(); void test_download_add_zero_info_hash(); void test_download_find_unknown_hash(); void test_download_find_wrong_size();