|
|
167b4f |
From a83dae404feac517695c23ff43ce1e116e2bfbe0 Mon Sep 17 00:00:00 2001
|
|
|
167b4f |
From: Michael Catanzaro <mcatanzaro@gnome.org>
|
|
|
167b4f |
Date: Wed, 9 Sep 2020 11:12:02 -0500
|
|
|
167b4f |
Subject: [PATCH] Rewrite url::recvline to be nonrecursive
|
|
|
167b4f |
|
|
|
167b4f |
This function processes network input. It's semi-trusted, because the
|
|
|
167b4f |
PAC ought to be trusted. But we still shouldn't allow it to control how
|
|
|
167b4f |
far we recurse. A malicious PAC can cause us to overflow the stack by
|
|
|
167b4f |
sending a sufficiently-long line without any '\n' character.
|
|
|
167b4f |
|
|
|
167b4f |
Also, this function failed to properly handle EINTR, so let's fix that
|
|
|
167b4f |
too, for good measure.
|
|
|
167b4f |
|
|
|
167b4f |
Fixes #134
|
|
|
167b4f |
---
|
|
|
167b4f |
libproxy/url.cpp | 28 ++++++++++++++++++----------
|
|
|
167b4f |
1 file changed, 18 insertions(+), 10 deletions(-)
|
|
|
167b4f |
|
|
|
167b4f |
diff --git a/libproxy/url.cpp b/libproxy/url.cpp
|
|
|
167b4f |
index ee776b2..68d69cd 100644
|
|
|
167b4f |
--- a/libproxy/url.cpp
|
|
|
167b4f |
+++ b/libproxy/url.cpp
|
|
|
167b4f |
@@ -388,16 +388,24 @@ string url::to_string() const {
|
|
|
167b4f |
return m_orig;
|
|
|
167b4f |
}
|
|
|
167b4f |
|
|
|
167b4f |
-static inline string recvline(int fd) {
|
|
|
167b4f |
- // Read a character.
|
|
|
167b4f |
- // If we don't get a character, return empty string.
|
|
|
167b4f |
- // If we are at the end of the line, return empty string.
|
|
|
167b4f |
- char c = '\0';
|
|
|
167b4f |
-
|
|
|
167b4f |
- if (recv(fd, &c, 1, 0) != 1 || c == '\n')
|
|
|
167b4f |
- return "";
|
|
|
167b4f |
-
|
|
|
167b4f |
- return string(1, c) + recvline(fd);
|
|
|
167b4f |
+static string recvline(int fd) {
|
|
|
167b4f |
+ string line;
|
|
|
167b4f |
+ int ret;
|
|
|
167b4f |
+
|
|
|
167b4f |
+ // Reserve arbitrary amount of space to avoid small memory reallocations.
|
|
|
167b4f |
+ line.reserve(128);
|
|
|
167b4f |
+
|
|
|
167b4f |
+ do {
|
|
|
167b4f |
+ char c;
|
|
|
167b4f |
+ ret = recv(fd, &c, 1, 0);
|
|
|
167b4f |
+ if (ret == 1) {
|
|
|
167b4f |
+ if (c == '\n')
|
|
|
167b4f |
+ return line;
|
|
|
167b4f |
+ line += c;
|
|
|
167b4f |
+ }
|
|
|
167b4f |
+ } while (ret == 1 || (ret == -1 && errno == EINTR));
|
|
|
167b4f |
+
|
|
|
167b4f |
+ return line;
|
|
|
167b4f |
}
|
|
|
167b4f |
|
|
|
167b4f |
char* url::get_pac() {
|