Skip to content

Commit

Permalink
Merge tag 'for-5.16/dm-changes' of git://git.kernel.org/pub/scm/linux…
Browse files Browse the repository at this point in the history
…/kernel/git/device-mapper/linux-dm

Pull device mapper updates from Mike Snitzer:

 - Add DM core support for emitting audit events through the audit
   subsystem. Also enhance both the integrity and crypt targets to emit
   events to via dm-audit.

 - Various other simple code improvements and cleanups.

* tag 'for-5.16/dm-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/device-mapper/linux-dm:
  dm table: log table creation error code
  dm: make workqueue names device-specific
  dm writecache: Make use of the helper macro kthread_run()
  dm crypt: Make use of the helper macro kthread_run()
  dm verity: use bvec_kmap_local in verity_for_bv_block
  dm log writes: use memcpy_from_bvec in log_writes_map
  dm integrity: use bvec_kmap_local in __journal_read_write
  dm integrity: use bvec_kmap_local in integrity_metadata
  dm: add add_disk() error handling
  dm: Remove redundant flush_workqueue() calls
  dm crypt: log aead integrity violations to audit subsystem
  dm integrity: log audit events for dm-integrity target
  dm: introduce audit event module for device mapper
  • Loading branch information
torvalds committed Nov 9, 2021
2 parents 3725949 + 7552750 commit c183e17
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 31 deletions.
10 changes: 10 additions & 0 deletions drivers/md/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ config DM_INTEGRITY
select CRYPTO
select CRYPTO_SKCIPHER
select ASYNC_XOR
select DM_AUDIT if AUDIT
help
This device-mapper target emulates a block device that has
additional per-sector tags that can be used for storing
Expand Down Expand Up @@ -642,4 +643,13 @@ config DM_ZONED

If unsure, say N.

config DM_AUDIT
bool "DM audit events"
depends on AUDIT
help
Generate audit events for device-mapper.

Enables audit logging of several security relevant events in the
particular device-mapper targets, especially the integrity target.

endif # MD
4 changes: 4 additions & 0 deletions drivers/md/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ endif
ifeq ($(CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG),y)
dm-verity-objs += dm-verity-verify-sig.o
endif

ifeq ($(CONFIG_DM_AUDIT),y)
dm-mod-objs += dm-audit.o
endif
84 changes: 84 additions & 0 deletions drivers/md/dm-audit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Creating audit records for mapped devices.
*
* Copyright (C) 2021 Fraunhofer AISEC. All rights reserved.
*
* Authors: Michael Weiß <[email protected]>
*/

#include <linux/audit.h>
#include <linux/module.h>
#include <linux/device-mapper.h>
#include <linux/bio.h>
#include <linux/blkdev.h>

#include "dm-audit.h"
#include "dm-core.h"

static struct audit_buffer *dm_audit_log_start(int audit_type,
const char *dm_msg_prefix,
const char *op)
{
struct audit_buffer *ab;

if (audit_enabled == AUDIT_OFF)
return NULL;

ab = audit_log_start(audit_context(), GFP_KERNEL, audit_type);
if (unlikely(!ab))
return NULL;

audit_log_format(ab, "module=%s op=%s", dm_msg_prefix, op);
return ab;
}

void dm_audit_log_ti(int audit_type, const char *dm_msg_prefix, const char *op,
struct dm_target *ti, int result)
{
struct audit_buffer *ab = NULL;
struct mapped_device *md = dm_table_get_md(ti->table);
int dev_major = dm_disk(md)->major;
int dev_minor = dm_disk(md)->first_minor;

switch (audit_type) {
case AUDIT_DM_CTRL:
ab = dm_audit_log_start(audit_type, dm_msg_prefix, op);
if (unlikely(!ab))
return;
audit_log_task_info(ab);
audit_log_format(ab, " dev=%d:%d error_msg='%s'", dev_major,
dev_minor, !result ? ti->error : "success");
break;
case AUDIT_DM_EVENT:
ab = dm_audit_log_start(audit_type, dm_msg_prefix, op);
if (unlikely(!ab))
return;
audit_log_format(ab, " dev=%d:%d sector=?", dev_major,
dev_minor);
break;
default: /* unintended use */
return;
}

audit_log_format(ab, " res=%d", result);
audit_log_end(ab);
}
EXPORT_SYMBOL_GPL(dm_audit_log_ti);

