diff options
| author | Roberto Sassu <roberto.sassu@polito.it> | 2013-12-02 13:40:34 -0500 |
|---|---|---|
| committer | Mimi Zohar <zohar@linux.vnet.ibm.com> | 2013-12-02 20:46:56 -0500 |
| commit | a7ed7c60e14df5b986f93549717235b882643e7e (patch) | |
| tree | 7e615a0664541d91f99c5875164b335b74fd8d8d | |
| parent | 09ae6345721afbb7cf3e0920209b140cbe7bff0d (diff) | |
ima: properly free ima_template_entry structures
The new templates management mechanism records information associated
to an event into an array of 'ima_field_data' structures and makes it
available through the 'template_data' field of the 'ima_template_entry'
structure (the element of the measurements list created by IMA).
Since 'ima_field_data' contains dynamically allocated data (which length
varies depending on the data associated to a selected template field),
it is not enough to just free the memory reserved for a
'ima_template_entry' structure if something goes wrong.
This patch creates the new function ima_free_template_entry() which
walks the array of 'ima_field_data' structures, frees the memory
referenced by the 'data' pointer and finally the space reserved for
the 'ima_template_entry' structure. Further, it replaces existing kfree()
that have a pointer to an 'ima_template_entry' structure as argument
with calls to the new function.
Fixes: a71dc65: ima: switch to new template management mechanism
Signed-off-by: Roberto Sassu <roberto.sassu@polito.it>
Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
| -rw-r--r-- | security/integrity/ima/ima.h | 1 | ||||
| -rw-r--r-- | security/integrity/ima/ima_api.c | 21 | ||||
| -rw-r--r-- | security/integrity/ima/ima_init.c | 2 |
3 files changed, 19 insertions, 5 deletions
diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h index 9636e17c9f5d..0356e1d437ca 100644 --- a/security/integrity/ima/ima.h +++ b/security/integrity/ima/ima.h | |||
| @@ -148,6 +148,7 @@ int ima_alloc_init_template(struct integrity_iint_cache *iint, | |||
| 148 | int xattr_len, struct ima_template_entry **entry); | 148 | int xattr_len, struct ima_template_entry **entry); |
| 149 | int ima_store_template(struct ima_template_entry *entry, int violation, | 149 | int ima_store_template(struct ima_template_entry *entry, int violation, |
| 150 | struct inode *inode, const unsigned char *filename); | 150 | struct inode *inode, const unsigned char *filename); |
| 151 | void ima_free_template_entry(struct ima_template_entry *entry); | ||
| 151 | const char *ima_d_path(struct path *path, char **pathbuf); | 152 | const char *ima_d_path(struct path *path, char **pathbuf); |
| 152 | 153 | ||
| 153 | /* rbtree tree calls to lookup, insert, delete | 154 | /* rbtree tree calls to lookup, insert, delete |
diff --git a/security/integrity/ima/ima_api.c b/security/integrity/ima/ima_api.c index 80374842fe0b..c38bbce8c6a6 100644 --- a/security/integrity/ima/ima_api.c +++ b/security/integrity/ima/ima_api.c | |||
| @@ -22,6 +22,19 @@ | |||
| 22 | #include "ima.h" | 22 | #include "ima.h" |
| 23 | 23 | ||
| 24 | /* | 24 | /* |
| 25 | * ima_free_template_entry - free an existing template entry | ||
| 26 | */ | ||
| 27 | void ima_free_template_entry(struct ima_template_entry *entry) | ||
| 28 | { | ||
| 29 | int i; | ||
| 30 | |||
| 31 | for (i = 0; i < entry->template_desc->num_fields; i++) | ||
| 32 | kfree(entry->template_data[i].data); | ||
| 33 | |||
| 34 | kfree(entry); | ||
| 35 | } | ||
| 36 | |||
| 37 | /* | ||
| 25 | * ima_alloc_init_template - create and initialize a new template entry | 38 | * ima_alloc_init_template - create and initialize a new template entry |
| 26 | */ | 39 | */ |
| 27 | int ima_alloc_init_template(struct integrity_iint_cache *iint, | 40 | int ima_alloc_init_template(struct integrity_iint_cache *iint, |
| @@ -37,6 +50,7 @@ int ima_alloc_init_template(struct integrity_iint_cache *iint, | |||
| 37 | if (!*entry) | 50 | if (!*entry) |
| 38 | return -ENOMEM; | 51 | return -ENOMEM; |
| 39 | 52 | ||
| 53 | (*entry)->template_desc = template_desc; | ||
| 40 | for (i = 0; i < template_desc->num_fields; i++) { | 54 | for (i = 0; i < template_desc->num_fields; i++) { |
| 41 | struct ima_template_field *field = template_desc->fields[i]; | 55 | struct ima_template_field *field = template_desc->fields[i]; |
| 42 | u32 len; | 56 | u32 len; |
| @@ -51,10 +65,9 @@ int ima_alloc_init_template(struct integrity_iint_cache *iint, | |||
| 51 | (*entry)->template_data_len += sizeof(len); | 65 | (*entry)->template_data_len += sizeof(len); |
| 52 | (*entry)->template_data_len += len; | 66 | (*entry)->template_data_len += len; |
| 53 | } | 67 | } |
| 54 | (*entry)->template_desc = template_desc; | ||
| 55 | return 0; | 68 | return 0; |
| 56 | out: | 69 | out: |
| 57 | kfree(*entry); | 70 | ima_free_template_entry(*entry); |
| 58 | *entry = NULL; | 71 | *entry = NULL; |
| 59 | return result; | 72 | return result; |
| 60 | } | 73 | } |
| @@ -134,7 +147,7 @@ void ima_add_violation(struct file *file, const unsigned char *filename, | |||
| 134 | } | 147 | } |
| 135 | result = ima_store_template(entry, violation, inode, filename); | 148 | result = ima_store_template(entry, violation, inode, filename); |
| 136 | if (result < 0) | 149 | if (result < 0) |
| 137 | kfree(entry); | 150 | ima_free_template_entry(entry); |
| 138 | err_out: | 151 | err_out: |
| 139 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename, | 152 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename, |
| 140 | op, cause, result, 0); | 153 | op, cause, result, 0); |
| @@ -269,7 +282,7 @@ void ima_store_measurement(struct integrity_iint_cache *iint, | |||
| 269 | if (!result || result == -EEXIST) | 282 | if (!result || result == -EEXIST) |
| 270 | iint->flags |= IMA_MEASURED; | 283 | iint->flags |= IMA_MEASURED; |
| 271 | if (result < 0) | 284 | if (result < 0) |
| 272 | kfree(entry); | 285 | ima_free_template_entry(entry); |
| 273 | } | 286 | } |
| 274 | 287 | ||
| 275 | void ima_audit_measurement(struct integrity_iint_cache *iint, | 288 | void ima_audit_measurement(struct integrity_iint_cache *iint, |
diff --git a/security/integrity/ima/ima_init.c b/security/integrity/ima/ima_init.c index 76b8e2c4fd38..37122768554a 100644 --- a/security/integrity/ima/ima_init.c +++ b/security/integrity/ima/ima_init.c | |||
| @@ -75,7 +75,7 @@ static void __init ima_add_boot_aggregate(void) | |||
| 75 | result = ima_store_template(entry, violation, NULL, | 75 | result = ima_store_template(entry, violation, NULL, |
| 76 | boot_aggregate_name); | 76 | boot_aggregate_name); |
| 77 | if (result < 0) | 77 | if (result < 0) |
| 78 | kfree(entry); | 78 | ima_free_template_entry(entry); |
| 79 | return; | 79 | return; |
| 80 | err_out: | 80 | err_out: |
| 81 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op, | 81 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, NULL, boot_aggregate_name, op, |
