From patchwork Mon Jul 15 20:05:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Chikunov X-Patchwork-Id: 11044893 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 B4A2C746 for ; Mon, 15 Jul 2019 20:06:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A0EBD28438 for ; Mon, 15 Jul 2019 20:06:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8EE9928481; Mon, 15 Jul 2019 20:06:02 +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 2B3B028438 for ; Mon, 15 Jul 2019 20:06:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730688AbfGOUGB (ORCPT ); Mon, 15 Jul 2019 16:06:01 -0400 Received: from vmicros1.altlinux.org ([194.107.17.57]:58194 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730872AbfGOUGB (ORCPT ); Mon, 15 Jul 2019 16:06:01 -0400 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id B7E7A72CA65; Mon, 15 Jul 2019 23:05:59 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id 94BA64A4A29; Mon, 15 Jul 2019 23:05:59 +0300 (MSK) From: Vitaly Chikunov To: Mimi Zohar , Dmitry Kasatkin , linux-integrity@vger.kernel.org Subject: [PATCH v2 1/5] ima-evm-utils: Fix null dereference from file2bin to memcpy Date: Mon, 15 Jul 2019 23:05:49 +0300 Message-Id: <20190715200553.22403-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. Fixes: CID 229904. Signed-off-by: Vitaly Chikunov --- src/evmctl.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/evmctl.c b/src/evmctl.c index a6d07c9..d6e0b2c 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("Signature file is too big: %s\n", file); + free(tmp); + return -1; + } memcpy(sig, tmp, len); free(tmp); } else { From patchwork Mon Jul 15 20:05:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Chikunov X-Patchwork-Id: 11044895 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 26D6614DB for ; Mon, 15 Jul 2019 20:06:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1640928500 for ; Mon, 15 Jul 2019 20:06:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0977428538; Mon, 15 Jul 2019 20:06:08 +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 A682D28500 for ; Mon, 15 Jul 2019 20:06:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731008AbfGOUGH (ORCPT ); Mon, 15 Jul 2019 16:06:07 -0400 Received: from vmicros1.altlinux.org ([194.107.17.57]:58332 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730872AbfGOUGH (ORCPT ); Mon, 15 Jul 2019 16:06:07 -0400 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id 3407072CA65; Mon, 15 Jul 2019 23:06:05 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id F383F4A4AE9; Mon, 15 Jul 2019 23:06:04 +0300 (MSK) From: Vitaly Chikunov To: Mimi Zohar , Dmitry Kasatkin , linux-integrity@vger.kernel.org Subject: [PATCH v2 2/5] ima-evm-utils: Fix possible xattr_value overflows in calc_evm_hash Date: Mon, 15 Jul 2019 23:05:50 +0300 Message-Id: <20190715200553.22403-2-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 `selinux_str',`caps_str', and `ima_str' are passed from the command line but copied into the fixed-size buffer. Yes, length of `selinux_str' is calculated differently than of `caps_str'. Fixes: CID 229895. --- src/evmctl.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/evmctl.c b/src/evmctl.c index d6e0b2c..04dc546 100644 --- a/src/evmctl.c +++ b/src/evmctl.c @@ -401,16 +401,31 @@ static int calc_evm_hash(const char *file, unsigned char *hash) for (xattrname = evm_config_xattrnames; *xattrname != NULL; xattrname++) { if (!strcmp(*xattrname, XATTR_NAME_SELINUX) && selinux_str) { - strcpy(xattr_value, selinux_str); err = strlen(selinux_str) + 1; + if (err > sizeof(xattr_value)) { + log_err("selinux[%u] value is too long to fit into xattr[%zu]\n", + err, sizeof(xattr_value)); + return -1; + } + strcpy(xattr_value, selinux_str); } else if (!strcmp(*xattrname, XATTR_NAME_IMA) && ima_str) { - hex2bin(xattr_value, ima_str, strlen(ima_str) / 2); err = strlen(ima_str) / 2; + if (err > sizeof(xattr_value)) { + log_err("ima[%u] value is too long to fit into xattr[%zu]\n", + err, sizeof(xattr_value)); + return -1; + } + hex2bin(xattr_value, ima_str, err); } 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); + if (err >= sizeof(xattr_value)) { + log_err("caps[%u] value is too long to fit into xattr[%zu]\n", + err + 1, sizeof(xattr_value)); + return -1; + } + strcpy(xattr_value, caps_str); } else { err = lgetxattr(file, *xattrname, xattr_value, sizeof(xattr_value)); if (err < 0) { From patchwork Mon Jul 15 20:05:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Chikunov X-Patchwork-Id: 11044897 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 BC4E86C5 for ; Mon, 15 Jul 2019 20:06:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ACE3128500 for ; Mon, 15 Jul 2019 20:06:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A107628538; Mon, 15 Jul 2019 20:06:12 +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 5F36928500 for ; Mon, 15 Jul 2019 20:06:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731187AbfGOUGM (ORCPT ); Mon, 15 Jul 2019 16:06:12 -0400 Received: from vmicros1.altlinux.org ([194.107.17.57]:58400 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731054AbfGOUGL (ORCPT ); Mon, 15 Jul 2019 16:06:11 -0400 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id 2A5D672CA65; Mon, 15 Jul 2019 23:06:10 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id 0ECB54A4A29; Mon, 15 Jul 2019 23:06:10 +0300 (MSK) From: Vitaly Chikunov To: Mimi Zohar , Dmitry Kasatkin , linux-integrity@vger.kernel.org Subject: [PATCH v2 3/5] ima-evm-utils: Fix memory leak in get_password Date: Mon, 15 Jul 2019 23:05:51 +0300 Message-Id: <20190715200553.22403-3-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 Free allocated password buffer when returning NULL. Fixes: CID 229894 (partially). --- src/evmctl.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/evmctl.c b/src/evmctl.c index 04dc546..f15056b 100644 --- a/src/evmctl.c +++ b/src/evmctl.c @@ -1841,6 +1841,7 @@ static char *get_password(void) if (tcsetattr(fileno(stdin), TCSANOW, &tmp_flags) != 0) { perror("tcsetattr"); + free(password); return NULL; } @@ -1850,6 +1851,7 @@ static char *get_password(void) /* restore terminal */ if (tcsetattr(fileno(stdin), TCSANOW, &flags) != 0) { perror("tcsetattr"); + free(password); return NULL; } From patchwork Mon Jul 15 20:05:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Chikunov X-Patchwork-Id: 11044899 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 2EA3B6C5 for ; Mon, 15 Jul 2019 20:06:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1F0BB28500 for ; Mon, 15 Jul 2019 20:06:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 138F128538; Mon, 15 Jul 2019 20:06:19 +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 AFBB228500 for ; Mon, 15 Jul 2019 20:06:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731210AbfGOUGS (ORCPT ); Mon, 15 Jul 2019 16:06:18 -0400 Received: from vmicros1.altlinux.org ([194.107.17.57]:58456 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731054AbfGOUGS (ORCPT ); Mon, 15 Jul 2019 16:06:18 -0400 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id 2122B72CA65; Mon, 15 Jul 2019 23:06:15 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id 859B44A4A29; Mon, 15 Jul 2019 23:06:14 +0300 (MSK) From: Vitaly Chikunov To: Mimi Zohar , Dmitry Kasatkin , linux-integrity@vger.kernel.org Subject: [PATCH v2 4/5] ima-evm-utils: Fix file2bin stat and fopen relations Date: Mon, 15 Jul 2019 23:05:52 +0300 Message-Id: <20190715200553.22403-4-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 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 | 26 +++++++++++++++++++++----- src/imaevm.h | 1 - src/libimaevm.c | 8 -------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/evmctl.c b/src/evmctl.c index f15056b..61808d2 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,33 @@ 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); + free(data); + 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 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)) {