From patchwork Thu Jun 16 09:47:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Reinecke X-Patchwork-Id: 9180393 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 7FBC960573 for ; Thu, 16 Jun 2016 09:52:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7235020410 for ; Thu, 16 Jun 2016 09:52:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 66E1027F07; Thu, 16 Jun 2016 09:52:26 +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 mx3-phx2.redhat.com (mx3-phx2.redhat.com [209.132.183.24]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BEDAB20410 for ; Thu, 16 Jun 2016 09:52:25 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx3-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id u5G9mPkw023233; Thu, 16 Jun 2016 05:48:25 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id u5G9lxKr005797 for ; Thu, 16 Jun 2016 05:47:59 -0400 Received: from mx1.redhat.com (ext-mx03.extmail.prod.ext.phx2.redhat.com [10.5.110.27]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5G9lxjk017385 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 16 Jun 2016 05:47:59 -0400 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8019280F67; Thu, 16 Jun 2016 09:47:58 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id ECEC5ADD2; Thu, 16 Jun 2016 09:47:52 +0000 (UTC) From: Hannes Reinecke To: Christophe Varoqui Date: Thu, 16 Jun 2016 11:47:21 +0200 Message-Id: <1466070465-1021-16-git-send-email-hare@suse.de> In-Reply-To: <1466070465-1021-1-git-send-email-hare@suse.de> References: <1466070465-1021-1-git-send-email-hare@suse.de> X-Greylist: Sender IP whitelisted by DNSRBL, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 16 Jun 2016 09:47:58 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 16 Jun 2016 09:47:58 +0000 (UTC) for IP:'195.135.220.15' DOMAIN:'mx2.suse.de' HELO:'mx2.suse.de' FROM:'hare@suse.de' RCPT:'' X-RedHat-Spam-Score: -3.327 (BAYES_50, DCC_REPUT_00_12, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, SPF_PASS) 195.135.220.15 mx2.suse.de 195.135.220.15 mx2.suse.de X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Scanned-By: MIMEDefang 2.76 on 10.5.110.27 X-loop: dm-devel@redhat.com Cc: Hannes Reinecke , dm-devel@redhat.com Subject: [dm-devel] [PATCH 15/39] libmultipath: use fstat() when reading sysfs attributes 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-Virus-Scanned: ClamAV using ClamSMTP Coverity pointed out that we shouldn't be using stat()/open(), as the file might have vanished or changed in between those two calls. So modify it to open()/fstat() instead. Signed-off-by: Hannes Reinecke --- libmultipath/sysfs.c | 59 +++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/libmultipath/sysfs.c b/libmultipath/sysfs.c index de7df40..a74de9f 100644 --- a/libmultipath/sysfs.c +++ b/libmultipath/sysfs.c @@ -57,30 +57,31 @@ ssize_t sysfs_attr_get_value(struct udev_device *dev, const char *attr_name, snprintf(devpath, PATH_SIZE, "%s/%s", udev_device_get_syspath(dev), attr_name); condlog(4, "open '%s'", devpath); - if (stat(devpath, &statbuf) != 0) { + /* read attribute value */ + fd = open(devpath, O_RDONLY); + if (fd < 0) { + condlog(4, "attribute '%s' can not be opened: %s", + devpath, strerror(errno)); + return -errno; + } + if (fstat(fd, &statbuf) < 0) { condlog(4, "stat '%s' failed: %s", devpath, strerror(errno)); + close(fd); return -ENXIO; } - /* skip directories */ if (S_ISDIR(statbuf.st_mode)) { condlog(4, "%s is a directory", devpath); + close(fd); return -EISDIR; } - /* skip non-writeable files */ if ((statbuf.st_mode & S_IRUSR) == 0) { condlog(4, "%s is not readable", devpath); + close(fd); return -EPERM; } - /* read attribute value */ - fd = open(devpath, O_RDONLY); - if (fd < 0) { - condlog(4, "attribute '%s' can not be opened: %s", - devpath, strerror(errno)); - return -errno; - } size = read(fd, value, value_len); if (size < 0) { condlog(4, "read from %s failed: %s", devpath, strerror(errno)); @@ -112,30 +113,33 @@ ssize_t sysfs_bin_attr_get_value(struct udev_device *dev, const char *attr_name, snprintf(devpath, PATH_SIZE, "%s/%s", udev_device_get_syspath(dev), attr_name); condlog(4, "open '%s'", devpath); - if (stat(devpath, &statbuf) != 0) { + /* read attribute value */ + fd = open(devpath, O_RDONLY); + if (fd < 0) { + condlog(4, "attribute '%s' can not be opened: %s", + devpath, strerror(errno)); + return -errno; + } + if (fstat(fd, &statbuf) != 0) { condlog(4, "stat '%s' failed: %s", devpath, strerror(errno)); + close(fd); return -ENXIO; } /* skip directories */ if (S_ISDIR(statbuf.st_mode)) { condlog(4, "%s is a directory", devpath); + close(fd); return -EISDIR; } /* skip non-writeable files */ if ((statbuf.st_mode & S_IRUSR) == 0) { condlog(4, "%s is not readable", devpath); + close(fd); return -EPERM; } - /* read attribute value */ - fd = open(devpath, O_RDONLY); - if (fd < 0) { - condlog(4, "attribute '%s' can not be opened: %s", - devpath, strerror(errno)); - return -errno; - } size = read(fd, value, value_len); if (size < 0) { condlog(4, "read from %s failed: %s", devpath, strerror(errno)); @@ -163,30 +167,33 @@ ssize_t sysfs_attr_set_value(struct udev_device *dev, const char *attr_name, snprintf(devpath, PATH_SIZE, "%s/%s", udev_device_get_syspath(dev), attr_name); condlog(4, "open '%s'", devpath); - if (stat(devpath, &statbuf) != 0) { + /* write attribute value */ + fd = open(devpath, O_WRONLY); + if (fd < 0) { + condlog(4, "attribute '%s' can not be opened: %s", + devpath, strerror(errno)); + return -errno; + } + if (fstat(fd, &statbuf) != 0) { condlog(4, "stat '%s' failed: %s", devpath, strerror(errno)); + close(fd); return -errno; } /* skip directories */ if (S_ISDIR(statbuf.st_mode)) { condlog(4, "%s is a directory", devpath); + close(fd); return -EISDIR; } /* skip non-writeable files */ if ((statbuf.st_mode & S_IWUSR) == 0) { condlog(4, "%s is not writeable", devpath); + close(fd); return -EPERM; } - /* write attribute value */ - fd = open(devpath, O_WRONLY); - if (fd < 0) { - condlog(4, "attribute '%s' can not be opened: %s", - devpath, strerror(errno)); - return -errno; - } size = write(fd, value, value_len); if (size < 0) { condlog(4, "write to %s failed: %s", devpath, strerror(errno));