blob: fa675c907e0fc97bff65199a2d974a6fad876a59 [file] [log] [blame]
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -05001/*
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
21static 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 */
Dmitry Kasatkind26e1932012-09-27 18:26:53 +030037int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050038{
Mimi Zohar07f6a792011-03-09 22:25:48 -050039 if (!ima_appraise)
40 return 0;
41
42 return ima_match_policy(inode, func, mask, IMA_APPRAISE);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050043}
44
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +030045static int ima_fix_xattr(struct dentry *dentry,
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050046 struct integrity_iint_cache *iint)
47{
Mimi Zohar5a44b412012-01-09 22:59:36 -050048 iint->ima_xattr.type = IMA_XATTR_DIGEST;
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +030049 return __vfs_setxattr_noperm(dentry, XATTR_NAME_IMA,
50 (u8 *)&iint->ima_xattr,
51 sizeof(iint->ima_xattr), 0);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050052}
53
54/*
55 * ima_appraise_measurement - appraise file measurement
56 *
57 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
58 * Assuming success, compare the xattr hash with the collected measurement.
59 *
60 * Return 0 on success, error code otherwise
61 */
62int ima_appraise_measurement(struct integrity_iint_cache *iint,
63 struct file *file, const unsigned char *filename)
64{
65 struct dentry *dentry = file->f_dentry;
66 struct inode *inode = dentry->d_inode;
Dmitry Kasatkin86064042011-08-31 14:07:06 +030067 struct evm_ima_xattr_data *xattr_value = NULL;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050068 enum integrity_status status = INTEGRITY_UNKNOWN;
69 const char *op = "appraise_data";
70 char *cause = "unknown";
71 int rc;
72
73 if (!ima_appraise)
74 return 0;
75 if (!inode->i_op->getxattr)
76 return INTEGRITY_UNKNOWN;
77
78 if (iint->flags & IMA_APPRAISED)
79 return iint->ima_status;
80
Dmitry Kasatkin86064042011-08-31 14:07:06 +030081 rc = vfs_getxattr_alloc(dentry, XATTR_NAME_IMA, (char **)&xattr_value,
82 0, GFP_NOFS);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050083 if (rc <= 0) {
84 if (rc && rc != -ENODATA)
85 goto out;
86
87 cause = "missing-hash";
88 status =
89 (inode->i_size == 0) ? INTEGRITY_PASS : INTEGRITY_NOLABEL;
90 goto out;
91 }
92
Dmitry Kasatkin86064042011-08-31 14:07:06 +030093 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value, rc, iint);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -050094 if ((status != INTEGRITY_PASS) && (status != INTEGRITY_UNKNOWN)) {
95 if ((status == INTEGRITY_NOLABEL)
96 || (status == INTEGRITY_NOXATTRS))
97 cause = "missing-HMAC";
98 else if (status == INTEGRITY_FAIL)
99 cause = "invalid-HMAC";
100 goto out;
101 }
102
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300103 switch (xattr_value->type) {
104 case IMA_XATTR_DIGEST:
105 rc = memcmp(xattr_value->digest, iint->ima_xattr.digest,
106 IMA_DIGEST_SIZE);
107 if (rc) {
108 cause = "invalid-hash";
109 status = INTEGRITY_FAIL;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300110 break;
111 }
112 status = INTEGRITY_PASS;
113 break;
114 case EVM_IMA_XATTR_DIGSIG:
115 iint->flags |= IMA_DIGSIG;
116 rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
117 xattr_value->digest, rc - 1,
118 iint->ima_xattr.digest,
119 IMA_DIGEST_SIZE);
120 if (rc == -EOPNOTSUPP) {
121 status = INTEGRITY_UNKNOWN;
122 } else if (rc) {
123 cause = "invalid-signature";
124 status = INTEGRITY_FAIL;
125 } else {
126 status = INTEGRITY_PASS;
127 }
128 break;
129 default:
130 status = INTEGRITY_UNKNOWN;
131 cause = "unknown-ima-data";
132 break;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500133 }
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300134
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500135out:
136 if (status != INTEGRITY_PASS) {
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300137 if ((ima_appraise & IMA_APPRAISE_FIX) &&
138 (!xattr_value ||
139 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
Dmitry Kasatkindef3e8b2012-09-20 22:38:53 +0300140 if (!ima_fix_xattr(dentry, iint))
141 status = INTEGRITY_PASS;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500142 }
143 integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, filename,
144 op, cause, rc, 0);
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300145 } else {
146 iint->flags |= IMA_APPRAISED;
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500147 }
148 iint->ima_status = status;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300149 kfree(xattr_value);
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500150 return status;
151}
152
153/*
154 * ima_update_xattr - update 'security.ima' hash value
155 */
156void ima_update_xattr(struct integrity_iint_cache *iint, struct file *file)
157{
158 struct dentry *dentry = file->f_dentry;
159 int rc = 0;
160
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300161 /* do not collect and update hash for digital signatures */
162 if (iint->flags & IMA_DIGSIG)
163 return;
164
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500165 rc = ima_collect_measurement(iint, file);
166 if (rc < 0)
167 return;
Dmitry Kasatkin86064042011-08-31 14:07:06 +0300168
Mimi Zohar2fe5d6d2012-02-13 10:15:05 -0500169 ima_fix_xattr(dentry, iint);
170}
171
172/**
173 * ima_inode_post_setattr - reflect file metadata changes
174 * @dentry: pointer to the affected dentry
175 *
176 * Changes to a dentry's metadata might result in needing to appraise.
177 *
178 * This function is called from notify_change(), which expects the caller
179 * to lock the inode's i_mutex.
180 */
181void ima_inode_post_setattr(struct dentry *dentry)
182{
183 struct inode *inode = dentry->d_inode;
184 struct integrity_iint_cache *iint;
185 int must_appraise, rc;
186
187 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode)
188 || !inode->i_op->removexattr)
189 return;
190
191 must_appraise = ima_must_appraise(inode, MAY_ACCESS, POST_SETATTR);
192 iint = integrity_iint_find(inode);
193 if (iint) {
194 if (must_appraise)
195 iint->flags |= IMA_APPRAISE;
196 else
197 iint->flags &= ~(IMA_APPRAISE | IMA_APPRAISED);
198 }
199 if (!must_appraise)
200 rc = inode->i_op->removexattr(dentry, XATTR_NAME_IMA);
201 return;
202}
Mimi Zohar42c63332011-03-10 18:54:15 -0500203
204/*
205 * ima_protect_xattr - protect 'security.ima'
206 *
207 * Ensure that not just anyone can modify or remove 'security.ima'.
208 */
209static int ima_protect_xattr(struct dentry *dentry, const char *xattr_name,
210 const void *xattr_value, size_t xattr_value_len)
211{
212 if (strcmp(xattr_name, XATTR_NAME_IMA) == 0) {
213 if (!capable(CAP_SYS_ADMIN))
214 return -EPERM;
215 return 1;
216 }
217 return 0;
218}
219
220static void ima_reset_appraise_flags(struct inode *inode)
221{
222 struct integrity_iint_cache *iint;
223
224 if (!ima_initialized || !ima_appraise || !S_ISREG(inode->i_mode))
225 return;
226
227 iint = integrity_iint_find(inode);
228 if (!iint)
229 return;
230
Dmitry Kasatkin45e24722012-09-12 20:51:32 +0300231 iint->flags &= ~IMA_DONE_MASK;
Mimi Zohar42c63332011-03-10 18:54:15 -0500232 return;
233}
234
235int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,
236 const void *xattr_value, size_t xattr_value_len)
237{
238 int result;
239
240 result = ima_protect_xattr(dentry, xattr_name, xattr_value,
241 xattr_value_len);
242 if (result == 1) {
243 ima_reset_appraise_flags(dentry->d_inode);
244 result = 0;
245 }
246 return result;
247}
248
249int ima_inode_removexattr(struct dentry *dentry, const char *xattr_name)
250{
251 int result;
252
253 result = ima_protect_xattr(dentry, xattr_name, NULL, 0);
254 if (result == 1) {
255 ima_reset_appraise_flags(dentry->d_inode);
256 result = 0;
257 }
258 return result;
259}