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>
18 #include <crypto/hash_info.h>
22 static int __init
default_appraise_setup(char *str
)
24 if (strncmp(str
, "off", 3) == 0)
26 else if (strncmp(str
, "fix", 3) == 0)
27 ima_appraise
= IMA_APPRAISE_FIX
;
31 __setup("ima_appraise=", default_appraise_setup
);
34 * ima_must_appraise - set appraise flag
36 * Return 1 to appraise
38 int ima_must_appraise(struct inode
*inode
, int mask
, enum ima_hooks func
)
43 return ima_match_policy(inode
, func
, mask
, IMA_APPRAISE
);
46 static int ima_fix_xattr(struct dentry
*dentry
,
47 struct integrity_iint_cache
*iint
)
50 u8 algo
= iint
->ima_hash
->algo
;
52 if (algo
<= HASH_ALGO_SHA1
) {
54 iint
->ima_hash
->xattr
.sha1
.type
= IMA_XATTR_DIGEST
;
57 iint
->ima_hash
->xattr
.ng
.type
= IMA_XATTR_DIGEST_NG
;
58 iint
->ima_hash
->xattr
.ng
.algo
= algo
;
60 rc
= __vfs_setxattr_noperm(dentry
, XATTR_NAME_IMA
,
61 &iint
->ima_hash
->xattr
.data
[offset
],
62 (sizeof(iint
->ima_hash
->xattr
) - offset
) +
63 iint
->ima_hash
->length
, 0);
67 /* Return specific func appraised cached result */
68 enum integrity_status
ima_get_cache_status(struct integrity_iint_cache
*iint
,
73 return iint
->ima_mmap_status
;
75 return iint
->ima_bprm_status
;
77 return iint
->ima_module_status
;
80 return iint
->ima_file_status
;
84 static void ima_set_cache_status(struct integrity_iint_cache
*iint
,
85 int func
, enum integrity_status status
)
89 iint
->ima_mmap_status
= status
;
92 iint
->ima_bprm_status
= status
;
95 iint
->ima_module_status
= status
;
99 iint
->ima_file_status
= status
;
104 static void ima_cache_flags(struct integrity_iint_cache
*iint
, int func
)
108 iint
->flags
|= (IMA_MMAP_APPRAISED
| IMA_APPRAISED
);
111 iint
->flags
|= (IMA_BPRM_APPRAISED
| IMA_APPRAISED
);
114 iint
->flags
|= (IMA_MODULE_APPRAISED
| IMA_APPRAISED
);
118 iint
->flags
|= (IMA_FILE_APPRAISED
| IMA_APPRAISED
);
123 void ima_get_hash_algo(struct evm_ima_xattr_data
*xattr_value
, int xattr_len
,
124 struct ima_digest_data
*hash
)
126 struct signature_v2_hdr
*sig
;
128 if (!xattr_value
|| xattr_len
< 2)
131 switch (xattr_value
->type
) {
132 case EVM_IMA_XATTR_DIGSIG
:
133 sig
= (typeof(sig
))xattr_value
;
134 if (sig
->version
!= 2 || xattr_len
<= sizeof(*sig
))
136 hash
->algo
= sig
->hash_algo
;
138 case IMA_XATTR_DIGEST_NG
:
139 hash
->algo
= xattr_value
->digest
[0];
141 case IMA_XATTR_DIGEST
:
142 /* this is for backward compatibility */
143 if (xattr_len
== 21) {
144 unsigned int zero
= 0;
145 if (!memcmp(&xattr_value
->digest
[16], &zero
, 4))
146 hash
->algo
= HASH_ALGO_MD5
;
148 hash
->algo
= HASH_ALGO_SHA1
;
149 } else if (xattr_len
== 17)
150 hash
->algo
= HASH_ALGO_MD5
;
155 int ima_read_xattr(struct dentry
*dentry
,
156 struct evm_ima_xattr_data
**xattr_value
)
158 struct inode
*inode
= dentry
->d_inode
;
160 if (!inode
->i_op
->getxattr
)
163 return vfs_getxattr_alloc(dentry
, XATTR_NAME_IMA
, (char **)xattr_value
,
168 * ima_appraise_measurement - appraise file measurement
170 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
171 * Assuming success, compare the xattr hash with the collected measurement.
173 * Return 0 on success, error code otherwise
175 int ima_appraise_measurement(int func
, struct integrity_iint_cache
*iint
,
176 struct file
*file
, const unsigned char *filename
,
177 struct evm_ima_xattr_data
*xattr_value
,
180 static const char op
[] = "appraise_data";
181 char *cause
= "unknown";
182 struct dentry
*dentry
= file
->f_dentry
;
183 struct inode
*inode
= dentry
->d_inode
;
184 enum integrity_status status
= INTEGRITY_UNKNOWN
;
185 int rc
= xattr_len
, hash_start
= 0;
189 if (!inode
->i_op
->getxattr
)
190 return INTEGRITY_UNKNOWN
;
193 if (rc
&& rc
!= -ENODATA
)
196 cause
= "missing-hash";
198 (inode
->i_size
== 0) ? INTEGRITY_PASS
: INTEGRITY_NOLABEL
;
202 status
= evm_verifyxattr(dentry
, XATTR_NAME_IMA
, xattr_value
, rc
, iint
);
203 if ((status
!= INTEGRITY_PASS
) && (status
!= INTEGRITY_UNKNOWN
)) {
204 if ((status
== INTEGRITY_NOLABEL
)
205 || (status
== INTEGRITY_NOXATTRS
))
206 cause
= "missing-HMAC";
207 else if (status
== INTEGRITY_FAIL
)
208 cause
= "invalid-HMAC";
211 switch (xattr_value
->type
) {
212 case IMA_XATTR_DIGEST_NG
:
213 /* first byte contains algorithm id */
215 case IMA_XATTR_DIGEST
:
216 if (iint
->flags
& IMA_DIGSIG_REQUIRED
) {
217 cause
= "IMA-signature-required";
218 status
= INTEGRITY_FAIL
;
221 if (xattr_len
- sizeof(xattr_value
->type
) - hash_start
>=
222 iint
->ima_hash
->length
)
223 /* xattr length may be longer. md5 hash in previous
224 version occupied 20 bytes in xattr, instead of 16
226 rc
= memcmp(&xattr_value
->digest
[hash_start
],
227 iint
->ima_hash
->digest
,
228 iint
->ima_hash
->length
);
232 cause
= "invalid-hash";
233 status
= INTEGRITY_FAIL
;
236 status
= INTEGRITY_PASS
;
238 case EVM_IMA_XATTR_DIGSIG
:
239 iint
->flags
|= IMA_DIGSIG
;
240 rc
= integrity_digsig_verify(INTEGRITY_KEYRING_IMA
,
241 (const char *)xattr_value
, rc
,
242 iint
->ima_hash
->digest
,
243 iint
->ima_hash
->length
);
244 if (rc
== -EOPNOTSUPP
) {
245 status
= INTEGRITY_UNKNOWN
;
247 cause
= "invalid-signature";
248 status
= INTEGRITY_FAIL
;
250 status
= INTEGRITY_PASS
;
254 status
= INTEGRITY_UNKNOWN
;
255 cause
= "unknown-ima-data";
260 if (status
!= INTEGRITY_PASS
) {
261 if ((ima_appraise
& IMA_APPRAISE_FIX
) &&
263 xattr_value
->type
!= EVM_IMA_XATTR_DIGSIG
)) {
264 if (!ima_fix_xattr(dentry
, iint
))
265 status
= INTEGRITY_PASS
;
267 integrity_audit_msg(AUDIT_INTEGRITY_DATA
, inode
, filename
,
270 ima_cache_flags(iint
, func
);
272 ima_set_cache_status(iint
, func
, status
);
277 * ima_update_xattr - update 'security.ima' hash value
279 void ima_update_xattr(struct integrity_iint_cache
*iint
, struct file
*file
)
281 struct dentry
*dentry
= file
->f_dentry
;
284 /* do not collect and update hash for digital signatures */
285 if (iint
->flags
& IMA_DIGSIG
)
288 rc
= ima_collect_measurement(iint
, file
, NULL
, NULL
);
292 ima_fix_xattr(dentry
, iint
);
296 * ima_inode_post_setattr - reflect file metadata changes
297 * @dentry: pointer to the affected dentry
299 * Changes to a dentry's metadata might result in needing to appraise.
301 * This function is called from notify_change(), which expects the caller
302 * to lock the inode's i_mutex.
304 void ima_inode_post_setattr(struct dentry
*dentry
)
306 struct inode
*inode
= dentry
->d_inode
;
307 struct integrity_iint_cache
*iint
;
308 int must_appraise
, rc
;
310 if (!ima_initialized
|| !ima_appraise
|| !S_ISREG(inode
->i_mode
)
311 || !inode
->i_op
->removexattr
)
314 must_appraise
= ima_must_appraise(inode
, MAY_ACCESS
, POST_SETATTR
);
315 iint
= integrity_iint_find(inode
);
317 iint
->flags
&= ~(IMA_APPRAISE
| IMA_APPRAISED
|
318 IMA_APPRAISE_SUBMASK
| IMA_APPRAISED_SUBMASK
|
321 iint
->flags
|= IMA_APPRAISE
;
324 rc
= inode
->i_op
->removexattr(dentry
, XATTR_NAME_IMA
);
329 * ima_protect_xattr - protect 'security.ima'
331 * Ensure that not just anyone can modify or remove 'security.ima'.
333 static int ima_protect_xattr(struct dentry
*dentry
, const char *xattr_name
,
334 const void *xattr_value
, size_t xattr_value_len
)
336 if (strcmp(xattr_name
, XATTR_NAME_IMA
) == 0) {
337 if (!capable(CAP_SYS_ADMIN
))
344 static void ima_reset_appraise_flags(struct inode
*inode
, int digsig
)
346 struct integrity_iint_cache
*iint
;
348 if (!ima_initialized
|| !ima_appraise
|| !S_ISREG(inode
->i_mode
))
351 iint
= integrity_iint_find(inode
);
355 iint
->flags
&= ~IMA_DONE_MASK
;
357 iint
->flags
|= IMA_DIGSIG
;
361 int ima_inode_setxattr(struct dentry
*dentry
, const char *xattr_name
,
362 const void *xattr_value
, size_t xattr_value_len
)
364 const struct evm_ima_xattr_data
*xvalue
= xattr_value
;
367 result
= ima_protect_xattr(dentry
, xattr_name
, xattr_value
,
370 ima_reset_appraise_flags(dentry
->d_inode
,
371 (xvalue
->type
== EVM_IMA_XATTR_DIGSIG
) ? 1 : 0);
377 int ima_inode_removexattr(struct dentry
*dentry
, const char *xattr_name
)
381 result
= ima_protect_xattr(dentry
, xattr_name
, NULL
, 0);
383 ima_reset_appraise_flags(dentry
->d_inode
, 0);