Skip to content

Commit 7bc5f44

Browse files
Roberto SassuMimi Zohar
authored andcommitted
ima: define new function ima_alloc_init_template() to API
Instead of allocating and initializing the template entry from multiple places (eg. boot aggregate, violation, and regular measurements), this patch defines a new function called ima_alloc_init_template(). The new function allocates and initializes the measurement entry with the inode digest and the filename. In respect to the current behavior, it truncates the file name passed in the 'filename' argument if the latter's size is greater than 255 bytes and the passed file descriptor is NULL. Changelog: - initialize 'hash' variable for non TPM case - Mimi - conform to expectation for 'iint' to be defined as a pointer. - Mimi - add missing 'file' dependency for recalculating file hash. - Mimi Signed-off-by: Roberto Sassu <roberto.sassu@polito.it> Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
1 parent 9803d41 commit 7bc5f44

3 files changed

Lines changed: 76 additions & 39 deletions

File tree

‎security/integrity/ima/ima.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,
107107
const unsigned char *filename);
108108
void ima_audit_measurement(struct integrity_iint_cache *iint,
109109
const unsigned char *filename);
110+
int ima_alloc_init_template(struct integrity_iint_cache *iint,
111+
struct file *file, const unsigned char *filename,
112+
struct ima_template_entry **entry);
110113
int ima_store_template(struct ima_template_entry *entry, int violation,
111114
struct inode *inode, const unsigned char *filename);
112115
void ima_template_show(struct seq_file *m, void *e, enum ima_show_type show);

‎security/integrity/ima/ima_api.c‎

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,62 @@
2323

2424
static const char *IMA_TEMPLATE_NAME = "ima";
2525

26+
/*
27+
* ima_alloc_init_template - create and initialize a new template entry
28+
*/
29+
int ima_alloc_init_template(struct integrity_iint_cache *iint,
30+
struct file *file, const unsigned char *filename,
31+
struct ima_template_entry **entry)
32+
{
33+
struct ima_template_entry *e;
34+
int result = 0;
35+
36+
e = kzalloc(sizeof(**entry), GFP_NOFS);
37+
if (!e)
38+
return -ENOMEM;
39+
40+
memset(&(e)->template, 0, sizeof(e->template));
41+
if (!iint) /* IMA measurement violation entry */
42+
goto out;
43+
44+
if (iint->ima_hash->algo != ima_hash_algo) {
45+
struct inode *inode;
46+
struct {
47+
struct ima_digest_data hdr;
48+
char digest[IMA_MAX_DIGEST_SIZE];
49+
} hash;
50+
51+
if (!file) {
52+
result = -EINVAL;
53+
goto out_free;
54+
}
55+
56+
inode = file_inode(file);
57+
hash.hdr.algo = ima_hash_algo;
58+
hash.hdr.length = SHA1_DIGEST_SIZE;
59+
result = ima_calc_file_hash(file, &hash.hdr);
60+
if (result) {
61+
integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
62+
filename, "collect_data",
63+
"failed", result, 0);
64+
goto out_free;
65+
} else
66+
memcpy(e->template.digest, hash.hdr.digest,
67+
hash.hdr.length);
68+
} else
69+
memcpy(e->template.digest, iint->ima_hash->digest,
70+
iint->ima_hash->length);
71+
out:
72+
strcpy(e->template.file_name,
73+
(strlen(filename) > IMA_EVENT_NAME_LEN_MAX && file != NULL) ?
74+
file->f_dentry->d_name.name : filename);
75+
*entry = e;
76+
return 0;
77+
out_free:
78+
kfree(e);
79+
return result;
80+
}
81+
2682
/*
2783
* ima_store_template - store ima template measurements
2884
*
@@ -90,13 +146,11 @@ void ima_add_violation(struct file *file, const unsigned char *filename,
90146
/* can overflow, only indicator */
91147
atomic_long_inc(&ima_htable.violations);
92148