void dm_audit_log_bio(const char *dm_msg_prefix, const char *op,
struct bio *bio, sector_t sector, int result)
{
struct audit_buffer *ab;
int dev_major = MAJOR(bio->bi_bdev->bd_dev);
int dev_minor = MINOR(bio->bi_bdev->bd_dev);

ab = dm_audit_log_start(AUDIT_DM_EVENT, dm_msg_prefix, op);
if (unlikely(!ab))
return;

audit_log_format(ab, " dev=%d:%d sector=%llu res=%d",
dev_major, dev_minor, sector, result);
audit_log_end(ab);
}
EXPORT_SYMBOL_GPL(dm_audit_log_bio);
66 changes: 66 additions & 0 deletions drivers/md/dm-audit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Creating audit records for mapped devices.
*
* Copyright (C) 2021 Fraunhofer AISEC. All rights reserved.
*
* Authors: Michael Weiß <[email protected]>
*/

#ifndef DM_AUDIT_H
#define DM_AUDIT_H

#include <linux/device-mapper.h>
#include <linux/audit.h>

#ifdef CONFIG_DM_AUDIT
void dm_audit_log_bio(const char *dm_msg_prefix, const char *op,
struct bio *bio, sector_t sector, int result);

/*
* dm_audit_log_ti() is not intended to be used directly in dm modules,
* the wrapper functions below should be called by dm modules instead.
*/
void dm_audit_log_ti(int audit_type, const char *dm_msg_prefix, const char *op,
struct dm_target *ti, int result);

static inline void dm_audit_log_ctr(const char *dm_msg_prefix,
struct dm_target *ti, int result)
{
dm_audit_log_ti(AUDIT_DM_CTRL, dm_msg_prefix, "ctr", ti, result);
}

static inline void dm_audit_log_dtr(const char *dm_msg_prefix,
struct dm_target *ti, int result)
{
dm_audit_log_ti(AUDIT_DM_CTRL, dm_msg_prefix, "dtr", ti, result);
}

static inline void dm_audit_log_target(const char *dm_msg_prefix, const char *op,
struct dm_target *ti, int result)
{
dm_audit_log_ti(AUDIT_DM_EVENT, dm_msg_prefix, op, ti, result);
}
#else
static inline void dm_audit_log_bio(const char *dm_msg_prefix, const char *op,
struct bio *bio, sector_t sector,
int result)
{
}
static inline void dm_audit_log_target(const char *dm_msg_prefix,
const char *op, struct dm_target *ti,
int result)
{
}
static inline void dm_audit_log_ctr(const char *dm_msg_prefix,
struct dm_target *ti, int result)
{
}

static inline void dm_audit_log_dtr(const char *dm_msg_prefix,
struct dm_target *ti, int result)
{
}
#endif

#endif
1 change: 0 additions & 1 deletion drivers/md/dm-bufio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,6 @@ static void __exit dm_bufio_exit(void)
int bug = 0;

cancel_delayed_work_sync(&dm_bufio_cleanup_old_work);
flush_workqueue(dm_bufio_wq);
destroy_workqueue(dm_bufio_wq);

