Reputation: 23
I'm wondering if it's possible to write c code to open and mmap a memory pointer to /dev/mem of a remote file system. I've mounted this system under /mnt/ of the system that I'm planning to run the code from. I mounted /dev using sshfs, so the full path would just look like /mnt/mem from the system where the c code is run.
In my experience, I could open/mmap /dev/mem if I was ssh'ed into the remote system directly and access the components I wanted. That is, if I change the string used to open the file descriptor from /mnt/mem to /dev/mem, and compile and run that locally instead that works and fetches the register data I expect.
It seems like I can successfully open and mmap to /home/whatever on the remote system as well. Is there some reason why a remotely mounted /dev/mem just can't be opened the same way?
Some extra info for my case: -Linux distros on both systems -ssh installed on both -sshfs installed where I did the mounting from -I have root access to both systems
properties of /dev/mem as observed from the host system:
crw-rw-rw- 1 root root 0, 0 Dec 8 2105 /mnt/mem
The errno returns permission denied as if even with root privilege I still cannot open memory this way, but I'm not sure why this should not work if r/w permission is set in all the paths and mem itself.
I also performed the sshfs mount using the remote system's root and root pw.
//Mmap a target to a pointer variable/fd
struct reg_ptrs mmap_target(struct access_method_info mthd_info)
{
struct reg_ptrs mapped_ptrs;
mapped_ptrs.iic_bar = NULL;
char mem_path1[100];
strcpy(mem_path1,"/mnt/mem");
int mem_fd1;
mem_fd1 = open(mem_path1, O_RDWR);
if(mem_fd1 == -1) {
printf("Couldn't open()! %s\n", strerror(errno));
}
mapped_ptrs.iic_bar = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd1, 0xff0e0000);
//With pointers mapped to appropriate memory, we should be able to close these now
close(mem_fd1);
return mapped_ptrs;
} /* mmap_target */
The expected results would be for the open() and mmap() c functions to succeed at opening and mapping the remotely mounted /dev/mem file
Upvotes: 0
Views: 907
Reputation: 126488
Device files are not really files that get opened -- when you open one, it uses the device number from the inode as an index in the kernel's device table to know which one to open. So if you could open /mnt/mem (the device from the remote machine, it would look up the index of the remote kernel's memory device in the local kernel. So you'd get some random device on your local kernel, whatever happened to be in that slot. Even if it was the memory device (and it might be, if the kernels were configured similarly), you would get the local kernel's memory, not the remote kernel's memory. For this reason, remote file systems are (almost) invariably mounted with the -nodev option (its the default), which means device files cannot be opened.
Upvotes: 1