security/integrity/ima/ima_main.c
Source file repositories/reference/linux-study-clean/security/integrity/ima/ima_main.c
File Facts
- System
- Linux kernel
- Corpus path
security/integrity/ima/ima_main.c- Extension
.c- Size
- 40119 bytes
- Lines
- 1342
- Domain
- Core OS
- Bucket
- Security And Isolation
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/module.hlinux/file.hlinux/binfmts.hlinux/kernel_read_file.hlinux/mount.hlinux/mman.hlinux/slab.hlinux/xattr.hlinux/ima.hlinux/fs.hlinux/iversion.hlinux/evm.hlinux/crash_dump.hima.h
Detected Declarations
function ima_setupfunction hash_setupfunction ima_get_current_hash_algofunction mmap_violation_checkfunction ima_rdwr_violation_checkfunction ima_detect_file_changefunction ima_check_last_writerfunction ima_file_freefunction process_measurementfunction ima_must_measurefunction ima_file_mprotectfunction deny_write_accessfunction deny_write_accessfunction execveatfunction ima_must_measurefunction __ima_inode_hashfunction ima_file_hashfunction ima_inode_hashfunction process_measurementfunction ima_post_path_mknodfunction ima_read_filefunction ima_post_read_filefunction ima_post_load_datafunction ima_post_load_datafunction process_buffer_measurementfunction ima_kexec_cmdlinefunction ima_measure_critical_datafunction public_key_verify_signaturefunction init_imafunction init_ima_lsmexport ima_file_hashexport ima_inode_hashexport ima_measure_critical_data
Annotated Snippet
if (strncmp(str, "sha1", 4) == 0) {
ima_hash_algo = HASH_ALGO_SHA1;
} else if (strncmp(str, "md5", 3) == 0) {
ima_hash_algo = HASH_ALGO_MD5;
} else {
pr_err("invalid hash algorithm \"%s\" for template \"%s\"",
str, IMA_TEMPLATE_IMA_NAME);
return 1;
}
goto out;
}
i = match_string(hash_algo_name, HASH_ALGO__LAST, str);
if (i < 0) {
pr_err("invalid hash algorithm \"%s\"", str);
return 1;
}
ima_hash_algo = i;
out:
hash_setup_done = 1;
return 1;
}
__setup("ima_hash=", hash_setup);
enum hash_algo ima_get_current_hash_algo(void)
{
return ima_hash_algo;
}
/* Prevent mmap'ing a file execute that is already mmap'ed write */
static int mmap_violation_check(enum ima_hooks func, struct file *file,
char **pathbuf, const char **pathname,
char *filename)
{
struct inode *inode;
int rc = 0;
if ((func == MMAP_CHECK || func == MMAP_CHECK_REQPROT) &&
mapping_writably_mapped(file->f_mapping)) {
rc = -ETXTBSY;
inode = file_inode(file);
if (!*pathbuf) /* ima_rdwr_violation possibly pre-fetched */
*pathname = ima_d_path(&file->f_path, pathbuf,
filename);
integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode, *pathname,
"mmap_file", "mmapped_writers", rc, 0);
}
return rc;
}
/*
* ima_rdwr_violation_check
*
* Only invalidate the PCR for measured files:
* - Opening a file for write when already open for read,
* results in a time of measure, time of use (ToMToU) error.
* - Opening a file for read when already open for write,
* could result in a file measurement error.
*
*/
static void ima_rdwr_violation_check(struct file *file,
struct ima_iint_cache *iint,
int must_measure,
char **pathbuf,
const char **pathname,
char *filename)
{
struct inode *inode = file_inode(file);
fmode_t mode = file->f_mode;
bool send_tomtou = false, send_writers = false;
if (mode & FMODE_WRITE) {
if (atomic_read(&inode->i_readcount) && IS_IMA(inode)) {
if (!iint)
iint = ima_iint_find(inode);
/* IMA_MEASURE is set from reader side */
if (iint && test_and_clear_bit(IMA_MAY_EMIT_TOMTOU,
&iint->atomic_flags))
send_tomtou = true;
}
} else {
if (must_measure)
set_bit(IMA_MAY_EMIT_TOMTOU, &iint->atomic_flags);
/* Limit number of open_writers violations */
if (inode_is_open_for_write(inode) && must_measure) {
if (!test_and_set_bit(IMA_EMITTED_OPENWRITERS,
Annotation
- Immediate include surface: `linux/module.h`, `linux/file.h`, `linux/binfmts.h`, `linux/kernel_read_file.h`, `linux/mount.h`, `linux/mman.h`, `linux/slab.h`, `linux/xattr.h`.
- Detected declarations: `function ima_setup`, `function hash_setup`, `function ima_get_current_hash_algo`, `function mmap_violation_check`, `function ima_rdwr_violation_check`, `function ima_detect_file_change`, `function ima_check_last_writer`, `function ima_file_free`, `function process_measurement`, `function ima_must_measure`.
- Atlas domain: Core OS / Security And Isolation.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.