From patchwork Wed May 17 15:24:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 9731457 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 98E56602B4 for ; Wed, 17 May 2017 15:27:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 88CB0287BE for ; Wed, 17 May 2017 15:27:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7C360287C2; Wed, 17 May 2017 15:27:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 76345287BE for ; Wed, 17 May 2017 15:27:10 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E969883F3E; Wed, 17 May 2017 15:27:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E969883F3E Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dm-devel-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com E969883F3E Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 18E2C18367; Wed, 17 May 2017 15:27:01 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id C3272180BAF2; Wed, 17 May 2017 15:26:58 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v4HFPViG016568 for ; Wed, 17 May 2017 11:25:31 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6A8B817C1F; Wed, 17 May 2017 15:25:31 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from mx1.redhat.com (ext-mx10.extmail.prod.ext.phx2.redhat.com [10.5.110.39]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 64F6D17AC0 for ; Wed, 17 May 2017 15:25:29 +0000 (UTC) Received: from prv3-mh.provo.novell.com (victor.provo.novell.com [137.65.250.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3A66E5F7B6 for ; Wed, 17 May 2017 15:25:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3A66E5F7B6 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mwilck@suse.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 3A66E5F7B6 Received: from apollon.suse.de.de (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (TLS encrypted); Wed, 17 May 2017 09:25:24 -0600 From: Martin Wilck To: Christophe Varoqui , Hannes Reinecke , Bart Van Assche Date: Wed, 17 May 2017 17:24:45 +0200 Message-Id: <20170517152446.2526-2-mwilck@suse.com> In-Reply-To: <20170517152446.2526-1-mwilck@suse.com> References: <20170517152446.2526-1-mwilck@suse.com> X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 203 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 17 May 2017 15:25:28 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 17 May 2017 15:25:28 +0000 (UTC) for IP:'137.65.250.26' DOMAIN:'victor.provo.novell.com' HELO:'prv3-mh.provo.novell.com' FROM:'mwilck@suse.com' RCPT:'' X-RedHat-Spam-Score: -1.501 (BAYES_50, RCVD_IN_DNSWL_MED, SPF_PASS) 137.65.250.26 victor.provo.novell.com 137.65.250.26 victor.provo.novell.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.39 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: dm-devel@redhat.com Cc: dm-devel@redhat.com Subject: [dm-devel] [PATCH v2 1/2] libmultipath: print.c: make sure lines are 0-terminated X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 17 May 2017 15:27:09 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP Under certain conditions, the output of "multipath -ll" or "multipathd show topology", or the multipathd logs, may contain garbage characters. Fix that by making sure that the strings are always properly zero-terminated. While touching this code, substitute a function for the complex ENDLINE macro. Note 1: The way this is coded, the previously written character is overwritten by the newline. That behavior is unchanged by this patch. I didn't want to fuzz with the carefully crafted print.c code more than necessary at this point. Note 2: The condition (c <= line + len - 1) is equivalent to (TAIL >= 0). It should always hold the way ENDLINE is called after PRINT and PAD in print.c, and is only added as an additional safeguard, e.g. against future code changes. Signed-off-by: Martin Wilck --- libmultipath/print.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/libmultipath/print.c b/libmultipath/print.c index 7c2a1588..95dff90b 100644 --- a/libmultipath/print.c +++ b/libmultipath/print.c @@ -39,9 +39,18 @@ do { \ s = c; \ } while (0) -#define ENDLINE \ - if (c > line) \ - line[c - line - 1] = '\n' +static char * +__endline(char *line, size_t len, char *c) +{ + if (c > line) { + if (c >= line + len) + c = line + len - 1; + *(c - 1) = '\n'; + *c = '\0'; + } + return c; +} + #define PRINT(var, size, format, args...) \ do { \ fwd = snprintf(var, size, format, ##args); \ @@ -802,7 +811,7 @@ snprint_multipath_header (char * line, int len, char * format) PAD(data->width); } while (*f++); - ENDLINE; + __endline(line, len, c); return (c - line); } @@ -838,7 +847,7 @@ snprint_multipath (char * line, int len, char * format, buff[0] = '\0'; } while (*f++); - ENDLINE; + __endline(line, len, c); return (c - line); } @@ -869,7 +878,7 @@ snprint_path_header (char * line, int len, char * format) PAD(data->width); } while (*f++); - ENDLINE; + __endline(line, len, c); return (c - line); } @@ -904,7 +913,7 @@ snprint_path (char * line, int len, char * format, PAD(data->width); } while (*f++); - ENDLINE; + __endline(line, len, c); return (c - line); } @@ -938,7 +947,7 @@ snprint_pathgroup (char * line, int len, char * format, PAD(data->width); } while (*f++); - ENDLINE; + __endline(line, len, c); return (c - line); }