diff -U0 ./ChangeLog.old ./ChangeLog
--- ./ChangeLog.old 2009-06-14 14:31:31.659446202 -0700
+++ ./ChangeLog 2009-06-14 14:31:48.369321893 -0700
@@ -2,0 +3 @@
+ * fixed torrent file path vulnerability
diff -up ./src/torrent_info.cpp.old ./src/torrent_info.cpp
--- ./src/torrent_info.cpp.old 2009-06-14 14:31:38.315506116 -0700
+++ ./src/torrent_info.cpp 2009-06-14 14:43:22.349322362 -0700
@@ -37,6 +37,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include <iterator>
#include <algorithm>
#include <set>
+#include <string>
+#include <cassert>
#ifdef _MSC_VER
#pragma warning(push, 1)
@@ -68,6 +69,30 @@ namespace
str += 0x80 | (chr & 0x3f);
}
+ bool valid_path_element(std::string const& element)
+ {
+ if (element.empty()
+ || element == "." || element == ".."
+ || element[0] == '/' || element[0] == '\\'
+ || element[element.size()-1] == ':')
+ return false;
+ return true;
+ }
+
+ namespace fs = boost::filesystem;
+ fs::path sanitize_path(fs::path const& p)
+ {
+ fs::path new_path;
+ for (fs::path::const_iterator i = p.begin(); i != p.end(); ++i)
+ {
+ if (!valid_path_element(*i)) continue;
+ std::string pe = *i;
+ new_path /= pe;
+ }
+ assert(!new_path.is_complete());
+ return new_path;
+ }
+
void verify_encoding(file_entry& target)
{
std::string tmp_path;
@@ -178,9 +202,9 @@ namespace
for (entry::list_type::const_iterator i = list->begin();
i != list->end(); ++i)
{
- if (i->string() != "..")
- target.path /= i->string();
+ target.path /= i->string();
}
+ target.path = sanitize_path(target.path);
verify_encoding(target);
if (target.path.is_complete()) throw std::runtime_error("torrent contains "
"a file with an absolute path: '"
@@ -310,11 +334,9 @@ namespace libtorrent
else
{ m_name = info["name"].string(); }
- path tmp = m_name;
- if (tmp.is_complete()) throw std::runtime_error("torrent contains "
- "a file with an absolute path: '" + m_name + "'");
- if (tmp.has_branch_path()) throw std::runtime_error(
- "torrent contains name with directories: '" + m_name + "'");
+ m_name = sanitize_path(m_name).string();
+ if (!valid_path_element(m_name))
+ throw std::runtime_error("invalid 'name' of torrent (possible exploit attempt)");
// extract file list
entry const* i = info.find_key("files");