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