开发者

How exactly does OS protect kernel

开发者 https://www.devze.com 2023-01-18 05:12 出处:网络
my question is how exactly does operating system protect it\'s kernel part. From what I\'ve found there are bas开发者_运维技巧ically 2 modes kernel and user. And there should be some bits in memory s

my question is how exactly does operating system protect it's kernel part.

From what I've found there are bas开发者_运维技巧ically 2 modes kernel and user. And there should be some bits in memory segments which tels if a memory segment is kernel or user space segment. But where is the origin of those bits? Is there some "switch" in compiler that marks programs as kernel programs? And for example if driver is in kernel mode how does OS manages its integration to system so there is not malicious software added as a driver?

If someone could enlighten me on this issue, I would be very grateful, thank you


The normal technique is by using a feature of the virtual memmory manager present in most modern cpus.

The way that piece of hardware works is that it keeps a list of fragments of memory in a cache, and a list of the addresses to which they correspond. When a program tries to read some memory that is not present in that cache, the MMU doesn't just go and fetch the memory from main ram, because the addresses in the cacher are only 'logical' addresses. Instead, it invokes another program that will interpret the address and fetch that memory from wherever it should be.

That program, called a pager, is supplied by the kernel, and special flags in the MMU prevent that program from being overridden.

If that program determines that the address corresponds to memory the process should get to use, it supplies the MMU with the physical address in main memory that corresponds to the logical address the user program asked for, the MMU fetches it into its cache, and resumes running the user program.

If that address is a 'special' address, like for a memory mapped file, then the kernel fetches the corresponding part of the file into the cache and lets the program run along with that.

If the address is in the range that belongs to the kernel, or that the program hasn't allocated that address to itself yet, the pager raises a SEGFAULT, killing the program.

Because the addresses are logical addresses, not physical addresses, different user programs may use the same logical addresses to mean different physical addresses, the kernel pager program and the MMU make this all transparent and automatic.

This level of protection is not available on older CPU's (like 80286 cpus) and some very low power devices (like ARM CortexM3 or Attiny CPUs) because there is no MMU, all addresses on these systems are physical addresses, with a 1 to 1 correspondence between ram and address space


The “switch” is actually in the processor itself. Some instructions are only available in kernel mode (a.k.a. ring 0 on i386). Switching from kernel mode to user mode is easy. However, there are not so many ways to switch back to kernel mode. You can either:

  • send an interrupt to the processor
  • make a system call.

In either case, the operation has the side effect of transferring the control to some trusted, kernel code.


When a computer boots up, it starts running code from some well known location. That code ultimately ends up loading some OS kernel to memory and passing control to it. The OS kernel then sets up the CPU memory map via some CPU specific method.


And for example if driver is in kernel mode how does OS manages its integration to system so there is not malicious software added as a driver?

It actually depends on the OS architecture. I will give you two examples:

  1. Linux kernel: A driver code can be very powerful. The level of protections are following:

a) A driver is allowed to access limited number of symbols in the kernel, specified using EXPORT_SYMBOL. The exported symbols are generally functions. But nothing prevents a driver from trashing a kernel using wild pointers. And the security using EXPORT_SYMBOL is nominal.

b) A driver can only be loaded by the privileged user who has root permission on the box. So as long as root privileges are not breached system is safe.

  1. Micro kernel like QNX: The operating system exports enough interface to the user so that a driver can be implemented as a user space program. Hence the driver at least cannot easily trash the system.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号