Преглед на файлове

ima: reduce memory usage when a template containing the n field is used

Before this change, to correctly calculate the template digest for the
'ima' template, the event name field (id: 'n') length was set to the fixed
size of 256 bytes.

This patch reduces the length of the event name field to the string
length incremented of one (to make room for the termination character '\0')
and handles the specific case of the digest calculation for the 'ima'
template directly in ima_calc_field_array_hash_tfm().

Signed-off-by: Roberto Sassu <roberto.sassu@polito.it>
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Roberto Sassu преди 11 години
родител
ревизия
e3b64c268b
променени са 2 файла, в които са добавени 13 реда и са изтрити 17 реда
  1. 9 2
      security/integrity/ima/ima_crypto.c
  2. 4 15
      security/integrity/ima/ima_template_lib.c

+ 9 - 2
security/integrity/ima/ima_crypto.c

@@ -161,15 +161,22 @@ static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
 		return rc;
 		return rc;
 
 
 	for (i = 0; i < num_fields; i++) {
 	for (i = 0; i < num_fields; i++) {
+		u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 };
+		u8 *data_to_hash = field_data[i].data;
+		u32 datalen = field_data[i].len;
+
 		if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) {
 		if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) {
 			rc = crypto_shash_update(&desc.shash,
 			rc = crypto_shash_update(&desc.shash,
 						(const u8 *) &field_data[i].len,
 						(const u8 *) &field_data[i].len,
 						sizeof(field_data[i].len));
 						sizeof(field_data[i].len));
 			if (rc)
 			if (rc)
 				break;
 				break;
+		} else if (strcmp(td->fields[i]->field_id, "n") == 0) {
+			memcpy(buffer, data_to_hash, datalen);
+			data_to_hash = buffer;
+			datalen = IMA_EVENT_NAME_LEN_MAX + 1;
 		}
 		}
-		rc = crypto_shash_update(&desc.shash, field_data[i].data,
-					 field_data[i].len);
+		rc = crypto_shash_update(&desc.shash, data_to_hash, datalen);
 		if (rc)
 		if (rc)
 			break;
 			break;
 	}
 	}

+ 4 - 15
security/integrity/ima/ima_template_lib.c

@@ -27,7 +27,6 @@ static bool ima_template_hash_algo_allowed(u8 algo)
 enum data_formats {
 enum data_formats {
 	DATA_FMT_DIGEST = 0,
 	DATA_FMT_DIGEST = 0,
 	DATA_FMT_DIGEST_WITH_ALGO,
 	DATA_FMT_DIGEST_WITH_ALGO,
-	DATA_FMT_EVENT_NAME,
 	DATA_FMT_STRING,
 	DATA_FMT_STRING,
 	DATA_FMT_HEX
 	DATA_FMT_HEX
 };
 };
@@ -37,18 +36,10 @@ static int ima_write_template_field_data(const void *data, const u32 datalen,
 					 struct ima_field_data *field_data)
 					 struct ima_field_data *field_data)
 {
 {
 	u8 *buf, *buf_ptr;
 	u8 *buf, *buf_ptr;
-	u32 buflen;
+	u32 buflen = datalen;
 
 
-	switch (datafmt) {
-	case DATA_FMT_EVENT_NAME:
-		buflen = IMA_EVENT_NAME_LEN_MAX + 1;
-		break;
-	case DATA_FMT_STRING:
+	if (datafmt == DATA_FMT_STRING)
 		buflen = datalen + 1;
 		buflen = datalen + 1;
-		break;
-	default:
-		buflen = datalen;
-	}
 
 
 	buf = kzalloc(buflen, GFP_KERNEL);
 	buf = kzalloc(buflen, GFP_KERNEL);
 	if (!buf)
 	if (!buf)
@@ -63,7 +54,7 @@ static int ima_write_template_field_data(const void *data, const u32 datalen,
 	 * split into multiple template fields (the space is the delimitator
 	 * split into multiple template fields (the space is the delimitator
 	 * character for measurements lists in ASCII format).
 	 * character for measurements lists in ASCII format).
 	 */
 	 */
-	if (datafmt == DATA_FMT_EVENT_NAME || datafmt == DATA_FMT_STRING) {
+	if (datafmt == DATA_FMT_STRING) {
 		for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
 		for (buf_ptr = buf; buf_ptr - buf < datalen; buf_ptr++)
 			if (*buf_ptr == ' ')
 			if (*buf_ptr == ' ')
 				*buf_ptr = '_';
 				*buf_ptr = '_';
@@ -281,8 +272,6 @@ static int ima_eventname_init_common(struct integrity_iint_cache *iint,
 {
 {
 	const char *cur_filename = NULL;
 	const char *cur_filename = NULL;
 	u32 cur_filename_len = 0;
 	u32 cur_filename_len = 0;
-	enum data_formats fmt = size_limit ?
-	    DATA_FMT_EVENT_NAME : DATA_FMT_STRING;
 
 
 	BUG_ON(filename == NULL && file == NULL);
 	BUG_ON(filename == NULL && file == NULL);
 
 
@@ -305,7 +294,7 @@ static int ima_eventname_init_common(struct integrity_iint_cache *iint,
 		cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
 		cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
 out:
 out:
 	return ima_write_template_field_data(cur_filename, cur_filename_len,
 	return ima_write_template_field_data(cur_filename, cur_filename_len,
-					     fmt, field_data);
+					     DATA_FMT_STRING, field_data);
 }
 }
 
 
 /*
 /*