Skip to content

Commit 34e980b

Browse files
nramasmimizohar
authored andcommitted
IMA: Add audit log for failure conditions
process_buffer_measurement() and ima_alloc_key_entry() functions need to log an audit message for auditing integrity measurement failures. Add audit message in these two functions. Remove "pr_devel" log message in process_buffer_measurement(). Sample audit messages: [ 6.303048] audit: type=1804 audit(1592506281.627:2): pid=1 uid=0 auid=4294967295 ses=4294967295 subj=kernel op=measuring_key cause=ENOMEM comm="swapper/0" name=".builtin_trusted_keys" res=0 errno=-12 [ 8.019432] audit: type=1804 audit(1592506283.344:10): pid=1 uid=0 auid=4294967295 ses=4294967295 subj=system_u:system_r:init_t:s0 op=measuring_kexec_cmdline cause=hashing_error comm="systemd" name="kexec-cmdline" res=0 errno=-22 Signed-off-by: Lakshmi Ramasubramanian <[email protected]> Suggested-by: Mimi Zohar <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent 2f84588 commit 34e980b

File tree

4 files changed

+51
-22
lines changed

4 files changed

+51
-22
lines changed

security/integrity/ima/ima.h

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,27 +186,43 @@ static inline unsigned int ima_hash_key(u8 *digest)
186186
return (digest[0] | digest[1] << 8) % IMA_MEASURE_HTABLE_SIZE;
187187
}
188188

189-
#define __ima_hooks(hook) \
190-
hook(NONE) \
191-
hook(FILE_CHECK) \
192-
hook(MMAP_CHECK) \
193-
hook(BPRM_CHECK) \
194-
hook(CREDS_CHECK) \
195-
hook(POST_SETATTR) \
196-
hook(MODULE_CHECK) \
197-
hook(FIRMWARE_CHECK) \
198-
hook(KEXEC_KERNEL_CHECK) \
199-
hook(KEXEC_INITRAMFS_CHECK) \
200-
hook(POLICY_CHECK) \
201-
hook(KEXEC_CMDLINE) \
202-
hook(KEY_CHECK) \
203-
hook(MAX_CHECK)
204-
#define __ima_hook_enumify(ENUM) ENUM,
189+
#define __ima_hooks(hook) \
190+
hook(NONE, none) \
191+
hook(FILE_CHECK, file) \
192+
hook(MMAP_CHECK, mmap) \
193+
hook(BPRM_CHECK, bprm) \
194+
hook(CREDS_CHECK, creds) \
195+
hook(POST_SETATTR, post_setattr) \
196+
hook(MODULE_CHECK, module) \
197+
hook(FIRMWARE_CHECK, firmware) \
198+
hook(KEXEC_KERNEL_CHECK, kexec_kernel) \
199+
hook(KEXEC_INITRAMFS_CHECK, kexec_initramfs) \
200+
hook(POLICY_CHECK, policy) \
201+
hook(KEXEC_CMDLINE, kexec_cmdline) \
202+
hook(KEY_CHECK, key) \
203+
hook(MAX_CHECK, none)
204+
205+
#define __ima_hook_enumify(ENUM, str) ENUM,
206+
#define __ima_stringify(arg) (#arg)
207+
#define __ima_hook_measuring_stringify(ENUM, str) \
208+
(__ima_stringify(measuring_ ##str)),
205209

206210
enum ima_hooks {
207211
__ima_hooks(__ima_hook_enumify)
208212
};
209213

214+
static const char * const ima_hooks_measure_str[] = {
215+
__ima_hooks(__ima_hook_measuring_stringify)
216+
};
217+
218+
static inline const char *func_measure_str(enum ima_hooks func)
219+
{
220+
if (func >= MAX_CHECK)
221+
return ima_hooks_measure_str[NONE];
222+
223+
return ima_hooks_measure_str[func];
224+
}
225+
210226
extern const char *const func_tokens[];
211227

212228
struct modsig;

security/integrity/ima/ima_main.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ void process_buffer_measurement(const void *buf, int size,
740740
int pcr, const char *keyring)
741741
{
742742
int ret = 0;
743+
const char *audit_cause = "ENOMEM";
743744
struct ima_template_entry *entry = NULL;
744745
struct integrity_iint_cache iint = {};
745746
struct ima_event_data event_data = {.iint = &iint,
@@ -794,21 +795,28 @@ void process_buffer_measurement(const void *buf, int size,
794795
iint.ima_hash->length = hash_digest_size[ima_hash_algo];
795796

796797
ret = ima_calc_buffer_hash(buf, size, iint.ima_hash);
797-
if (ret < 0)
798+
if (ret < 0) {
799+
audit_cause = "hashing_error";
798800
goto out;
801+
}
799802

800803
ret = ima_alloc_init_template(&event_data, &entry, template);
801-
if (ret < 0)
804+
if (ret < 0) {
805+
audit_cause = "alloc_entry";
802806
goto out;
807+
}
803808

804809
ret = ima_store_template(entry, violation, NULL, buf, pcr);
805-
806-
if (ret < 0)
810+
if (ret < 0) {
811+
audit_cause = "store_entry";
807812
ima_free_template_entry(entry);
813+
}
808814

809815
out:
810816
if (ret < 0)
811-
pr_devel("%s: failed, result: %d\n", __func__, ret);
817+
integrity_audit_message(AUDIT_INTEGRITY_PCR, NULL, eventname,
818+
func_measure_str(func),
819+
audit_cause, ret, 0, ret);
812820

813821
return;
814822
}

security/integrity/ima/ima_policy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ void ima_delete_rules(void)
14141414
}
14151415
}
14161416

1417-
#define __ima_hook_stringify(str) (#str),
1417+
#define __ima_hook_stringify(func, str) (#func),
14181418

14191419
const char *const func_tokens[] = {
14201420
__ima_hooks(__ima_hook_stringify)

security/integrity/ima/ima_queue_keys.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ static struct ima_key_entry *ima_alloc_key_entry(struct key *keyring,
6868
size_t payload_len)
6969
{
7070
int rc = 0;
71+
const char *audit_cause = "ENOMEM";
7172
struct ima_key_entry *entry;
7273

7374
entry = kzalloc(sizeof(*entry), GFP_KERNEL);
@@ -88,6 +89,10 @@ static struct ima_key_entry *ima_alloc_key_entry(struct key *keyring,
8889

8990
out:
9091
if (rc) {
92+
integrity_audit_message(AUDIT_INTEGRITY_PCR, NULL,
93+
keyring->description,
94+
func_measure_str(KEY_CHECK),
95+
audit_cause, rc, 0, rc);
9196
ima_free_key_entry(entry);
9297
entry = NULL;
9398
}

0 commit comments

Comments
 (0)