Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open /dev/mem - Operation not permitted

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.

like image 456
Sandeep Avatar asked Mar 13 '14 05:03

Sandeep


Video Answer


1 Answers

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.

like image 106
Basile Starynkevitch Avatar answered Oct 19 '22 06:10

Basile Starynkevitch