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
, "log", 3) == 0)
27 ima_appraise
= IMA_APPRAISE_LOG
;
28 else if (strncmp(str
, "fix", 3) == 0)
29 ima_appraise
= IMA_APPRAISE_FIX
;
33 __setup("ima_appraise=", default_appraise_setup
);
36 * ima_must_appraise - set appraise flag
38 * Return 1 to appraise
40 int ima_must_appraise(struct inode
*inode
, int mask
, enum ima_hooks func
)
45 return ima_match_policy(inode
, func
, mask
, IMA_APPRAISE
);
48 static int ima_fix_xattr(struct dentry
*dentry
,
49 struct integrity_iint_cache
*iint
)
52 u8 algo
= iint
->ima_hash
->algo
;
54 if (algo
<= HASH_ALGO_SHA1
) {
56 iint
->ima_hash
->xattr
.sha1
.type
= IMA_XATTR_DIGEST
;
59 iint
->ima_hash
->xattr
.ng
.type
= IMA_XATTR_DIGEST_NG
;
60 iint
->ima_hash
->xattr
.ng
.algo
= algo
;
62 rc
= __vfs_setxattr_noperm(dentry
, XATTR_NAME_IMA
,
63 &iint
->ima_hash
->xattr
.data
[offset
],
64 (sizeof(iint
->ima_hash
->xattr
) - offset
) +
65 iint
->ima_hash
->length
, 0);
69 /* Return specific func appraised cached result */
70 enum integrity_status
ima_get_cache_status(struct integrity_iint_cache
*iint
,
75 return iint
->ima_mmap_status
;
77 return iint
->ima_bprm_status
;
79 return iint
->ima_module_status
;
81 return iint
->ima_firmware_status
;
84 return iint
->ima_file_status
;
88 static void ima_set_cache_status(struct integrity_iint_cache
*iint
,
89 int func
, enum integrity_status status
)
93 iint
->ima_mmap_status
= status
;
96 iint
->ima_bprm_status
= status
;
99 iint
->ima_module_status
= status
;
102 iint
->ima_firmware_status
= status
;
106 iint
->ima_file_status
= status
;
111 static void ima_cache_flags(struct integrity_iint_cache
*iint
, int func
)
115 iint
->flags
|= (IMA_MMAP_APPRAISED
| IMA_APPRAISED
);
118 iint
->flags
|= (IMA_BPRM_APPRAISED
| IMA_APPRAISED
);
121 iint
->flags
|= (IMA_MODULE_APPRAISED
| IMA_APPRAISED
);
124 iint
->flags
|= (IMA_FIRMWARE_APPRAISED
| IMA_APPRAISED
);
128 iint
->flags
|= (IMA_FILE_APPRAISED
| IMA_APPRAISED
);
133 void ima_get_hash_algo(struct evm_ima_xattr_data
*xattr_value
, int xattr_len
,
134 struct ima_digest_data
*hash
)
136 struct signature_v2_hdr
*sig
;
138 if (!xattr_value
|| xattr_len
< 2)
141 switch (xattr_value
->type
) {
142 case EVM_IMA_XATTR_DIGSIG
:
143 sig
= (typeof(sig
))xattr_value
;
144 if (sig
->version
!= 2 || xattr_len
<= sizeof(*sig
))
146 hash
->algo
= sig
->hash_algo
;
148 case IMA_XATTR_DIGEST_NG
:
149 hash
->algo
= xattr_value
->digest
[0];
151 case IMA_XATTR_DIGEST
:
152 /* this is for backward compatibility */
153 if (xattr_len
== 21) {
154 unsigned int zero
= 0;
155 if (!memcmp(&xattr_value
->digest
[16], &zero
, 4))
156 hash
->algo
= HASH_ALGO_MD5
;
158 hash
->algo
= HASH_ALGO_SHA1
;
159 } else if (xattr_len
== 17)
160 hash
->algo
= HASH_ALGO_MD5
;
165 int ima_read_xattr(struct dentry
*dentry
,
166 struct evm_ima_xattr_data
**xattr_value
)
168 struct inode
*inode
= d_backing_inode(dentry
);
170 if (!inode
->i_op
->getxattr
)
173 return vfs_getxattr_alloc(dentry
, XATTR_NAME_IMA
, (char **)xattr_value
,
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(int func
, struct integrity_iint_cache
*iint
,
186 struct file
*file
, const unsigned char *filename
,
187 struct evm_ima_xattr_data
*xattr_value
,
188 int xattr_len
, int opened
)
190 static const char op
[] = "appraise_data";
191 char *cause
= "unknown";
192 struct dentry
*dentry
= file_dentry(file
);
193 struct inode
*inode
= d_backing_inode(dentry
);
194 enum integrity_status status
= INTEGRITY_UNKNOWN
;
195 int rc
= xattr_len
, hash_start
= 0;
197 if (!inode
->i_op
->getxattr
)
198 return INTEGRITY_UNKNOWN
;
201 if (rc
&& rc
!= -ENODATA
)
204 cause
= "missing-hash";
205 status
= INTEGRITY_NOLABEL
;
206 if (opened
& FILE_CREATED
)
207 iint
->flags
|= IMA_NEW_FILE
;
208 if ((iint
->flags
& IMA_NEW_FILE
) &&
209 (!(iint
->flags
& IMA_DIGSIG_REQUIRED
) ||
210 (inode
->i_size
== 0)))
211 status
= INTEGRITY_PASS
;
215 status
= evm_verifyxattr(dentry
, XATTR_NAME_IMA
, xattr_value
, rc
, iint
);
216 if ((status
!= INTEGRITY_PASS
) && (status
!= INTEGRITY_UNKNOWN
)) {
217 if ((status
== INTEGRITY_NOLABEL
)
218 || (status
== INTEGRITY_NOXATTRS
))
219 cause
= "missing-HMAC";
220 else if (status
== INTEGRITY_FAIL
)
221 cause
= "invalid-HMAC";
224 switch (xattr_value
->type
) {
225 case IMA_XATTR_DIGEST_NG
:
226 /* first byte contains algorithm id */
228 case IMA_XATTR_DIGEST
:
229 if (iint
->flags
& IMA_DIGSIG_REQUIRED
) {
230 cause
= "IMA-signature-required";
231 status
= INTEGRITY_FAIL
;
234 if (xattr_len
- sizeof(xattr_value
->type
) - hash_start
>=
235 iint
->ima_hash
->length
)
236 /* xattr length may be longer. md5 hash in previous
237 version occupied 20 bytes in xattr, instead of 16
239 rc
= memcmp(&xattr_value
->digest
[hash_start
],
240 iint
->ima_hash
->digest
,
241 iint
->ima_hash
->length
);
245 cause
= "invalid-hash";
246 status
= INTEGRITY_FAIL
;
249 status
= INTEGRITY_PASS
;
251 case EVM_IMA_XATTR_DIGSIG
:
252 iint
->flags
|= IMA_DIGSIG
;
253 rc
= integrity_digsig_verify(INTEGRITY_KEYRING_IMA
,
254 (const char *)xattr_value
, rc
,
255 iint
->ima_hash
->digest
,
256 iint
->ima_hash
->length
);
257 if (rc
== -EOPNOTSUPP
) {
258 status
= INTEGRITY_UNKNOWN
;
260 cause
= "invalid-signature";
261 status
= INTEGRITY_FAIL
;
263 status
= INTEGRITY_PASS
;
267 status
= INTEGRITY_UNKNOWN
;
268 cause
= "unknown-ima-data";
273 if (status
!= INTEGRITY_PASS
) {
274 if ((ima_appraise
& IMA_APPRAISE_FIX
) &&
276 xattr_value
->type
!= EVM_IMA_XATTR_DIGSIG
)) {
277 if (!ima_fix_xattr(dentry
, iint
))
278 status
= INTEGRITY_PASS
;
280 integrity_audit_msg(AUDIT_INTEGRITY_DATA
, inode
, filename
,
283 ima_cache_flags(iint
, func
);
285 ima_set_cache_status(iint
, func
, status
);
290 * ima_update_xattr - update 'security.ima' hash value
292 void ima_update_xattr(struct integrity_iint_cache
*iint
, struct file
*file
)
294 struct dentry
*dentry
= file_dentry(file
);
297 /* do not collect and update hash for digital signatures */
298 if (iint
->flags
& IMA_DIGSIG
)
301 if (iint
->ima_file_status
!= INTEGRITY_PASS
)
304 rc
= ima_collect_measurement(iint
, file
, NULL
, NULL
);
308 ima_fix_xattr(dentry
, iint
);
312 * ima_inode_post_setattr - reflect file metadata changes
313 * @dentry: pointer to the affected dentry
315 * Changes to a dentry's metadata might result in needing to appraise.
317 * This function is called from notify_change(), which expects the caller
318 * to lock the inode's i_mutex.
320 void ima_inode_post_setattr(struct dentry
*dentry
)
322 struct inode
*inode
= d_backing_inode(dentry
);
323 struct integrity_iint_cache
*iint
;
324 int must_appraise
, rc
;
326 if (!(ima_policy_flag
& IMA_APPRAISE
) || !S_ISREG(inode
->i_mode
)
327 || !inode
->i_op
->removexattr
)
330 must_appraise
= ima_must_appraise(inode
, MAY_ACCESS
, POST_SETATTR
);
331 iint
= integrity_iint_find(inode
);
333 iint
->flags
&= ~(IMA_APPRAISE
| IMA_APPRAISED
|
334 IMA_APPRAISE_SUBMASK
| IMA_APPRAISED_SUBMASK
|
337 iint
->flags
|= IMA_APPRAISE
;
340 rc
= inode
->i_op
->removexattr(dentry
, XATTR_NAME_IMA
);
345 * ima_protect_xattr - protect 'security.ima'
347 * Ensure that not just anyone can modify or remove 'security.ima'.
349 static int ima_protect_xattr(struct dentry
*dentry
, const char *xattr_name
,
350 const void *xattr_value
, size_t xattr_value_len
)
352 if (strcmp(xattr_name
, XATTR_NAME_IMA
) == 0) {
353 if (!capable(CAP_SYS_ADMIN
))
360 static void ima_reset_appraise_flags(struct inode
*inode
, int digsig
)
362 struct integrity_iint_cache
*iint
;
364 if (!(ima_policy_flag
& IMA_APPRAISE
) || !S_ISREG(inode
->i_mode
))
367 iint
= integrity_iint_find(inode
);
371 iint
->flags
&= ~IMA_DONE_MASK
;
373 iint
->flags
|= IMA_DIGSIG
;
377 int ima_inode_setxattr(struct dentry
*dentry
, const char *xattr_name
,
378 const void *xattr_value
, size_t xattr_value_len
)
380 const struct evm_ima_xattr_data
*xvalue
= xattr_value
;
383 result
= ima_protect_xattr(dentry
, xattr_name
, xattr_value
,
386 if (!xattr_value_len
|| (xvalue
->type
>= IMA_XATTR_LAST
))
388 ima_reset_appraise_flags(d_backing_inode(dentry
),
389 (xvalue
->type
== EVM_IMA_XATTR_DIGSIG
) ? 1 : 0);
395 int ima_inode_removexattr(struct dentry
*dentry
, const char *xattr_name
)
399 result
= ima_protect_xattr(dentry
, xattr_name
, NULL
, 0);
401 ima_reset_appraise_flags(d_backing_inode(dentry
), 0);