ima: provide support for arbitrary hash algorithms
[platform/adaptation/renesas_rcar/renesas_kernel.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 #include <linux/module.h>
20 #include <linux/file.h>
21 #include <linux/binfmts.h>
22 #include <linux/mount.h>
23 #include <linux/mman.h>
24 #include <linux/slab.h>
25 #include <linux/xattr.h>
26 #include <linux/ima.h>
27 #include <crypto/hash_info.h>
28
29 #include "ima.h"
30
31 int ima_initialized;
32
33 #ifdef CONFIG_IMA_APPRAISE
34 int ima_appraise = IMA_APPRAISE_ENFORCE;
35 #else
36 int ima_appraise;
37 #endif
38
39 int ima_hash_algo = HASH_ALGO_SHA1;
40
41 static int __init hash_setup(char *str)
42 {
43         if (strncmp(str, "md5", 3) == 0)
44                 ima_hash_algo = HASH_ALGO_MD5;
45         return 1;
46 }
47 __setup("ima_hash=", hash_setup);
48
49 /*
50  * ima_rdwr_violation_check
51  *
52  * Only invalidate the PCR for measured files:
53  *      - Opening a file for write when already open for read,
54  *        results in a time of measure, time of use (ToMToU) error.
55  *      - Opening a file for read when already open for write,
56  *        could result in a file measurement error.
57  *
58  */
59 static void ima_rdwr_violation_check(struct file *file)
60 {
61         struct dentry *dentry = file->f_path.dentry;
62         struct inode *inode = file_inode(file);
63         fmode_t mode = file->f_mode;
64         int must_measure;
65         bool send_tomtou = false, send_writers = false;
66         char *pathbuf = NULL;
67         const char *pathname;
68
69         if (!S_ISREG(inode->i_mode) || !ima_initialized)
70                 return;
71
72         mutex_lock(&inode->i_mutex);    /* file metadata: permissions, xattr */
73
74         if (mode & FMODE_WRITE) {
75                 if (atomic_read(&inode->i_readcount) && IS_IMA(inode))
76                         send_tomtou = true;
77                 goto out;
78         }
79
80         must_measure = ima_must_measure(inode, MAY_READ, FILE_CHECK);
81         if (!must_measure)
82                 goto out;
83
84         if (atomic_read(&inode->i_writecount) > 0)
85                 send_writers = true;
86 out:
87         mutex_unlock(&inode->i_mutex);
88
89         if (!send_tomtou && !send_writers)
90                 return;
91
92         pathname = ima_d_path(&file->f_path, &pathbuf);
93         if (!pathname || strlen(pathname) > IMA_EVENT_NAME_LEN_MAX)
94                 pathname = dentry->d_name.name;
95
96         if (send_tomtou)
97                 ima_add_violation(inode, pathname,
98                                   "invalid_pcr", "ToMToU");
99         if (send_writers)
100                 ima_add_violation(inode, pathname,
101                                   "invalid_pcr", "open_writers");
102         kfree(pathbuf);
103 }
104
105 static void ima_check_last_writer(struct integrity_iint_cache *iint,
106                                   struct inode *inode, struct file *file)
107 {
108         fmode_t mode = file->f_mode;
109
110         if (!(mode & FMODE_WRITE))
111                 return;
112
113         mutex_lock(&inode->i_mutex);
114         if (atomic_read(&inode->i_writecount) == 1 &&
115             iint->version != inode->i_version) {
116                 iint->flags &= ~IMA_DONE_MASK;
117                 if (iint->flags & IMA_APPRAISE)
118                         ima_update_xattr(iint, file);
119         }
120         mutex_unlock(&inode->i_mutex);
121 }
122
123 /**
124  * ima_file_free - called on __fput()
125  * @file: pointer to file structure being freed
126  *
127  * Flag files that changed, based on i_version
128  */
129 void ima_file_free(struct file *file)
130 {
131         struct inode *inode = file_inode(file);
132         struct integrity_iint_cache *iint;
133
134         if (!iint_initialized || !S_ISREG(inode->i_mode))
135                 return;
136
137         iint = integrity_iint_find(inode);
138         if (!iint)
139                 return;
140
141         ima_check_last_writer(iint, inode, file);
142 }
143
144 static int process_measurement(struct file *file, const char *filename,
145                                int mask, int function)
146 {
147         struct inode *inode = file_inode(file);
148         struct integrity_iint_cache *iint;
149         char *pathbuf = NULL;
150         const char *pathname = NULL;
151         int rc = -ENOMEM, action, must_appraise, _func;
152
153         if (!ima_initialized || !S_ISREG(inode->i_mode))
154                 return 0;
155
156         /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
157          * bitmask based on the appraise/audit/measurement policy.
158          * Included is the appraise submask.
159          */
160         action = ima_get_action(inode, mask, function);
161         if (!action)
162                 return 0;
163
164         must_appraise = action & IMA_APPRAISE;
165
166         /*  Is the appraise rule hook specific?  */
167         _func = (action & IMA_FILE_APPRAISE) ? FILE_CHECK : function;
168
169         mutex_lock(&inode->i_mutex);
170
171         iint = integrity_inode_get(inode);
172         if (!iint)
173                 goto out;
174
175         /* Determine if already appraised/measured based on bitmask
176          * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
177          *  IMA_AUDIT, IMA_AUDITED)
178          */
179         iint->flags |= action;
180         action &= IMA_DO_MASK;
181         action &= ~((iint->flags & IMA_DONE_MASK) >> 1);
182
183         /* Nothing to do, just return existing appraised status */
184         if (!action) {
185                 if (must_appraise)
186                         rc = ima_get_cache_status(iint, _func);
187                 goto out_digsig;
188         }
189
190         rc = ima_collect_measurement(iint, file);
191         if (rc != 0)
192                 goto out_digsig;
193
194         pathname = !filename ? ima_d_path(&file->f_path, &pathbuf) : filename;
195         if (!pathname)
196                 pathname = (const char *)file->f_dentry->d_name.name;
197
198         if (action & IMA_MEASURE)
199                 ima_store_measurement(iint, file, pathname);
200         if (action & IMA_APPRAISE_SUBMASK)
201                 rc = ima_appraise_measurement(_func, iint, file, pathname);
202         if (action & IMA_AUDIT)
203                 ima_audit_measurement(iint, pathname);
204         kfree(pathbuf);
205 out_digsig:
206         if ((mask & MAY_WRITE) && (iint->flags & IMA_DIGSIG))
207                 rc = -EACCES;
208 out:
209         mutex_unlock(&inode->i_mutex);
210         if ((rc && must_appraise) && (ima_appraise & IMA_APPRAISE_ENFORCE))
211                 return -EACCES;
212         return 0;
213 }
214
215 /**
216  * ima_file_mmap - based on policy, collect/store measurement.
217  * @file: pointer to the file to be measured (May be NULL)
218  * @prot: contains the protection that will be applied by the kernel.
219  *
220  * Measure files being mmapped executable based on the ima_must_measure()
221  * policy decision.
222  *
223  * On success return 0.  On integrity appraisal error, assuming the file
224  * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
225  */
226 int ima_file_mmap(struct file *file, unsigned long prot)
227 {
228         if (file && (prot & PROT_EXEC))
229                 return process_measurement(file, NULL, MAY_EXEC, MMAP_CHECK);
230         return 0;
231 }
232
233 /**
234  * ima_bprm_check - based on policy, collect/store measurement.
235  * @bprm: contains the linux_binprm structure
236  *
237  * The OS protects against an executable file, already open for write,
238  * from being executed in deny_write_access() and an executable file,
239  * already open for execute, from being modified in get_write_access().
240  * So we can be certain that what we verify and measure here is actually
241  * what is being executed.
242  *
243  * On success return 0.  On integrity appraisal error, assuming the file
244  * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
245  */
246 int ima_bprm_check(struct linux_binprm *bprm)
247 {
248         return process_measurement(bprm->file,
249                                    (strcmp(bprm->filename, bprm->interp) == 0) ?
250                                    bprm->filename : bprm->interp,
251                                    MAY_EXEC, BPRM_CHECK);
252 }
253
254 /**
255  * ima_path_check - based on policy, collect/store measurement.
256  * @file: pointer to the file to be measured
257  * @mask: contains MAY_READ, MAY_WRITE or MAY_EXECUTE
258  *
259  * Measure files based on the ima_must_measure() policy decision.
260  *
261  * On success return 0.  On integrity appraisal error, assuming the file
262  * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
263  */
264 int ima_file_check(struct file *file, int mask)
265 {
266         ima_rdwr_violation_check(file);
267         return process_measurement(file, NULL,
268                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
269                                    FILE_CHECK);
270 }
271 EXPORT_SYMBOL_GPL(ima_file_check);
272
273 /**
274  * ima_module_check - based on policy, collect/store/appraise measurement.
275  * @file: pointer to the file to be measured/appraised
276  *
277  * Measure/appraise kernel modules based on policy.
278  *
279  * On success return 0.  On integrity appraisal error, assuming the file
280  * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
281  */
282 int ima_module_check(struct file *file)
283 {
284         if (!file) {
285 #ifndef CONFIG_MODULE_SIG_FORCE
286                 if ((ima_appraise & IMA_APPRAISE_MODULES) &&
287                     (ima_appraise & IMA_APPRAISE_ENFORCE))
288                         return -EACCES; /* INTEGRITY_UNKNOWN */
289 #endif
290                 return 0;       /* We rely on module signature checking */
291         }
292         return process_measurement(file, NULL, MAY_EXEC, MODULE_CHECK);
293 }
294
295 static int __init init_ima(void)
296 {
297         int error;
298
299         error = ima_init();
300         if (!error)
301                 ima_initialized = 1;
302         return error;
303 }
304
305 late_initcall(init_ima);        /* Start IMA after the TPM is available */
306
307 MODULE_DESCRIPTION("Integrity Measurement Architecture");
308 MODULE_LICENSE("GPL");