Michael Weiß | 2cc1ae4 | 2021-09-04 11:59:28 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Creating audit records for mapped devices. |
| 4 | * |
| 5 | * Copyright (C) 2021 Fraunhofer AISEC. All rights reserved. |
| 6 | * |
| 7 | * Authors: Michael Weiß <michael.weiss@aisec.fraunhofer.de> |
| 8 | */ |
| 9 | |
| 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> |
| 15 | |
| 16 | #include "dm-audit.h" |
| 17 | #include "dm-core.h" |
| 18 | |
| 19 | static struct audit_buffer *dm_audit_log_start(int audit_type, |
| 20 | const char *dm_msg_prefix, |
| 21 | const char *op) |
| 22 | { |
| 23 | struct audit_buffer *ab; |
| 24 | |
| 25 | if (audit_enabled == AUDIT_OFF) |
| 26 | return NULL; |
| 27 | |
| 28 | ab = audit_log_start(audit_context(), GFP_KERNEL, audit_type); |
| 29 | if (unlikely(!ab)) |
| 30 | return NULL; |
| 31 | |
| 32 | audit_log_format(ab, "module=%s op=%s", dm_msg_prefix, op); |
| 33 | return ab; |
| 34 | } |
| 35 | |
| 36 | void dm_audit_log_ti(int audit_type, const char *dm_msg_prefix, const char *op, |
| 37 | struct dm_target *ti, int result) |
| 38 | { |
| 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; |
| 43 | |
| 44 | switch (audit_type) { |
| 45 | case AUDIT_DM_CTRL: |
| 46 | ab = dm_audit_log_start(audit_type, dm_msg_prefix, op); |
| 47 | if (unlikely(!ab)) |
| 48 | return; |
| 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"); |
| 52 | break; |
| 53 | case AUDIT_DM_EVENT: |
| 54 | ab = dm_audit_log_start(audit_type, dm_msg_prefix, op); |
| 55 | if (unlikely(!ab)) |
| 56 | return; |
| 57 | audit_log_format(ab, " dev=%d:%d sector=?", dev_major, |
| 58 | dev_minor); |
| 59 | break; |
| 60 | default: /* unintended use */ |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | audit_log_format(ab, " res=%d", result); |
| 65 | audit_log_end(ab); |
| 66 | } |
| 67 | EXPORT_SYMBOL_GPL(dm_audit_log_ti); |
| 68 | |
| 69 | void dm_audit_log_bio(const char *dm_msg_prefix, const char *op, |
| 70 | struct bio *bio, sector_t sector, int result) |
| 71 | { |
| 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); |
| 75 | |
| 76 | ab = dm_audit_log_start(AUDIT_DM_EVENT, dm_msg_prefix, op); |
| 77 | if (unlikely(!ab)) |
| 78 | return; |
| 79 | |
| 80 | audit_log_format(ab, " dev=%d:%d sector=%llu res=%d", |
| 81 | dev_major, dev_minor, sector, result); |
| 82 | audit_log_end(ab); |
| 83 | } |
| 84 | EXPORT_SYMBOL_GPL(dm_audit_log_bio); |