Linux write into Superblock in C

276 views Asked by At

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)

0

There are 0 answers