From patchwork Tue May 9 16:57:04 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Marzinski X-Patchwork-Id: 9718699 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 3478F60237 for ; Tue, 9 May 2017 16:57:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 314D7200E9 for ; Tue, 9 May 2017 16:57:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 24B9D2029C; Tue, 9 May 2017 16:57:38 +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 BA05A200E9 for ; Tue, 9 May 2017 16:57:37 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 615B33D8; Tue, 9 May 2017 16:57:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 615B33D8 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.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 615B33D8 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 3CD53783C4; Tue, 9 May 2017 16:57:36 +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 EFEE01800C8E; Tue, 9 May 2017 16:57:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v49GvOoU016135 for ; Tue, 9 May 2017 12:57:24 -0400 Received: by smtp.corp.redhat.com (Postfix) id EC162171D9; Tue, 9 May 2017 16:57:24 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from redhat.com (octiron.msp.redhat.com [10.15.80.209]) by smtp.corp.redhat.com (Postfix) with SMTP id B83AF17143; Tue, 9 May 2017 16:57:23 +0000 (UTC) Received: by redhat.com (sSMTP sendmail emulation); Tue, 09 May 2017 11:57:22 -0500 From: "Benjamin Marzinski" To: device-mapper development Date: Tue, 9 May 2017 11:57:04 -0500 Message-Id: <1494349025-6561-6-git-send-email-bmarzins@redhat.com> In-Reply-To: <1494349025-6561-1-git-send-email-bmarzins@redhat.com> References: <1494349025-6561-1-git-send-email-bmarzins@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: dm-devel@redhat.com Subject: [dm-devel] [PATCH 5/6] kpartx: fix device checks 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.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 09 May 2017 16:57:37 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP There are a number of issues in the the kpartx device checking code. First, it accepts files that are neither regular files or a block device nodes (you can run kpartx on character devices or directories, and it will treat them as block devices). When trying to figure out the basename of a device, the code returns garbage if the path doesn't include a '/'. Finally, the set_delimiter code can access memory outside of the string if an empty string is passed in. Signed-off-by: Benjamin Marzinski Reviewed-by: Martin Wilck --- kpartx/kpartx.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/kpartx/kpartx.c b/kpartx/kpartx.c index cc7e2e7..a1af156 100644 --- a/kpartx/kpartx.c +++ b/kpartx/kpartx.c @@ -135,10 +135,13 @@ set_delimiter (char * device, char * delimiter) { char * p = device; - while (*(p++) != 0x0) + if (*p == 0x0) + return; + + while (*(++p) != 0x0) continue; - if (isdigit(*(p - 2))) + if (isdigit(*(p - 1))) *delimiter = 'p'; } @@ -157,15 +160,17 @@ strip_slash (char * device) static int find_devname_offset (char * device) { - char *p, *q = NULL; + char *p, *q; - p = device; + q = p = device; - while (*p++) + while (*p) { if (*p == '/') - q = p; + q = p + 1; + p++; + } - return (int)(q - device) + 1; + return (int)(q - device); } static char * @@ -381,6 +386,10 @@ main(int argc, char **argv){ exit (1); } } + else if (!S_ISBLK(buf.st_mode)) { + fprintf(stderr, "invalid device: %s\n", device); + exit(1); + } off = find_devname_offset(device);