daandemeyer / rpms / systemd

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