Welcome to the Linux Foundation Forum!

Write blocker for linux ubuntu kernel

freezing
freezing Posts: 1

Hello all,

I am trying to create a write blocker on linux kernel level.

I have successfully added write blocking instructions for USB in usb.c and transport.c

But i am struggling to add to SCSI and NVME, as far as i understand SCSI will cover both ATA/SATA.

I am trying this in drivers/scsi/scsi_lib.c :

static int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
{
struct Scsi_Host *host = cmd->device->host;
int rtn = 0;

atomic_inc(&cmd->device->iorequest_cnt);

unsigned char opcode = cmd->cmnd[0];

   // Check for write commands
if (cmd->sc_data_direction == DMA_TO_DEVICE) {
    printk(KERN_INFO "Blocking write command in scsi_dispatch_cmd. Opcode: 0x%02x\n", opcode);

    // Set appropriate error
    cmd->result = (DID_ERROR << 24) | SAM_STAT_CHECK_CONDITION;
    scsi_build_sense_buffer(0, cmd->sense_buffer, ILLEGAL_REQUEST, 0x20, 0x00);

    // Complete the command
    goto done;
} else {
    printk(KERN_INFO "Allowed command in scsi_dispatch_cmd. Opcode: 0x%02x\n", opcode);
}

any help is appreciated.

Thanks in advance

Categories

Upcoming Training