From patchwork Wed Aug 7 12:42:13 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Ian King X-Patchwork-Id: 265505 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) by ozlabs.org (Postfix) with ESMTP id 068062C022D for ; Wed, 7 Aug 2013 22:42:21 +1000 (EST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1V734E-0008Ae-Li; Wed, 07 Aug 2013 12:42:18 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtp (Exim 4.76) (envelope-from ) id 1V7349-0008AZ-SO for fwts-devel@lists.ubuntu.com; Wed, 07 Aug 2013 12:42:13 +0000 Received: from cpc3-craw6-2-0-cust180.croy.cable.virginmedia.com ([77.100.248.181] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1V7349-0007xO-Nj for fwts-devel@lists.ubuntu.com; Wed, 07 Aug 2013 12:42:13 +0000 From: Colin King To: fwts-devel@lists.ubuntu.com Subject: [PATCH] lib: fwts_cpu.c: fix time-of-use race on stat/open (LP: #1209225) Date: Wed, 7 Aug 2013 13:42:13 +0100 Message-Id: <1375879333-17487-1-git-send-email-colin.king@canonical.com> X-Mailer: git-send-email 1.8.3.2 X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.14 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: fwts-devel-bounces@lists.ubuntu.com Sender: fwts-devel-bounces@lists.ubuntu.com From: Colin Ian King Coverity Scan reports that the stat/open may give a small window of opportunity for a stat/open race condition. So just forget the stat as open will do. Signed-off-by: Colin Ian King Acked-by: Keng-Yu Lin Acked-by: Alex Hung --- src/lib/src/fwts_cpu.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/lib/src/fwts_cpu.c b/src/lib/src/fwts_cpu.c index 44711fd..fc52fa5 100644 --- a/src/lib/src/fwts_cpu.c +++ b/src/lib/src/fwts_cpu.c @@ -51,30 +51,23 @@ static pid_t *fwts_cpu_pids; */ int fwts_cpu_readmsr(const int cpu, const uint32_t reg, uint64_t *val) { - struct stat statbuf; char buffer[PATH_MAX]; - uint64_t value; + uint64_t value = 0; int fd; int ret; - value = 0; - snprintf(buffer, sizeof(buffer), "/dev/cpu/%d/msr", cpu); - - if (stat(buffer, &statbuf)) { + if ((fd = open(buffer, O_RDONLY)) < 0) { /* Hrm, msr not there, so force modprobe msr and see what happens */ pid_t pid; if ((fd = fwts_pipe_open("modprobe msr", &pid)) < 0) return FWTS_ERROR; fwts_pipe_close(fd, pid); - if (stat(buffer, &statbuf)) + if ((fd = open(buffer, O_RDONLY)) < 0) return FWTS_ERROR; /* Really failed */ } - if ((fd = open(buffer, O_RDONLY)) < 0) - return FWTS_ERROR; - ret = pread(fd, &value, 8, reg); close(fd);