Skip to content

Commit 1bd7fac

Browse files
Roberto SassuMimi Zohar
authored andcommitted
ima: allocate field pointers array on demand in template_desc_init_fields()
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 <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent 9f3166b commit 1bd7fac

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

security/integrity/ima/ima_template.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,50 +117,42 @@ static int template_desc_init_fields(const char *template_fmt,
117117
int *num_fields)
118118
{
119119
const char *template_fmt_ptr;
120+
struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
120121
int template_num_fields = template_fmt_size(template_fmt);
121-
int i, len, result = 0;
122+
int i, len;
122123

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

129-
*fields = kzalloc(template_num_fields * sizeof(*fields), GFP_KERNEL);
130-
if (*fields == NULL) {
131-
result = -ENOMEM;
132-
goto out;
133-
}
134-
135130
for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
136131
i++, template_fmt_ptr += len + 1) {
137132
char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
138-
struct ima_template_field *f;
139133

140134
len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
141135
if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
142136
pr_err("Invalid field with length %d\n", len);
143-
result = -EINVAL;
144-
goto out;
137+
return -EINVAL;
145138
}
146139

147140
memcpy(tmp_field_id, template_fmt_ptr, len);
148141
tmp_field_id[len] = '\0';
149-
f = lookup_template_field(tmp_field_id);
150-
if (!f) {
142+
found_fields[i] = lookup_template_field(tmp_field_id);
143+
if (!found_fields[i]) {
151144
pr_err("field '%s' not found\n", tmp_field_id);
152-
result = -ENOENT;
153-
goto out;
145+
return -ENOENT;
154146
}
155-
(*fields)[i] = f;
156147
}
148+
149+
*fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
150+
if (*fields == NULL)
151+
return -ENOMEM;
152+
153+
memcpy(*fields, found_fields, i * sizeof(*fields));
157154
*num_fields = i;
158-
out:
159-
if (result < 0) {
160-
kfree(*fields);
161-
*fields = NULL;
162-
}
163-
return result;
155+
return 0;
164156
}
165157

166158
struct ima_template_desc *ima_template_desc_current(void)

0 commit comments

Comments
 (0)