From patchwork Fri Aug 25 05:59:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Mendoza-Jonas X-Patchwork-Id: 805753 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 3xdr9Q4pd2z9sR9 for ; Fri, 25 Aug 2017 16:00:10 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="ZD8mCDe3"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="R/IXvRKW"; 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 3xdr9Q3gr6zDrSV for ; Fri, 25 Aug 2017 16:00:10 +1000 (AEST) 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="ZD8mCDe3"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="R/IXvRKW"; dkim-atps=neutral X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3xdr966vTrzDqXc for ; Fri, 25 Aug 2017 15:59:54 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="ZD8mCDe3"; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="R/IXvRKW"; dkim-atps=neutral Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 9804E2134A; Fri, 25 Aug 2017 01:59:52 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute2.internal (MEProxy); Fri, 25 Aug 2017 01:59:52 -0400 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 :x-sasl-enc; s=fm1; bh=dpxXNsb1NNxqy1hSVWwySYC9jbfrWM9OhNJNMhPsB Fg=; b=ZD8mCDe3Mkvu9ilxTUq1nsIkUuRuZMZjvJmct+9oTaMqMAzrTVd7QMPNc 31IHj+nQWS0zOn1HPw1XjtkVd+SquLGUPIQrt32QDI+oiTT9onniheJt4T6OL2tD kMmuqDFLqqco6ZG8zta6G68mHXb2j+jsMjHQ5Rw5WWgj8HJXIcYlTVFy8EYLpvg9 Ke6EbbV5RROZwj8o8d+xGrD+zQmNxMpDqh6+V4UuUMu4Akeuv99+mFFWrwl7HsW1 twA+YIhTCopJzS1NFUUImIX5wujdlnG5riHNIjOkDSdSR2d0nsR/dvfEhAO6p+hc UDk4Amy++5eoYhGJrzA60ClCF127w== 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 :x-sasl-enc; s=fm1; bh=dpxXNsb1NNxqy1hSVWwySYC9jbfrWM9OhNJNMhPsB Fg=; b=R/IXvRKW2MBmJStcsVKPdIJvUuAc7Xvc1NlPfMhutGWjr76J1OLgqNAPZ mOnGwZmbSErNXV+xElxA8S9vFddsfulCgJr9wHe75mI+4gx5YZx5FDINWA1G7sTj 4U6PEJKpNLer6p0/KGSFtgewHmJrVgzL/2PrVy9DbpxQHzdh/caND7pCPY2fs0qS 5MPQoD0xOBG+4K4B5h1+jBEDnUfnzlqXyAfA1GFKv2AP5uEDCGTqPxVMBBwapyXk gxvlHuM0ff1fKIDVFpRsMpmi2mZJGarh8lPtq6EDbTPILbKywLqPBOoG2kd+9sdS yOg+OFZh8a9xcWvqjCdTBlI5R98RA== X-ME-Sender: X-Sasl-enc: df740lrLV/XLn8k7wifqngMUENnGUK9042FKJZDAsSry 1503640791 Received: from v4.ozlabs.ibm.com (unknown [122.99.82.10]) by mail.messagingengine.com (Postfix) with ESMTPA id 6F70F2444E; Fri, 25 Aug 2017 01:59:51 -0400 (EDT) From: Samuel Mendoza-Jonas To: petitboot@lists.ozlabs.org Subject: [PATCH 01/10] lib/process: Safely read partial stdout buffer Date: Fri, 25 Aug 2017 15:59:31 +1000 Message-Id: <20170825055940.24134-2-sam@mendozajonas.com> X-Mailer: git-send-email 2.14.0 In-Reply-To: <20170825055940.24134-1-sam@mendozajonas.com> References: <20170825055940.24134-1-sam@mendozajonas.com> X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.23 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);