2. Another possible use of segmentation is to strictly separate code and data ... or more precisely executable and writable bytes.
As a system designer, you should be aware that most Unix security problems comes from so-called
buffers overflow : an unsafe function
Code: Select all
void fct() {
char buffer[256];
...
strcpy(buffer, <something that could possibly be larger than 256 bytes>)
}
could see the
strcpy function go beyond the space allocated to
buffer and starts erasing the stack content. As this stack contains (among others) the return address where we must branch to when the function is over, it is possible (using some reverse-engineering on the target program) to send the execution to bytes that were passed from an untrusted sources (a message coming from Internet, a user input, a file, whatever).
Unix & clones protects the text of the program to be overwritten, but they don't prevent the stack to be executed...
This can be done very easily by restricting the size of the user code segment to the size of the .text section at load time...
Note that it is absolutely not necessarily that code and data share the same addresses (unless you want to read the code instead of executing it), so you can safely locate code segment anywhere in memory, and using some quick remapping techniques (moving pages entries and segments bases accordingly) and even make it grow to add plugins, etc.
because you have no bits that is both writable and executable, the risk of buffer-overflow, worms, etc. is suppressed... Your OS is secure and only the kernel bugs can cause ennemy code to be executed.