Welcome to the Linux Foundation Forum!

Write blocker for linux ubuntu kernel

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;

  1. atomic_inc(&cmd->device->iorequest_cnt);
  2.  
  3. unsigned char opcode = cmd->cmnd[0];
  4.  
  5. // Check for write commands
  6. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  7. printk(KERN_INFO "Blocking write command in scsi_dispatch_cmd. Opcode: 0x%02x\n", opcode);
  8.  
  9. // Set appropriate error
  10. cmd->result = (DID_ERROR << 24) | SAM_STAT_CHECK_CONDITION;
  11. scsi_build_sense_buffer(0, cmd->sense_buffer, ILLEGAL_REQUEST, 0x20, 0x00);
  12.  
  13. // Complete the command
  14. goto done;
  15. } else {
  16. printk(KERN_INFO "Allowed command in scsi_dispatch_cmd. Opcode: 0x%02x\n", opcode);
  17. }

any help is appreciated.

Thanks in advance

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training