1 // SPDX-License-Identifier: GPL-2.0-only
3 * Creating audit records for mapped devices.
5 * Copyright (C) 2021 Fraunhofer AISEC. All rights reserved.
7 * Authors: Michael Weiß <michael.weiss@aisec.fraunhofer.de>
10 #include <linux/audit.h>
11 #include <linux/module.h>
12 #include <linux/device-mapper.h>
13 #include <linux/bio.h>
14 #include <linux/blkdev.h>
19 static struct audit_buffer
*dm_audit_log_start(int audit_type
,
20 const char *dm_msg_prefix
,
23 struct audit_buffer
*ab
;
25 if (audit_enabled
== AUDIT_OFF
)
28 ab
= audit_log_start(audit_context(), GFP_KERNEL
, audit_type
);
32 audit_log_format(ab
, "module=%s op=%s", dm_msg_prefix
, op
);
36 void dm_audit_log_ti(int audit_type
, const char *dm_msg_prefix
, const char *op
,
37 struct dm_target
*ti
, int result
)
39 struct audit_buffer
*ab
= NULL
;
40 struct mapped_device
*md
= dm_table_get_md(ti
->table
);
41 int dev_major
= dm_disk(md
)->major
;
42 int dev_minor
= dm_disk(md
)->first_minor
;
46 ab
= dm_audit_log_start(audit_type
, dm_msg_prefix
, op
);
49 audit_log_task_info(ab
);
50 audit_log_format(ab
, " dev=%d:%d error_msg='%s'", dev_major
,
51 dev_minor
, !result
? ti
->error
: "success");
54 ab
= dm_audit_log_start(audit_type
, dm_msg_prefix
, op
);
57 audit_log_format(ab
, " dev=%d:%d sector=?", dev_major
,
60 default: /* unintended use */
64 audit_log_format(ab
, " res=%d", result
);
67 EXPORT_SYMBOL_GPL(dm_audit_log_ti
);
69 void dm_audit_log_bio(const char *dm_msg_prefix
, const char *op
,
70 struct bio
*bio
, sector_t sector
, int result
)
72 struct audit_buffer
*ab
;
73 int dev_major
= MAJOR(bio
->bi_bdev
->bd_dev
);
74 int dev_minor
= MINOR(bio
->bi_bdev
->bd_dev
);
76 ab
= dm_audit_log_start(AUDIT_DM_EVENT
, dm_msg_prefix
, op
);
80 audit_log_format(ab
, " dev=%d:%d sector=%llu res=%d",
81 dev_major
, dev_minor
, sector
, result
);
84 EXPORT_SYMBOL_GPL(dm_audit_log_bio
);