4865f61f9044c68cfaf9da5f6cd7b0901bc72f8d
[deliverable/linux.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 if (strncmp(str, "off", 3) == 0)
24 ima_appraise = 0;
25 else if (strncmp(str, "fix", 3) == 0)
26 ima_appraise = IMA_APPRAISE_FIX;
27 return 1;
28 }
29
30 __setup("ima_appraise=", default_appraise_setup);
31
32 /*
33 * ima_must_appraise - set appraise flag
34 *
35 * Return 1 to appraise
36 */
37 int ima_must_appraise(struct inode *inode, enum ima_hooks func, int mask)
38 {
39 return 0;
40 }
41
42 static void ima_fix_xattr(struct dentry *dentry,
43 struct integrity_iint_cache *iint)
44 {
45 iint->digest[0] = IMA_XATTR_DIGEST;
46 __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
47 iint->digest, IMA_DIGEST_SIZE + 1, 0);
48 }
49
50 /*
51 * ima_appraise_measurement - appraise file measurement
52 *
53 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
54 * Assuming success, compare the xattr hash with the collected measurement.
55 *
56 * Return 0 on success, error code otherwise
57 */
58 int ima_appraise_measurement(struct integrity_iint_cache *iint,
59 struct file *file, const unsigned char *filename)
60 {
61 struct dentry *dentry = file->f_dentry;
62 struct inode *inode = dentry->d_inode;
63 u8 xattr_value[IMA_DIGEST_SIZE];
64 enum integrity_status status = INTEGRITY_UNKNOWN;
65 const char *op = "appraise_data";
66 char *cause = "unknown";
67 int rc;
68
69 if (!ima_appraise)
70 return 0;
71 if (!inode->i_op->getxattr)
72 return INTEGRITY_UNKNOWN;
73
74 if (iint->flags & IMA_APPRAISED)
75 return iint->ima_status;
76
77 rc = inode->i_op->getxattr(dentry, XATTR_NAME_IMA, xattr_value,
78 IMA_DIGEST_SIZE);
79 if (rc <= 0) {
80 if (rc && rc != -ENODATA)
81 goto out;
82
83 cause = "missing-hash";
84 status =
85 (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
86 goto out;
87 }
88
89 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
90 if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
91 if ((status == INTEGRITY_NOLABEL)
92 || (status == INTEGRITY_NOXATTRS))
93 cause = "missing-HMAC";
94 else if (status == INTEGRITY_FAIL)
95 cause = "invalid-HMAC";
96 goto out;
97 }
98
99 rc = memcmp(xattr_value, iint->digest, IMA_DIGEST_SIZE);
100 if (rc) {
101 status = INTEGRITY_FAIL;
102 cause = "invalid-hash";
103 print_hex_dump_bytes("security.ima: ", DUMP_PREFIX_NONE,
104 xattr_value, IMA_DIGEST_SIZE);
105 print_hex_dump_bytes("collected: ", DUMP_PREFIX_NONE,
106 iint->digest, IMA_DIGEST_SIZE);
107 goto out;
108 }
109 status = INTEGRITY_PASS;
110 iint->flags |= IMA_APPRAISED;
111 out:
112 if (status != INTEGRITY_PASS) {
113 if (ima_appraise & IMA_APPRAISE_FIX) {
114 ima_fix_xattr(dentry, iint);
115 status = INTEGRITY_PASS;
116 }
117 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
118 op, cause, rc, 0);
119 }
120 iint->ima_status = status;
121 return status;
122 }
123
124 /*
125 * ima_update_xattr - update 'security.ima' hash value
126 */
127 void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
128 {
129 struct dentry *dentry = file->f_dentry;
130 int rc = 0;
131
132 rc = ima_collect_measurement(iint, file);
133 if (rc < 0)
134 return;
135 ima_fix_xattr(dentry, iint);
136 }
137
138 /**
139 * ima_inode_post_setattr - reflect file metadata changes
140 * @dentry: pointer to the affected dentry
141 *
142 * Changes to a dentry's metadata might result in needing to appraise.
143 *
144 * This function is called from notify_change(), which expects the caller
145 * to lock the inode's i_mutex.
146 */
147 void ima_inode_post_setattr(struct dentry *dentry)
148 {
149 struct inode *inode = dentry->d_inode;
150 struct integrity_iint_cache *iint;
151 int must_appraise, rc;
152
153 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
154 || !inode->i_op->removexattr)
155 return;
156
157 must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
158 iint = integrity_iint_find(inode);
159 if (iint) {
160 if (must_appraise)
161 iint->flags |= IMA_APPRAISE;
162 else
163 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED);
164 }
165 if (!must_appraise)
166 rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
167 return;
168 }
This page took 0.032458 seconds and 4 git commands to generate.