]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
process-util: don't use overly large buffer to store process command line
authorMichal Sekletar <msekleta@redhat.com>
Tue, 22 Jan 2019 13:29:50 +0000 (14:29 +0100)
committerLukáš Nykrýn <lnykryn@redhat.com>
Thu, 7 Feb 2019 11:57:43 +0000 (12:57 +0100)
Allocate new string as a return value and free our "scratch pad"
buffer that is potentially much larger than needed (up to
_SC_ARG_MAX).

Fixes #11502

(cherry-picked from commit eb1ec489eef8a32918bbfc56a268c9d10464584d)

Related: #1664976

src/basic/process-util.c

index a20f1e3ccfa6d68fe184eea763af28860a53cb5d..aa3eff779a581c661d957aa466adf3b4356a063a 100644 (file)
@@ -101,7 +101,8 @@ int get_process_comm(pid_t pid, char **ret) {
 int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
         _cleanup_fclose_ FILE *f = NULL;
         bool space = false;
-        char *k, *ans = NULL;
+        char *k;
+        _cleanup_free_ char *ans = NULL;
         const char *p;
         int c;
 
@@ -142,7 +143,7 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
                 if (!ans)
                         return -ENOMEM;
 
-                *line = ans;
+                *line = TAKE_PTR(ans);
                 return 0;
 
         } else {
@@ -207,7 +208,7 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
                 _cleanup_free_ char *t = NULL;
                 int h;
 
-                free(ans);
+                ans = mfree(ans);
 
                 if (!comm_fallback)
                         return -ENOENT;
@@ -240,9 +241,18 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
                         if (!ans)
                                 return -ENOMEM;
                 }
+
+                *line = TAKE_PTR(ans);
+                return 0;
         }
 
-        *line = ans;
+        k = realloc(ans, strlen(ans) + 1);
+        if (!k)
+                return -ENOMEM;
+
+        ans = NULL;
+        *line = k;
+
         return 0;
 }