Score:0

/dev/mem and even /dev/port can't open even as root!

de flag

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?

waltinator avatar
it flag
Writing directly to hardware is discouraged in Linux. Try another approach. `man -k sound`
TheRandomGuyNamedJoe12 avatar
de flag
See: [/dev/mem: mmap: Operation not permitted: Using dmidecode](https://askubuntu.com/q/508754/1391094)
TheRandomGuyNamedJoe12 avatar
de flag
@Rinzwind I never mmaped /dev/port, only /dev/mem I tried to open /dev/port though
TheRandomGuyNamedJoe12 avatar
de flag
@Rinzwind It still dosen't explain the od failing (outputting 0 characters) on /dev/mem and /dev/port
Score:0
de flag

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

Score:0
cn flag

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"):

TheRandomGuyNamedJoe12 avatar
de flag
Memory-mapped PCI devices should still work it explicitly says, and that's what I'm doing, opening should still be allowed as root.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.