Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Creating audit records for mapped devices.
0004  *
0005  * Copyright (C) 2021 Fraunhofer AISEC. All rights reserved.
0006  *
0007  * Authors: Michael Weiß <michael.weiss@aisec.fraunhofer.de>
0008  */
0009 
0010 #include <linux/audit.h>
0011 #include <linux/module.h>
0012 #include <linux/device-mapper.h>
0013 #include <linux/bio.h>
0014 #include <linux/blkdev.h>
0015 
0016 #include "dm-audit.h"
0017 #include "dm-core.h"
0018 
0019 static struct audit_buffer *dm_audit_log_start(int audit_type,
0020                            const char *dm_msg_prefix,
0021                            const char *op)
0022 {
0023     struct audit_buffer *ab;
0024 
0025     if (audit_enabled == AUDIT_OFF)
0026         return NULL;
0027 
0028     ab = audit_log_start(audit_context(), GFP_KERNEL, audit_type);
0029     if (unlikely(!ab))
0030         return NULL;
0031 
0032     audit_log_format(ab, "module=%s op=%s", dm_msg_prefix, op);
0033     return ab;
0034 }
0035 
0036 void dm_audit_log_ti(int audit_type, const char *dm_msg_prefix, const char *op,
0037              struct dm_target *ti, int result)
0038 {
0039     struct audit_buffer *ab = NULL;
0040     struct mapped_device *md = dm_table_get_md(ti->table);
0041     int dev_major = dm_disk(md)->major;
0042     int dev_minor = dm_disk(md)->first_minor;
0043 
0044     switch (audit_type) {
0045     case AUDIT_DM_CTRL:
0046         ab = dm_audit_log_start(audit_type, dm_msg_prefix, op);
0047         if (unlikely(!ab))
0048             return;
0049         audit_log_task_info(ab);
0050         audit_log_format(ab, " dev=%d:%d error_msg='%s'", dev_major,
0051                  dev_minor, !result ? ti->error : "success");
0052         break;
0053     case AUDIT_DM_EVENT:
0054         ab = dm_audit_log_start(audit_type, dm_msg_prefix, op);
0055         if (unlikely(!ab))
0056             return;
0057         audit_log_format(ab, " dev=%d:%d sector=?", dev_major,
0058                  dev_minor);
0059         break;
0060     default: /* unintended use */
0061         return;
0062     }
0063 
0064     audit_log_format(ab, " res=%d", result);
0065     audit_log_end(ab);
0066 }
0067 EXPORT_SYMBOL_GPL(dm_audit_log_ti);
0068 
0069 void dm_audit_log_bio(const char *dm_msg_prefix, const char *op,
0070               struct bio *bio, sector_t sector, int result)
0071 {
0072     struct audit_buffer *ab;
0073     int dev_major = MAJOR(bio->bi_bdev->bd_dev);
0074     int dev_minor = MINOR(bio->bi_bdev->bd_dev);
0075 
0076     ab = dm_audit_log_start(AUDIT_DM_EVENT, dm_msg_prefix, op);
0077     if (unlikely(!ab))
0078         return;
0079 
0080     audit_log_format(ab, " dev=%d:%d sector=%llu res=%d",
0081              dev_major, dev_minor, sector, result);
0082     audit_log_end(ab);
0083 }
0084 EXPORT_SYMBOL_GPL(dm_audit_log_bio);