1

I have a file that is mounted as a loop device. In order to implement the functionality of dm-integrity in C, I have to overwrite the superblock with NULL.

Is there anyway how I can write directly into the superblock? Or is a file write into the loop device starting with the superblock? As it contains metadata I would assume that a "data area" is used when writing to it, instead of the metadata area?

Thanks for any help!

Edit: Here is my current formatting of the superblock:

DEBUG("[+] Formatting the Device");
// Create 4kb NULL buffer
char null_array[4096];
memset(&null_array[0],0,4096);

// Format superblock
int ret_val = file_write(loop_device,&null_array[0],4096);
if(ret_val < 0)
{
    ERROR_ERRNO("[+] Could not write to file! Superblock not clear. Aborting");
    return -1;
}else
{
    DEBUG("[+] Formatting worked! Return value: %d", ret_val);
}

It works for a test loop device created with a random file:

dd if=/dev/urandom of=./diskimage.img bs=1M count=512
losetup /dev/loop6 ./diskimage.img

But on the real target it will not work (as seen as the ioctl system call with the DM_TABLE_LOAD exits (errno 22: Invalid Argument + device-mapper: table: 252:0: integrity: The device is not initialized -> that happens when the superblock is not fully zero)

MajorasKid
  • 733
  • 2
  • 5
  • 24
  • 1
    If I'm reading correctly, you need to zero a 4KiB block after the reserved sectors (if any). If you're initializing the whole thing, you could just zero everything. You might also want to use the `integritysetup` utility to do all this for you. – Hasturkun Mar 10 '21 at 10:10
  • So, I am able to overwrite the superblock (i guess) by directly accessing the loop device. The problem is that on one system the superblock gets fully overwritten, on another not. Is there a relyable way to retrieve the superblock size? – MajorasKid Mar 10 '21 at 10:17
  • Thanks @Hasturkun. Thats what I'm currently trying (i cannot use integritysetup unfortunally... ). It works on my local test machine, but i cannot port it to another maschine. I guess the superblock size differs? I will edit my current state – MajorasKid Mar 10 '21 at 10:19
  • By the documentation, I think the superblock size is fixed, but again, may have reserved sectors before it (depending on the target arguments). Maybe you should look at what integritysetup does? I've been browsing through the code, and it seems to be zeroing the first 8 sectors (8 * 512 bytes) of the device. – Hasturkun Mar 10 '21 at 12:50

0 Answers0