From b00c59f95fcf2e585dab5c79eca698e7d2e7a8c8 Mon Sep 17 00:00:00 2001 From: Manu Garg Date: Mon, 9 Nov 2026 22:22:27 -0800 Subject: [PATCH 1/2] Fix one-byte stack overflow in myIpAddress() with an over-long client IP pacparser_setmyip() accepted strings of up to INET6_ADDRSTRLEN (46) characters, but my_ip() copies the value into an INET6_ADDRSTRLEN-byte stack buffer, so a 46-character value overflowed it by the terminating NUL. INET6_ADDRSTRLEN already counts the NUL, so no valid IP address is that long: reject such values in pacparser_setmyip(), and size my_ip()'s buffer to match my_ip_buf. Add tests that the longest valid IP is accepted and returned by myIpAddress(), and that a longer value is rejected. Reported by Tushar98644. --- src/pacparser.c | 7 +++++-- tests/runtests.sh | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/pacparser.c b/src/pacparser.c index 38a6e1d7..2b23b144 100644 --- a/src/pacparser.c +++ b/src/pacparser.c @@ -247,7 +247,8 @@ pac_console_log(JSContext *ctx, JSValueConst UNUSED(this_val), int argc, JSValue static JSValue my_ip(JSContext *ctx, JSValueConst UNUSED(this_val), int UNUSED(argc), JSValueConst *UNUSED(argv)) { - char ipaddr[INET6_ADDRSTRLEN]; + // Same size as my_ip_buf, which is copied into it. + char ipaddr[sizeof(my_ip_buf)]; if (my_ip_set) // If my (client's) IP address is already set. strcpy(ipaddr, my_ip_buf); @@ -292,7 +293,9 @@ static const char *proxy_result = NULL; int pacparser_setmyip(const char *ip) { - if (strlen(ip) > INET6_ADDRSTRLEN) { + // INET6_ADDRSTRLEN includes the terminating NUL, so no valid IP address + // is INET6_ADDRSTRLEN characters or longer. + if (strlen(ip) >= INET6_ADDRSTRLEN) { fprintf(stderr, "pacparser_setmyip: IP too long: %s\n", ip); return 0; } diff --git a/tests/runtests.sh b/tests/runtests.sh index 63e8aff1..a76f5b10 100755 --- a/tests/runtests.sh +++ b/tests/runtests.sh @@ -79,4 +79,23 @@ if [ "$actual_stderr" != "$expected_stderr" ]; then exit 1 fi +# Client IP length tests: the longest textual IP address (45 characters) is +# accepted and returned by myIpAddress(); anything longer is rejected. +myip_pac=$(mktemp) +echo 'function FindProxyForURL(url, host) { return myIpAddress(); }' > $myip_pac +longest_ip="ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255" +result=$($pactester -p $myip_pac -u http://example.com/ -c $longest_ip) +if [ "$result" != "$longest_ip" ]; then + echo "Client IP test failed: got \"$result\", expected \"$longest_ip\"" + rm -f $myip_pac + exit 1 +fi +if $pactester -p $myip_pac -u http://example.com/ -c "${longest_ip}0" \ + >/dev/null 2>&1; then + echo "Client IP test failed: a 46-character client IP was accepted" + rm -f $myip_pac + exit 1 +fi +rm -f $myip_pac + echo "All tests were successful." From 8960f4affbdc8698d55fdfc448448b68f963466c Mon Sep 17 00:00:00 2001 From: Manu Garg Date: Mon, 9 Nov 2026 22:25:12 -0800 Subject: [PATCH 2/2] Raise ValueError from Python setmyip() and address review comments - The Python binding ignored pacparser_setmyip()'s return value, so a rejected client IP was silently dropped. Raise ValueError instead. - Size my_ip_buf for the longest textual IP address (46 bytes with NUL) instead of INET6_ADDRSTRLEN, which is 65 on Windows, so the limit is the same on every platform. - Return my_ip_buf directly from my_ip() and my_ip_ex() instead of copying it to a stack buffer first. - Report the rejection through print_error(), so it honours pacparser_set_error_printer(). - Tests: check the error message in the shell test, quote and clean up the temp PAC file, and add a Python test for setmyip(). --- src/pacparser.c | 42 +++++++++++++++------------------ src/pymod/pacparser/__init__.py | 2 ++ src/pymod/pacparser_py.c | 5 +++- tests/runtests.py | 20 ++++++++++++++++ tests/runtests.sh | 15 ++++++------ 5 files changed, 52 insertions(+), 32 deletions(-) diff --git a/src/pacparser.c b/src/pacparser.c index 2b23b144..da9cc785 100644 --- a/src/pacparser.c +++ b/src/pacparser.c @@ -51,7 +51,10 @@ # define UNUSED(x) UNUSED_ ## x #endif -static char my_ip_buf[INET6_ADDRSTRLEN+1]; +// Client IP set via pacparser_setmyip(). Sized for the longest textual IP +// address (45 characters, an IPv6 address with an embedded IPv4 address) plus +// the terminating NUL. Not INET6_ADDRSTRLEN, which is larger on Windows. +static char my_ip_buf[46]; static int my_ip_set = 0; // Default error printer function. @@ -247,19 +250,16 @@ pac_console_log(JSContext *ctx, JSValueConst UNUSED(this_val), int argc, JSValue static JSValue my_ip(JSContext *ctx, JSValueConst UNUSED(this_val), int UNUSED(argc), JSValueConst *UNUSED(argv)) { - // Same size as my_ip_buf, which is copied into it. - char ipaddr[sizeof(my_ip_buf)]; + char ipaddr[INET6_ADDRSTRLEN]; if (my_ip_set) // If my (client's) IP address is already set. - strcpy(ipaddr, my_ip_buf); - else { - char name[256]; - gethostname(name, sizeof(name)); - if (resolve_host(name, ipaddr, 1, AF_INET)) { - strcpy(ipaddr, "127.0.0.1"); - } - } + return JS_NewString(ctx, my_ip_buf); + char name[256]; + gethostname(name, sizeof(name)); + if (resolve_host(name, ipaddr, 1, AF_INET)) { + strcpy(ipaddr, "127.0.0.1"); + } return JS_NewString(ctx, ipaddr); } @@ -271,15 +271,13 @@ my_ip_ex(JSContext *ctx, JSValueConst UNUSED(this_val), int UNUSED(argc), JSValu char ipaddr[INET6_ADDRSTRLEN * MAX_IP_RESULTS + MAX_IP_RESULTS]; if (my_ip_set) // If my (client's) IP address is already set. - strcpy(ipaddr, my_ip_buf); - else { - char name[256]; - gethostname(name, sizeof(name)); - if (resolve_host(name, ipaddr, MAX_IP_RESULTS, AF_UNSPEC)) { - strcpy(ipaddr, ""); - } - } + return JS_NewString(ctx, my_ip_buf); + char name[256]; + gethostname(name, sizeof(name)); + if (resolve_host(name, ipaddr, MAX_IP_RESULTS, AF_UNSPEC)) { + strcpy(ipaddr, ""); + } return JS_NewString(ctx, ipaddr); } @@ -293,10 +291,8 @@ static const char *proxy_result = NULL; int pacparser_setmyip(const char *ip) { - // INET6_ADDRSTRLEN includes the terminating NUL, so no valid IP address - // is INET6_ADDRSTRLEN characters or longer. - if (strlen(ip) >= INET6_ADDRSTRLEN) { - fprintf(stderr, "pacparser_setmyip: IP too long: %s\n", ip); + if (strlen(ip) >= sizeof(my_ip_buf)) { + print_error("pacparser_setmyip: IP too long: %s\n", ip); return 0; } diff --git a/src/pymod/pacparser/__init__.py b/src/pymod/pacparser/__init__.py index b4a81e1f..739ce11e 100644 --- a/src/pymod/pacparser/__init__.py +++ b/src/pymod/pacparser/__init__.py @@ -114,6 +114,8 @@ def just_find_proxy(pacfile, url, host=None): def setmyip(ip_address): """ Set my ip address. This is the IP address returned by myIpAddress() + + Raises ValueError if ip_address is too long to be an IP address. """ _pacparser.setmyip(ip_address) diff --git a/src/pymod/pacparser_py.c b/src/pymod/pacparser_py.c index e42b1538..9a14ac0b 100644 --- a/src/pymod/pacparser_py.c +++ b/src/pymod/pacparser_py.c @@ -128,7 +128,10 @@ py_pacparser_setmyip(PyObject *self, PyObject *args) const char *ip; if (!PyArg_ParseTuple(args, "s", &ip)) return NULL; - pacparser_setmyip(ip); + if (!pacparser_setmyip(ip)) { + PyErr_Format(PyExc_ValueError, "invalid IP address: %s", ip); + return NULL; + } Py_RETURN_NONE; } diff --git a/tests/runtests.py b/tests/runtests.py index 83b95ec4..5a435463 100644 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -109,6 +109,26 @@ def runtests(pacfile, testdata, tests_dir): raise Exception('Logging test failed: stderr mismatch\nExpected:\n%s\nGot:\n%s' % (expected_stderr, actual_stderr)) + # Client IP length test: the longest textual IP address (45 characters) is + # returned by myIpAddress(); anything longer raises ValueError. + longest_ip = 'ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255' + pacparser.setmyip(longest_ip) + pacparser.init() + pacparser.parse_pac_string( + 'function FindProxyForURL(url, host) { return myIpAddress(); }') + result = pacparser.find_proxy('http://example.com/') + pacparser.cleanup() + if result != longest_ip: + raise Exception('Client IP test failed: got "%s", expected "%s"' % + (result, longest_ip)) + try: + pacparser.setmyip(longest_ip + '0') + except ValueError: + pass + else: + raise Exception('Client IP test failed: a 46-character client IP was ' + 'accepted') + print('All tests were successful.') diff --git a/tests/runtests.sh b/tests/runtests.sh index a76f5b10..b9afa7e1 100755 --- a/tests/runtests.sh +++ b/tests/runtests.sh @@ -82,20 +82,19 @@ fi # Client IP length tests: the longest textual IP address (45 characters) is # accepted and returned by myIpAddress(); anything longer is rejected. myip_pac=$(mktemp) -echo 'function FindProxyForURL(url, host) { return myIpAddress(); }' > $myip_pac +trap 'rm -f "$myip_pac"' EXIT +echo 'function FindProxyForURL(url, host) { return myIpAddress(); }' > "$myip_pac" longest_ip="ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255" -result=$($pactester -p $myip_pac -u http://example.com/ -c $longest_ip) +result=$($pactester -p "$myip_pac" -u http://example.com/ -c $longest_ip) if [ "$result" != "$longest_ip" ]; then echo "Client IP test failed: got \"$result\", expected \"$longest_ip\"" - rm -f $myip_pac exit 1 fi -if $pactester -p $myip_pac -u http://example.com/ -c "${longest_ip}0" \ - >/dev/null 2>&1; then - echo "Client IP test failed: a 46-character client IP was accepted" - rm -f $myip_pac +result=$($pactester -p "$myip_pac" -u http://example.com/ -c "${longest_ip}0" 2>&1) +if [ $? = 0 ] || [ "${result/IP too long/}" = "$result" ]; then + echo "Client IP test failed: a 46-character client IP was not rejected" + echo "$result" exit 1 fi -rm -f $myip_pac echo "All tests were successful."