AleW
AleW

Reputation: 185

How are memory read, write, execute permissions enforced in the Linux kernel?

How is a system able to restrict processes from being able to write to regions of memory set read-only? In particular, how is the Linux kernel able to enforce these permissions, assuming that the hardware is not able to do this job for the kernel?

My initial guess is that these regions of memory are not mapped to the process's address space, so whenever the process tries accessing these regions of memory, a page fault is generated, which allows the kernel to take control to check that everything is in order. I realize this would probably greatly degrade performance, so I'm here asking for help understanding if there is a smarter method to enforcing these permissions.

Upvotes: 2

Views: 2478

Answers (2)

user3344003
user3344003

Reputation: 21617

There are four ways that prevent access to memory in a non-segmented system to restrict access to pages.

  1. There is no page table entry for specific address (easy to do with nested page tables).

  2. There is no page mapping for the page.

  3. The page table restricts access by mode. For example, does not allow user mode access but allows kernel mode.

  4. The page table restricts access by type (write,execute).

Setting up the page tables is done in hardware. Validating the access against the tables is done in hardware.

Upvotes: 0

Krzysztof Adamski
Krzysztof Adamski

Reputation: 2079

The task of enforcing memory protection is handled by MMU. I'm not aware of any architecture that does have MMU but don't have hardware support memory permissions. Thus I guess we are talking about MMU-less systems here.

For long time Linux required MMU to work. It still does if you want to but the is some support for MMU-less systems. It comes from uClinux project that was merged upstream some time ago. The system compiled with NOMMU does not, however, work like normal Linux system (a lot of applications won't work on it) and no memory protection is one of its limitations.

To answer your question directly - memory protection on Linux depends on hardware support for it. If it's absent, the kernel won't try to emulate it.

Now your idea seems fine (if impractical) but in order to do this, you still need virtual memory support which requires some kind of MMU. As stated earlier, I don't think there are any systems that do have MMU but does not support memory protection. Either way, Linux does not seem to support this case.

Upvotes: 1

Related Questions