LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 05-26-2011, 01:49 AM   #1
pavan8085
LQ Newbie
 
Registered: May 2011
Location: Bangalore, India
Distribution: Ubuntu
Posts: 13

Rep: Reputation: 8
Lightbulb /dev/mem permissions


Hi All,

I've a set of questions regarding /dev/mem-

1. Many articles on the net, seem to refer /dev/mem as the gateway to "Physical RAM". But if I am right, /dev/mem is the gateway to the "Physical Address Space" of the processor which might include control registers of many HW peripherals and not just the RAM? Please correct me if I am wrong!

2. In order to prevent attackers from misusing /dev/mem and altering kernel memory, a flag CONFIG_STRICT_DEVMEM needs to be enabled which will prevent user apps from accessing physical address space beyond 1MB. I checked the config file on my PC ( Ubuntu ) and found that CONFIG_STRICT_DEVMEM = y. And I wrote a program which tries to read to physical memory beyond 1 MB and I was able to read!! No segmentation fault or any operation permitted error. How is this possible?
(
My program roughly looks like this-
fd=open ( "/dev/mem", O_RDWR);
ptr=(int*)mmap(0,MAP_SIZE,PROT_READ, fd, myAddress & (~MAP_MASK));
printf("%d",*ptr);
)

Thanks,
Pavan
 
Old 05-27-2011, 11:13 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
/dev/mem is the gateway to the "Physical Address Space" of the processor which might include control registers of many HW peripherals and not just the RAM?
Yes it does include peripherals, because there is a preference for using memory mapped I/O in x86 systems. It is still the physical memory space, so the name of the device is reasonable (see also /dev/port).

A segmentation fault won't happen until the read occurs (I'm not clear what values you are using for 'myAddress' and 'MAP_MASK'. But you will need to check the result of the mmap. On my system it is accessible up to 0x101000.

For example, the following code fragment would typically fail, since the CONFIG_STRICT_DEVMEM=y will cause the mmap to fail:
Code:
   int fd = open("/dev/mem", O_RDWR);
   char *ptr = mmap(0, 0x180000, PROT_READ, MAP_PRIVATE, fd, 0);
   printf("%u\n", (unsigned int) ptr);
   return *ptr;

Last edited by neonsignal; 05-27-2011 at 11:31 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
/dev/mem permissions pavan8085 Linux - Mobile 3 05-27-2011 07:35 AM
[SOLVED] Stupidly ran "cat /dev/urandom > /dev/mem", worried I broke firmware crosstalk Linux - Hardware 2 10-25-2010 05:27 PM
What would happen if I where to cat /dev/mem > /dev/null Joey.Dale Linux - General 11 07-26-2009 12:46 PM
Startx Permission problems on /dev/null and /dev/mem on freshly compiled 2.6.22.1 Eric_Cartman Linux - Kernel 2 09-09-2007 01:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 07:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration