ima: relax requiring a file signature for new files with zero length
[linux-2.6-microblaze.git] / security / integrity / ima / ima_appraise.c
1 /*
2  * Copyright (C) 2011 IBM Corporation
3  *
4  * Author:
5  * Mimi Zohar <zohar@us.ibm.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, version 2 of the License.
10  */
11 #include <linux/module.h>
12 #include <linux/file.h>
13 #include <linux/fs.h>
14 #include <linux/xattr.h>
15 #include <linux/magic.h>
16 #include <linux/ima.h>
17 #include <linux/evm.h>
18
19 #include "ima.h"
20
21 static int __init default_appraise_setup(char *str)
22 {
23 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
24         if (strncmp(str, "off", 3) == 0)
25                 ima_appraise = 0;
26         else if (strncmp(str, "log", 3) == 0)
27                 ima_appraise = IMA_APPRAISE_LOG;
28         else if (strncmp(str, "fix", 3) == 0)
29                 ima_appraise = IMA_APPRAISE_FIX;
30 #endif
31         return 1;
32 }
33
34 __setup("ima_appraise=", default_appraise_setup);
35
36 /*
37  * is_ima_appraise_enabled - return appraise status
38  *
39  * Only return enabled, if not in ima_appraise="fix" or "log" modes.
40  */
41 bool is_ima_appraise_enabled(void)
42 {
43         return ima_appraise & IMA_APPRAISE_ENFORCE;
44 }
45
46 /*
47  * ima_must_appraise - set appraise flag
48  *
49  * Return 1 to appraise
50  */
51 int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
52 {
53         if (!ima_appraise)
54                 return 0;
55
56         return ima_match_policy(inode, func, mask, IMA_APPRAISE, NULL);
57 }
58
59 static int ima_fix_xattr(struct dentry *dentry,
60                          struct integrity_iint_cache *iint)
61 {
62         int rc, offset;
63         u8 algo = iint->ima_hash->algo;
64
65         if (algo <= HASH_ALGO_SHA1) {
66                 offset = 1;
67                 iint->ima_hash->xattr.sha1.type = IMA_XATTR_DIGEST;
68         } else {
69                 offset = 0;
70                 iint->ima_hash->xattr.ng.type = IMA_XATTR_DIGEST_NG;
71                 iint->ima_hash->xattr.ng.algo = algo;
72         }
73         rc = __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
74                                    &iint->ima_hash->xattr.data[offset],
75                                    (sizeof(iint->ima_hash->xattr) - offset) +
76                                    iint->ima_hash->length, 0);
77         return rc;
78 }
79
80 /* Return specific func appraised cached result */
81 enum integrity_status ima_get_cache_status(struct integrity_iint_cache *iint,
82                                            enum ima_hooks func)
83 {
84         switch (func) {
85         case MMAP_CHECK:
86                 return iint->ima_mmap_status;
87         case BPRM_CHECK:
88                 return iint->ima_bprm_status;
89         case FILE_CHECK:
90         case POST_SETATTR:
91                 return iint->ima_file_status;
92         case MODULE_CHECK ... MAX_CHECK - 1:
93         default:
94                 return iint->ima_read_status;
95         }
96 }
97
98 static void ima_set_cache_status(struct integrity_iint_cache *iint,
99                                  enum ima_hooks func,
100                                  enum integrity_status status)
101 {
102         switch (func) {
103         case MMAP_CHECK:
104                 iint->ima_mmap_status = status;
105                 break;
106         case BPRM_CHECK:
107                 iint->ima_bprm_status = status;
108                 break;
109         case FILE_CHECK:
110         case POST_SETATTR:
111                 iint->ima_file_status = status;
112                 break;
113         case MODULE_CHECK ... MAX_CHECK - 1:
114         default:
115                 iint->ima_read_status = status;
116                 break;
117         }
118 }
119
120 static void ima_cache_flags(struct integrity_iint_cache *iint,
121                              enum ima_hooks func)
122 {
123         switch (func) {
124         case MMAP_CHECK:
125                 iint->flags |= (IMA_MMAP_APPRAISED | IMA_APPRAISED);
126                 break;
127         case BPRM_CHECK:
128                 iint->flags |= (IMA_BPRM_APPRAISED | IMA_APPRAISED);
129                 break;
130         case FILE_CHECK:
131         case POST_SETATTR:
132                 iint->flags |= (IMA_FILE_APPRAISED | IMA_APPRAISED);
133                 break;
134         case MODULE_CHECK ... MAX_CHECK - 1:
135         default:
136                 iint->flags |= (IMA_READ_APPRAISED | IMA_APPRAISED);
137                 break;
138         }
139 }
140
141 enum hash_algo ima_get_hash_algo(struct evm_ima_xattr_data *xattr_value,
142                                  int xattr_len)
143 {
144         struct signature_v2_hdr *sig;
145         enum hash_algo ret;
146
147         if (!xattr_value || xattr_len < 2)
148                 /* return default hash algo */
149                 return ima_hash_algo;
150
151         switch (xattr_value->type) {
152         case EVM_IMA_XATTR_DIGSIG:
153                 sig = (typeof(sig))xattr_value;
154                 if (sig->version != 2 || xattr_len <= sizeof(*sig))
155                         return ima_hash_algo;
156                 return sig->hash_algo;
157                 break;
158         case IMA_XATTR_DIGEST_NG:
159                 ret = xattr_value->digest[0];
160                 if (ret < HASH_ALGO__LAST)
161                         return ret;
162                 break;
163         case IMA_XATTR_DIGEST:
164                 /* this is for backward compatibility */
165                 if (xattr_len == 21) {
166                         unsigned int zero = 0;
167                         if (!memcmp(&xattr_value->digest[16], &zero, 4))
168                                 return HASH_ALGO_MD5;
169                         else
170                                 return HASH_ALGO_SHA1;
171                 } else if (xattr_len == 17)
172                         return HASH_ALGO_MD5;
173                 break;
174         }
175
176         /* return default hash algo */
177         return ima_hash_algo;
178 }
179
180 int ima_read_xattr(struct dentry *dentry,
181                    struct evm_ima_xattr_data **xattr_value)
182 {
183         ssize_t ret;
184
185         ret = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)xattr_value,
186                                  0, GFP_NOFS);
187         if (ret == -EOPNOTSUPP)
188                 ret = 0;
189         return ret;
190 }
191
192 /*
193  * ima_appraise_measurement - appraise file measurement
194  *
195  * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
196  * Assuming success, compare the xattr hash with the collected measurement.
197  *
198  * Return 0 on success, error code otherwise
199  */
200 int ima_appraise_measurement(enum ima_hooks func,
201                              struct integrity_iint_cache *iint,
202                              struct file *file, const unsigned char *filename,
203                              struct evm_ima_xattr_data *xattr_value,
204                              int xattr_len, int opened)
205 {
206         static const char op[] = "appraise_data";
207         char *cause = "unknown";
208         struct dentry *dentry = file_dentry(file);
209         struct inode *inode = d_backing_inode(dentry);
210         enum integrity_status status = INTEGRITY_UNKNOWN;
211         int rc = xattr_len, hash_start = 0;
212
213         if (!(inode->i_opflags & IOP_XATTR))
214                 return INTEGRITY_UNKNOWN;
215
216         if (rc <= 0) {
217                 if (rc && rc != -ENODATA)
218                         goto out;
219
220                 cause = iint->flags & IMA_DIGSIG_REQUIRED ?
221                                 "IMA-signature-required" : "missing-hash";
222                 status = INTEGRITY_NOLABEL;
223                 if (opened & FILE_CREATED)
224                         iint->flags |= IMA_NEW_FILE;
225                 if ((iint->flags & IMA_NEW_FILE) &&
226                     (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
227                      (inode->i_size == 0)))
228                         status = INTEGRITY_PASS;
229                 goto out;
230         }
231
232         status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
233         if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
234                 if ((status == INTEGRITY_NOLABEL)
235                     || (status == INTEGRITY_NOXATTRS))
236                         cause = "missing-HMAC";
237                 else if (status == INTEGRITY_FAIL)
238                         cause = "invalid-HMAC";
239                 goto out;
240         }
241         switch (xattr_value->type) {
242         case IMA_XATTR_DIGEST_NG:
243                 /* first byte contains algorithm id */
244                 hash_start = 1;
245                 /* fall through */
246         case IMA_XATTR_DIGEST:
247                 if (iint->flags & IMA_DIGSIG_REQUIRED) {
248                         cause = "IMA-signature-required";
249                         status = INTEGRITY_FAIL;
250                         break;
251                 }
252                 if (xattr_len - sizeof(xattr_value->type) - hash_start >=
253                                 iint->ima_hash->length)
254                         /* xattr length may be longer. md5 hash in previous
255                            version occupied 20 bytes in xattr, instead of 16
256                          */
257                         rc = memcmp(&xattr_value->digest[hash_start],
258                                     iint->ima_hash->digest,
259                                     iint->ima_hash->length);
260                 else
261                         rc = -EINVAL;
262                 if (rc) {
263                         cause = "invalid-hash";
264                         status = INTEGRITY_FAIL;
265                         break;
266                 }
267                 status = INTEGRITY_PASS;
268                 break;
269         case EVM_IMA_XATTR_DIGSIG:
270                 iint->flags |= IMA_DIGSIG;
271                 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
272                                              (const char *)xattr_value, rc,
273                                              iint->ima_hash->digest,
274                                              iint->ima_hash->length);
275                 if (rc == -EOPNOTSUPP) {
276                         status = INTEGRITY_UNKNOWN;
277                 } else if (rc) {
278                         cause = "invalid-signature";
279                         status = INTEGRITY_FAIL;
280                 } else {
281                         status = INTEGRITY_PASS;
282                 }
283                 break;
284         default:
285                 status = INTEGRITY_UNKNOWN;
286                 cause = "unknown-ima-data";
287                 break;
288         }
289
290 out:
291         if (status != INTEGRITY_PASS) {
292                 if ((ima_appraise & IMA_APPRAISE_FIX) &&
293                     (!xattr_value ||
294                      xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
295                         if (!ima_fix_xattr(dentry, iint))
296                                 status = INTEGRITY_PASS;
297                 } else if ((inode->i_size == 0) &&
298                            (iint->flags & IMA_NEW_FILE) &&
299                            (xattr_value &&
300                             xattr_value->type == EVM_IMA_XATTR_DIGSIG)) {
301                         status = INTEGRITY_PASS;
302                 }
303                 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
304                                     op, cause, rc, 0);
305         } else {
306                 ima_cache_flags(iint, func);
307         }
308         ima_set_cache_status(iint, func, status);
309         return status;
310 }
311
312 /*
313  * ima_update_xattr - update 'security.ima' hash value
314  */
315 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
316 {
317         struct dentry *dentry = file_dentry(file);
318         int rc = 0;
319
320         /* do not collect and update hash for digital signatures */
321         if (iint->flags & IMA_DIGSIG)
322                 return;
323
324         if (iint->ima_file_status != INTEGRITY_PASS)
325                 return;
326
327         rc = ima_collect_measurement(iint, file, NULL, 0, ima_hash_algo);
328         if (rc < 0)
329                 return;
330
331         ima_fix_xattr(dentry, iint);
332 }
333
334 /**
335  * ima_inode_post_setattr - reflect file metadata changes
336  * @dentry: pointer to the affected dentry
337  *
338  * Changes to a dentry's metadata might result in needing to appraise.
339  *
340  * This function is called from notify_change(), which expects the caller
341  * to lock the inode's i_mutex.
342  */
343 void ima_inode_post_setattr(struct dentry *dentry)
344 {
345         struct inode *inode = d_backing_inode(dentry);
346         struct integrity_iint_cache *iint;
347         int must_appraise;
348
349         if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode)
350             || !(inode->i_opflags & IOP_XATTR))
351                 return;
352
353         must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
354         iint = integrity_iint_find(inode);
355         if (iint) {
356                 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED |
357                                  IMA_APPRAISE_SUBMASK | IMA_APPRAISED_SUBMASK |
358                                  IMA_ACTION_RULE_FLAGS);
359                 if (must_appraise)
360                         iint->flags |= IMA_APPRAISE;
361         }
362         if (!must_appraise)
363                 __vfs_removexattr(dentry, XATTR_NAME_IMA);
364 }
365
366 /*
367  * ima_protect_xattr - protect 'security.ima'
368  *
369  * Ensure that not just anyone can modify or remove 'security.ima'.
370  */
371 static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
372                              const void *xattr_value, size_t xattr_value_len)
373 {
374         if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
375                 if (!capable(CAP_SYS_ADMIN))
376                         return -EPERM;
377                 return 1;
378         }
379         return 0;
380 }
381
382 static void ima_reset_appraise_flags(struct inode *inode, int digsig)
383 {
384         struct integrity_iint_cache *iint;
385
386         if (!(ima_policy_flag & IMA_APPRAISE) || !S_ISREG(inode->i_mode))
387                 return;
388
389         iint = integrity_iint_find(inode);
390         if (!iint)
391                 return;
392
393         iint->flags &= ~IMA_DONE_MASK;
394         iint->measured_pcrs = 0;
395         if (digsig)
396                 iint->flags |= IMA_DIGSIG;
397         return;
398 }
399
400 int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
401                        const void *xattr_value, size_t xattr_value_len)
402 {
403         const struct evm_ima_xattr_data *xvalue = xattr_value;
404         int result;
405
406         result = ima_protect_xattr(dentry, xattr_name, xattr_value,
407                                    xattr_value_len);
408         if (result == 1) {
409                 if (!xattr_value_len || (xvalue->type >= IMA_XATTR_LAST))
410                         return -EINVAL;
411                 ima_reset_appraise_flags(d_backing_inode(dentry),
412                         xvalue->type == EVM_IMA_XATTR_DIGSIG);
413                 result = 0;
414         }
415         return result;
416 }
417
418 int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
419 {
420         int result;
421
422         result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
423         if (result == 1) {
424                 ima_reset_appraise_flags(d_backing_inode(dentry), 0);
425                 result = 0;
426         }
427         return result;
428 }