From patchwork Wed May 26 00:59:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tushar Sugandhi X-Patchwork-Id: 12280463 X-Patchwork-Delegate: snitzer@redhat.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 319B0C47088 for ; Wed, 26 May 2021 01:10:14 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9E69A61417 for ; Wed, 26 May 2021 01:10:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9E69A61417 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: mail.kernel.org; spf=tempfail smtp.mailfrom=dm-devel-bounces@redhat.com Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-230-fYS3JjEsOfe1lLfqreoSAw-1; Tue, 25 May 2021 21:10:10 -0400 X-MC-Unique: fYS3JjEsOfe1lLfqreoSAw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 02063800FF0; Wed, 26 May 2021 01:10:07 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D65445C1A1; Wed, 26 May 2021 01:10:06 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 97E69180B463; Wed, 26 May 2021 01:10:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14Q19jfW020114 for ; Tue, 25 May 2021 21:09:45 -0400 Received: by smtp.corp.redhat.com (Postfix) id B87D010F26EE; Wed, 26 May 2021 01:09:45 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast04.extmail.prod.ext.rdu2.redhat.com [10.11.55.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B3CA910F26ED for ; Wed, 26 May 2021 01:09:42 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0FBD2101A531 for ; Wed, 26 May 2021 01:09:42 +0000 (UTC) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by relay.mimecast.com with ESMTP id us-mta-406-4mxto6IhMFynb1oifFu98g-1; Tue, 25 May 2021 21:09:39 -0400 X-MC-Unique: 4mxto6IhMFynb1oifFu98g-1 Received: from tusharsu-Ubuntu.lan (c-71-197-163-6.hsd1.wa.comcast.net [71.197.163.6]) by linux.microsoft.com (Postfix) with ESMTPSA id 17F7720B7188; Tue, 25 May 2021 18:00:04 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 17F7720B7188 From: Tushar Sugandhi To: dm-devel@redhat.com Date: Tue, 25 May 2021 17:59:48 -0700 Message-Id: <20210526005954.31564-2-tusharsu@linux.microsoft.com> In-Reply-To: <20210526005954.31564-1-tusharsu@linux.microsoft.com> References: <20210526005954.31564-1-tusharsu@linux.microsoft.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 14Q19jfW020114 X-loop: dm-devel@redhat.com Cc: tusharsu@linux.microsoft.com, nramas@linux.microsoft.com, zohar@linux.ibm.com, snitzer@redhat.com, agk@redhat.com Subject: [dm-devel] [RFC 1/7] dm: measure data on table load X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=dm-devel-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com DM configures a block device with various target specific attributes passed to it as a table. DM loads the table, and calls each target’s respective constructors with the attributes as input parameters. Some of these attributes are critical to ensure the device meets certain security bar. Thus, IMA should measure these attributes, to ensure they are not tampered with, during the lifetime of the device. So that external services can have high confidence in the configuration of the block-devices on a given system. Some devices may have large tables. And a given device may change its state (table-load, suspend, resume, rename, remove, table-clear etc.) many times. Measuring these attributes each time when the device changes its state will significantly increase the size of the IMA logs. Further, once configured, these attributes are not expected to change unless a new table is loaded, or a device is removed and recreated. Therefore the clear text of the attributes should only be measured during table load, and the hash of the active/inactive table should be measured for the remaining device state changes. Measure device parameters, as well as target specific attributes, during table load, using IMA function ima_measure_critical_data(). Compute the hash of the inactive table and store it for measurements during future state change. If a load is called multiple times, update the inactive table hash with the hash of the latest populated table. So that the correct inactive table hash is measured when the device transitions to different states like resume, remove, rename etc. Signed-off-by: Tushar Sugandhi --- drivers/md/Makefile | 2 + drivers/md/dm-core.h | 5 + drivers/md/dm-ima.c | 219 ++++++++++++++++++++++++++++++++++ drivers/md/dm-ima.h | 36 ++++++ drivers/md/dm-ioctl.c | 7 ++ include/linux/device-mapper.h | 2 +- include/uapi/linux/dm-ioctl.h | 6 + 7 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 drivers/md/dm-ima.c create mode 100644 drivers/md/dm-ima.h diff --git a/drivers/md/Makefile b/drivers/md/Makefile index ef7ddc27685c..af349b7cbf8b 100644 --- a/drivers/md/Makefile +++ b/drivers/md/Makefile @@ -92,6 +92,8 @@ ifeq ($(CONFIG_DM_UEVENT),y) dm-mod-objs += dm-uevent.o endif +dm-mod-objs += dm-ima.o + ifeq ($(CONFIG_DM_VERITY_FEC),y) dm-verity-objs += dm-verity-fec.o endif diff --git a/drivers/md/dm-core.h b/drivers/md/dm-core.h index 5953ff2bd260..f67f898803db 100644 --- a/drivers/md/dm-core.h +++ b/drivers/md/dm-core.h @@ -18,6 +18,7 @@ #include #include "dm.h" +#include "dm-ima.h" #define DM_RESERVED_MAX_IOS 1024 @@ -114,6 +115,10 @@ struct mapped_device { bool init_tio_pdu:1; struct srcu_struct io_barrier; + +#ifdef CONFIG_IMA + struct dm_ima_measurements ima; +#endif }; void disable_discard(struct mapped_device *md); diff --git a/drivers/md/dm-ima.c b/drivers/md/dm-ima.c new file mode 100644 index 000000000000..8903e746ead6 --- /dev/null +++ b/drivers/md/dm-ima.c @@ -0,0 +1,219 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2021 Microsoft Corporation + * + * Author: Tushar Sugandhi + * + * File: dm-ima.c + * Enables IMA measurements for DM targets + */ + +#include "dm-core.h" +#include "dm-ima.h" + +#include +#include +#include +#include +#include + +#define DM_MSG_PREFIX "dm-ima" + +#ifdef CONFIG_IMA +/* + * Internal function to prefix separator characters in input buffer with escape + * character, so that they don't interfere with the construction of key-value pairs, + * and clients can split the key1=value1;key2=value2; pairs properly. + */ +void fix_separator_chars(char **buf) +{ + int l = strlen(*buf); + int i, j, sp = 0; + + for (i = 0; i < l; i++) + if ((*buf)[i] == '\\' || (*buf)[i] == ';' || (*buf)[i] == '=') + sp++; + + if (sp == 0) + return; + + for (i = l-1, j = i+sp; i >= 0; i--) { + (*buf)[j--] = (*buf)[i]; + if ((*buf)[i] == '\\' || (*buf)[i] == ';' || (*buf)[i] == '=') + (*buf)[j--] = '\\'; + } +} + +/* + * Internal wrapper function to call IMA to measure DM data. + */ +void dm_ima_measure_data(const char *event_name, const void *buf, size_t buf_len) +{ + unsigned int noio_flag; + + noio_flag = memalloc_noio_save(); + ima_measure_critical_data(DM_NAME, event_name, buf, buf_len, false); + memalloc_noio_restore(noio_flag); +} + +/* + * Build up the IMA data for each target, and finally measure. + */ +void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_flags) +{ + char *ima_buf_head = NULL, *ima_buf_cur = NULL, *device_data = NULL, *digest_buf = NULL; + size_t total_buf_len = DM_IMA_DEVICE_BUF_LEN + DM_IMA_TABLE_BUF_LEN; + unsigned int num_targets, device_data_len, i; + int desc_size, digest_size, last_i = -1, r; + char *dev_name = NULL, *dev_uuid = NULL; + status_type_t type = STATUSTYPE_IMA; + struct shash_desc *desc = NULL; + struct crypto_shash *tfm; + long ima_remaining; + sector_t capacity; + u8 *digest = NULL; + + dev_name = kzalloc(DM_NAME_LEN*2, GFP_KERNEL); + if (!dev_name) + goto error; + + dev_uuid = kzalloc(DM_UUID_LEN*2, GFP_KERNEL); + if (!dev_uuid) + goto error; + + if (dm_copy_name_and_uuid(table->md, dev_name, dev_uuid)) + goto error; + + fix_separator_chars(&dev_name); + fix_separator_chars(&dev_uuid); + + ima_buf_head = ima_buf_cur = kzalloc(total_buf_len, GFP_KERNEL); + if (!ima_buf_head) + goto error; + + device_data = kzalloc(DM_IMA_DEVICE_BUF_LEN, GFP_KERNEL); + if (!device_data) + goto error; + + tfm = crypto_alloc_shash("sha256", 0, 0); + if (IS_ERR(tfm)) + goto error; + + desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); + digest_size = crypto_shash_digestsize(tfm); + + digest = kzalloc(digest_size, GFP_KERNEL); + if (!digest) + goto error; + + desc = kzalloc(desc_size, GFP_KERNEL); + if (!desc) + goto error; + + desc->tfm = tfm; + + num_targets = dm_table_get_num_targets(table); + capacity = get_capacity(table->md->disk); + + scnprintf(device_data, DM_IMA_DEVICE_BUF_LEN, + "name=%s;uuid=%s;capacity=%llu;major=%d;minor=%d;minor_count=%d;num_targets=%u;", + dev_name, dev_uuid, capacity, table->md->disk->major, + table->md->disk->first_minor, table->md->disk->minors, num_targets); + + device_data_len = strlen(device_data); + memcpy(ima_buf_cur, device_data, device_data_len); + ima_buf_cur += device_data_len; + + for (i = 0; i < num_targets; i++) { + struct dm_target *ti = dm_table_get_target(table, i); + size_t l = 0; + + ima_remaining = total_buf_len - (ima_buf_cur - ima_buf_head); + + if (ti->type->status) + ti->type->status(ti, type, status_flags, ima_buf_cur, ima_remaining); + else + ima_buf_cur[0] = '\0'; + + l = strlen(ima_buf_cur); + + ima_remaining -= (l+1); + + if (ima_remaining > 0) { + ima_buf_cur += l; + last_i = -1; + continue; + } + + if (unlikely(i == last_i)) { + /* IMA measurements for DM targets are best-effort. + * If the IMA data from current target type is too large to fit + * into total_buf_len, measure what we have in the current buffer, and bail-out. + */ + DMERR("IMA data for %s target too large.", ti->type->name); + break; + } + /* The buffer ima_buf_head is full if we reach here. + * In this case, measure the current buffer, free it, and reallocate a new buffer, + * and continue measuring the remaining targets in the table. + */ + ima_buf_cur[0] = '\0'; + dm_ima_measure_data("table_load", ima_buf_head, strlen(ima_buf_head)); + crypto_shash_update(desc, (const u8 *)ima_buf_head, strlen(ima_buf_head)); + kfree(ima_buf_head); + ima_buf_head = ima_buf_cur = kzalloc(total_buf_len, GFP_KERNEL); + if (!ima_buf_head) + goto error; + /* + * Each new "table_load" entry in IMA log should have device data prefix, + * so that multiple records from the same table load can be linked together. + */ + memcpy(ima_buf_cur, device_data, device_data_len); + ima_buf_cur += device_data_len; + + last_i = i; + i--; + } + + dm_ima_measure_data("table_load", ima_buf_head, strlen(ima_buf_head)); + + kfree(table->md->ima.inactive_table_hash); + table->md->ima.inactive_table_hash = NULL; + table->md->ima.inactive_table_hash_len = 0; + + r = crypto_shash_update(desc, (const u8 *)ima_buf_head, strlen(ima_buf_head)); + if (r < 0) + goto error; + + r = crypto_shash_final(desc, digest); + if (r < 0) + goto error; + + digest_buf = kzalloc((digest_size*2)+1, GFP_KERNEL); + if (!digest_buf) + goto error; + + for (i = 0; i < digest_size; i++) + snprintf((digest_buf+(i*2)), 3, "%02x", digest[i]); + + table->md->ima.inactive_table_hash = digest_buf; + table->md->ima.inactive_table_hash_len = strlen(digest_buf); + table->md->ima.device_data = device_data; + table->md->ima.device_data_len = device_data_len; + table->md->ima.num_targets = num_targets; + +error: + kfree(dev_name); + kfree(dev_uuid); + kfree(digest); + kfree(desc); + crypto_free_shash(tfm); + kfree(ima_buf_head); +} + +#else +void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_flags) {} +#endif +MODULE_AUTHOR("Tushar Sugandhi "); +MODULE_DESCRIPTION("Enables IMA measurements for DM targets"); +MODULE_LICENSE("GPL"); diff --git a/drivers/md/dm-ima.h b/drivers/md/dm-ima.h new file mode 100644 index 000000000000..a71e0682fcb9 --- /dev/null +++ b/drivers/md/dm-ima.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Copyright (C) 2021 Microsoft Corporation + * + * Author: Tushar Sugandhi + * + * File: dm-ima.h + * Header file for device mapper IMA measurements. + */ + +#ifndef DM_IMA_H +#define DM_IMA_H + +#define DM_IMA_TABLE_BUF_LEN 4096 +#define DM_IMA_DEVICE_BUF_LEN 1024 + +struct dm_ima_measurements { + /* Contains data specific to the device which is common across + * all the targets in the table.e.g. name, uuid, major, minor, capacity etc. + * The values are stored in semicolon separated key=value; pairs. + */ + char *device_data; + unsigned int device_data_len; + unsigned int num_targets; + + /* Contains the sha256 hashs of the IMA measurements of the + * target attributes key-value pairs from the active/inactive tables. + */ + char *active_table_hash; + unsigned int active_table_hash_len; + char *inactive_table_hash; + unsigned int inactive_table_hash_len; +}; + +void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_flags); +#endif /*DM_IMA_H*/ diff --git a/drivers/md/dm-ioctl.c b/drivers/md/dm-ioctl.c index 2209cbcd84db..21deff8513a7 100644 --- a/drivers/md/dm-ioctl.c +++ b/drivers/md/dm-ioctl.c @@ -21,6 +21,9 @@ #include +#include +#include "dm-ima.h" + #define DM_MSG_PREFIX "ioctl" #define DM_DRIVER_EMAIL "dm-devel@redhat.com" @@ -1224,6 +1227,8 @@ static void retrieve_status(struct dm_table *table, if (param->flags & DM_STATUS_TABLE_FLAG) type = STATUSTYPE_TABLE; + else if (param->flags & DM_IMA_MEASUREMENT_FLAG) + type = STATUSTYPE_IMA; else type = STATUSTYPE_INFO; @@ -1391,6 +1396,8 @@ static int populate_table(struct dm_table *table, next = spec->next; } + dm_ima_measure_on_table_load(table, STATUSTYPE_IMA); + return dm_table_complete(table); } diff --git a/include/linux/device-mapper.h b/include/linux/device-mapper.h index ff700fb6ce1d..738a7d023650 100644 --- a/include/linux/device-mapper.h +++ b/include/linux/device-mapper.h @@ -31,7 +31,7 @@ enum dm_queue_mode { DM_TYPE_DAX_BIO_BASED = 3, }; -typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t; +typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE, STATUSTYPE_IMA } status_type_t; union map_info { void *ptr; diff --git a/include/uapi/linux/dm-ioctl.h b/include/uapi/linux/dm-ioctl.h index e5c6e458bdf7..c12ce30b52df 100644 --- a/include/uapi/linux/dm-ioctl.h +++ b/include/uapi/linux/dm-ioctl.h @@ -376,4 +376,10 @@ enum { */ #define DM_INTERNAL_SUSPEND_FLAG (1 << 18) /* Out */ +/* + * If set, returns in the in buffer passed by UM, the raw table information + * that would be measured by IMA subsystem on device state change. + */ +#define DM_IMA_MEASUREMENT_FLAG (1 << 19) /* In */ + #endif /* _LINUX_DM_IOCTL_H */