6

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.

Community
  • 1
  • 1
Sandeep
  • 18,356
  • 16
  • 68
  • 108
  • 1
    What is your ["`id`"](http://linux.about.com/library/cmd/blcmdl1_id.htm), are you root? – osgx Mar 13 '14 at 05:29
  • possible duplicate of [Access permissions of /dev/mem](http://stackoverflow.com/questions/6134984/access-permissions-of-dev-mem) – osgx Mar 13 '14 at 05:32
  • @osgx I clarified the point of this Q being a duplicate of the one you mentioned. Request you to look at that part in my question. No i am not root. With root i am able to read.I want to read without being a root as a normal user – Sandeep Mar 13 '14 at 05:59
  • 1
    @Manty you cannot. That's what permissions are for. – Henno Brandsma Mar 13 '14 at 06:53
  • 1
    Please explain why do you need to access `/dev/mem` from an ordinary id .... – Basile Starynkevitch Mar 13 '14 at 08:03

1 Answers1

10

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 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) – Zibri Mar 12 '18 at 15:34
  • @Zibri: run your program as root with `sudo` or make your program setuid – Basile Starynkevitch Mar 12 '18 at 15:41
  • 1
    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. – Zibri Mar 13 '18 at 15:45
  • 3
    For some reason even as root I still cannot open it. – Paul Stelian Sep 19 '18 at 10:46
  • 1
    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 https://github.com/torvalds/linux/commit/9b9d8dda1ed72e9bd560ab0ca93d322a9440510e – vk5tu May 03 '20 at 12:13