Linux Kernel : What is /dev/mem or /dev/kmem?

来源:互联网 发布:欧洲 人俄罗斯 知乎 编辑:程序博客网 时间:2024/06/05 06:43

本文转载至:http://priyaranjan-technicalzone.blogspot.com/2014/02/linux-kernel-what-is-devmem-or-devkmem.html

The /dev/mem and /dev/kmem character special files provide access to a pseudo device driver that allows read and write access to system memory or I/O address space. 
Programs accessing these special files must have appropriate privilege. Commercial application programs should avoid using the /dev/mem and/dev/kmem files, since the virtual memory image is quite specific to the operating system level and machine platform. Use of these special files thus seriously affects the portability of the application program to other systems.When incorrect access to virtual memory is made through these files, process termination, a system crash, or loss of system data integrity can result.

/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. In order to prevent attackers from misusing /dev/mem and altering kernel memory, a flagCONFIG_STRICT_DEVMEM needs to be enabled which will prevent user apps from accessing physical address space beyond 1MB. The option  makes the kernel check addresses in /dev/mem with devmem_is_allowed() in arch/.../init.c 


Config Option in the Kernel
CONFIG_STRICT_DEVMEM:
  │ 
  │ If this option is disabled, you allow userspace (root) access to all
  │ of memory, including kernel and userspace memory. Accidental    
  │ access to this is obviously disastrous, but specific access can   
  │ be used by people debugging the kernel. Note that with PAT support    
  │ enabled, even in this case there are restrictions on /dev/mem     
  │ use due to the cache aliasing requirements.         
  │                                                          
  │ If this option is switched on, the /dev/mem file only allows     
  │ userspace access to PCI space and the BIOS code and data regions.    
  │ This is sufficient for dosemu and X and all common users of          
  │ /dev/mem.                                          
  │                                                    
  │ If in doubt, say Y.                             
  │                                                        
  │ Symbol: STRICT_DEVMEM [=y]    
  │ Type  : boolean                                               
  │ Prompt: Filter access to /dev/mem                        
  │   Defined at arch/x86/Kconfig.debug:8      
  │     -> Kernel hacking


0 0