[[RFC PATCH] v2 01/14] lib/process: Safely read partial stdout buffer

Samuel Mendoza-Jonas sam at mendozajonas.com
Thu Jan 18 16:05:04 AEDT 2018


If a line pointer is provided to process_read_stdout_once() it is set to
the latest position in stdout_buf, however this is not guaranteed to be
properly terminated.
Instead allocate a copy of the new stdout_buf portion which is safely
terminated by talloc_strndup(). The caller must free this string.

Signed-off-by: Samuel Mendoza-Jonas <sam at mendozajonas.com>
---
 discover/paths.c      | 3 +--
 lib/process/process.c | 8 +++++++-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/discover/paths.c b/discover/paths.c
index 3a69488..2d67a32 100644
--- a/discover/paths.c
+++ b/discover/paths.c
@@ -191,11 +191,10 @@ static int busybox_progress_cb(void *arg)
 	device_handler_status_download(handler, procinfo,
 			percentage, size, suffix);
 
+	talloc_free(line);
 	return 0;
 }
 
-
-
 static void load_process_to_local_file(struct load_task *task,
 		const char **argv, int argv_local_idx)
 {
diff --git a/lib/process/process.c b/lib/process/process.c
index c1606a5..760dbaf 100644
--- a/lib/process/process.c
+++ b/lib/process/process.c
@@ -97,7 +97,9 @@ static int process_read_stdout_once(struct process_info *procinfo, char **line)
 	}
 
 	if (line)
-		*line = process->stdout_buf + process->stdout_len;
+		*line = talloc_strndup(procinfo,
+				process->stdout_buf + process->stdout_len,
+				rc + 1);
 
 	process->stdout_len += rc;
 	if (process->stdout_len == procinfo->stdout_buf_len - 1) {
@@ -268,6 +270,10 @@ static int sigchld_pipe_event(void *arg)
 	/* ensure we have all of the child's stdout */
 	process_read_stdout(procinfo);
 
+	/* clean up custom stdout handler if it still exists */
+	if (procinfo->stdout_waiter)
+		waiter_remove(procinfo->stdout_waiter);
+
 	if (process->exit_cb)
 		process->exit_cb(process);
 
-- 
2.15.1



More information about the Petitboot mailing list