dm-audit.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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ß <[email protected]>
  8. */
  9. #include <linux/audit.h>
  10. #include <linux/module.h>
  11. #include <linux/device-mapper.h>
  12. #include <linux/bio.h>
  13. #include <linux/blkdev.h>
  14. #include "dm-audit.h"
  15. #include "dm-core.h"
  16. static struct audit_buffer *dm_audit_log_start(int audit_type,
  17. const char *dm_msg_prefix,
  18. const char *op)
  19. {
  20. struct audit_buffer *ab;
  21. if (audit_enabled == AUDIT_OFF)
  22. return NULL;
  23. ab = audit_log_start(audit_context(), GFP_KERNEL, audit_type);
  24. if (unlikely(!ab))
  25. return NULL;
  26. audit_log_format(ab, "module=%s op=%s", dm_msg_prefix, op);
  27. return ab;
  28. }
  29. void dm_audit_log_ti(int audit_type, const char *dm_msg_prefix, const char *op,
  30. struct dm_target *ti, int result)
  31. {
  32. struct audit_buffer *ab = NULL;
  33. struct mapped_device *md = dm_table_get_md(ti->table);
  34. int dev_major = dm_disk(md)->major;
  35. int dev_minor = dm_disk(md)->first_minor;
  36. switch (audit_type) {
  37. case AUDIT_DM_CTRL:
  38. ab = dm_audit_log_start(audit_type, dm_msg_prefix, op);
  39. if (unlikely(!ab))
  40. return;
  41. audit_log_task_info(ab);
  42. audit_log_format(ab, " dev=%d:%d error_msg='%s'", dev_major,
  43. dev_minor, !result ? ti->error : "success");
  44. break;
  45. case AUDIT_DM_EVENT:
  46. ab = dm_audit_log_start(audit_type, dm_msg_prefix, op);
  47. if (unlikely(!ab))
  48. return;
  49. audit_log_format(ab, " dev=%d:%d sector=?", dev_major,
  50. dev_minor);
  51. break;
  52. default: /* unintended use */
  53. return;
  54. }
  55. audit_log_format(ab, " res=%d", result);
  56. audit_log_end(ab);
  57. }
  58. EXPORT_SYMBOL_GPL(dm_audit_log_ti);
  59. void dm_audit_log_bio(const char *dm_msg_prefix, const char *op,
  60. struct bio *bio, sector_t sector, int result)
  61. {
  62. struct audit_buffer *ab;
  63. int dev_major = MAJOR(bio->bi_bdev->bd_dev);
  64. int dev_minor = MINOR(bio->bi_bdev->bd_dev);
  65. ab = dm_audit_log_start(AUDIT_DM_EVENT, dm_msg_prefix, op);
  66. if (unlikely(!ab))
  67. return;
  68. audit_log_format(ab, " dev=%d:%d sector=%llu res=%d",
  69. dev_major, dev_minor, sector, result);
  70. audit_log_end(ab);
  71. }
  72. EXPORT_SYMBOL_GPL(dm_audit_log_bio);