4bff0a
From a652268ae11633cf64c87586bed1fd3c7141707a Mon Sep 17 00:00:00 2001
4bff0a
From: Yu Watanabe <watanabe.yu+github@gmail.com>
4bff0a
Date: Wed, 22 Aug 2018 12:33:27 +0900
4bff0a
Subject: [PATCH] util: do not use stack frame for parsing arbitrary inputs
4bff0a
4bff0a
This replaces strndupa() by strndup() in socket_address_parse(),
4bff0a
as input string may be too long.
4bff0a
4bff0a
Fixes issue 10007 by ClusterFuzz-External:
4bff0a
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=10007
4bff0a
4bff0a
(cherry picked from commit 8d30fcb9b51b1d102a589171b6e28f5f370236f6)
4bff0a
4bff0a
Resolves: #1696224
4bff0a
---
4bff0a
 src/basic/socket-util.c | 16 ++++++++++++----
4bff0a
 1 file changed, 12 insertions(+), 4 deletions(-)
4bff0a
4bff0a
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
4bff0a
index a913102e13..3f90a81d35 100644
4bff0a
--- a/src/basic/socket-util.c
4bff0a
+++ b/src/basic/socket-util.c
4bff0a
@@ -50,7 +50,8 @@ static const char* const socket_address_type_table[] = {
4bff0a
 DEFINE_STRING_TABLE_LOOKUP(socket_address_type, int);
4bff0a
 
4bff0a
 int socket_address_parse(SocketAddress *a, const char *s) {
4bff0a
-        char *e, *n;
4bff0a
+        _cleanup_free_ char *n = NULL;
4bff0a
+        char *e;
4bff0a
         int r;
4bff0a
 
4bff0a
         assert(a);
4bff0a
@@ -68,7 +69,9 @@ int socket_address_parse(SocketAddress *a, const char *s) {
4bff0a
                 if (!e)
4bff0a
                         return -EINVAL;
4bff0a
 
4bff0a
-                n = strndupa(s+1, e-s-1);
4bff0a
+                n = strndup(s+1, e-s-1);
4bff0a
+                if (!n)
4bff0a
+                        return -ENOMEM;
4bff0a
 
4bff0a
                 errno = 0;
4bff0a
                 if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0)
4bff0a
@@ -125,7 +128,10 @@ int socket_address_parse(SocketAddress *a, const char *s) {
4bff0a
                 if (r < 0)
4bff0a
                         return r;
4bff0a
 
4bff0a
-                n = strndupa(cid_start, e - cid_start);
4bff0a
+                n = strndup(cid_start, e - cid_start);
4bff0a
+                if (!n)
4bff0a
+                        return -ENOMEM;
4bff0a
+
4bff0a
                 if (!isempty(n)) {
4bff0a
                         r = safe_atou(n, &a->sockaddr.vm.svm_cid);
4bff0a
                         if (r < 0)
4bff0a
@@ -146,7 +152,9 @@ int socket_address_parse(SocketAddress *a, const char *s) {
4bff0a
                         if (r < 0)
4bff0a
                                 return r;
4bff0a
 
4bff0a
-                        n = strndupa(s, e-s);
4bff0a
+                        n = strndup(s, e-s);
4bff0a
+                        if (!n)
4bff0a
+                                return -ENOMEM;
4bff0a
 
4bff0a
                         /* IPv4 in w.x.y.z:p notation? */
4bff0a
                         r = inet_pton(AF_INET, n, &a->sockaddr.in.sin_addr);