From patchwork Fri Jul 12 21:28:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Chikunov X-Patchwork-Id: 11042693 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 990EC138D for ; Fri, 12 Jul 2019 21:28:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8031F26AE3 for ; Fri, 12 Jul 2019 21:28:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7452D27FA8; Fri, 12 Jul 2019 21:28:43 +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 B9E6A26AE3 for ; Fri, 12 Jul 2019 21:28:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728029AbfGLV2m (ORCPT ); Fri, 12 Jul 2019 17:28:42 -0400 Received: from vmicros1.altlinux.org ([194.107.17.57]:60988 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727994AbfGLV2m (ORCPT ); Fri, 12 Jul 2019 17:28:42 -0400 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id 2632972CC64; Sat, 13 Jul 2019 00:28:40 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id CCD754A4AE8; Sat, 13 Jul 2019 00:28:39 +0300 (MSK) From: Vitaly Chikunov To: Mimi Zohar , Dmitry Kasatkin , linux-integrity@vger.kernel.org Subject: [PATCH v1 1/5] ima-evm-utils: Fix null dereference from file2bin to memcpy Date: Sat, 13 Jul 2019 00:28:29 +0300 Message-Id: <20190712212833.29280-1-vt@altlinux.org> X-Mailer: git-send-email 2.11.0 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 file2bin() may return NULL, which is set to tmp, which is passed to memcpy. Add explicit check for it. CID 229904. --- src/evmctl.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/evmctl.c b/src/evmctl.c index a6d07c9..39bc3d9 100644 --- a/src/evmctl.c +++ b/src/evmctl.c @@ -821,7 +821,15 @@ static int verify_ima(const char *file) if (sigfile) { void *tmp = file2bin(file, "sig", &len); - assert(len <= sizeof(sig)); + if (!tmp) { + log_err("Failed reading: %s\n", file); + return -1; + } + if (len > sizeof(sig)) { + log_err("File is too big: %s\n", file); + free(tmp); + return -1; + } memcpy(sig, tmp, len); free(tmp); } else { From patchwork Fri Jul 12 21:28:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Chikunov X-Patchwork-Id: 11042695 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 682B1138D for ; Fri, 12 Jul 2019 21:28:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 59DD126AE3 for ; Fri, 12 Jul 2019 21:28:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4E35627FA8; Fri, 12 Jul 2019 21:28:45 +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 0BB2526AE3 for ; Fri, 12 Jul 2019 21:28:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728032AbfGLV2o (ORCPT ); Fri, 12 Jul 2019 17:28:44 -0400 Received: from vmicros1.altlinux.org ([194.107.17.57]:32796 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727994AbfGLV2o (ORCPT ); Fri, 12 Jul 2019 17:28:44 -0400 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id C863372CCD6; Sat, 13 Jul 2019 00:28:42 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id ABA2D4A4AE8; Sat, 13 Jul 2019 00:28:42 +0300 (MSK) From: Vitaly Chikunov To: Mimi Zohar , Dmitry Kasatkin , linux-integrity@vger.kernel.org Subject: [PATCH v1 2/5] ima-evm-utils: Fix possible strcpy overflow Date: Sat, 13 Jul 2019 00:28:30 +0300 Message-Id: <20190712212833.29280-2-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 caps_str is passed from command line but copied into fixed-size buffer. CID 229895. --- src/evmctl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/evmctl.c b/src/evmctl.c index 39bc3d9..e07cff4 100644 --- a/src/evmctl.c +++ b/src/evmctl.c @@ -409,8 +409,9 @@ static int calc_evm_hash(const char *file, unsigned char *hash) } else if (!strcmp(*xattrname, XATTR_NAME_CAPS) && (hmac_flags & HMAC_FLAG_CAPS_SET)) { if (!caps_str) continue; - strcpy(xattr_value, caps_str); err = strlen(caps_str); + assert(err < sizeof(xattr_value)); + strcpy(xattr_value, caps_str); } else { err = lgetxattr(file, *xattrname, xattr_value, sizeof(xattr_value)); if (err < 0) { From patchwork Fri Jul 12 21:28:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Chikunov X-Patchwork-Id: 11042697 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 0044D138B for ; Fri, 12 Jul 2019 21:28:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E541026AE3 for ; Fri, 12 Jul 2019 21:28:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D9CF727FA8; Fri, 12 Jul 2019 21:28:47 +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 9343426AE3 for ; Fri, 12 Jul 2019 21:28:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728043AbfGLV2r (ORCPT ); Fri, 12 Jul 2019 17:28:47 -0400 Received: from vmicros1.altlinux.org ([194.107.17.57]:32826 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727994AbfGLV2r (ORCPT ); Fri, 12 Jul 2019 17:28:47 -0400 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id 4D03672CCD9; Sat, 13 Jul 2019 00:28:45 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id B7A334A4AE8; Sat, 13 Jul 2019 00:28:44 +0300 (MSK) From: Vitaly Chikunov To: Mimi Zohar , Dmitry Kasatkin , linux-integrity@vger.kernel.org Subject: [PATCH v1 3/5] ima-evm-utils: Fix memory leak in get_password Date: Sat, 13 Jul 2019 00:28:31 +0300 Message-Id: <20190712212833.29280-3-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 Free password buffer when return NULL. Partially fix CID 229894. --- src/evmctl.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/evmctl.c b/src/evmctl.c index e07cff4..c556d05 100644 --- a/src/evmctl.c +++ b/src/evmctl.c @@ -1827,6 +1827,7 @@ static char *get_password(void) if (tcsetattr(fileno(stdin), TCSANOW, &tmp_flags) != 0) { perror("tcsetattr"); + free(password); return NULL; } @@ -1836,6 +1837,7 @@ static char *get_password(void) /* restore terminal */ if (tcsetattr(fileno(stdin), TCSANOW, &flags) != 0) { perror("tcsetattr"); + free(password); return NULL; } From patchwork Fri Jul 12 21:28:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Chikunov X-Patchwork-Id: 11042699 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 05E11138D 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 DF2D426AE3 for ; Fri, 12 Jul 2019 21:28:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D37E127FA8; Fri, 12 Jul 2019 21:28:49 +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 7A5F726AE3 for ; Fri, 12 Jul 2019 21:28:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728059AbfGLV2t (ORCPT ); Fri, 12 Jul 2019 17:28:49 -0400 Received: from vmicros1.altlinux.org ([194.107.17.57]:32858 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728040AbfGLV2t (ORCPT ); Fri, 12 Jul 2019 17:28:49 -0400 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id 3DF2D72CCDB; Sat, 13 Jul 2019 00:28:46 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id 1BFDA4A4AE8; Sat, 13 Jul 2019 00:28:46 +0300 (MSK) From: Vitaly Chikunov To: Mimi Zohar , Dmitry Kasatkin , linux-integrity@vger.kernel.org Subject: [PATCH v1 4/5] ima-evm-utils: Fix file2bin stat and fopen relations Date: Sat, 13 Jul 2019 00:28:32 +0300 Message-Id: <20190712212833.29280-4-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 stat(2) return value, use fstat(2) to avoid race between stat() and fopen(), remove now unused get_filesize(). Fixes CID 229889. --- src/evmctl.c | 25 ++++++++++++++++++++----- src/imaevm.h | 1 - src/libimaevm.c | 8 -------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/evmctl.c b/src/evmctl.c index c556d05..f60c12e 100644 --- a/src/evmctl.c +++ b/src/evmctl.c @@ -175,9 +175,10 @@ static int bin2file(const char *file, const char *ext, const unsigned char *data static unsigned char *file2bin(const char *file, const char *ext, int *size) { FILE *fp; - int len; + size_t len; unsigned char *data; char name[strlen(file) + (ext ? strlen(ext) : 0) + 2]; + struct stat stats; if (ext) sprintf(name, "%s.%s", file, ext); @@ -186,18 +187,32 @@ static unsigned char *file2bin(const char *file, const char *ext, int *size) log_info("Reading to %s\n", name); - len = get_filesize(name); fp = fopen(name, "r"); if (!fp) { log_err("Failed to open: %s\n", name); return NULL; } + if (fstat(fileno(fp), &stats) == -1) { + log_err("Failed to fstat: %s (%s)\n", name, strerror(errno)); + fclose(fp); + return NULL; + } + len = stats.st_size; + data = malloc(len); - if (!fread(data, len, 1, fp)) - len = 0; + if (!data) { + log_err("Failed to malloc %zu bytes: %s\n", len, name); + fclose(fp); + return NULL; + } + if (fread(data, len, 1, fp) != len) { + log_err("Failed to fread %zu bytes: %s\n", len, name); + fclose(fp); + return NULL; + } fclose(fp); - *size = len; + *size = (int)len; return data; } diff --git a/src/imaevm.h b/src/imaevm.h index dc81a3a..36050f4 100644 --- a/src/imaevm.h +++ b/src/imaevm.h @@ -211,7 +211,6 @@ extern struct libevm_params params; void do_dump(FILE *fp, const void *ptr, int len, bool cr); void dump(const void *ptr, int len); -int get_filesize(const char *filename); int ima_calc_hash(const char *file, uint8_t *hash); int get_hash_algo(const char *algo); RSA *read_pub_key(const char *keyfile, int x509); diff --git a/src/libimaevm.c b/src/libimaevm.c index f8ab812..1562aaf 100644 --- a/src/libimaevm.c +++ b/src/libimaevm.c @@ -116,14 +116,6 @@ const char *get_hash_algo_by_id(int algo) return "unknown"; } -int get_filesize(const char *filename) -{ - struct stat stats; - /* Need to know the file length */ - stat(filename, &stats); - return (int)stats.st_size; -} - static inline off_t get_fdsize(int fd) { struct stat stats; 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)) {