~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/security/integrity/ima/ima_appraise.c

Version: ~ [ linux-4.16-rc1 ] ~ [ linux-4.15.2 ] ~ [ linux-4.14.18 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.80 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.115 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.49 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.94 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.53 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.98 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.27.62 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp