Commit | Line | Data |
---|---|---|
3323eec9 MZ |
1 | /* |
2 | * Copyright (C) 2008 IBM Corporation | |
3 | * | |
4 | * Author: Mimi Zohar <zohar@us.ibm.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License as | |
8 | * published by the Free Software Foundation, version 2 of the | |
9 | * License. | |
10 | * | |
11 | * File: ima_api.c | |
2fe5d6de MZ |
12 | * Implements must_appraise_or_measure, collect_measurement, |
13 | * appraise_measurement, store_measurement and store_template. | |
3323eec9 MZ |
14 | */ |
15 | #include <linux/module.h> | |
5a0e3ad6 | 16 | #include <linux/slab.h> |
2fe5d6de MZ |
17 | #include <linux/file.h> |
18 | #include <linux/fs.h> | |
19 | #include <linux/xattr.h> | |
20 | #include <linux/evm.h> | |
ea593993 | 21 | #include <crypto/hash_info.h> |
3323eec9 | 22 | #include "ima.h" |
2fe5d6de | 23 | |
523979ad | 24 | static const char *IMA_TEMPLATE_NAME = "ima"; |
3323eec9 MZ |
25 | |
26 | /* | |
27 | * ima_store_template - store ima template measurements | |
28 | * | |
29 | * Calculate the hash of a template entry, add the template entry | |
30 | * to an ordered list of measurement entries maintained inside the kernel, | |
31 | * and also update the aggregate integrity value (maintained inside the | |
32 | * configured TPM PCR) over the hashes of the current list of measurement | |
33 | * entries. | |
34 | * | |
35 | * Applications retrieve the current kernel-held measurement list through | |
36 | * the securityfs entries in /sys/kernel/security/ima. The signed aggregate | |
37 | * TPM PCR (called quote) can be retrieved using a TPM user space library | |
38 | * and is used to validate the measurement list. | |
39 | * | |
40 | * Returns 0 on success, error code otherwise | |
41 | */ | |
42 | int ima_store_template(struct ima_template_entry *entry, | |
43 | int violation, struct inode *inode) | |
44 | { | |
45 | const char *op = "add_template_measure"; | |
46 | const char *audit_cause = "hashing_error"; | |
47 | int result; | |
a35c3fb6 DK |
48 | struct { |
49 | struct ima_digest_data hdr; | |
140d8022 | 50 | char digest[TPM_DIGEST_SIZE]; |
a35c3fb6 | 51 | } hash; |
3323eec9 MZ |
52 | |
53 | memset(entry->digest, 0, sizeof(entry->digest)); | |
54 | entry->template_name = IMA_TEMPLATE_NAME; | |
55 | entry->template_len = sizeof(entry->template); | |
56 | ||
57 | if (!violation) { | |
ea593993 DK |
58 | /* this function uses default algo */ |
59 | hash.hdr.algo = HASH_ALGO_SHA1; | |
50af5544 | 60 | result = ima_calc_buffer_hash(&entry->template, |
a35c3fb6 | 61 | entry->template_len, &hash.hdr); |
3323eec9 MZ |
62 | if (result < 0) { |
63 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, | |
64 | entry->template_name, op, | |
65 | audit_cause, result, 0); | |
66 | return result; | |
67 | } | |
a35c3fb6 | 68 | memcpy(entry->digest, hash.hdr.digest, hash.hdr.length); |
3323eec9 MZ |
69 | } |
70 | result = ima_add_template_entry(entry, violation, op, inode); | |
71 | return result; | |
72 | } | |
73 | ||
74 | /* | |
75 | * ima_add_violation - add violation to measurement list. | |
76 | * | |
77 | * Violations are flagged in the measurement list with zero hash values. | |
78 | * By extending the PCR with 0xFF's instead of with zeroes, the PCR | |
79 | * value is invalidated. | |
80 | */ | |
81 | void ima_add_violation(struct inode *inode, const unsigned char *filename, | |
82 | const char *op, const char *cause) | |
83 | { | |
84 | struct ima_template_entry *entry; | |
85 | int violation = 1; | |
86 | int result; | |
87 | ||
88 | /* can overflow, only indicator */ | |
89 | atomic_long_inc(&ima_htable.violations); | |
90 | ||
91 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); | |
92 | if (!entry) { | |
93 | result = -ENOMEM; | |
94 | goto err_out; | |
95 | } | |
96 | memset(&entry->template, 0, sizeof(entry->template)); | |
97 | strncpy(entry->template.file_name, filename, IMA_EVENT_NAME_LEN_MAX); | |
98 | result = ima_store_template(entry, violation, inode); | |
99 | if (result < 0) | |
100 | kfree(entry); | |
101 | err_out: | |
102 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename, | |
103 | op, cause, result, 0); | |
104 | } | |
105 | ||
106 | /** | |
d9d300cd | 107 | * ima_get_action - appraise & measure decision based on policy. |
3323eec9 MZ |
108 | * @inode: pointer to inode to measure |
109 | * @mask: contains the permission mask (MAY_READ, MAY_WRITE, MAY_EXECUTE) | |
16cac49f | 110 | * @function: calling function (FILE_CHECK, BPRM_CHECK, MMAP_CHECK, MODULE_CHECK) |
3323eec9 MZ |
111 | * |
112 | * The policy is defined in terms of keypairs: | |
113 | * subj=, obj=, type=, func=, mask=, fsmagic= | |
114 | * subj,obj, and type: are LSM specific. | |
16cac49f | 115 | * func: FILE_CHECK | BPRM_CHECK | MMAP_CHECK | MODULE_CHECK |
3323eec9 MZ |
116 | * mask: contains the permission mask |
117 | * fsmagic: hex value | |
118 | * | |
2fe5d6de MZ |
119 | * Returns IMA_MEASURE, IMA_APPRAISE mask. |
120 | * | |
121 | */ | |
d9d300cd | 122 | int ima_get_action(struct inode *inode, int mask, int function) |
3323eec9 | 123 | { |
e7c568e0 | 124 | int flags = IMA_MEASURE | IMA_AUDIT | IMA_APPRAISE; |
2fe5d6de MZ |
125 | |
126 | if (!ima_appraise) | |
127 | flags &= ~IMA_APPRAISE; | |
128 | ||
129 | return ima_match_policy(inode, function, mask, flags); | |
130 | } | |
3323eec9 | 131 | |
2fe5d6de MZ |
132 | int ima_must_measure(struct inode *inode, int mask, int function) |
133 | { | |
134 | return ima_match_policy(inode, function, mask, IMA_MEASURE); | |
3323eec9 MZ |
135 | } |
136 | ||
137 | /* | |
138 | * ima_collect_measurement - collect file measurement | |
139 | * | |
140 | * Calculate the file hash, if it doesn't already exist, | |
141 | * storing the measurement and i_version in the iint. | |
142 | * | |
143 | * Must be called with iint->mutex held. | |
144 | * | |
145 | * Return 0 on success, error code otherwise | |
146 | */ | |
f381c272 | 147 | int ima_collect_measurement(struct integrity_iint_cache *iint, |
d3634d0f DK |
148 | struct file *file, |
149 | struct evm_ima_xattr_data **xattr_value, | |
150 | int *xattr_len) | |
3323eec9 | 151 | { |
496ad9aa | 152 | struct inode *inode = file_inode(file); |
2fe5d6de MZ |
153 | const char *filename = file->f_dentry->d_name.name; |
154 | int result = 0; | |
a35c3fb6 DK |
155 | struct { |
156 | struct ima_digest_data hdr; | |
157 | char digest[IMA_MAX_DIGEST_SIZE]; | |
158 | } hash; | |
3323eec9 | 159 | |
d3634d0f DK |
160 | if (xattr_value) |
161 | *xattr_len = ima_read_xattr(file->f_dentry, xattr_value); | |
162 | ||
2fe5d6de | 163 | if (!(iint->flags & IMA_COLLECTED)) { |
496ad9aa | 164 | u64 i_version = file_inode(file)->i_version; |
3323eec9 | 165 | |
c7c8bb23 | 166 | /* use default hash algorithm */ |
a35c3fb6 | 167 | hash.hdr.algo = ima_hash_algo; |
d3634d0f DK |
168 | |
169 | if (xattr_value) | |
a35c3fb6 | 170 | ima_get_hash_algo(*xattr_value, *xattr_len, &hash.hdr); |
d3634d0f | 171 | |
a35c3fb6 | 172 | result = ima_calc_file_hash(file, &hash.hdr); |
2fe5d6de | 173 | if (!result) { |
a35c3fb6 DK |
174 | int length = sizeof(hash.hdr) + hash.hdr.length; |
175 | void *tmpbuf = krealloc(iint->ima_hash, length, | |
176 | GFP_NOFS); | |
177 | if (tmpbuf) { | |
178 | iint->ima_hash = tmpbuf; | |
179 | memcpy(iint->ima_hash, &hash, length); | |
180 | iint->version = i_version; | |
181 | iint->flags |= IMA_COLLECTED; | |
182 | } else | |
183 | result = -ENOMEM; | |
2fe5d6de | 184 | } |
3323eec9 | 185 | } |
2fe5d6de MZ |
186 | if (result) |
187 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, | |
188 | filename, "collect_data", "failed", | |
189 | result, 0); | |
3323eec9 MZ |
190 | return result; |
191 | } | |
192 | ||
193 | /* | |
194 | * ima_store_measurement - store file measurement | |
195 | * | |
196 | * Create an "ima" template and then store the template by calling | |
197 | * ima_store_template. | |
198 | * | |
199 | * We only get here if the inode has not already been measured, | |
200 | * but the measurement could already exist: | |
201 | * - multiple copies of the same file on either the same or | |
202 | * different filesystems. | |
203 | * - the inode was previously flushed as well as the iint info, | |
204 | * containing the hashing info. | |
205 | * | |
206 | * Must be called with iint->mutex held. | |
207 | */ | |
f381c272 MZ |
208 | void ima_store_measurement(struct integrity_iint_cache *iint, |
209 | struct file *file, const unsigned char *filename) | |
3323eec9 MZ |
210 | { |
211 | const char *op = "add_template_measure"; | |
212 | const char *audit_cause = "ENOMEM"; | |
213 | int result = -ENOMEM; | |
496ad9aa | 214 | struct inode *inode = file_inode(file); |
3323eec9 MZ |
215 | struct ima_template_entry *entry; |
216 | int violation = 0; | |
217 | ||
2fe5d6de MZ |
218 | if (iint->flags & IMA_MEASURED) |
219 | return; | |
220 | ||
3323eec9 MZ |
221 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
222 | if (!entry) { | |
223 | integrity_audit_msg(AUDIT_INTEGRITY_PCR, inode, filename, | |
224 | op, audit_cause, result, 0); | |
225 | return; | |
226 | } | |
227 | memset(&entry->template, 0, sizeof(entry->template)); | |
a35c3fb6 DK |
228 | if (iint->ima_hash->algo != ima_hash_algo) { |
229 | struct { | |
230 | struct ima_digest_data hdr; | |
231 | char digest[IMA_MAX_DIGEST_SIZE]; | |
232 | } hash; | |
c7c8bb23 | 233 | |
a35c3fb6 DK |
234 | hash.hdr.algo = ima_hash_algo; |
235 | result = ima_calc_file_hash(file, &hash.hdr); | |
c7c8bb23 DK |
236 | if (result) |
237 | integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, | |
238 | filename, "collect_data", "failed", | |
239 | result, 0); | |
240 | else | |
a35c3fb6 DK |
241 | memcpy(entry->template.digest, hash.hdr.digest, |
242 | hash.hdr.length); | |
c7c8bb23 | 243 | } else |
a35c3fb6 DK |
244 | memcpy(entry->template.digest, iint->ima_hash->digest, |
245 | iint->ima_hash->length); | |
08e1b76a MZ |
246 | strcpy(entry->template.file_name, |
247 | (strlen(filename) > IMA_EVENT_NAME_LEN_MAX) ? | |
248 | file->f_dentry->d_name.name : filename); | |
3323eec9 MZ |
249 | |
250 | result = ima_store_template(entry, violation, inode); | |
45fae749 | 251 | if (!result || result == -EEXIST) |
3323eec9 | 252 | iint->flags |= IMA_MEASURED; |
45fae749 | 253 | if (result < 0) |
3323eec9 MZ |
254 | kfree(entry); |
255 | } | |
e7c568e0 PM |
256 | |
257 | void ima_audit_measurement(struct integrity_iint_cache *iint, | |
258 | const unsigned char *filename) | |
259 | { | |
260 | struct audit_buffer *ab; | |
a35c3fb6 | 261 | char hash[(iint->ima_hash->length * 2) + 1]; |
e7c568e0 PM |
262 | int i; |
263 | ||
264 | if (iint->flags & IMA_AUDITED) | |
265 | return; | |
266 | ||
a35c3fb6 DK |
267 | for (i = 0; i < iint->ima_hash->length; i++) |
268 | hex_byte_pack(hash + (i * 2), iint->ima_hash->digest[i]); | |
e7c568e0 PM |
269 | hash[i * 2] = '\0'; |
270 | ||
271 | ab = audit_log_start(current->audit_context, GFP_KERNEL, | |
272 | AUDIT_INTEGRITY_RULE); | |
273 | if (!ab) | |
274 | return; | |
275 | ||
276 | audit_log_format(ab, "file="); | |
277 | audit_log_untrustedstring(ab, filename); | |
278 | audit_log_format(ab, " hash="); | |
279 | audit_log_untrustedstring(ab, hash); | |
280 | ||
281 | audit_log_task_info(ab, current); | |
282 | audit_log_end(ab); | |
283 | ||
284 | iint->flags |= IMA_AUDITED; | |
285 | } | |
ea1046d4 DK |
286 | |
287 | const char *ima_d_path(struct path *path, char **pathbuf) | |
288 | { | |
289 | char *pathname = NULL; | |
290 | ||
291 | /* We will allow 11 spaces for ' (deleted)' to be appended */ | |
292 | *pathbuf = kmalloc(PATH_MAX + 11, GFP_KERNEL); | |
293 | if (*pathbuf) { | |
294 | pathname = d_path(path, *pathbuf, PATH_MAX + 11); | |
295 | if (IS_ERR(pathname)) { | |
296 | kfree(*pathbuf); | |
297 | *pathbuf = NULL; | |
298 | pathname = NULL; | |
299 | } | |
300 | } | |
301 | return pathname; | |
302 | } |