2 * Copyright (C) 2011 IBM Corporation
5 * Mimi Zohar <zohar@us.ibm.com>
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.
11 #include <linux/module.h>
12 #include <linux/file.h>
14 #include <linux/xattr.h>
15 #include <linux/magic.h>
16 #include <linux/ima.h>
17 #include <linux/evm.h>
21 static int __init
default_appraise_setup(char *str
)
23 if (strncmp(str
, "off", 3) == 0)
25 else if (strncmp(str
, "log", 3) == 0)
26 ima_appraise
= IMA_APPRAISE_LOG
;
27 else if (strncmp(str
, "fix", 3) == 0)
28 ima_appraise
= IMA_APPRAISE_FIX
;
32 __setup("ima_appraise=", default_appraise_setup
);
35 * ima_must_appraise - set appraise flag
37 * Return 1 to appraise
39 int ima_must_appraise(struct inode
*inode
, int mask
, enum ima_hooks func
)
44 return ima_match_policy(inode
, func
, mask
, IMA_APPRAISE
, NULL
);
47 static int ima_fix_xattr(struct dentry
*dentry
,
48 struct integrity_iint_cache
*iint
)
51 u8 algo
= iint
->ima_hash
->algo
;
53 if (algo
<= HASH_ALGO_SHA1
) {
55 iint
->ima_hash
->xattr
.sha1
.type
= IMA_XATTR_DIGEST
;
58 iint
->ima_hash
->xattr
.ng
.type
= IMA_XATTR_DIGEST_NG
;
59 iint
->ima_hash
->xattr
.ng
.algo
= algo
;
61 rc
= __vfs_setxattr_noperm(dentry
, XATTR_NAME_IMA
,
62 &iint
->ima_hash
->xattr
.data
[offset
],
63 (sizeof(iint
->ima_hash
->xattr
) - offset
) +
64 iint
->ima_hash
->length
, 0);
68 /* Return specific func appraised cached result */
69 enum integrity_status
ima_get_cache_status(struct integrity_iint_cache
*iint
,
74 return iint
->ima_mmap_status
;
76 return iint
->ima_bprm_status
;
79 return iint
->ima_file_status
;
80 case MODULE_CHECK
... MAX_CHECK
- 1:
82 return iint
->ima_read_status
;
86 static void ima_set_cache_status(struct integrity_iint_cache
*iint
,
88 enum integrity_status status
)
92 iint
->ima_mmap_status
= status
;
95 iint
->ima_bprm_status
= status
;
99 iint
->ima_file_status
= status
;
101 case MODULE_CHECK
... MAX_CHECK
- 1:
103 iint
->ima_read_status
= status
;
108 static void ima_cache_flags(struct integrity_iint_cache
*iint
,
113 iint
->flags
|= (IMA_MMAP_APPRAISED
| IMA_APPRAISED
);
116 iint
->flags
|= (IMA_BPRM_APPRAISED
| IMA_APPRAISED
);
120 iint
->flags
|= (IMA_FILE_APPRAISED
| IMA_APPRAISED
);
122 case MODULE_CHECK
... MAX_CHECK
- 1:
124 iint
->flags
|= (IMA_READ_APPRAISED
| IMA_APPRAISED
);
129 enum hash_algo
ima_get_hash_algo(struct evm_ima_xattr_data
*xattr_value
,
132 struct signature_v2_hdr
*sig
;
134 if (!xattr_value
|| xattr_len
< 2)
135 /* return default hash algo */
136 return ima_hash_algo
;
138 switch (xattr_value
->type
) {
139 case EVM_IMA_XATTR_DIGSIG
:
140 sig
= (typeof(sig
))xattr_value
;
141 if (sig
->version
!= 2 || xattr_len
<= sizeof(*sig
))
142 return ima_hash_algo
;
143 return sig
->hash_algo
;
145 case IMA_XATTR_DIGEST_NG
:
146 return xattr_value
->digest
[0];
148 case IMA_XATTR_DIGEST
:
149 /* this is for backward compatibility */
150 if (xattr_len
== 21) {
151 unsigned int zero
= 0;
152 if (!memcmp(&xattr_value
->digest
[16], &zero
, 4))
153 return HASH_ALGO_MD5
;
155 return HASH_ALGO_SHA1
;
156 } else if (xattr_len
== 17)
157 return HASH_ALGO_MD5
;
161 /* return default hash algo */
162 return ima_hash_algo
;
165 int ima_read_xattr(struct dentry
*dentry
,
166 struct evm_ima_xattr_data
**xattr_value
)
170 ret
= vfs_getxattr_alloc(dentry
, XATTR_NAME_IMA
, (char **)xattr_value
,
172 if (ret
== -EOPNOTSUPP
)
178 * ima_appraise_measurement - appraise file measurement
180 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
181 * Assuming success, compare the xattr hash with the collected measurement.
183 * Return 0 on success, error code otherwise
185 int ima_appraise_measurement(enum ima_hooks func
,
186 struct integrity_iint_cache
*iint
,
187 struct file
*file
, const unsigned char *filename
,
188 struct evm_ima_xattr_data
*xattr_value
,
189 int xattr_len
, int opened
)
191 static const char op
[] = "appraise_data";
192 char *cause
= "unknown";
193 struct dentry
*dentry
= file_dentry(file
);
194 struct inode
*inode
= d_backing_inode(dentry
);
195 enum integrity_status status
= INTEGRITY_UNKNOWN
;
196 int rc
= xattr_len
, hash_start
= 0;
198 if (!(inode
->i_opflags
& IOP_XATTR
))
199 return INTEGRITY_UNKNOWN
;
202 if (rc
&& rc
!= -ENODATA
)
205 cause
= "missing-hash";
206 status
= INTEGRITY_NOLABEL
;
207 if (opened
& FILE_CREATED
)
208 iint
->flags
|= IMA_NEW_FILE
;
209 if ((iint
->flags
& IMA_NEW_FILE
) &&
210 (!(iint
->flags
& IMA_DIGSIG_REQUIRED
) ||
211 (inode
->i_size
== 0)))
212 status
= INTEGRITY_PASS
;
216 status
= evm_verifyxattr(dentry
, XATTR_NAME_IMA
, xattr_value
, rc
, iint
);
217 if ((status
!= INTEGRITY_PASS
) &&
218 (status
!= INTEGRITY_PASS_IMMUTABLE
) &&
219 (status
!= INTEGRITY_UNKNOWN
)) {
220 if ((status
== INTEGRITY_NOLABEL
)
221 || (status
== INTEGRITY_NOXATTRS
))
222 cause
= "missing-HMAC";
223 else if (status
== INTEGRITY_FAIL
)
224 cause
= "invalid-HMAC";
227 switch (xattr_value
->type
) {
228 case IMA_XATTR_DIGEST_NG
:
229 /* first byte contains algorithm id */
231 case IMA_XATTR_DIGEST
:
232 if (iint
->flags
& IMA_DIGSIG_REQUIRED
) {
233 cause
= "IMA-signature-required";
234 status
= INTEGRITY_FAIL
;
237 clear_bit(IMA_DIGSIG
, &iint
->atomic_flags
);
238 if (xattr_len
- sizeof(xattr_value
->type
) - hash_start
>=
239 iint
->ima_hash
->length
)
240 /* xattr length may be longer. md5 hash in previous
241 version occupied 20 bytes in xattr, instead of 16
243 rc
= memcmp(&xattr_value
->digest
[hash_start
],
244 iint
->ima_hash
->digest
,
245 iint
->ima_hash
->length
);
249 cause
= "invalid-hash";
250 status
= INTEGRITY_FAIL
;
253 status
= INTEGRITY_PASS
;
255 case EVM_IMA_XATTR_DIGSIG
:
256 set_bit(IMA_DIGSIG
, &iint
->atomic_flags
);
257 rc
= integrity_digsig_verify(INTEGRITY_KEYRING_IMA
,
258 (const char *)xattr_value
, rc
,
259 iint
->ima_hash
->digest
,
260 iint
->ima_hash
->length
);
261 if (rc
== -EOPNOTSUPP
) {
262 status
= INTEGRITY_UNKNOWN
;
264 cause
= "invalid-signature";
265 status
= INTEGRITY_FAIL
;
267 status
= INTEGRITY_PASS
;
271 status
= INTEGRITY_UNKNOWN
;
272 cause
= "unknown-ima-data";
277 if (status
!= INTEGRITY_PASS
) {
278 if ((ima_appraise
& IMA_APPRAISE_FIX
) &&
280 xattr_value
->type
!= EVM_IMA_XATTR_DIGSIG
)) {
281 if (!ima_fix_xattr(dentry
, iint
))
282 status
= INTEGRITY_PASS
;
283 } else if ((inode
->i_size
== 0) &&
284 (iint
->flags
& IMA_NEW_FILE
) &&
286 xattr_value
->type
== EVM_IMA_XATTR_DIGSIG
)) {
287 status
= INTEGRITY_PASS
;
289 integrity_audit_msg(AUDIT_INTEGRITY_DATA
, inode
, filename
,
292 ima_cache_flags(iint
, func
);
294 ima_set_cache_status(iint
, func
, status
);
299 * ima_update_xattr - update 'security.ima' hash value
301 void ima_update_xattr(struct integrity_iint_cache
*iint
, struct file
*file
)
303 struct dentry
*dentry
= file_dentry(file
);
306 /* do not collect and update hash for digital signatures */
307 if (test_bit(IMA_DIGSIG
, &iint
->atomic_flags
))
310 if (iint
->ima_file_status
!= INTEGRITY_PASS
)
313 rc
= ima_collect_measurement(iint
, file
, NULL
, 0, ima_hash_algo
);
317 inode_lock(file_inode(file
));
318 ima_fix_xattr(dentry
, iint
);
319 inode_unlock(file_inode(file
));
323 * ima_inode_post_setattr - reflect file metadata changes
324 * @dentry: pointer to the affected dentry
326 * Changes to a dentry's metadata might result in needing to appraise.
328 * This function is called from notify_change(), which expects the caller
329 * to lock the inode's i_mutex.
331 void ima_inode_post_setattr(struct dentry
*dentry
)
333 struct inode
*inode
= d_backing_inode(dentry
);
334 struct integrity_iint_cache
*iint
;
337 if (!(ima_policy_flag
& IMA_APPRAISE
) || !S_ISREG(inode
->i_mode
)
338 || !(inode
->i_opflags
& IOP_XATTR
))
341 must_appraise
= ima_must_appraise(inode
, MAY_ACCESS
, POST_SETATTR
);
343 __vfs_removexattr(dentry
, XATTR_NAME_IMA
);
344 iint
= integrity_iint_find(inode
);
346 set_bit(IMA_CHANGE_ATTR
, &iint
->atomic_flags
);
348 clear_bit(IMA_UPDATE_XATTR
, &iint
->atomic_flags
);
353 * ima_protect_xattr - protect 'security.ima'
355 * Ensure that not just anyone can modify or remove 'security.ima'.
357 static int ima_protect_xattr(struct dentry
*dentry
, const char *xattr_name
,
358 const void *xattr_value
, size_t xattr_value_len
)
360 if (strcmp(xattr_name
, XATTR_NAME_IMA
) == 0) {
361 if (!capable(CAP_SYS_ADMIN
))
368 static void ima_reset_appraise_flags(struct inode
*inode
, int digsig
)
370 struct integrity_iint_cache
*iint
;
372 if (!(ima_policy_flag
& IMA_APPRAISE
) || !S_ISREG(inode
->i_mode
))
375 iint
= integrity_iint_find(inode
);
378 iint
->measured_pcrs
= 0;
379 set_bit(IMA_CHANGE_XATTR
, &iint
->atomic_flags
);
381 set_bit(IMA_DIGSIG
, &iint
->atomic_flags
);
383 clear_bit(IMA_DIGSIG
, &iint
->atomic_flags
);
386 int ima_inode_setxattr(struct dentry
*dentry
, const char *xattr_name
,
387 const void *xattr_value
, size_t xattr_value_len
)
389 const struct evm_ima_xattr_data
*xvalue
= xattr_value
;
392 result
= ima_protect_xattr(dentry
, xattr_name
, xattr_value
,
395 if (!xattr_value_len
|| (xvalue
->type
>= IMA_XATTR_LAST
))
397 ima_reset_appraise_flags(d_backing_inode(dentry
),
398 (xvalue
->type
== EVM_IMA_XATTR_DIGSIG
) ? 1 : 0);
404 int ima_inode_removexattr(struct dentry
*dentry
, const char *xattr_name
)
408 result
= ima_protect_xattr(dentry
, xattr_name
, NULL
, 0);
410 ima_reset_appraise_flags(d_backing_inode(dentry
), 0);