From patchwork Thu Jan 18 05:05:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Mendoza-Jonas X-Patchwork-Id: 862755 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zMXDl1mznz9s7f for ; Thu, 18 Jan 2018 16:13:59 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="ZposMMz3"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="ik4HogvU"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3zMXDl0JCPzF0hN for ; Thu, 18 Jan 2018 16:13:58 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="ZposMMz3"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="ik4HogvU"; dkim-atps=neutral X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=mendozajonas.com (client-ip=66.111.4.25; helo=out1-smtp.messagingengine.com; envelope-from=sam@mendozajonas.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="ZposMMz3"; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="ik4HogvU"; dkim-atps=neutral Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3zMXCf6R5xzF0hR for ; Thu, 18 Jan 2018 16:13:02 +1100 (AEDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 9AD0820C6E; Thu, 18 Jan 2018 00:05:29 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute2.internal (MEProxy); Thu, 18 Jan 2018 00:05:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= mendozajonas.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=IHWNhRw1ebJz3kq8Zn95bJS77LZHNxUxHcc7sPHLREc=; b=ZposMMz3 6+FkbBPn9E+VR4D6eTuqk0VlP0VyyT/MQvnOIBuB6XFgWl3QWbv2GXwzLY7oJJ/G 0+N/aRvzBjXCRhkveA2EcqlJsMcQU0KHfoH1igywLnygbtg0WU8l9DEO1MK96etN +lEC/6KrYSx/+Azbcl0pntLrk2UI+21SoiG55vVraYbfL/WIK+wbt+X7pAwyarmK wuRGlL7Rh+mFrpRZmOZi2eCRATj3GbL4XPKUuqaqMzui47/spaQwDZOura46mQIW oJkVShBfwYSwwNsZ3Dsk7sy0aT0sArlmGUxzORzpdvv+nhlsXM3bwbttVv6n+ETI WtjMXZ5oOJIsrA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=IHWNhRw1ebJz3kq8Zn95bJS77LZHNxUxHcc7sPHLREc=; b=ik4HogvU YdYm0eeXh4rXTxz86vqcd9TNL//TeN2lAha/Z9AhuSD4XoSVdWbTU60c0bnthPZS +42CHEhbG/KMOQsmDY4JHm2yFBp2hhqrdTaRu3zsftaU+PTRMul1z+6Qbd8zJvDu xFZ3N614HkFXSEH9JHIUGccUZMnxkSU583c9jQvepXca/FlDoJClildEtfHlj32W poEibZG1wI9ehaGgD6kvORLOjWlEb9GXklOXcXbAa4eLWQBZyQy1MwQStvuj7XKT t45QzniDLDVEL5a1qu8hGBwMXYLYgjXPjRthk2wGJ85V24BMkx+AqQzwXO5/m6oh 7IrHp5pE1TPCHQ== X-ME-Sender: Received: from v4.ozlabs.ibm.com (unknown [122.99.82.10]) by mail.messagingengine.com (Postfix) with ESMTPA id 802AE240F8; Thu, 18 Jan 2018 00:05:28 -0500 (EST) From: Samuel Mendoza-Jonas To: petitboot@lists.ozlabs.org Subject: [[RFC PATCH] v2 01/14] lib/process: Safely read partial stdout buffer Date: Thu, 18 Jan 2018 16:05:04 +1100 Message-Id: <20180118050517.2442-2-sam@mendozajonas.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180118050517.2442-1-sam@mendozajonas.com> References: <20180118050517.2442-1-sam@mendozajonas.com> X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.24 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Samuel Mendoza-Jonas MIME-Version: 1.0 Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" 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 --- 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);