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
)
58 security_task_getsecid(current
, &secid
);
59 return ima_match_policy(inode
, current_cred(), secid
, func
, mask
,
60 IMA_APPRAISE
| IMA_HASH
, NULL
);
63 static int ima_fix_xattr(struct dentry
*dentry
,
64 struct integrity_iint_cache
*iint
)
67 u8 algo
= iint
->ima_hash
->algo
;
69 if (algo
<= HASH_ALGO_SHA1
) {
71 iint
->ima_hash
->xattr
.sha1
.type
= IMA_XATTR_DIGEST
;
74 iint
->ima_hash
->xattr
.ng
.type
= IMA_XATTR_DIGEST_NG
;
75 iint
->ima_hash
->xattr
.ng
.algo
= algo
;
77 rc
= __vfs_setxattr_noperm(dentry
, XATTR_NAME_IMA
,
78 &iint
->ima_hash
->xattr
.data
[offset
],
79 (sizeof(iint
->ima_hash
->xattr
) - offset
) +
80 iint
->ima_hash
->length
, 0);
84 /* Return specific func appraised cached result */
85 enum integrity_status
ima_get_cache_status(struct integrity_iint_cache
*iint
,
90 return iint
->ima_mmap_status
;
92 return iint
->ima_bprm_status
;
94 return iint
->ima_creds_status
;
97 return iint
->ima_file_status
;
98 case MODULE_CHECK
... MAX_CHECK
- 1:
100 return iint
->ima_read_status
;
104 static void ima_set_cache_status(struct integrity_iint_cache
*iint
,
106 enum integrity_status status
)
110 iint
->ima_mmap_status
= status
;
113 iint
->ima_bprm_status
= status
;
116 iint
->ima_creds_status
= status
;
119 iint
->ima_file_status
= status
;
121 case MODULE_CHECK
... MAX_CHECK
- 1:
123 iint
->ima_read_status
= status
;
128 static void ima_cache_flags(struct integrity_iint_cache
*iint
,
133 iint
->flags
|= (IMA_MMAP_APPRAISED
| IMA_APPRAISED
);
136 iint
->flags
|= (IMA_BPRM_APPRAISED
| IMA_APPRAISED
);
139 iint
->flags
|= (IMA_CREDS_APPRAISED
| IMA_APPRAISED
);
143 iint
->flags
|= (IMA_FILE_APPRAISED
| IMA_APPRAISED
);
145 case MODULE_CHECK
... MAX_CHECK
- 1:
147 iint
->flags
|= (IMA_READ_APPRAISED
| IMA_APPRAISED
);
152 enum hash_algo
ima_get_hash_algo(struct evm_ima_xattr_data
*xattr_value
,
155 struct signature_v2_hdr
*sig
;
158 if (!xattr_value
|| xattr_len
< 2)
159 /* return default hash algo */
160 return ima_hash_algo
;
162 switch (xattr_value
->type
) {
163 case EVM_IMA_XATTR_DIGSIG
:
164 sig
= (typeof(sig
))xattr_value
;
165 if (sig
->version
!= 2 || xattr_len
<= sizeof(*sig
))
166 return ima_hash_algo
;
167 return sig
->hash_algo
;
169 case IMA_XATTR_DIGEST_NG
:
170 ret
= xattr_value
->digest
[0];
171 if (ret
< HASH_ALGO__LAST
)
174 case IMA_XATTR_DIGEST
:
175 /* this is for backward compatibility */
176 if (xattr_len
== 21) {
177 unsigned int zero
= 0;
178 if (!memcmp(&xattr_value
->digest
[16], &zero
, 4))
179 return HASH_ALGO_MD5
;
181 return HASH_ALGO_SHA1
;
182 } else if (xattr_len
== 17)
183 return HASH_ALGO_MD5
;
187 /* return default hash algo */
188 return ima_hash_algo
;
191 int ima_read_xattr(struct dentry
*dentry
,
192 struct evm_ima_xattr_data
**xattr_value
)
196 ret
= vfs_getxattr_alloc(dentry
, XATTR_NAME_IMA
, (char **)xattr_value
,
198 if (ret
== -EOPNOTSUPP
)
204 * ima_appraise_measurement - appraise file measurement
206 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
207 * Assuming success, compare the xattr hash with the collected measurement.
209 * Return 0 on success, error code otherwise
211 int ima_appraise_measurement(enum ima_hooks func
,
212 struct integrity_iint_cache
*iint
,
213 struct file
*file
, const unsigned char *filename
,
214 struct evm_ima_xattr_data
*xattr_value
,
217 static const char op
[] = "appraise_data";
218 const char *cause
= "unknown";
219 struct dentry
*dentry
= file_dentry(file
);
220 struct inode
*inode
= d_backing_inode(dentry
);
221 enum integrity_status status
= INTEGRITY_UNKNOWN
;
222 int rc
= xattr_len
, hash_start
= 0;
224 if (!(inode
->i_opflags
& IOP_XATTR
))
225 return INTEGRITY_UNKNOWN
;
228 if (rc
&& rc
!= -ENODATA
)
231 cause
= iint
->flags
& IMA_DIGSIG_REQUIRED
?
232 "IMA-signature-required" : "missing-hash";
233 status
= INTEGRITY_NOLABEL
;
234 if (file
->f_mode
& FMODE_CREATED
)
235 iint
->flags
|= IMA_NEW_FILE
;
236 if ((iint
->flags
& IMA_NEW_FILE
) &&
237 (!(iint
->flags
& IMA_DIGSIG_REQUIRED
) ||
238 (inode
->i_size
== 0)))
239 status
= INTEGRITY_PASS
;
243 status
= evm_verifyxattr(dentry
, XATTR_NAME_IMA
, xattr_value
, rc
, iint
);
246 case INTEGRITY_PASS_IMMUTABLE
:
247 case INTEGRITY_UNKNOWN
:
249 case INTEGRITY_NOXATTRS
: /* No EVM protected xattrs. */
250 case INTEGRITY_NOLABEL
: /* No security.evm xattr. */
251 cause
= "missing-HMAC";
253 case INTEGRITY_FAIL
: /* Invalid HMAC/signature. */
254 cause
= "invalid-HMAC";
257 WARN_ONCE(true, "Unexpected integrity status %d\n", status
);
260 switch (xattr_value
->type
) {
261 case IMA_XATTR_DIGEST_NG
:
262 /* first byte contains algorithm id */
265 case IMA_XATTR_DIGEST
:
266 if (iint
->flags
& IMA_DIGSIG_REQUIRED
) {
267 cause
= "IMA-signature-required";
268 status
= INTEGRITY_FAIL
;
271 clear_bit(IMA_DIGSIG
, &iint
->atomic_flags
);
272 if (xattr_len
- sizeof(xattr_value
->type
) - hash_start
>=
273 iint
->ima_hash
->length
)
274 /* xattr length may be longer. md5 hash in previous
275 version occupied 20 bytes in xattr, instead of 16
277 rc
= memcmp(&xattr_value
->digest
[hash_start
],
278 iint
->ima_hash
->digest
,
279 iint
->ima_hash
->length
);
283 cause
= "invalid-hash";
284 status
= INTEGRITY_FAIL
;
287 status
= INTEGRITY_PASS
;
289 case EVM_IMA_XATTR_DIGSIG
:
290 set_bit(IMA_DIGSIG
, &iint
->atomic_flags
);
291 rc
= integrity_digsig_verify(INTEGRITY_KEYRING_IMA
,
292 (const char *)xattr_value
, rc
,
293 iint
->ima_hash
->digest
,
294 iint
->ima_hash
->length
);
295 if (rc
== -EOPNOTSUPP
) {
296 status
= INTEGRITY_UNKNOWN
;
298 cause
= "invalid-signature";
299 status
= INTEGRITY_FAIL
;
301 status
= INTEGRITY_PASS
;
305 status
= INTEGRITY_UNKNOWN
;
306 cause
= "unknown-ima-data";
312 * File signatures on some filesystems can not be properly verified.
313 * When such filesystems are mounted by an untrusted mounter or on a
314 * system not willing to accept such a risk, fail the file signature
317 if ((inode
->i_sb
->s_iflags
& SB_I_IMA_UNVERIFIABLE_SIGNATURE
) &&
318 ((inode
->i_sb
->s_iflags
& SB_I_UNTRUSTED_MOUNTER
) ||
319 (iint
->flags
& IMA_FAIL_UNVERIFIABLE_SIGS
))) {
320 status
= INTEGRITY_FAIL
;
321 cause
= "unverifiable-signature";
322 integrity_audit_msg(AUDIT_INTEGRITY_DATA
, inode
, filename
,
324 } else if (status
!= INTEGRITY_PASS
) {
325 /* Fix mode, but don't replace file signatures. */
326 if ((ima_appraise
& IMA_APPRAISE_FIX
) &&
328 xattr_value
->type
!= EVM_IMA_XATTR_DIGSIG
)) {
329 if (!ima_fix_xattr(dentry
, iint
))
330 status
= INTEGRITY_PASS
;
333 /* Permit new files with file signatures, but without data. */
334 if (inode
->i_size
== 0 && iint
->flags
& IMA_NEW_FILE
&&
335 xattr_value
&& xattr_value
->type
== EVM_IMA_XATTR_DIGSIG
) {
336 status
= INTEGRITY_PASS
;
339 integrity_audit_msg(AUDIT_INTEGRITY_DATA
, inode
, filename
,
342 ima_cache_flags(iint
, func
);
345 ima_set_cache_status(iint
, func
, status
);
350 * ima_update_xattr - update 'security.ima' hash value
352 void ima_update_xattr(struct integrity_iint_cache
*iint
, struct file
*file
)
354 struct dentry
*dentry
= file_dentry(file
);
357 /* do not collect and update hash for digital signatures */
358 if (test_bit(IMA_DIGSIG
, &iint
->atomic_flags
))
361 if ((iint
->ima_file_status
!= INTEGRITY_PASS
) &&
362 !(iint
->flags
& IMA_HASH
))
365 rc
= ima_collect_measurement(iint
, file
, NULL
, 0, ima_hash_algo
);
369 inode_lock(file_inode(file
));
370 ima_fix_xattr(dentry
, iint
);
371 inode_unlock(file_inode(file
));
375 * ima_inode_post_setattr - reflect file metadata changes
376 * @dentry: pointer to the affected dentry
378 * Changes to a dentry's metadata might result in needing to appraise.
380 * This function is called from notify_change(), which expects the caller
381 * to lock the inode's i_mutex.
383 void ima_inode_post_setattr(struct dentry
*dentry
)
385 struct inode
*inode
= d_backing_inode(dentry
);
386 struct integrity_iint_cache
*iint
;
389 if (!(ima_policy_flag
& IMA_APPRAISE
) || !S_ISREG(inode
->i_mode
)
390 || !(inode
->i_opflags
& IOP_XATTR
))
393 action
= ima_must_appraise(inode
, MAY_ACCESS
, POST_SETATTR
);
395 __vfs_removexattr(dentry
, XATTR_NAME_IMA
);
396 iint
= integrity_iint_find(inode
);
398 set_bit(IMA_CHANGE_ATTR
, &iint
->atomic_flags
);
400 clear_bit(IMA_UPDATE_XATTR
, &iint
->atomic_flags
);
405 * ima_protect_xattr - protect 'security.ima'
407 * Ensure that not just anyone can modify or remove 'security.ima'.
409 static int ima_protect_xattr(struct dentry
*dentry
, const char *xattr_name
,
410 const void *xattr_value
, size_t xattr_value_len
)
412 if (strcmp(xattr_name
, XATTR_NAME_IMA
) == 0) {
413 if (!capable(CAP_SYS_ADMIN
))
420 static void ima_reset_appraise_flags(struct inode
*inode
, int digsig
)
422 struct integrity_iint_cache
*iint
;
424 if (!(ima_policy_flag
& IMA_APPRAISE
) || !S_ISREG(inode
->i_mode
))
427 iint
= integrity_iint_find(inode
);
430 iint
->measured_pcrs
= 0;
431 set_bit(IMA_CHANGE_XATTR
, &iint
->atomic_flags
);
433 set_bit(IMA_DIGSIG
, &iint
->atomic_flags
);
435 clear_bit(IMA_DIGSIG
, &iint
->atomic_flags
);
438 int ima_inode_setxattr(struct dentry
*dentry
, const char *xattr_name
,
439 const void *xattr_value
, size_t xattr_value_len
)
441 const struct evm_ima_xattr_data
*xvalue
= xattr_value
;
444 result
= ima_protect_xattr(dentry
, xattr_name
, xattr_value
,
447 if (!xattr_value_len
|| (xvalue
->type
>= IMA_XATTR_LAST
))
449 ima_reset_appraise_flags(d_backing_inode(dentry
),
450 xvalue
->type
== EVM_IMA_XATTR_DIGSIG
);
456 int ima_inode_removexattr(struct dentry
*dentry
, const char *xattr_name
)
460 result
= ima_protect_xattr(dentry
, xattr_name
, NULL
, 0);
462 ima_reset_appraise_flags(d_backing_inode(dentry
), 0);