Iftikhar Ali
Iftikhar Ali

Reputation: 401

Memory access security implementation question

I understand that if a program needs access to certain protected instructions it needs to use the system call interface. IO Device access could be one example. An interrupt is generated which sets the mode to kernel mode etc.

My question is following though: How is a program prevented from accessing memory which is out of its process boundary. For example accessing array beyond its length or just arbitrary access to a random memory address.

The way I understood, only operating system has capability to check if program is accessing memory within it bounds. And to be able to engage operating system an interrupt is required. So does it mean array and variables memory allocation has to be system call. I doubt it.

The other option is that memory controller or the processor has capability to make sure a program doesn’t access a memory beyond its bounds.

Any clarification in this regard will be greatly appreciated.

Upvotes: 0

Views: 57

Answers (1)

Nate Eldredge
Nate Eldredge

Reputation: 58473

The other option is that memory controller or the processor has capability to make sure a program doesn’t access a memory beyond its bounds.

That's how it works. Modern operating systems run on CPUs with memory protection. You can find the details in any arch/OS book, but the most basic idea is that the OS sets up page tables which tell the CPU what addresses in virtual memory the program may access, and how they map to physical memory. Then it simply has to ensure that only memory which is properly allocated to the process is included in that map.

As the program runs, on every instruction that accesses memory, the CPU will check the address against the page tables (or a copy cached within the CPU in special memory called a TLB). Should the page tables not show the access as permitted, the CPU won't perform it but will instead trap to the operating system, which can decide what to do (e.g. kill the process).

Upvotes: 2

Related Questions