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 #ifdef CONFIG_IMA_APPRAISE_BOOTPARAM
24 if (strncmp(str
, "off", 3) == 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
;
34 __setup("ima_appraise=", default_appraise_setup
);
37 * is_ima_appraise_enabled - return appraise status
39 * Only return enabled, if not in ima_appraise="fix" or "log" modes.
41 bool is_ima_appraise_enabled(void)
43 return ima_appraise
& IMA_APPRAISE_ENFORCE
;
47 * ima_must_appraise - set appraise flag
49 * Return 1 to appraise or hash
51 int ima_must_appraise(struct inode
*inode
, int mask
, enum ima_hooks func
)
56 return ima_match_policy(inode
, func
, mask
, IMA_APPRAISE
| IMA_HASH
,
60 static int ima_fix_xattr(struct dentry
*dentry
,
61 struct integrity_iint_cache
*iint
)
64 u8 algo
= iint
->ima_hash
->algo
;
66 if (algo
<= HASH_ALGO_SHA1
) {
68 iint
->ima_hash
->xattr
.sha1
.type
= IMA_XATTR_DIGEST
;
71 iint
->ima_hash
->xattr
.ng
.type
= IMA_XATTR_DIGEST_NG
;
72 iint
->ima_hash
->xattr
.ng
.algo
= algo
;
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);
81 /* Return specific func appraised cached result */
82 enum integrity_status
ima_get_cache_status(struct integrity_iint_cache
*iint
,
87 return iint
->ima_mmap_status
;
89 return iint
->ima_bprm_status
;
92 return iint
->ima_file_status
;
93 case MODULE_CHECK
... MAX_CHECK
- 1:
95 return iint
->ima_read_status
;
99 static void ima_set_cache_status(struct integrity_iint_cache
*iint
,
101 enum integrity_status status
)
105 iint
->ima_mmap_status
= status
;
108 iint
->ima_bprm_status
= status
;
112 iint
->ima_file_status
= status
;
114 case MODULE_CHECK
... MAX_CHECK
- 1:
116 iint
->ima_read_status
= status
;
121 static void ima_cache_flags(struct integrity_iint_cache
*iint
,
126 iint
->flags
|= (IMA_MMAP_APPRAISED
| IMA_APPRAISED
);
129 iint
->flags
|= (IMA_BPRM_APPRAISED
| IMA_APPRAISED
);
133 iint
->flags
|= (IMA_FILE_APPRAISED
| IMA_APPRAISED
);
135 case MODULE_CHECK
... MAX_CHECK
- 1:
137 iint
->flags
|= (IMA_READ_APPRAISED
| IMA_APPRAISED
);
142 enum hash_algo
ima_get_hash_algo(struct evm_ima_xattr_data
*xattr_value
,
145 struct signature_v2_hdr
*sig
;
148 if (!xattr_value
|| xattr_len
< 2)
149 /* return default hash algo */
150 return ima_hash_algo
;
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
;
159 case IMA_XATTR_DIGEST_NG
:
160 ret
= xattr_value
->digest
[0];
161 if (ret
< HASH_ALGO__LAST
)
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
;
171 return HASH_ALGO_SHA1
;
172 } else if (xattr_len
== 17)
173 return HASH_ALGO_MD5
;
177 /* return default hash algo */
178 return ima_hash_algo
;
181 int ima_read_xattr(struct dentry
*dentry
,
182 struct evm_ima_xattr_data
**xattr_value
)
186 ret
= vfs_getxattr_alloc(dentry
, XATTR_NAME_IMA
, (char **)xattr_value
,
188 if (ret
== -EOPNOTSUPP
)
194 * ima_appraise_measurement - appraise file measurement
196 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
197 * Assuming success, compare the xattr hash with the collected measurement.
199 * Return 0 on success, error code otherwise
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
)
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;
214 if (!(inode
->i_opflags
& IOP_XATTR
))
215 return INTEGRITY_UNKNOWN
;
218 if (rc
&& rc
!= -ENODATA
)
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
;
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";
244 switch (xattr_value
->type
) {
245 case IMA_XATTR_DIGEST_NG
:
246 /* first byte contains algorithm id */
249 case IMA_XATTR_DIGEST
:
250 if (iint
->flags
& IMA_DIGSIG_REQUIRED
) {
251 cause
= "IMA-signature-required";
252 status
= INTEGRITY_FAIL
;
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
261 rc
= memcmp(&xattr_value
->digest
[hash_start
],
262 iint
->ima_hash
->digest
,
263 iint
->ima_hash
->length
);
267 cause
= "invalid-hash";
268 status
= INTEGRITY_FAIL
;
271 status
= INTEGRITY_PASS
;
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
;
282 cause
= "invalid-signature";
283 status
= INTEGRITY_FAIL
;
285 status
= INTEGRITY_PASS
;
289 status
= INTEGRITY_UNKNOWN
;
290 cause
= "unknown-ima-data";
295 if (status
!= INTEGRITY_PASS
) {
296 if ((ima_appraise
& IMA_APPRAISE_FIX
) &&
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
) &&
304 xattr_value
->type
== EVM_IMA_XATTR_DIGSIG
)) {
305 status
= INTEGRITY_PASS
;
307 integrity_audit_msg(AUDIT_INTEGRITY_DATA
, inode
, filename
,
310 ima_cache_flags(iint
, func
);
312 ima_set_cache_status(iint
, func
, status
);
317 * ima_update_xattr - update 'security.ima' hash value
319 void ima_update_xattr(struct integrity_iint_cache
*iint
, struct file
*file
)
321 struct dentry
*dentry
= file_dentry(file
);
324 /* do not collect and update hash for digital signatures */
325 if (test_bit(IMA_DIGSIG
, &iint
->atomic_flags
))
328 if ((iint
->ima_file_status
!= INTEGRITY_PASS
) &&
329 !(iint
->flags
& IMA_HASH
))
332 rc
= ima_collect_measurement(iint
, file
, NULL
, 0, ima_hash_algo
);
336 inode_lock(file_inode(file
));
337 ima_fix_xattr(dentry
, iint
);
338 inode_unlock(file_inode(file
));
342 * ima_inode_post_setattr - reflect file metadata changes
343 * @dentry: pointer to the affected dentry
345 * Changes to a dentry's metadata might result in needing to appraise.
347 * This function is called from notify_change(), which expects the caller
348 * to lock the inode's i_mutex.
350 void ima_inode_post_setattr(struct dentry
*dentry
)
352 struct inode
*inode
= d_backing_inode(dentry
);
353 struct integrity_iint_cache
*iint
;
356 if (!(ima_policy_flag
& IMA_APPRAISE
) || !S_ISREG(inode
->i_mode
)
357 || !(inode
->i_opflags
& IOP_XATTR
))
360 action
= ima_must_appraise(inode
, MAY_ACCESS
, POST_SETATTR
);
362 __vfs_removexattr(dentry
, XATTR_NAME_IMA
);
363 iint
= integrity_iint_find(inode
);
365 set_bit(IMA_CHANGE_ATTR
, &iint
->atomic_flags
);
367 clear_bit(IMA_UPDATE_XATTR
, &iint
->atomic_flags
);
372 * ima_protect_xattr - protect 'security.ima'
374 * Ensure that not just anyone can modify or remove 'security.ima'.
376 static int ima_protect_xattr(struct dentry
*dentry
, const char *xattr_name
,
377 const void *xattr_value
, size_t xattr_value_len
)
379 if (strcmp(xattr_name
, XATTR_NAME_IMA
) == 0) {
380 if (!capable(CAP_SYS_ADMIN
))
387 static void ima_reset_appraise_flags(struct inode
*inode
, int digsig
)
389 struct integrity_iint_cache
*iint
;
391 if (!(ima_policy_flag
& IMA_APPRAISE
) || !S_ISREG(inode
->i_mode
))
394 iint
= integrity_iint_find(inode
);
397 iint
->measured_pcrs
= 0;
398 set_bit(IMA_CHANGE_XATTR
, &iint
->atomic_flags
);
400 set_bit(IMA_DIGSIG
, &iint
->atomic_flags
);
402 clear_bit(IMA_DIGSIG
, &iint
->atomic_flags
);
405 int ima_inode_setxattr(struct dentry
*dentry
, const char *xattr_name
,
406 const void *xattr_value
, size_t xattr_value_len
)
408 const struct evm_ima_xattr_data
*xvalue
= xattr_value
;
411 result
= ima_protect_xattr(dentry
, xattr_name
, xattr_value
,
414 if (!xattr_value_len
|| (xvalue
->type
>= IMA_XATTR_LAST
))
416 ima_reset_appraise_flags(d_backing_inode(dentry
),
417 xvalue
->type
== EVM_IMA_XATTR_DIGSIG
);
423 int ima_inode_removexattr(struct dentry
*dentry
, const char *xattr_name
)
427 result
= ima_protect_xattr(dentry
, xattr_name
, NULL
, 0);
429 ima_reset_appraise_flags(d_backing_inode(dentry
), 0);