Re: Trying to set up a stack segment with limits and failing
Posted: Tue Mar 04, 2025 2:40 am
Of course it does. Mark a code page as read only and the program can't overwrite it. Use guard pages and you protect against stack overflow.
The Place to Start for Operating System Developers
http://f.osdev.org/
Of course it does. Mark a code page as read only and the program can't overwrite it. Use guard pages and you protect against stack overflow.
That's pretty useless. The problem is not that you might overwrite code, but that you start to execute code you shouldn't execute. Guard pages might work for the stack, but only if you don't attempt to grow it more than 4k. You also forgot about the biggest problem: malloc/free. In the flat kernel, drivers use malloc to allocate global addresses, and when they happen to overwrite them, they overwrite unrelated stuff, like the scheduling lists, task blocks or some vital device driver data. I have yet to see an OS that provides local malloc per device driver, or some other more efficient means of protecting drivers and kernel from each others.
That's what the NX bit is for.
Stack probes protect against that by touching every 4K page.
Segments do not solve that problem. As you mention, microkernels partially address this, but what is better is the use of a language with a proper type system (i.e. not C).
A popular "efficient means" is Rust, which outright rule out the class of bug you mention, the only requirement for prevention being a small set of thoroughly reviewed code wrapped in "unsafe" blocks. If those unsafe blocks are proven safe, then "Safe Rust" statically cannot cause memory bugs.
ASID (or PCID in x86 terms) significantly reduce the amount of TLB flushing necessary by allowing multiple address spaces to exist in the TLB simultaneously.
You keep repeating this talking point and I am bloody well tired of it. In practice, there is no protecting the kernel from the drivers or vice versa, because each driver can DMA over any "protected" part of the address space, entirely circumventing all CPU-based protection mechanisms. For this reason, any discussion on the relative merits of segmentation vs. paging is entirely academic.
Just because things like PCI devices can write to any physical location in memory without consulting the OS, doesn't mean protecting drivers from each others has no benefit. Protection is not a black-and-white concept that you either have or not have. It needs to be layered in an appropriate way. The most vulnerable part is physical memory, so access to it should be restricted to a few trusted modules. There are also smart algorithms for constructing in-memory schedules that many modern PCI devices work with that makes the probability of misusing physical memory lower.nullplan wrote: ↑Tue Mar 04, 2025 10:41 pm In practice, there is no protecting the kernel from the drivers or vice versa, because each driver can DMA over any "protected" part of the address space, entirely circumventing all CPU-based protection mechanisms. For this reason, any discussion on the relative merits of segmentation vs. paging is entirely academic.
User processes typically don't have direct access to devices, so those issues do not apply for regular applications.