if (dm_bufio_client_count) {
Expand Down
25 changes: 19 additions & 6 deletions drivers/md/dm-crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

#include <linux/device-mapper.h>

#include "dm-audit.h"

#define DM_MSG_PREFIX "crypt"

/*
Expand Down Expand Up @@ -1363,8 +1365,12 @@ static int crypt_convert_block_aead(struct crypt_config *cc,

if (r == -EBADMSG) {
char b[BDEVNAME_SIZE];
DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b),
(unsigned long long)le64_to_cpu(*sector));
sector_t s = le64_to_cpu(*sector);

DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu",
bio_devname(ctx->bio_in, b), s);
dm_audit_log_bio(DM_MSG_PREFIX, "integrity-aead",
ctx->bio_in, s, 0);
}

if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
Expand Down Expand Up @@ -2174,8 +2180,12 @@ static void kcryptd_async_done(struct crypto_async_request *async_req,

if (error == -EBADMSG) {
char b[BDEVNAME_SIZE];
DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b),
(unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
sector_t s = le64_to_cpu(*org_sector_of_dmreq(cc, dmreq));

DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu",
bio_devname(ctx->bio_in, b), s);
dm_audit_log_bio(DM_MSG_PREFIX, "integrity-aead",
ctx->bio_in, s, 0);
io->error = BLK_STS_PROTECTION;
} else if (error < 0)
io->error = BLK_STS_IOERR;
Expand Down Expand Up @@ -2735,6 +2745,8 @@ static void crypt_dtr(struct dm_target *ti)
dm_crypt_clients_n--;
crypt_calculate_pages_per_client();
spin_unlock(&dm_crypt_clients_lock);

dm_audit_log_dtr(DM_MSG_PREFIX, ti, 1);
}

static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
Expand Down Expand Up @@ -3351,21 +3363,22 @@ static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
spin_lock_init(&cc->write_thread_lock);
cc->write_tree = RB_ROOT;

cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write/%s", devname);
cc->write_thread = kthread_run(dmcrypt_write, cc, "dmcrypt_write/%s", devname);
if (IS_ERR(cc->write_thread)) {
ret = PTR_ERR(cc->write_thread);
cc->write_thread = NULL;
ti->error = "Couldn't spawn write thread";
goto bad;
}
wake_up_process(cc->write_thread);

ti->num_flush_bios = 1;
ti->limit_swap_bios = true;

dm_audit_log_ctr(DM_MSG_PREFIX, ti, 1);
return 0;

bad:
dm_audit_log_ctr(DM_MSG_PREFIX, ti, 0);
crypt_dtr(ti);
return ret;
}
Expand Down
35 changes: 26 additions & 9 deletions drivers/md/dm-integrity.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <linux/async_tx.h>
#include <linux/dm-bufio.h>

#include "dm-audit.h"

#define DM_MSG_PREFIX "integrity"

#define DEFAULT_INTERLEAVE_SECTORS 32768
Expand Down Expand Up @@ -539,6 +541,7 @@ static int sb_mac(struct dm_integrity_c *ic, bool wr)
}
if (memcmp((__u8 *)ic->sb + (1 << SECTOR_SHIFT) - size, result, size)) {
dm_integrity_io_error(ic, "superblock mac", -EILSEQ);
dm_audit_log_target(DM_MSG_PREFIX, "mac-superblock", ic->ti, 0);
return -EILSEQ;
}
}
Expand Down Expand Up @@ -876,8 +879,10 @@ static void rw_section_mac(struct dm_integrity_c *ic, unsigned section, bool wr)
if (likely(wr))
memcpy(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR);
else {
if (memcmp(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR))
if (memcmp(&js->mac, result + (j * JOURNAL_MAC_PER_SECTOR), JOURNAL_MAC_PER_SECTOR)) {
dm_integrity_io_error(ic, "journal mac", -EILSEQ);
dm_audit_log_target(DM_MSG_PREFIX, "mac-journal", ic->ti, 0);
}
}
}
}
Expand Down Expand Up @@ -1765,7 +1770,7 @@ static void integrity_metadata(struct work_struct *w)
char *mem, *checksums_ptr;

again:
mem = (char *)kmap_atomic(bv.bv_page) + bv.bv_offset;
mem = bvec_kmap_local(&bv);
pos = 0;
checksums_ptr = checksums;
do {
Expand All @@ -1775,17 +1780,22 @@ static void integrity_metadata(struct work_struct *w)
pos += ic->sectors_per_block << SECTOR_SHIFT;
sector += ic->sectors_per_block;
} while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
kunmap_atomic(mem);
kunmap_local(mem);

r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
checksums_ptr - checksums, dio->op == REQ_OP_READ ? TAG_CMP : TAG_WRITE);
if (unlikely(r)) {
if (r > 0) {
char b[BDEVNAME_SIZE];
DMERR_LIMIT("%s: Checksum failed at sector 0x%llx", bio_devname(bio, b),
(sector - ((r + ic->tag_size - 1) / ic->tag_size)));
sector_t s;

s = sector - ((r + ic->tag_size - 1) / ic->tag_size);
DMERR_LIMIT("%s: Checksum failed at sector 0x%llx",
bio_devname(bio, b), s);
r = -EILSEQ;
atomic64_inc(&ic->number_of_mismatches);
dm_audit_log_bio(DM_MSG_PREFIX, "integrity-checksum",
bio, s, 0);
}
if (likely(checksums != checksums_onstack))
kfree(checksums);
Expand Down Expand Up @@ -1953,7 +1963,7 @@ static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,
n_sectors -= bv.bv_len >> SECTOR_SHIFT;
bio_advance_iter(bio, &bio->bi_iter, bv.bv_len);
retry_kmap:
mem = kmap_atomic(bv.bv_page);
mem = bvec_kmap_local(&bv);
if (likely(dio->op == REQ_OP_WRITE))
flush_dcache_page(bv.bv_page);

Expand All @@ -1967,7 +1977,7 @@ static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,

if (unlikely(journal_entry_is_inprogress(je))) {
flush_dcache_page(bv.bv_page);
kunmap_atomic(mem);
kunmap_local(mem);

__io_wait_event(ic->copy_to_journal_wait, !journal_entry_is_inprogress(je));
goto retry_kmap;
Expand All @@ -1991,6 +2001,8 @@ static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,
if (unlikely(memcmp(checksums_onstack, journal_entry_tag(ic, je), ic->tag_size))) {
DMERR_LIMIT("Checksum failed when reading from journal, at sector 0x%llx",
logical_sector);
dm_audit_log_bio(DM_MSG_PREFIX, "journal-checksum",
bio, logical_sector, 0);
}
}
#endif
Expand Down Expand Up @@ -2058,7 +2070,7 @@ static bool __journal_read_write(struct dm_integrity_io *dio, struct bio *bio,

if (unlikely(dio->op == REQ_OP_READ))
flush_dcache_page(bv.bv_page);
kunmap_atomic(mem);
kunmap_local(mem);
} while (n_sectors);

if (likely(dio->op == REQ_OP_WRITE)) {
Expand Down Expand Up @@ -2534,8 +2546,10 @@ static void do_journal_write(struct dm_integrity_c *ic, unsigned write_start,

integrity_sector_checksum(ic, sec + ((l - j) << ic->sb->log2_sectors_per_block),
(char *)access_journal_data(ic, i, l), test_tag);
if (unlikely(memcmp(test_tag, journal_entry_tag(ic, je2), ic->tag_size)))
if (unlikely(memcmp(test_tag, journal_entry_tag(ic, je2), ic->tag_size))) {
dm_integrity_io_error(ic, "tag mismatch when replaying journal", -EILSEQ);
dm_audit_log_target(DM_MSG_PREFIX, "integrity-replay-journal", ic->ti, 0);
}
}

journal_entry_set_unused(je2);
Expand Down Expand Up @@ -4514,9 +4528,11 @@ static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv)
if (ic->discard)
ti->num_discard_bios = 1;

dm_audit_log_ctr(DM_MSG_PREFIX, ti, 1);
return 0;

bad:
dm_audit_log_ctr(DM_MSG_PREFIX, ti, 0);
dm_integrity_dtr(ti);
return r;
}
Expand Down Expand Up @@ -4590,6 +4606,7 @@ static void dm_integrity_dtr(struct dm_target *ti)
free_alg(&ic->journal_mac_alg);

kfree(ic);
dm_audit_log_dtr(DM_MSG_PREFIX, ti, 1);
}

static struct target_type integrity_target = {
Expand Down
Loading

0 comments on commit c183e17

Please sign in to comment.