3

I made a program that tries to do mmaping on /dev/mem and it fails with:

Failed to open /dev/mem: Operation not permitted

And I tried using od as a test for my code being bad too:

od: /dev/mem: Operation not permitted

od: /dev/port: Operation not permitted

I am also running Kubuntu 22.04 LTS

To clarify, I never mmaped /dev/port.

How do I make these files open-able?

marbens
  • 217

2 Answers2

2

It is a kernel parameter named CONFIG_STRICT_DEVMEM. By default, since kernel 2.6.26, it is disabled and as such does not allow access to it.

Find out what you have it set to:

$ ll /proc/ | grep conf
$ ll /boot/ | grep conf
-rw-r--r--  1 root root   261604 Apr 19 14:01 config-5.15.0-1033-gcp
-rw-r--r--  1 root root   234065 Apr 10  2020 config-5.4.0-1009-gcp
$ cd /boot/
$ more config-5.15.0-1033-gcp | grep CONFIG_STRICT_DEVMEM
CONFIG_STRICT_DEVMEM=y

See man 4 mem on command line. Excerpt:

Since Linux 2.6.26, and depending on the architecture, the CONFIG_STRICT_DEVMEM kernel configuration option limits the areas which can be accessed through this file. For example: on x86, RAM access is not allowed but accessing memory-mapped PCI regions is.

It is typically created by:

mknod -m 660 /dev/mem c 1 1
chown root:kmem /dev/mem

So you need to disable CONFIG_STRICT_DEVMEM and that will require recompiling the kernel. 2 links about it (but it is a little bit beyond "Ubuntu"):

andrew.46
  • 39,359
Rinzwind
  • 309,379
2

For me it was secure boot in the BIOS; run systemctl reboot --firmware-setup then disable secure boot in the BIOS setup.

This is something called lockdown mode.

marbens
  • 217