93-
entry = kmalloc(sizeof(*entry), GFP_KERNEL);
94-
if (!entry) {
149+
result = ima_alloc_init_template(NULL, file, filename, &entry);
150+
if (result < 0) {
95151
result = -ENOMEM;
96152
goto err_out;
97153
}
98-
memset(&entry->template, 0, sizeof(entry->template));
99-
strncpy(entry->template.file_name, filename, IMA_EVENT_NAME_LEN_MAX);
100154
result = ima_store_template(entry, violation, inode, filename);
101155
if (result < 0)
102156
kfree(entry);
@@ -220,34 +274,12 @@ void ima_store_measurement(struct integrity_iint_cache *iint,
220274
if (iint->flags & IMA_MEASURED)
221275
return;
222276

223-
entry = kmalloc(sizeof(*entry), GFP_KERNEL);
224-
if (!entry) {
277+
result = ima_alloc_init_template(iint, file, filename, &entry);
278+
if (result < 0) {
225279
integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename,
226280
op, audit_cause, result, 0);
227281
return;
228282
}
229-
memset(&entry->template, 0, sizeof(entry->template));
230-
if (iint->ima_hash->algo != ima_hash_algo) {
231-
struct {
232-
struct ima_digest_data hdr;
233-
char digest[IMA_MAX_DIGEST_SIZE];
234-
} hash;
235-
236-
hash.hdr.algo = ima_hash_algo;
237-
result = ima_calc_file_hash(file, &hash.hdr);
238-
if (result)
239-
integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
240-
filename, "collect_data", "failed",
241-
result, 0);
242-
else
243-
memcpy(entry->template.digest, hash.hdr.digest,
244-
hash.hdr.length);
245-
} else
246-
memcpy(entry->template.digest, iint->ima_hash->digest,
247-
iint->ima_hash->length);
248-
strcpy(entry->template.file_name,
249-
(strlen(filename) > IMA_EVENT_NAME_LEN_MAX) ?
250-
file->f_dentry->d_name.name : filename);
251283

252284
result = ima_store_template(entry, violation, inode, filename);
253285
if (!result || result == -EEXIST)

‎security/integrity/ima/ima_init.c‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,36 @@ int ima_used_chip;
4343
static void __init ima_add_boot_aggregate(void)
4444
{
4545
struct ima_template_entry *entry;
46+
struct integrity_iint_cache tmp_iint, *iint = &tmp_iint;
4647
const char *op = "add_boot_aggregate";
4748
const char *audit_cause = "ENOMEM";
4849
int result = -ENOMEM;
49-
int violation = 1;
50+
int violation = 0;
5051
struct {
5152
struct ima_digest_data hdr;
5253
char digest[TPM_DIGEST_SIZE];
5354
} hash;
5455

55-
entry = kmalloc(sizeof(*entry), GFP_KERNEL);
56-
if (!entry)
57-
goto err_out;
56+
memset(iint, 0, sizeof(*iint));
57+
memset(&hash, 0, sizeof(hash));
58+
iint->ima_hash = &hash.hdr;
59+
iint->ima_hash->algo = HASH_ALGO_SHA1;
60+
iint->ima_hash->length = SHA1_DIGEST_SIZE;
5861

59-
memset(&entry->template, 0, sizeof(entry->template));
60-
strncpy(entry->template.file_name, boot_aggregate_name,
61-
IMA_EVENT_NAME_LEN_MAX);
6262
if (ima_used_chip) {
63-
violation = 0;
64-
hash.hdr.algo = HASH_ALGO_SHA1;
6563
result = ima_calc_boot_aggregate(&hash.hdr);
6664
if (result < 0) {
6765
audit_cause = "hashing_error";
6866
kfree(entry);
6967
goto err_out;
7068
}
71-
memcpy(entry->template.digest, hash.hdr.digest,
72-
hash.hdr.length);
7369
}
70+
71+
result = ima_alloc_init_template(iint, NULL, boot_aggregate_name,
72+
&entry);
73+
if (result < 0)
74+
return;
75+
7476
result = ima_store_template(entry, violation, NULL,
7577
boot_aggregate_name);
7678
if (result < 0)

0 commit comments

Comments
 (0)