You guys befuddled the man again...

A segment violation is an access violation on a page of memory, commited
by the program or by the kernel. A sigsegv done by a user program causes
a special piece of code in the kernel to trigger and to stop the user's
task, by delivering a SIGSEGV to it. By default, when this happens, a core
file is generated and the program stops abruptly (w/o saving data etc).
The program can catch SIGSEGV and prevent the core dump. That is all it
can do, and it MUST exit. There is no way a SIGSEGV handler can return to
the program (well, almost ;).

When a kernel subroutine generates a segmentation violation, then a
so-called OOPS (oops = Linux slang for small turd afaik) is generated. 
This appears in the syslogs. 

The reasons for segment violations are two, and two only: poor programming
practice (buggy programs) or hardware faults.

At the hw level: The addressable memory on an Intel proecssor is divided
into pages (currently 4k each ?). This is the minimum size of any
allocated memory, file, program etc. Each page has read write and exec
permission bits (among others). When a program or the kernel code tries to
use a page as source or target for something that its protection bits say
it can't be used, then a segmentation fault occurs. This is a hardware
event in which the processor refuses to execute the offending instruction,
and executes the segmentation fault error handler instead. That one makes
the oops or causes the SIGSEGV or the core dump, depending on what eactly
caused the event. Page permission bits are set by the kernel paging
functions, and/or by certain other kernel utilities (INSIDE the kernel).
   
Typical examples for page permission bit use, is the non-exec stack patch,
a Linux security enhancement. It resets the exec bit of all the pages that
belog to the stack memory (which are by default rwx normally).

The best way to avoid sigsegvs is to keep track of ALL of your pointers in
C code (and what they are pointing to). This includes range and size
checks on every increment/decrement, cast, etc. 

bye,

        Peter


Reply via email to