open /dev/mem - Operation not permitted

26,794

You cannot read /dev/mem if you are not root.

There is no reason for an ordinary application to access /dev/mem, i.e. the physical RAM, since applications are running in virtual memory !

If you change the permission of /dev/mem to enable that (you should not), you will open a huge security hole in your system. Only trusted root processes should access /dev/mem. See mem(4)

(you could use setuid techniques if so wanted, or run your program with sudo)

If you need to access virtual memory in the address space of some other process, consider proc(5) e.g. /proc/1234/mem for process of pid 1234.

Share:
26,794
Sandeep
Author by

Sandeep

An open source enthusiast. Interested in Robotics/embedded systems and Android OS. For queries, you can contact me on [email protected]

Updated on March 13, 2020

Comments

  • Sandeep
    Sandeep about 4 years

    I am working on ubuntu.

    I am trying to open /dev/mem and i am getting permission denied

    int32_t open_memdev()
    {
            int32_t fd;
    
            fd = open("/dev/mem", O_RDONLY);
            if (fd < 0) {
                    printf("Failed to open /dev/mem : %s\n", strerror(errno));
                    return-EINVAL;
            }
    
            return fd;
    }
    

    This code always prints "Failed to open /dev/mem : Operation not permitted"

    I have searched for this on SO

    1. access-permissions-of-dev-mem

    2. accessing-mmaped-dev-mem

    These q's seem to discuss the issue of not being able to access above 1 MB, but my problem is that i am unable to open even once.

    Addtitional details if they help:

    1) I checked my configuration that CONFIG_STRICT_DEVMEM is enabled.

    2) ls -l /dev/mem
    crw-r----- 1 root kmem 1, 1 2014-03-13 13:57 /dev/mem
    

    Please let me know if additional information is required.

  • Zibri
    Zibri about 6 years
    ok, let's say I want to open the huge security hole. How can I setup dev/mem so every user can use it? (I need it for a test)
  • Basile Starynkevitch
    Basile Starynkevitch about 6 years
    @Zibri: run your program as root with sudo or make your program setuid
  • Zibri
    Zibri about 6 years
    I know that I was searching for a way to have /dev/mem free to use for all (for a test study). Anyway I found it. I just had to comment one line in mem.c and add return 0; instead.
  • Paul Stelian
    Paul Stelian over 5 years
    For some reason even as root I still cannot open it.
  • vk5tu
    vk5tu about 4 years
    Access to /dev/mem is often subject to additional restrictions even when the user is root, such as from SELinux's memory_device_t or from kernel lockdown (often automatically initiated from Secure Boot), see github.com/torvalds/linux/commit/…