2 * Copyright (C) 2005,2006,2007,2008 IBM Corporation
5 * Reiner Sailer <sailer@watson.ibm.com>
6 * Serge Hallyn <serue@us.ibm.com>
7 * Kylene Hall <kylene@us.ibm.com>
8 * Mimi Zohar <zohar@us.ibm.com>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, version 2 of the
16 * implements the IMA hooks: ima_bprm_check, ima_file_mmap,
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/module.h>
23 #include <linux/file.h>
24 #include <linux/binfmts.h>
25 #include <linux/mount.h>
26 #include <linux/mman.h>
27 #include <linux/slab.h>
28 #include <linux/xattr.h>
29 #include <linux/ima.h>
30 #include <linux/iversion.h>
35 #ifdef CONFIG_IMA_APPRAISE
36 int ima_appraise
= IMA_APPRAISE_ENFORCE
;
41 int ima_hash_algo
= HASH_ALGO_SHA1
;
42 static int hash_setup_done
;
44 static int __init
hash_setup(char *str
)
46 struct ima_template_desc
*template_desc
= ima_template_desc_current();
52 if (strcmp(template_desc
->name
, IMA_TEMPLATE_IMA_NAME
) == 0) {
53 if (strncmp(str
, "sha1", 4) == 0)
54 ima_hash_algo
= HASH_ALGO_SHA1
;
55 else if (strncmp(str
, "md5", 3) == 0)
56 ima_hash_algo
= HASH_ALGO_MD5
;
62 i
= match_string(hash_algo_name
, HASH_ALGO__LAST
, str
);
71 __setup("ima_hash=", hash_setup
);
74 * ima_rdwr_violation_check
76 * Only invalidate the PCR for measured files:
77 * - Opening a file for write when already open for read,
78 * results in a time of measure, time of use (ToMToU) error.
79 * - Opening a file for read when already open for write,
80 * could result in a file measurement error.
83 static void ima_rdwr_violation_check(struct file
*file
,
84 struct integrity_iint_cache
*iint
,
87 const char **pathname
,
90 struct inode
*inode
= file_inode(file
);
91 fmode_t mode
= file
->f_mode
;
92 bool send_tomtou
= false, send_writers
= false;
94 if (mode
& FMODE_WRITE
) {
95 if (atomic_read(&inode
->i_readcount
) && IS_IMA(inode
)) {
97 iint
= integrity_iint_find(inode
);
98 /* IMA_MEASURE is set from reader side */
99 if (iint
&& test_bit(IMA_MUST_MEASURE
,
100 &iint
->atomic_flags
))
105 set_bit(IMA_MUST_MEASURE
, &iint
->atomic_flags
);
106 if ((atomic_read(&inode
->i_writecount
) > 0) && must_measure
)
110 if (!send_tomtou
&& !send_writers
)
113 *pathname
= ima_d_path(&file
->f_path
, pathbuf
, filename
);
116 ima_add_violation(file
, *pathname
, iint
,
117 "invalid_pcr", "ToMToU");
119 ima_add_violation(file
, *pathname
, iint
,
120 "invalid_pcr", "open_writers");
123 static void ima_check_last_writer(struct integrity_iint_cache
*iint
,
124 struct inode
*inode
, struct file
*file
)
126 fmode_t mode
= file
->f_mode
;
129 if (!(mode
& FMODE_WRITE
))
132 mutex_lock(&iint
->mutex
);
133 if (atomic_read(&inode
->i_writecount
) == 1) {
134 update
= test_and_clear_bit(IMA_UPDATE_XATTR
,
135 &iint
->atomic_flags
);
136 if (!IS_I_VERSION(inode
) ||
137 !inode_eq_iversion(inode
, iint
->version
) ||
138 (iint
->flags
& IMA_NEW_FILE
)) {
139 iint
->flags
&= ~(IMA_DONE_MASK
| IMA_NEW_FILE
);
140 iint
->measured_pcrs
= 0;
142 ima_update_xattr(iint
, file
);
145 mutex_unlock(&iint
->mutex
);
149 * ima_file_free - called on __fput()
150 * @file: pointer to file structure being freed
152 * Flag files that changed, based on i_version
154 void ima_file_free(struct file
*file
)
156 struct inode
*inode
= file_inode(file
);
157 struct integrity_iint_cache
*iint
;
159 if (!ima_policy_flag
|| !S_ISREG(inode
->i_mode
))
162 iint
= integrity_iint_find(inode
);
166 ima_check_last_writer(iint
, inode
, file
);
169 static int process_measurement(struct file
*file
, const struct cred
*cred
,
170 u32 secid
, char *buf
, loff_t size
, int mask
,
173 struct inode
*inode
= file_inode(file
);
174 struct integrity_iint_cache
*iint
= NULL
;
175 struct ima_template_desc
*template_desc
;
176 char *pathbuf
= NULL
;
177 char filename
[NAME_MAX
];
178 const char *pathname
= NULL
;
179 int rc
= 0, action
, must_appraise
= 0;
180 int pcr
= CONFIG_IMA_MEASURE_PCR_IDX
;
181 struct evm_ima_xattr_data
*xattr_value
= NULL
;
183 bool violation_check
;
184 enum hash_algo hash_algo
;
186 if (!ima_policy_flag
|| !S_ISREG(inode
->i_mode
))
189 /* Return an IMA_MEASURE, IMA_APPRAISE, IMA_AUDIT action
190 * bitmask based on the appraise/audit/measurement policy.
191 * Included is the appraise submask.
193 action
= ima_get_action(inode
, cred
, secid
, mask
, func
, &pcr
);
194 violation_check
= ((func
== FILE_CHECK
|| func
== MMAP_CHECK
) &&
195 (ima_policy_flag
& IMA_MEASURE
));
196 if (!action
&& !violation_check
)
199 must_appraise
= action
& IMA_APPRAISE
;
201 /* Is the appraise rule hook specific? */
202 if (action
& IMA_FILE_APPRAISE
)
208 iint
= integrity_inode_get(inode
);
213 if (!rc
&& violation_check
)
214 ima_rdwr_violation_check(file
, iint
, action
& IMA_MEASURE
,
215 &pathbuf
, &pathname
, filename
);
224 mutex_lock(&iint
->mutex
);
226 if (test_and_clear_bit(IMA_CHANGE_ATTR
, &iint
->atomic_flags
))
227 /* reset appraisal flags if ima_inode_post_setattr was called */
228 iint
->flags
&= ~(IMA_APPRAISE
| IMA_APPRAISED
|
229 IMA_APPRAISE_SUBMASK
| IMA_APPRAISED_SUBMASK
|
233 * Re-evaulate the file if either the xattr has changed or the
234 * kernel has no way of detecting file change on the filesystem.
235 * (Limited to privileged mounted filesystems.)
237 if (test_and_clear_bit(IMA_CHANGE_XATTR
, &iint
->atomic_flags
) ||
238 ((inode
->i_sb
->s_iflags
& SB_I_IMA_UNVERIFIABLE_SIGNATURE
) &&
239 !(inode
->i_sb
->s_iflags
& SB_I_UNTRUSTED_MOUNTER
) &&
240 !(action
& IMA_FAIL_UNVERIFIABLE_SIGS
))) {
241 iint
->flags
&= ~IMA_DONE_MASK
;
242 iint
->measured_pcrs
= 0;
245 /* Determine if already appraised/measured based on bitmask
246 * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAISED,
247 * IMA_AUDIT, IMA_AUDITED)
249 iint
->flags
|= action
;
250 action
&= IMA_DO_MASK
;
251 action
&= ~((iint
->flags
& (IMA_DONE_MASK
^ IMA_MEASURED
)) >> 1);
253 /* If target pcr is already measured, unset IMA_MEASURE action */
254 if ((action
& IMA_MEASURE
) && (iint
->measured_pcrs
& (0x1 << pcr
)))
255 action
^= IMA_MEASURE
;
257 /* HASH sets the digital signature and update flags, nothing else */
258 if ((action
& IMA_HASH
) &&
259 !(test_bit(IMA_DIGSIG
, &iint
->atomic_flags
))) {
260 xattr_len
= ima_read_xattr(file_dentry(file
), &xattr_value
);
261 if ((xattr_value
&& xattr_len
> 2) &&
262 (xattr_value
->type
== EVM_IMA_XATTR_DIGSIG
))
263 set_bit(IMA_DIGSIG
, &iint
->atomic_flags
);
264 iint
->flags
|= IMA_HASHED
;
266 set_bit(IMA_UPDATE_XATTR
, &iint
->atomic_flags
);
269 /* Nothing to do, just return existing appraised status */
272 rc
= ima_get_cache_status(iint
, func
);
276 template_desc
= ima_template_desc_current();
277 if ((action
& IMA_APPRAISE_SUBMASK
) ||
278 strcmp(template_desc
->name
, IMA_TEMPLATE_IMA_NAME
) != 0)
279 /* read 'security.ima' */
280 xattr_len
= ima_read_xattr(file_dentry(file
), &xattr_value
);
282 hash_algo
= ima_get_hash_algo(xattr_value
, xattr_len
);
284 rc
= ima_collect_measurement(iint
, file
, buf
, size
, hash_algo
);
285 if (rc
!= 0 && rc
!= -EBADF
&& rc
!= -EINVAL
)
288 if (!pathbuf
) /* ima_rdwr_violation possibly pre-fetched */
289 pathname
= ima_d_path(&file
->f_path
, &pathbuf
, filename
);
291 if (action
& IMA_MEASURE
)
292 ima_store_measurement(iint
, file
, pathname
,
293 xattr_value
, xattr_len
, pcr
);
294 if (rc
== 0 && (action
& IMA_APPRAISE_SUBMASK
)) {
296 rc
= ima_appraise_measurement(func
, iint
, file
, pathname
,
297 xattr_value
, xattr_len
);
300 if (action
& IMA_AUDIT
)
301 ima_audit_measurement(iint
, pathname
);
303 if ((file
->f_flags
& O_DIRECT
) && (iint
->flags
& IMA_PERMIT_DIRECTIO
))
306 if ((mask
& MAY_WRITE
) && test_bit(IMA_DIGSIG
, &iint
->atomic_flags
) &&
307 !(iint
->flags
& IMA_NEW_FILE
))
309 mutex_unlock(&iint
->mutex
);
315 if (rc
&& (ima_appraise
& IMA_APPRAISE_ENFORCE
))
317 if (file
->f_mode
& FMODE_WRITE
)
318 set_bit(IMA_UPDATE_XATTR
, &iint
->atomic_flags
);
324 * ima_file_mmap - based on policy, collect/store measurement.
325 * @file: pointer to the file to be measured (May be NULL)
326 * @prot: contains the protection that will be applied by the kernel.
328 * Measure files being mmapped executable based on the ima_must_measure()
331 * On success return 0. On integrity appraisal error, assuming the file
332 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
334 int ima_file_mmap(struct file
*file
, unsigned long prot
)
338 if (file
&& (prot
& PROT_EXEC
)) {
339 security_task_getsecid(current
, &secid
);
340 return process_measurement(file
, current_cred(), secid
, NULL
,
341 0, MAY_EXEC
, MMAP_CHECK
);
348 * ima_bprm_check - based on policy, collect/store measurement.
349 * @bprm: contains the linux_binprm structure
351 * The OS protects against an executable file, already open for write,
352 * from being executed in deny_write_access() and an executable file,
353 * already open for execute, from being modified in get_write_access().
354 * So we can be certain that what we verify and measure here is actually
355 * what is being executed.
357 * On success return 0. On integrity appraisal error, assuming the file
358 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
360 int ima_bprm_check(struct linux_binprm
*bprm
)
365 security_task_getsecid(current
, &secid
);
366 ret
= process_measurement(bprm
->file
, current_cred(), secid
, NULL
, 0,
367 MAY_EXEC
, BPRM_CHECK
);
371 security_cred_getsecid(bprm
->cred
, &secid
);
372 return process_measurement(bprm
->file
, bprm
->cred
, secid
, NULL
, 0,
373 MAY_EXEC
, CREDS_CHECK
);
377 * ima_path_check - based on policy, collect/store measurement.
378 * @file: pointer to the file to be measured
379 * @mask: contains MAY_READ, MAY_WRITE, MAY_EXEC or MAY_APPEND
381 * Measure files based on the ima_must_measure() policy decision.
383 * On success return 0. On integrity appraisal error, assuming the file
384 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
386 int ima_file_check(struct file
*file
, int mask
)
390 security_task_getsecid(current
, &secid
);
391 return process_measurement(file
, current_cred(), secid
, NULL
, 0,
392 mask
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
|
393 MAY_APPEND
), FILE_CHECK
);
395 EXPORT_SYMBOL_GPL(ima_file_check
);
398 * ima_post_path_mknod - mark as a new inode
399 * @dentry: newly created dentry
401 * Mark files created via the mknodat syscall as new, so that the
402 * file data can be written later.
404 void ima_post_path_mknod(struct dentry
*dentry
)
406 struct integrity_iint_cache
*iint
;
407 struct inode
*inode
= dentry
->d_inode
;
410 must_appraise
= ima_must_appraise(inode
, MAY_ACCESS
, FILE_CHECK
);
414 iint
= integrity_inode_get(inode
);
416 iint
->flags
|= IMA_NEW_FILE
;
420 * ima_read_file - pre-measure/appraise hook decision based on policy
421 * @file: pointer to the file to be measured/appraised/audit
422 * @read_id: caller identifier
424 * Permit reading a file based on policy. The policy rules are written
425 * in terms of the policy identifier. Appraising the integrity of
426 * a file requires a file descriptor.
428 * For permission return 0, otherwise return -EACCES.
430 int ima_read_file(struct file
*file
, enum kernel_read_file_id read_id
)
433 * READING_FIRMWARE_PREALLOC_BUFFER
435 * Do devices using pre-allocated memory run the risk of the
436 * firmware being accessible to the device prior to the completion
437 * of IMA's signature verification any more than when using two
443 static int read_idmap
[READING_MAX_ID
] = {
444 [READING_FIRMWARE
] = FIRMWARE_CHECK
,
445 [READING_FIRMWARE_PREALLOC_BUFFER
] = FIRMWARE_CHECK
,
446 [READING_MODULE
] = MODULE_CHECK
,
447 [READING_KEXEC_IMAGE
] = KEXEC_KERNEL_CHECK
,
448 [READING_KEXEC_INITRAMFS
] = KEXEC_INITRAMFS_CHECK
,
449 [READING_POLICY
] = POLICY_CHECK
453 * ima_post_read_file - in memory collect/appraise/audit measurement
454 * @file: pointer to the file to be measured/appraised/audit
455 * @buf: pointer to in memory file contents
456 * @size: size of in memory file contents
457 * @read_id: caller identifier
459 * Measure/appraise/audit in memory file based on policy. Policy rules
460 * are written in terms of a policy identifier.
462 * On success return 0. On integrity appraisal error, assuming the file
463 * is in policy and IMA-appraisal is in enforcing mode, return -EACCES.
465 int ima_post_read_file(struct file
*file
, void *buf
, loff_t size
,
466 enum kernel_read_file_id read_id
)
471 if (!file
&& read_id
== READING_FIRMWARE
) {
472 if ((ima_appraise
& IMA_APPRAISE_FIRMWARE
) &&
473 (ima_appraise
& IMA_APPRAISE_ENFORCE
)) {
474 pr_err("Prevent firmware loading_store.\n");
475 return -EACCES
; /* INTEGRITY_UNKNOWN */
480 /* permit signed certs */
481 if (!file
&& read_id
== READING_X509_CERTIFICATE
)
484 if (!file
|| !buf
|| size
== 0) { /* should never happen */
485 if (ima_appraise
& IMA_APPRAISE_ENFORCE
)
490 func
= read_idmap
[read_id
] ?: FILE_CHECK
;
491 security_task_getsecid(current
, &secid
);
492 return process_measurement(file
, current_cred(), secid
, buf
, size
,
497 * ima_load_data - appraise decision based on policy
498 * @id: kernel load data caller identifier
500 * Callers of this LSM hook can not measure, appraise, or audit the
501 * data provided by userspace. Enforce policy rules requring a file
502 * signature (eg. kexec'ed kernel image).
504 * For permission return 0, otherwise return -EACCES.
506 int ima_load_data(enum kernel_load_data_id id
)
510 if ((ima_appraise
& IMA_APPRAISE_ENFORCE
) != IMA_APPRAISE_ENFORCE
)
514 case LOADING_KEXEC_IMAGE
:
515 if (ima_appraise
& IMA_APPRAISE_KEXEC
) {
516 pr_err("impossible to appraise a kernel image without a file descriptor; try using kexec_file_load syscall.\n");
517 return -EACCES
; /* INTEGRITY_UNKNOWN */
520 case LOADING_FIRMWARE
:
521 if (ima_appraise
& IMA_APPRAISE_FIRMWARE
) {
522 pr_err("Prevent firmware sysfs fallback loading.\n");
523 return -EACCES
; /* INTEGRITY_UNKNOWN */
527 sig_enforce
= is_module_sig_enforced();
529 if (!sig_enforce
&& (ima_appraise
& IMA_APPRAISE_MODULES
)) {
530 pr_err("impossible to appraise a module without a file descriptor. sig_enforce kernel parameter might help\n");
531 return -EACCES
; /* INTEGRITY_UNKNOWN */
539 static int __init
init_ima(void)
543 ima_init_template_list();
544 hash_setup(CONFIG_IMA_DEFAULT_HASH
);
547 if (error
&& strcmp(hash_algo_name
[ima_hash_algo
],
548 CONFIG_IMA_DEFAULT_HASH
) != 0) {
549 pr_info("Allocating %s failed, going to use default hash algorithm %s\n",
550 hash_algo_name
[ima_hash_algo
], CONFIG_IMA_DEFAULT_HASH
);
552 hash_setup(CONFIG_IMA_DEFAULT_HASH
);
557 ima_update_policy_flag();
562 late_initcall(init_ima
); /* Start IMA after the TPM is available */
564 MODULE_DESCRIPTION("Integrity Measurement Architecture");
565 MODULE_LICENSE("GPL");