CJ wrote:
> Hello:
>
> We know that C programs are often vulnerable to buffer overflows which
> overwrite the stack.
>
Only if you can execute code in the stack
> But my question is: Why does C insist on storing local variables on the
> stack in the first place?
>
The principal reason is efficiency. Stack allocation is very fast,
in most cases just a single machine instruction. Deallocation is equally
fast, with a single instruction.
> I can see two definite disadvantages with this:
> 1) deeply nested recursive calls to a function (especially if it defines
> large local arrays) can easily overflow the stack
Yes, that is why stack allocation of large arrays is not a very
good idea.
> 2) the problems described above of security vulnerabilities.
>
This happens only if you have the buffer overflow in the first place.
Note that a buffer overflow of a heap allocated buffer is very
bad also.
> My solution would be for C instead to store its local variables on the
> heap - effectively separating data from executable code.
>
Yes, that is "a" solution. You can implement this easily in C
if you just instead of
int fn(void)
{
char buffer[BUFSIZ];
}
you write
int fn(void)
{
char *buffer = malloc(BUFSIZ);
}
> What do people think?
>
I think that you should allocate variables as you think is the best for
your application.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32