xref: /openbmc/linux/drivers/md/dm-audit.c (revision 3bd94003)
1*3bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only
22cc1ae48SMichael Weiß /*
32cc1ae48SMichael Weiß  * Creating audit records for mapped devices.
42cc1ae48SMichael Weiß  *
52cc1ae48SMichael Weiß  * Copyright (C) 2021 Fraunhofer AISEC. All rights reserved.
62cc1ae48SMichael Weiß  *
72cc1ae48SMichael Weiß  * Authors: Michael Weiß <michael.weiss@aisec.fraunhofer.de>
82cc1ae48SMichael Weiß  */
92cc1ae48SMichael Weiß 
102cc1ae48SMichael Weiß #include <linux/audit.h>
112cc1ae48SMichael Weiß #include <linux/module.h>
122cc1ae48SMichael Weiß #include <linux/device-mapper.h>
132cc1ae48SMichael Weiß #include <linux/bio.h>
142cc1ae48SMichael Weiß #include <linux/blkdev.h>
152cc1ae48SMichael Weiß 
162cc1ae48SMichael Weiß #include "dm-audit.h"
172cc1ae48SMichael Weiß #include "dm-core.h"
182cc1ae48SMichael Weiß 
dm_audit_log_start(int audit_type,const char * dm_msg_prefix,const char * op)192cc1ae48SMichael Weiß static struct audit_buffer *dm_audit_log_start(int audit_type,
202cc1ae48SMichael Weiß 					       const char *dm_msg_prefix,
212cc1ae48SMichael Weiß 					       const char *op)
222cc1ae48SMichael Weiß {
232cc1ae48SMichael Weiß 	struct audit_buffer *ab;
242cc1ae48SMichael Weiß 
252cc1ae48SMichael Weiß 	if (audit_enabled == AUDIT_OFF)
262cc1ae48SMichael Weiß 		return NULL;
272cc1ae48SMichael Weiß 
282cc1ae48SMichael Weiß 	ab = audit_log_start(audit_context(), GFP_KERNEL, audit_type);
292cc1ae48SMichael Weiß 	if (unlikely(!ab))
302cc1ae48SMichael Weiß 		return NULL;
312cc1ae48SMichael Weiß 
322cc1ae48SMichael Weiß 	audit_log_format(ab, "module=%s op=%s", dm_msg_prefix, op);
332cc1ae48SMichael Weiß 	return ab;
342cc1ae48SMichael Weiß }
352cc1ae48SMichael Weiß 
dm_audit_log_ti(int audit_type,const char * dm_msg_prefix,const char * op,struct dm_target * ti,int result)362cc1ae48SMichael Weiß void dm_audit_log_ti(int audit_type, const char *dm_msg_prefix, const char *op,
372cc1ae48SMichael Weiß 		     struct dm_target *ti, int result)
382cc1ae48SMichael Weiß {
392cc1ae48SMichael Weiß 	struct audit_buffer *ab = NULL;
402cc1ae48SMichael Weiß 	struct mapped_device *md = dm_table_get_md(ti->table);
412cc1ae48SMichael Weiß 	int dev_major = dm_disk(md)->major;
422cc1ae48SMichael Weiß 	int dev_minor = dm_disk(md)->first_minor;
432cc1ae48SMichael Weiß 
442cc1ae48SMichael Weiß 	switch (audit_type) {
452cc1ae48SMichael Weiß 	case AUDIT_DM_CTRL:
462cc1ae48SMichael Weiß 		ab = dm_audit_log_start(audit_type, dm_msg_prefix, op);
472cc1ae48SMichael Weiß 		if (unlikely(!ab))
482cc1ae48SMichael Weiß 			return;
492cc1ae48SMichael Weiß 		audit_log_task_info(ab);
502cc1ae48SMichael Weiß 		audit_log_format(ab, " dev=%d:%d error_msg='%s'", dev_major,
512cc1ae48SMichael Weiß 				 dev_minor, !result ? ti->error : "success");
522cc1ae48SMichael Weiß 		break;
532cc1ae48SMichael Weiß 	case AUDIT_DM_EVENT:
542cc1ae48SMichael Weiß 		ab = dm_audit_log_start(audit_type, dm_msg_prefix, op);
552cc1ae48SMichael Weiß 		if (unlikely(!ab))
562cc1ae48SMichael Weiß 			return;
572cc1ae48SMichael Weiß 		audit_log_format(ab, " dev=%d:%d sector=?", dev_major,
582cc1ae48SMichael Weiß 				 dev_minor);
592cc1ae48SMichael Weiß 		break;
602cc1ae48SMichael Weiß 	default: /* unintended use */
612cc1ae48SMichael Weiß 		return;
622cc1ae48SMichael Weiß 	}
632cc1ae48SMichael Weiß 
642cc1ae48SMichael Weiß 	audit_log_format(ab, " res=%d", result);
652cc1ae48SMichael Weiß 	audit_log_end(ab);
662cc1ae48SMichael Weiß }
672cc1ae48SMichael Weiß EXPORT_SYMBOL_GPL(dm_audit_log_ti);
682cc1ae48SMichael Weiß 
dm_audit_log_bio(const char * dm_msg_prefix,const char * op,struct bio * bio,sector_t sector,int result)692cc1ae48SMichael Weiß void dm_audit_log_bio(const char *dm_msg_prefix, const char *op,
702cc1ae48SMichael Weiß 		      struct bio *bio, sector_t sector, int result)
712cc1ae48SMichael Weiß {
722cc1ae48SMichael Weiß 	struct audit_buffer *ab;
732cc1ae48SMichael Weiß 	int dev_major = MAJOR(bio->bi_bdev->bd_dev);
742cc1ae48SMichael Weiß 	int dev_minor = MINOR(bio->bi_bdev->bd_dev);
752cc1ae48SMichael Weiß 
762cc1ae48SMichael Weiß 	ab = dm_audit_log_start(AUDIT_DM_EVENT, dm_msg_prefix, op);
772cc1ae48SMichael Weiß 	if (unlikely(!ab))
782cc1ae48SMichael Weiß 		return;
792cc1ae48SMichael Weiß 
802cc1ae48SMichael Weiß 	audit_log_format(ab, " dev=%d:%d sector=%llu res=%d",
812cc1ae48SMichael Weiß 			 dev_major, dev_minor, sector, result);
822cc1ae48SMichael Weiß 	audit_log_end(ab);
832cc1ae48SMichael Weiß }
842cc1ae48SMichael Weiß EXPORT_SYMBOL_GPL(dm_audit_log_bio);
85