Reputation:
In Windows, you can easily open a handle to PhysicalDrive (or even \\.\c:) and write to the first sector of disk using WriteFile from user-mode, was wondering what is the equivalent c/c++ code of doing this Linux which requires the lowest privilege possible? is it possible to do this in Linux without running the code with sudo?
Basically I want to compile an ELF program that does exactly that but in Linux with lowest privilege possible, how can I do this? because in Windows you can do this from user-mode.
Also are SCSI PASSTROUGH DIRECT message possible in Linux as well for reading and writing to disk sectors? if so then how can I send a SCSI passthrough IRP message in Linux to disk driver to write to a file, in windows we can use DeviceIOcontrol to send the message ( sorry for the rookie questions, I'm not really familiar with programming in Linux )
Upvotes: 0
Views: 326
Reputation: 1
On Linux, a disk is often some block device file such as /dev/sda
. See sd(4) and hd(4).
The pseudofile /proc/partitions
is giving the disk partitions. See proc(5) and try cat /proc/partitions
in a shell or terminal emulator.
You could have a setuid program accessing the disk. You would need ioctl(2). Beware: the setuid or setgid mechanism is tricky. Take several hours to understand it (or else you'll open a vulnerability).
For more, read Advanced Linux Programming then syscalls(2)
On well configured Linux systems, /dev/sda
is not accessible to all users. On Debian, it has:
% ls -l /dev/sda
brw-rw---- 1 root disk 8, 0 Jun 25 18:53 /dev/sda
so you might code a disk
setgid ELF executable. See credentials(7) and capabilities(7) and execve(2) and elf(5)
Look also on KernelNewbies, study the kernel source code, read about initrd and systemd.
Study then improve the source code of GNU parted, since it is free software.
Upvotes: 1
Reputation: 800
In linux, every device is a file. So your first, physical harddrive could be /dev/sda . If you have sufficient permissions you could communicate (open, read and write) directly with your harddrive. After all .. it's just a file.
As for the privilege: Any user could do that, provided he/she has the required permission to do whatever you want with the specific device. That works via file permissions. For example you could add a user into a certain group and assign THIS group to your device, giving the group the required permissions via chmod.
You could also set the suid-bit for the program which does stuff with your physical drive. Meaning: If the user (he still needs execute-permissions for this program) starts it, the program iteself runs with another user id. Which could be root. Meaning: This process alone runs with elevated permissions, doing for the user what he/she couldn't do otherwise.
Upvotes: 3