Blame SOURCES/0077-process-util-don-t-use-overly-large-buffer-to-store-.patch

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