1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008 IBM Corporation
6 * Mimi Zohar <zohar@us.ibm.com>
9 * - implements the IMA hook: ima_inode_free
10 * - cache integrity information in the inode security blob
12 #include <linux/slab.h>
16 static struct kmem_cache
*ima_iint_cache __ro_after_init
;
19 * ima_iint_find - Return the iint associated with an inode
20 * @inode: Pointer to the inode
22 * Return the IMA integrity information (iint) associated with an inode, if the
23 * inode was processed by IMA.
25 * Return: Found iint or NULL.
27 struct ima_iint_cache
*ima_iint_find(struct inode
*inode
)
32 return ima_inode_get_iint(inode
);
35 #define IMA_MAX_NESTING (FILESYSTEM_MAX_STACK_DEPTH + 1)
38 * It is not clear that IMA should be nested at all, but as long is it measures
39 * files both on overlayfs and on underlying fs, we need to annotate the iint
40 * mutex to avoid lockdep false positives related to IMA + overlayfs.
41 * See ovl_lockdep_annotate_inode_mutex_key() for more details.
43 static inline void ima_iint_lockdep_annotate(struct ima_iint_cache
*iint
,
47 static struct lock_class_key ima_iint_mutex_key
[IMA_MAX_NESTING
];
49 int depth
= inode
->i_sb
->s_stack_depth
;
51 if (WARN_ON_ONCE(depth
< 0 || depth
>= IMA_MAX_NESTING
))
54 lockdep_set_class(&iint
->mutex
, &ima_iint_mutex_key
[depth
]);
58 static void ima_iint_init_always(struct ima_iint_cache
*iint
,
61 iint
->ima_hash
= NULL
;
62 iint
->real_inode
.version
= 0;
64 iint
->atomic_flags
= 0UL;
65 iint
->ima_file_status
= INTEGRITY_UNKNOWN
;
66 iint
->ima_mmap_status
= INTEGRITY_UNKNOWN
;
67 iint
->ima_bprm_status
= INTEGRITY_UNKNOWN
;
68 iint
->ima_read_status
= INTEGRITY_UNKNOWN
;
69 iint
->ima_creds_status
= INTEGRITY_UNKNOWN
;
70 iint
->measured_pcrs
= 0;
71 mutex_init(&iint
->mutex
);
72 ima_iint_lockdep_annotate(iint
, inode
);
75 static void ima_iint_free(struct ima_iint_cache
*iint
)
77 kfree(iint
->ima_hash
);
78 mutex_destroy(&iint
->mutex
);
79 kmem_cache_free(ima_iint_cache
, iint
);
83 * ima_inode_get - Find or allocate an iint associated with an inode
84 * @inode: Pointer to the inode
86 * Find an iint associated with an inode, and allocate a new one if not found.
87 * Caller must lock i_mutex.
89 * Return: An iint on success, NULL on error.
91 struct ima_iint_cache
*ima_inode_get(struct inode
*inode
)
93 struct ima_iint_cache
*iint
;
95 iint
= ima_iint_find(inode
);
99 iint
= kmem_cache_alloc(ima_iint_cache
, GFP_NOFS
);
103 ima_iint_init_always(iint
, inode
);
105 inode
->i_flags
|= S_IMA
;
106 ima_inode_set_iint(inode
, iint
);
112 * ima_inode_free_rcu - Called to free an inode via a RCU callback
113 * @inode_security: The inode->i_security pointer
115 * Free the IMA data associated with an inode.
117 void ima_inode_free_rcu(void *inode_security
)
119 struct ima_iint_cache
**iint_p
= inode_security
+ ima_blob_sizes
.lbs_inode
;
121 /* *iint_p should be NULL if !IS_IMA(inode) */
123 ima_iint_free(*iint_p
);
126 static void ima_iint_init_once(void *foo
)
128 struct ima_iint_cache
*iint
= (struct ima_iint_cache
*)foo
;
130 memset(iint
, 0, sizeof(*iint
));
133 void __init
ima_iintcache_init(void)
136 kmem_cache_create("ima_iint_cache", sizeof(struct ima_iint_cache
),
137 0, SLAB_PANIC
, ima_iint_init_once
);