From patchwork Fri Jul 12 21:28:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Chikunov X-Patchwork-Id: 11042701 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 C1D58138B for ; Fri, 12 Jul 2019 21:28:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B2FB226AE3 for ; Fri, 12 Jul 2019 21:28:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A78D727FA8; Fri, 12 Jul 2019 21:28:50 +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 552C026AE3 for ; Fri, 12 Jul 2019 21:28:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728040AbfGLV2u (ORCPT ); Fri, 12 Jul 2019 17:28:50 -0400 Received: from vmicros1.altlinux.org ([194.107.17.57]:32884 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727994AbfGLV2u (ORCPT ); Fri, 12 Jul 2019 17:28:50 -0400 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id F216472CCDC; Sat, 13 Jul 2019 00:28:47 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id CB8174A4AE8; Sat, 13 Jul 2019 00:28:47 +0300 (MSK) From: Vitaly Chikunov To: Mimi Zohar , Dmitry Kasatkin , linux-integrity@vger.kernel.org Subject: [PATCH v1 5/5] ima-evm-utils: Add more error checking in add_file_hash Date: Sat, 13 Jul 2019 00:28:33 +0300 Message-Id: <20190712212833.29280-5-vt@altlinux.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190712212833.29280-1-vt@altlinux.org> References: <20190712212833.29280-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)) {