Skip to content

Commit

Permalink
ima: reduce memory usage when a template containing the n field is used
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Roberto Sassu authored and Mimi Zohar committed Mar 7, 2014
1 parent c019e30 commit e3b64c2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
11 changes: 9 additions & 2 deletions security/integrity/ima/ima_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,22 @@ static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
return rc;

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) {
rc = crypto_shash_update(&desc.shash,
(const u8 *) &field_data[i].len,
sizeof(field_data[i].len));
if (rc)
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)
break;
}
Expand Down
19 changes: 4 additions & 15 deletions security/integrity/ima/ima_template_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ static bool ima_template_hash_algo_allowed(u8 algo)
enum data_formats {
DATA_FMT_DIGEST = 0,
DATA_FMT_DIGEST_WITH_ALGO,
DATA_FMT_EVENT_NAME,
DATA_FMT_STRING,
DATA_FMT_HEX
};
Expand All @@ -37,18 +36,10 @@ static int ima_write_template_field_data(const void *data, const u32 datalen,
struct ima_field_data *field_data)
{
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;
break;
default:
buflen = datalen;
}

buf = kzalloc(buflen, GFP_KERNEL);
if (!buf)
Expand All @@ -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
* 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++)
if (*buf_ptr == ' ')
*buf_ptr = '_';
Expand Down Expand Up @@ -281,8 +272,6 @@ static int ima_eventname_init_common(struct integrity_iint_cache *iint,
{
const char *cur_filename = NULL;
u32 cur_filename_len = 0;
enum data_formats fmt = size_limit ?
DATA_FMT_EVENT_NAME : DATA_FMT_STRING;

BUG_ON(filename == NULL && file == NULL);

Expand All @@ -305,7 +294,7 @@ static int ima_eventname_init_common(struct integrity_iint_cache *iint,
cur_filename_len = IMA_EVENT_NAME_LEN_MAX;
out:
return ima_write_template_field_data(cur_filename, cur_filename_len,
fmt, field_data);
DATA_FMT_STRING, field_data);
}

/*
Expand Down

0 comments on commit e3b64c2

Please sign in to comment.