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