From patchwork Mon Jul 15 20:05:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Chikunov X-Patchwork-Id: 11044901 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7A8D7746 for ; Mon, 15 Jul 2019 20:06:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6ACAB28500 for ; Mon, 15 Jul 2019 20:06:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5EF5E28538; Mon, 15 Jul 2019 20:06:22 +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=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 02FFF28500 for ; Mon, 15 Jul 2019 20:06:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731055AbfGOUGV (ORCPT ); Mon, 15 Jul 2019 16:06:21 -0400 Received: from vmicros1.altlinux.org ([194.107.17.57]:58520 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731054AbfGOUGV (ORCPT ); Mon, 15 Jul 2019 16:06:21 -0400 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id 84D0572CA65; Mon, 15 Jul 2019 23:06:19 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id 63E3A4A4A29; Mon, 15 Jul 2019 23:06:19 +0300 (MSK) From: Vitaly Chikunov To: Mimi Zohar , Dmitry Kasatkin , linux-integrity@vger.kernel.org Subject: [PATCH v2 5/5] ima-evm-utils: Add more error checking in add_file_hash Date: Mon, 15 Jul 2019 23:05:53 +0300 Message-Id: <20190715200553.22403-5-vt@altlinux.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190715200553.22403-1-vt@altlinux.org> References: <20190715200553.22403-1-vt@altlinux.org> MIME-Version: 1.0 Sender: linux-integrity-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Check return value of fstat(2) in add_file_hash() and remove now unused get_fdsize(). --- src/libimaevm.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/libimaevm.c b/src/libimaevm.c index 1562aaf..ae487f9 100644 --- a/src/libimaevm.c +++ b/src/libimaevm.c @@ -116,20 +116,13 @@ const char *get_hash_algo_by_id(int algo) return "unknown"; } -static inline off_t get_fdsize(int fd) -{ - struct stat stats; - /* Need to know the file length */ - fstat(fd, &stats); - return stats.st_size; -} - static int add_file_hash(const char *file, EVP_MD_CTX *ctx) { uint8_t *data; int err = -1, bs = DATA_SIZE; off_t size, len; FILE *fp; + struct stat stats; fp = fopen(file, "r"); if (!fp) { @@ -143,7 +136,12 @@ static int add_file_hash(const char *file, EVP_MD_CTX *ctx) goto out; } - for (size = get_fdsize(fileno(fp)); size; size -= len) { + if (fstat(fileno(fp), &stats) == -1) { + log_err("Failed to fstat: %s (%s)\n", file, strerror(errno)); + goto out; + } + + for (size = stats.st_size; size; size -= len) { len = MIN(size, bs); if (!fread(data, len, 1, fp)) { if (ferror(fp)) {