GNU Linux-libre 4.4.288-gnu1
[releases.git] / security / integrity / ima / ima_main.c
1 /*
2  * Copyright (C) 2005,2006,2007,2008 IBM Corporation
3  *
4  * Authors:
5  * Reiner Sailer <sailer@watson.ibm.com>
6  * Serge Hallyn <serue@us.ibm.com>
7  * Kylene Hall <kylene@us.ibm.com>
8  * Mimi Zohar <zohar@us.ibm.com>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation, version 2 of the
13  * License.
14  *
15  * File: ima_main.c
16  *      implements the IMA hooks: ima_bprm_check, ima_file_mmap,
17  *      and ima_file_check.
18  */
19
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/module.h>
23 #include <linux/file.h>
24 #include <linux/binfmts.h>
25 #include <linux/mount.h>
26 #include <linux/mman.h>
27 #include <linux/slab.h>
28 #include <linux/xattr.h>
29 #include <linux/ima.h>
30 #include <crypto/hash_info.h>
31
32 #include "ima.h"
33
34 int ima_initialized;
35
36 #ifdef CONFIG_IMA_APPRAISE
37 int ima_appraise = IMA_APPRAISE_ENFORCE;
38 #else
39 int ima_appraise;
40 #endif
41
42 int ima_hash_algo = HASH_ALGO_SHA1;
43 static int hash_setup_done;
44
45 static int __init hash_setup(char *str)
46 {
47         struct ima_template_desc *template_desc = ima_template_desc_current();
48         int i;
49
50         if (hash_setup_done)
51                 return 1;
52
53         if (strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) == 0) {
54                 if (strncmp(str, "sha1", 4) == 0)
55                         ima_hash_algo = HASH_ALGO_SHA1;
56                 else if (strncmp(str, "md5", 3) == 0)
57                         ima_hash_algo = HASH_ALGO_MD5;
58                 else
59                         return 1;
60                 goto out;
61         }
62
63         for (i = 0; i < HASH_ALGO__LAST; i++) {
64                 if (strcmp(str, hash_algo_name[i]) == 0) {
65                         ima_hash_algo = i;
66                         break;
67                 }
68         }
69         if (i == HASH_ALGO__LAST)
70                 return 1;
71 out:
72         hash_setup_done = 1;
73         return 1;
74 }
75 __setup("ima_hash=", hash_setup);
76
77 /*
78  * ima_rdwr_violation_check
79  *
80  * Only invalidate the PCR for measured files:
81  *      - Opening a file for write when already open for read,
82  *        results in a time of measure, time of use (ToMToU) error.
83  *      - Opening a file for read when already open for write,
84  *        could result in a file measurement error.
85  *
86  */
87 static void ima_rdwr_violation_check(struct file *file,
88                                      struct integrity_iint_cache *iint,
89                                      int must_measure,
90                                      char **pathbuf,
91                                      const char **pathname)
92 {
93         struct inode *inode = file_inode(file);
94         fmode_t mode = file->f_mode;
95         bool send_tomtou = false, send_writers = false;
96
97         if (mode & FMODE_WRITE) {
98                 if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
99                         if (!iint)
100                                 iint = integrity_iint_find(inode);
101                         /* IMA_MEASURE is set from reader side */
102                         if (iint && (iint->flags & IMA_MEASURE))
103                                 send_tomtou = true;
104                 }
105         } else {
106                 if ((atomic_read(&inode->i_writecount) > 0) && must_measure)
107                         send_writers = true;
108         }
109
110         if (!send_tomtou && !send_writers)
111                 return;
112
113         *pathname = ima_d_path(&file->f_path, pathbuf);
114
115         if (send_tomtou)
116                 ima_add_violation(file, *pathname, iint,
117                                   "invalid_pcr", "ToMToU");
118         if (send_writers)
119                 ima_add_violation(file, *pathname, iint,
120                                   "invalid_pcr", "open_writers");
121 }
122
123 static void ima_check_last_writer(struct integrity_iint_cache *iint,
124                                   struct inode *inode, struct file *file)
125 {
126         fmode_t mode = file->f_mode;
127
128         if (!(mode & FMODE_WRITE))
129                 return;
130
131         mutex_lock(&inode->i_mutex);
132         if (atomic_read(&inode->i_writecount) == 1) {
133                 if ((iint->version != inode->i_version) ||
134                     (iint->flags & IMA_NEW_FILE)) {
135                         iint->flags &= ~(IMA_DONE_MASK | IMA_NEW_FILE);
136                         if (iint->flags & IMA_APPRAISE)
137                                 ima_update_xattr(iint, file);
138                 }
139         }
140         mutex_unlock(&inode->i_mutex);
141 }
142
143 /**
144  * ima_file_free - called on __fput()
145  * @file: pointer to file structure being freed
146  *
147  * Flag files that changed, based on i_version
148  */
149 void ima_file_free(struct file *file)
150 {
151         struct inode *inode = file_inode(file);
152         struct integrity_iint_cache *iint;
153
154         if (!ima_policy_flag || !S_ISREG(inode->i_mode))
155                 return;
156
157         iint = integrity_iint_find(inode);
158         if (!iint)
159                 return;
160
161         ima_check_last_writer(iint, inode, file);
162 }
163
164 static int process_measurement(struct file *file, int mask, int function,
165                                int opened)
166 {
167         struct inode *inode = file_inode(file);
168         struct integrity_iint_cache *iint = NULL;
169         struct ima_template_desc *template_desc;
170         char *pathbuf = NULL;
171         const char *pathname = NULL;
172         int rc = -ENOMEM, action, must_appraise;
173         struct evm_ima_xattr_data *xattr_value = NULL, **xattr_ptr = NULL;
174         int xattr_len = 0;
175         bool violation_check;
176
177         if (!ima_policy_flag || !S_ISREG(inode->i_mode))
178                 return 0;
179
180         /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
181          * bitmask based on the appraise/audit/measurement policy.
182          * Included is the appraise submask.
183          */
184         action = ima_get_action(inode, mask, function);
185         violation_check = ((function == FILE_CHECK || function == MMAP_CHECK) &&
186                            (ima_policy_flag & IMA_MEASURE));
187         if (!action && !violation_check)
188                 return 0;
189
190         must_appraise = action & IMA_APPRAISE;
191
192         /*  Is the appraise rule hook specific?  */
193         if (action & IMA_FILE_APPRAISE)
194                 function = FILE_CHECK;
195
196         mutex_lock(&inode->i_mutex);
197
198         if (action) {
199                 iint = integrity_inode_get(inode);
200                 if (!iint)
201                         goto out;
202         }
203
204         if (violation_check) {
205                 ima_rdwr_violation_check(file, iint, action & IMA_MEASURE,
206                                          &pathbuf, &pathname);
207                 if (!action) {
208                         rc = 0;
209                         goto out_free;
210                 }
211         }
212
213         /* Determine if already appraised/measured based on bitmask
214          * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
215          *  IMA_AUDIT, IMA_AUDITED)
216          */
217         iint->flags |= action;
218         action &= IMA_DO_MASK;
219         action &= ~((iint->flags & IMA_DONE_MASK) >> 1);
220
221         /* Nothing to do, just return existing appraised status */
222         if (!action) {
223                 if (must_appraise)
224                         rc = ima_get_cache_status(iint, function);
225                 goto out_digsig;
226         }
227
228         template_desc = ima_template_desc_current();
229         if ((action & IMA_APPRAISE_SUBMASK) ||
230                     strcmp(template_desc->name, IMA_TEMPLATE_IMA_NAME) != 0)
231                 xattr_ptr = &xattr_value;
232
233         rc = ima_collect_measurement(iint, file, xattr_ptr, &xattr_len);
234         if (rc != 0) {
235                 if (file->f_flags & O_DIRECT)
236                         rc = (iint->flags & IMA_PERMIT_DIRECTIO) ? 0 : -EACCES;
237                 goto out_digsig;
238         }
239
240         if (!pathname)  /* ima_rdwr_violation possibly pre-fetched */
241                 pathname = ima_d_path(&file->f_path, &pathbuf);
242
243         if (action & IMA_MEASURE)
244                 ima_store_measurement(iint, file, pathname,
245                                       xattr_value, xattr_len);
246         if (action & IMA_APPRAISE_SUBMASK)
247                 rc = ima_appraise_measurement(function, iint, file, pathname,
248                                               xattr_value, xattr_len, opened);
249         if (action & IMA_AUDIT)
250                 ima_audit_measurement(iint, pathname);
251
252 out_digsig:
253         if ((mask & MAY_WRITE) && (iint->flags & IMA_DIGSIG))
254                 rc = -EACCES;
255         kfree(xattr_value);
256 out_free:
257         if (pathbuf)
258                 __putname(pathbuf);
259 out:
260         mutex_unlock(&inode->i_mutex);
261         if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE))
262                 return -EACCES;
263         return 0;
264 }
265
266 /**
267  * ima_file_mmap - based on policy, collect/store measurement.
268  * @file: pointer to the file to be measured (May be NULL)
269  * @prot: contains the protection that will be applied by the kernel.
270  *
271  * Measure files being mmapped executable based on the ima_must_measure()
272  * policy decision.
273  *
274  * On success return 0.  On integrity appraisal error, assuming the file
275  * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
276  */
277 int ima_file_mmap(struct file *file, unsigned long prot)
278 {
279         if (file && (prot & PROT_EXEC))
280                 return process_measurement(file, MAY_EXEC, MMAP_CHECK, 0);
281         return 0;
282 }
283
284 /**
285  * ima_bprm_check - based on policy, collect/store measurement.
286  * @bprm: contains the linux_binprm structure
287  *
288  * The OS protects against an executable file, already open for write,
289  * from being executed in deny_write_access() and an executable file,
290  * already open for execute, from being modified in get_write_access().
291  * So we can be certain that what we verify and measure here is actually
292  * what is being executed.
293  *
294  * On success return 0.  On integrity appraisal error, assuming the file
295  * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
296  */
297 int ima_bprm_check(struct linux_binprm *bprm)
298 {
299         return process_measurement(bprm->file, MAY_EXEC, BPRM_CHECK, 0);
300 }
301
302 /**
303  * ima_path_check - based on policy, collect/store measurement.
304  * @file: pointer to the file to be measured
305  * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
306  *
307  * Measure files based on the ima_must_measure() policy decision.
308  *
309  * On success return 0.  On integrity appraisal error, assuming the file
310  * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
311  */
312 int ima_file_check(struct file *file, int mask, int opened)
313 {
314         return process_measurement(file,
315                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
316                                    FILE_CHECK, opened);
317 }
318 EXPORT_SYMBOL_GPL(ima_file_check);
319
320 /**
321  * ima_module_check - based on policy, collect/store/appraise measurement.
322  * @file: pointer to the file to be measured/appraised
323  *
324  * Measure/appraise kernel modules based on policy.
325  *
326  * On success return 0.  On integrity appraisal error, assuming the file
327  * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
328  */
329 int ima_module_check(struct file *file)
330 {
331         if (!file) {
332 #ifndef CONFIG_MODULE_SIG_FORCE
333                 if ((ima_appraise & IMA_APPRAISE_MODULES) &&
334                     (ima_appraise & IMA_APPRAISE_ENFORCE))
335                         return -EACCES; /* INTEGRITY_UNKNOWN */
336 #endif
337                 return 0;       /* We rely on module signature checking */
338         }
339         return process_measurement(file, MAY_EXEC, MODULE_CHECK, 0);
340 }
341
342 int ima_fw_from_file(struct file *file, char *buf, size_t size)
343 {
344         if (!file) {
345                 if ((ima_appraise & IMA_APPRAISE_FIRMWARE) &&
346                     (ima_appraise & IMA_APPRAISE_ENFORCE))
347                         return -EACCES; /* INTEGRITY_UNKNOWN */
348                 return 0;
349         }
350         return process_measurement(file, MAY_EXEC, FIRMWARE_CHECK, 0);
351 }
352
353 static int __init init_ima(void)
354 {
355         int error;
356
357         hash_setup(CONFIG_IMA_DEFAULT_HASH);
358         error = ima_init();
359
360         if (error && strcmp(hash_algo_name[ima_hash_algo],
361                             CONFIG_IMA_DEFAULT_HASH) != 0) {
362                 pr_info("Allocating %s failed, going to use default hash algorithm %s\n",
363                         hash_algo_name[ima_hash_algo], CONFIG_IMA_DEFAULT_HASH);
364                 hash_setup_done = 0;
365                 hash_setup(CONFIG_IMA_DEFAULT_HASH);
366                 error = ima_init();
367         }
368
369         if (!error) {
370                 ima_initialized = 1;
371                 ima_update_policy_flag();
372         }
373         return error;
374 }
375
376 late_initcall(init_ima);        /* Start IMA after the TPM is available */
377
378 MODULE_DESCRIPTION("Integrity Measurement Architecture");
379 MODULE_LICENSE("GPL");