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