From patchwork Thu Dec 1 01:05:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mimi Zohar X-Patchwork-Id: 9455221 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 31C7860235 for ; Thu, 1 Dec 2016 01:07:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2240F2846F for ; Thu, 1 Dec 2016 01:07:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 137C6284C2; Thu, 1 Dec 2016 01:07:26 +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=-6.9 required=2.0 tests=BAYES_00,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 3ECFB2846F for ; Thu, 1 Dec 2016 01:07:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751758AbcLABHE (ORCPT ); Wed, 30 Nov 2016 20:07:04 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:60181 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933638AbcLABHB (ORCPT ); Wed, 30 Nov 2016 20:07:01 -0500 Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.17/8.16.0.17) with SMTP id uB1146YZ094677 for ; Wed, 30 Nov 2016 20:07:00 -0500 Received: from e28smtp05.in.ibm.com (e28smtp05.in.ibm.com [125.16.236.5]) by mx0b-001b2d01.pphosted.com with ESMTP id 27277hpeq4-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 30 Nov 2016 20:07:00 -0500 Received: from localhost by e28smtp05.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 1 Dec 2016 06:36:55 +0530 Received: from d28dlp02.in.ibm.com (9.184.220.127) by e28smtp05.in.ibm.com (192.168.1.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 1 Dec 2016 06:36:54 +0530 Received: from d28relay10.in.ibm.com (d28relay10.in.ibm.com [9.184.220.161]) by d28dlp02.in.ibm.com (Postfix) with ESMTP id 8FADB394006F; Thu, 1 Dec 2016 06:36:53 +0530 (IST) Received: from d28av06.in.ibm.com (d28av06.in.ibm.com [9.184.220.48]) by d28relay10.in.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id uB116JF328639400; Thu, 1 Dec 2016 06:36:19 +0530 Received: from d28av06.in.ibm.com (localhost [127.0.0.1]) by d28av06.in.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id uB116pjN015743; Thu, 1 Dec 2016 06:36:53 +0530 Received: from localhost.localdomain.localdomain ([9.80.87.241]) by d28av06.in.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id uB115o0o013588; Thu, 1 Dec 2016 06:36:40 +0530 From: Mimi Zohar To: linux-security-module Cc: Mimi Zohar , Thiago Jung Bauermann , linux-ima-devel@lists.sourceforge.net, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Andrew Morton , Dmitry Kasatkin Subject: [PATCH v8 09/10] ima: define a canonical binary_runtime_measurements list format Date: Wed, 30 Nov 2016 20:05:45 -0500 X-Mailer: git-send-email 2.1.0 In-Reply-To: <1480554346-29071-1-git-send-email-zohar@linux.vnet.ibm.com> References: <1480554346-29071-1-git-send-email-zohar@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16120101-0016-0000-0000-000003959116 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16120101-0017-0000-0000-000027C5F780 Message-Id: <1480554346-29071-10-git-send-email-zohar@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-11-30_20:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1609300000 definitions=main-1612010018 Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP The IMA binary_runtime_measurements list is currently in platform native format. To allow restoring a measurement list carried across kexec with a different endianness than the targeted kernel, this patch defines little-endian as the canonical format. For big endian systems wanting to save/restore the measurement list from a system with a different endianness, a new boot command line parameter named "ima_canonical_fmt" is defined. Considerations: use of the "ima_canonical_fmt" boot command line option will break existing userspace applications on big endian systems expecting the binary_runtime_measurements list to be in platform native format. Changelog v3: - restore PCR value properly Signed-off-by: Mimi Zohar Acked-by: Dmitry Kasatkin --- Documentation/kernel-parameters.txt | 4 ++++ security/integrity/ima/ima.h | 6 ++++++ security/integrity/ima/ima_fs.c | 28 +++++++++++++++++++++------- security/integrity/ima/ima_kexec.c | 11 +++++++++-- security/integrity/ima/ima_template.c | 23 +++++++++++++++++++++-- security/integrity/ima/ima_template_lib.c | 7 +++++-- 6 files changed, 66 insertions(+), 13 deletions(-) diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt index 37babf91f2cb..3ee81afad7e9 100644 --- a/Documentation/kernel-parameters.txt +++ b/Documentation/kernel-parameters.txt @@ -1641,6 +1641,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted. The builtin appraise policy appraises all files owned by uid=0. + ima_canonical_fmt [IMA] + Use the canonical format for the binary runtime + measurements, instead of host native format. + ima_hash= [IMA] Format: { md5 | sha1 | rmd160 | sha256 | sha384 | sha512 | ... } diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h index 6b0540ad189f..5e6180a4da7d 100644 --- a/security/integrity/ima/ima.h +++ b/security/integrity/ima/ima.h @@ -122,6 +122,12 @@ void ima_load_kexec_buffer(void); static inline void ima_load_kexec_buffer(void) {} #endif /* CONFIG_HAVE_IMA_KEXEC */ +/* + * The default binary_runtime_measurements list format is defined as the + * platform native format. The canonical format is defined as little-endian. + */ +extern bool ima_canonical_fmt; + /* Internal IMA function definitions */ int ima_init(void); int ima_fs_init(void); diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c index 10bea0125fa1..ca303e5d2b94 100644 --- a/security/integrity/ima/ima_fs.c +++ b/security/integrity/ima/ima_fs.c @@ -28,6 +28,16 @@ static DEFINE_MUTEX(ima_write_mutex); +bool ima_canonical_fmt; +static int __init default_canonical_fmt_setup(char *str) +{ +#ifdef __BIG_ENDIAN + ima_canonical_fmt = 1; +#endif + return 1; +} +__setup("ima_canonical_fmt", default_canonical_fmt_setup); + static int valid_policy = 1; #define TMPBUFLEN 12 static ssize_t ima_show_htable_value(char __user *buf, size_t count, @@ -122,7 +132,7 @@ int ima_measurements_show(struct seq_file *m, void *v) struct ima_queue_entry *qe = v; struct ima_template_entry *e; char *template_name; - int namelen; + u32 pcr, namelen, template_data_len; /* temporary fields */ bool is_ima_template = false; int i; @@ -139,25 +149,29 @@ int ima_measurements_show(struct seq_file *m, void *v) * PCR used defaults to the same (config option) in * little-endian format, unless set in policy */ - ima_putc(m, &e->pcr, sizeof(e->pcr)); + pcr = !ima_canonical_fmt ? e->pcr : cpu_to_le32(e->pcr); + ima_putc(m, &pcr, sizeof(e->pcr)); /* 2nd: template digest */ ima_putc(m, e->digest, TPM_DIGEST_SIZE); /* 3rd: template name size */ - namelen = strlen(template_name); + namelen = !ima_canonical_fmt ? strlen(template_name) : + cpu_to_le32(strlen(template_name)); ima_putc(m, &namelen, sizeof(namelen)); /* 4th: template name */ - ima_putc(m, template_name, namelen); + ima_putc(m, template_name, strlen(template_name)); /* 5th: template length (except for 'ima' template) */ if (strcmp(template_name, IMA_TEMPLATE_IMA_NAME) == 0) is_ima_template = true; - if (!is_ima_template) - ima_putc(m, &e->template_data_len, - sizeof(e->template_data_len)); + if (!is_ima_template) { + template_data_len = !ima_canonical_fmt ? e->template_data_len : + cpu_to_le32(e->template_data_len); + ima_putc(m, &template_data_len, sizeof(e->template_data_len)); + } /* 6th: template specific data */ for (i = 0; i < e->template_desc->num_fields; i++) { diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/ima_kexec.c index 2c4824ac1ce1..e473eee913cb 100644 --- a/security/integrity/ima/ima_kexec.c +++ b/security/integrity/ima/ima_kexec.c @@ -21,8 +21,7 @@ static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer, { struct ima_queue_entry *qe; struct seq_file file; - struct ima_kexec_hdr khdr = { - .version = 1, .buffer_size = 0, .count = 0}; + struct ima_kexec_hdr khdr; int ret = 0; /* segment size can't change between kexec load and execute */ @@ -36,6 +35,8 @@ static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer, file.read_pos = 0; file.count = sizeof(khdr); /* reserved space */ + memset(&khdr, 0, sizeof(khdr)); + khdr.version = 1; list_for_each_entry_rcu(qe, &ima_measurements, later) { if (file.count < file.size) { khdr.count++; @@ -54,7 +55,13 @@ static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer, * (eg. version, buffer size, number of measurements) */ khdr.buffer_size = file.count; + if (ima_canonical_fmt) { + khdr.version = cpu_to_le16(khdr.version); + khdr.count = cpu_to_le64(khdr.count); + khdr.buffer_size = cpu_to_le64(khdr.buffer_size); + } memcpy(file.buf, &khdr, sizeof(khdr)); + print_hex_dump(KERN_DEBUG, "ima dump: ", DUMP_PREFIX_NONE, 16, 1, file.buf, file.count < 100 ? file.count : 100, true); diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c index 16bbb5a80fc8..cebb37c63629 100644 --- a/security/integrity/ima/ima_template.c +++ b/security/integrity/ima/ima_template.c @@ -302,6 +302,9 @@ static int ima_restore_template_data(struct ima_template_desc *template_desc, } offset += sizeof(*field_data); + if (ima_canonical_fmt) + field_data->len = le32_to_cpu(field_data->len); + if (offset > (template_data_size - field_data->len)) { pr_err("Restoring the template field data failed\n"); ret = -EINVAL; @@ -352,7 +355,7 @@ int ima_restore_measurement_list(loff_t size, void *buf) struct binary_data_v1 *data_v1; void *bufp = buf + sizeof(*khdr); - void *bufendp = buf + khdr->buffer_size; + void *bufendp; struct ima_template_entry *entry; struct ima_template_desc *template_desc; unsigned long count = 0; @@ -361,6 +364,12 @@ int ima_restore_measurement_list(loff_t size, void *buf) if (!buf || size < sizeof(*khdr)) return 0; + if (ima_canonical_fmt) { + khdr->version = le16_to_cpu(khdr->version); + khdr->count = le64_to_cpu(khdr->count); + khdr->buffer_size = le64_to_cpu(khdr->buffer_size); + } + if (khdr->version != 1) { pr_err("attempting to restore a incompatible measurement list"); return -EINVAL; @@ -376,6 +385,7 @@ int ima_restore_measurement_list(loff_t size, void *buf) * v1 format: pcr, digest, template-name-len, template-name, * template-data-size, template-data */ + bufendp = buf + khdr->buffer_size; while ((bufp < bufendp) && (count++ < khdr->count)) { hdr_v1 = bufp; if (bufp > (bufendp - sizeof(*hdr_v1))) { @@ -385,6 +395,10 @@ int ima_restore_measurement_list(loff_t size, void *buf) } bufp += sizeof(*hdr_v1); + if (ima_canonical_fmt) + hdr_v1->template_name_len = + le32_to_cpu(hdr_v1->template_name_len); + if ((hdr_v1->template_name_len >= MAX_TEMPLATE_NAME_LEN) || (bufp > (bufendp - hdr_v1->template_name_len))) { pr_err("attempting to restore a template name \ @@ -434,6 +448,10 @@ int ima_restore_measurement_list(loff_t size, void *buf) } bufp += (u_int8_t) sizeof(data_v1->template_data_size); + if (ima_canonical_fmt) + data_v1->template_data_size = + le32_to_cpu(data_v1->template_data_size); + if (bufp > (bufendp - data_v1->template_data_size)) { pr_err("restoring the template data failed\n"); ret = -EINVAL; @@ -449,7 +467,8 @@ int ima_restore_measurement_list(loff_t size, void *buf) break; memcpy(entry->digest, hdr_v1->digest, TPM_DIGEST_SIZE); - entry->pcr = hdr_v1->pcr; + entry->pcr = + !ima_canonical_fmt ? hdr_v1->pcr : le32_to_cpu(hdr_v1->pcr); ret = ima_restore_measurement_entry(entry); if (ret < 0) break; diff --git a/security/integrity/ima/ima_template_lib.c b/security/integrity/ima/ima_template_lib.c index f9bae04ba176..f9ba37b3928d 100644 --- a/security/integrity/ima/ima_template_lib.c +++ b/security/integrity/ima/ima_template_lib.c @@ -103,8 +103,11 @@ static void ima_show_template_data_binary(struct seq_file *m, u32 len = (show == IMA_SHOW_BINARY_OLD_STRING_FMT) ? strlen(field_data->data) : field_data->len; - if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) - ima_putc(m, &len, sizeof(len)); + if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) { + u32 field_len = !ima_canonical_fmt ? len : cpu_to_le32(len); + + ima_putc(m, &field_len, sizeof(field_len)); + } if (!len) return;