aboutsummaryrefslogtreecommitdiffstats
path: root/security/integrity
diff options
context:
space:
mode:
authorRoberto Sassu <roberto.sassu@polito.it>2014-10-13 08:08:41 -0400
committerMimi Zohar <zohar@linux.vnet.ibm.com>2014-10-13 08:39:02 -0400
commit1bd7face74391ddfc568b3e638f156da1ed77aa6 (patch)
tree9bf69f4d988df8c89e7405b6a52c17924536abbb /security/integrity
parent9f3166b8ca3b89c27b9f1c9039d3662ab7812cfa (diff)
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 <roberto.sassu@polito.it> Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Diffstat (limited to 'security/integrity')
-rw-r--r--security/integrity/ima/ima_template.c34
1 files changed, 13 insertions, 21 deletions
diff --git a/security/integrity/ima/ima_template.c b/security/integrity/ima/ima_template.c
index d93a58e603df..65117ba06809 100644
--- a/security/integrity/ima/ima_template.c
+++ b/security/integrity/ima/ima_template.c
@@ -117,8 +117,9 @@ static int template_desc_init_fields(const char *template_fmt,
117 int *num_fields) 117 int *num_fields)
118{ 118{
119 const char *template_fmt_ptr; 119 const char *template_fmt_ptr;
120 struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
120 int template_num_fields = template_fmt_size(template_fmt); 121 int template_num_fields = template_fmt_size(template_fmt);
121 int i, len, result = 0; 122 int i, len;
122 123
123 if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) { 124 if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
124 pr_err("format string '%s' contains too many fields\n", 125 pr_err("format string '%s' contains too many fields\n",
@@ -126,41 +127,32 @@ static int template_desc_init_fields(const char *template_fmt,
126 return -EINVAL; 127 return -EINVAL;
127 } 128 }
128 129
129 *fields = kzalloc(template_num_fields * sizeof(*fields), GFP_KERNEL);
130 if (*fields == NULL) {
131 result = -ENOMEM;
132 goto out;
133 }
134
135 for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields; 130 for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
136 i++, template_fmt_ptr += len + 1) { 131 i++, template_fmt_ptr += len + 1) {
137 char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1]; 132 char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
138 struct ima_template_field *f;
139 133
140 len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr; 134 len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
141 if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) { 135 if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
142 pr_err("Invalid field with length %d\n", len); 136 pr_err("Invalid field with length %d\n", len);
143 result = -EINVAL; 137 return -EINVAL;
144 goto out;
145 } 138 }
146 139
147 memcpy(tmp_field_id, template_fmt_ptr, len); 140 memcpy(tmp_field_id, template_fmt_ptr, len);
148 tmp_field_id[len] = '\0'; 141 tmp_field_id[len] = '\0';
149 f = lookup_template_field(tmp_field_id); 142 found_fields[i] = lookup_template_field(tmp_field_id);
150 if (!f) { 143 if (!found_fields[i]) {
151 pr_err("field '%s' not found\n", tmp_field_id); 144 pr_err("field '%s' not found\n", tmp_field_id);
152 result = -ENOENT; 145 return -ENOENT;
153 goto out;
154 } 146 }
155 (*fields)[i] = f;
156 } 147 }
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));
157 *num_fields = i; 154 *num_fields = i;
158out: 155 return 0;
159 if (result < 0) {
160 kfree(*fields);
161 *fields = NULL;
162 }
163 return result;
164} 156}
165 157
166struct ima_template_desc *ima_template_desc_current(void) 158struct ima_template_desc *ima_template_desc_current(void)