ff6046
From 9b9b6d8c7b10c069d36f85bd17f144011282cb58 Mon Sep 17 00:00:00 2001
ff6046
From: Michal Sekletar <msekleta@redhat.com>
ff6046
Date: Tue, 22 Jan 2019 14:29:50 +0100
ff6046
Subject: [PATCH] process-util: don't use overly large buffer to store process
ff6046
 command line
ff6046
ff6046
Allocate new string as a return value and free our "scratch pad"
ff6046
buffer that is potentially much larger than needed (up to
ff6046
_SC_ARG_MAX).
ff6046
ff6046
Fixes #11502
ff6046
ff6046
(cherry-picked from commit eb1ec489eef8a32918bbfc56a268c9d10464584d)
ff6046
ff6046
Related: #1664976
ff6046
---
ff6046
 src/basic/process-util.c | 18 ++++++++++++++----
ff6046
 1 file changed, 14 insertions(+), 4 deletions(-)
ff6046
ff6046
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
ff6046
index a20f1e3ccf..aa3eff779a 100644
ff6046
--- a/src/basic/process-util.c
ff6046
+++ b/src/basic/process-util.c
ff6046
@@ -101,7 +101,8 @@ int get_process_comm(pid_t pid, char **ret) {
ff6046
 int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
ff6046
         _cleanup_fclose_ FILE *f = NULL;
ff6046
         bool space = false;
ff6046
-        char *k, *ans = NULL;
ff6046
+        char *k;
ff6046
+        _cleanup_free_ char *ans = NULL;
ff6046
         const char *p;
ff6046
         int c;
ff6046
 
ff6046
@@ -142,7 +143,7 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
ff6046
                 if (!ans)
ff6046
                         return -ENOMEM;
ff6046
 
ff6046
-                *line = ans;
ff6046
+                *line = TAKE_PTR(ans);
ff6046
                 return 0;
ff6046
 
ff6046
         } else {
ff6046
@@ -207,7 +208,7 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
ff6046
                 _cleanup_free_ char *t = NULL;
ff6046
                 int h;
ff6046
 
ff6046
-                free(ans);
ff6046
+                ans = mfree(ans);
ff6046
 
ff6046
                 if (!comm_fallback)
ff6046
                         return -ENOENT;
ff6046
@@ -240,9 +241,18 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
ff6046
                         if (!ans)
ff6046
                                 return -ENOMEM;
ff6046
                 }
ff6046
+
ff6046
+                *line = TAKE_PTR(ans);
ff6046
+                return 0;
ff6046
         }
ff6046
 
ff6046
-        *line = ans;
ff6046
+        k = realloc(ans, strlen(ans) + 1);
ff6046
+        if (!k)
ff6046
+                return -ENOMEM;
ff6046
+
ff6046
+        ans = NULL;
ff6046
+        *line = k;
ff6046
+
ff6046
         return 0;
ff6046
 }
ff6046