Skip to content

Commit

Permalink
ima: allocate field pointers array on demand in template_desc_init_fi…
Browse files Browse the repository at this point in the history
…elds()

The allocation of a field pointers array is moved at the end of
template_desc_init_fields() and done only if the value of the 'fields'
and 'num_fields' parameters is not NULL. For just validating a template
format string, retrieved template field pointers are placed in a temporary
array.

Changelog:
 - v3:
   - do not check in this patch if 'fields' and 'num_fields' are NULL
     (suggested by Mimi Zohar)

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 Oct 13, 2014
1 parent 9f3166b commit 1bd7fac
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions security/integrity/ima/ima_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,50 +117,42 @@ static int template_desc_init_fields(const char *template_fmt,
int *num_fields)
{
const char *template_fmt_ptr;
struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
int template_num_fields = template_fmt_size(template_fmt);
int i, len, result = 0;
int i, len;

if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
pr_err("format string '%s' contains too many fields\n",
template_fmt);
return -EINVAL;
}

*fields = kzalloc(template_num_fields * sizeof(*fields), GFP_KERNEL);
if (*fields == NULL) {
result = -ENOMEM;
goto out;
}

for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
i++, template_fmt_ptr += len + 1) {
char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
struct ima_template_field *f;

len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
pr_err("Invalid field with length %d\n", len);
result = -EINVAL;
goto out;
return -EINVAL;
}

memcpy(tmp_field_id, template_fmt_ptr, len);
tmp_field_id[len] = '\0';
f = lookup_template_field(tmp_field_id);
if (!f) {
found_fields[i] = lookup_template_field(tmp_field_id);
if (!found_fields[i]) {
pr_err("field '%s' not found\n", tmp_field_id);
result = -ENOENT;
goto out;
return -ENOENT;
}
(*fields)[i] = f;
}

*fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
if (*fields == NULL)
return -ENOMEM;

memcpy(*fields, found_fields, i * sizeof(*fields));
*num_fields = i;
out:
if (result < 0) {
kfree(*fields);
*fields = NULL;
}
return result;
return 0;
}

struct ima_template_desc *ima_template_desc_current(void)
Expand Down

0 comments on commit 1bd7fac

Please sign in to comment.