473,513 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using static char arrays to be on the safe side

Hi

I program primarily in C++ , but once in a while one is forced to use
the odd strcpy or call API functions that dump results into char*
buffers.

I believe that most security exploits that work by thrashing the stack
to overwrite the return address, allowing arbitrary code execution.
I have now fallen into the habit of declaring temporary buffers as
static char arrays.

Is this a good idea in general?

Are there other kinds of exploits which do not rely on stack
thrashing?
Vivek
Nov 17 '07 #1
11 2033
rep_movsd wrote:
>
Hi

I program primarily in C++ , but once in a while one is forced to use
the odd strcpy or call API functions that dump results into char*
buffers.

I believe that most security exploits that work by thrashing the stack
to overwrite the return address, allowing arbitrary code execution.
I have now fallen into the habit of declaring temporary buffers as
static char arrays.

Is this a good idea in general?
No.
Either your program is correct or it isn't.

--
pete
Nov 17 '07 #2
rep_movsd wrote:
Hi

I program primarily in C++ , but once in a while one is forced to use
the odd strcpy or call API functions that dump results into char*
buffers.

I believe that most security exploits that work by thrashing the stack
to overwrite the return address, allowing arbitrary code execution.
I have now fallen into the habit of declaring temporary buffers as
static char arrays.

Is this a good idea in general?
No. Just for starters, it becomes extremely difficult
to write a re-entrant function. Also, it doesn't solve the
real problem, which is overrunning the array boundaries to
begin with. You'll probably trash a different chunk of
memory by running off the end of a static array than you
would if the array were auto or dynamic, but what makes you
think the trashed stuff is any less important, or that its
trashing is less of a threat? If the bad guy can overwrite
a function pointer variable, for example, that's about as
good as diddling a return address. Or what if he manages
to set the `bool passwordVerified' variable without providing
the password?
Are there other kinds of exploits which do not rely on stack
thrashing?
Define "exploit."

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 17 '07 #3
On Sat, 17 Nov 2007 08:27:10 -0800 (PST), rep_movsd
<re*******@gmail.comwrote:
>Hi

I program primarily in C++ , but once in a while one is forced to use
the odd strcpy or call API functions that dump results into char*
buffers.

I believe that most security exploits that work by thrashing the stack
to overwrite the return address, allowing arbitrary code execution.
I have now fallen into the habit of declaring temporary buffers as
static char arrays.

Is this a good idea in general?

Are there other kinds of exploits which do not rely on stack
thrashing?
Integer overflow, memory allocator, %n print format specifier,
overwriting static data structures...many others.

Jim
Nov 17 '07 #4
"rep_movsd" <re*******@gmail.comwrote in message
I program primarily in C++ , but once in a while one is forced to use
the odd strcpy or call API functions that dump results into char*
buffers.

I believe that most security exploits that work by thrashing the stack
to overwrite the return address, allowing arbitrary code execution.
I have now fallen into the habit of declaring temporary buffers as
static char arrays.

Is this a good idea in general?

Are there other kinds of exploits which do not rely on stack
thrashing?
The word is "trashing". Thrashing the stack means something quite different.

As Pete said, either your code is correct or it isn't. Whilst static arrays
may provide some protection from exploits, it's very partial - if you
overwrite another global you might also create a security hole, certainly
bugs will be harder to trace than if you corrupt the stack return address
with a random value, which can to all intents and purposes be guaranteed to
produce a crash.

It is not really easy to anwer the question "should I have an extra layer of
protection?". It creates costs elsewhere, for instance making code
non-rentrant. Because after the first call the static will be initialised to
a "sensible" value, it might also make any errors worse. Also, another
programmer would wonder why the value needs to be preserved across function
calls. However ultimately it is very difficult to say whether these outweigh
lose that one last final defence to a buffer exploit attack.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Nov 17 '07 #5
On Nov 17, 4:27 pm, rep_movsd <rep.mo...@gmail.comwrote:
Hi

I program primarily in C++ , but once in a while one is forced to use
the odd strcpy or call API functions that dump results into char*
buffers.

I believe that most security exploits that work by thrashing the stack
to overwrite the return address, allowing arbitrary code execution.
I have now fallen into the habit of declaring temporary buffers as
static char arrays.

Is this a good idea in general?
It is an awful idea. Any buffer overflows will now overwrite random
bits of global memory, in practice overwriting random other static or
global variables, with all kinds of possible consequences. Furthermore
you make it impossible to use any of your code in a multi-threaded
environment.
Nov 17 '07 #6
Oh well, I guess its best to avoid such "fixes" and make sure I always
know the size of the data that I strcpy or memcpy, in any case most
APIs ( at least WIN32 ones that i know of ) can be made to return the
size of the data that they will return....

Vivek
Nov 19 '07 #7
In article
<79**********************************@w28g2000hsf. googlegroups.com>,
rep_movsd <re*******@gmail.comwrote on Monday 19 Nov 2007 2:12 pm:
Oh well, I guess its best to avoid such "fixes" and make sure I always
know the size of the data that I strcpy or memcpy, in any case most
APIs ( at least WIN32 ones that i know of ) can be made to return the
size of the data that they will return....
At one level or another size and other information _has_ to be
maintained and respected, for things to work. The C language exposes
more of these "details" to the programmer than many other, more recent,
languages. This has both benefits and drawbacks.

Nov 19 '07 #8
rep_movsd wrote:
>
Oh well, I guess its best to avoid such "fixes" and make sure I
always know the size of the data that I strcpy or memcpy, in any
case most APIs ( at least WIN32 ones that i know of ) can be made
to return the size of the data that they will return....
Incomprehensible. See the advice in my sig, below.

--
If you want to post a followup via groups.google.com, ensure
you quote enough for the article to make sense. Google is only
an interface to Usenet; it's not Usenet itself. Don't assume
your readers can, or ever will, see any previous articles.
More details at: <http://cfaj.freeshell.org/google/>

--
Posted via a free Usenet account from http://www.teranews.com

Nov 20 '07 #9
On Nov 20, 3:52 am, CBFalconer <cbfalco...@yahoo.comwrote:
rep_movsd wrote:
Oh well, I guess its best to avoid such "fixes" and make sure I
always know the size of the data that I strcpy or memcpy, in any
case most APIs ( at least WIN32 ones that i know of ) can be made
to return the size of the data that they will return....

Incomprehensible. See the advice in my sig, below.

--
If you want to post a followup via groups.google.com, ensure
you quote enough for the article to make sense. Google is only
an interface to Usenet; it's not Usenet itself. Don't assume
your readers can, or ever will, see any previous articles.
More details at: <http://cfaj.freeshell.org/google/>

--
Posted via a free Usenet account fromhttp://www.teranews.com
Sorry, here is my original query and google groups thread link

rep_movsd wrote:
Hi
I program primarily in C++ , but once in a while one is forced to use
the odd strcpy or call API functions that dump results into char*
buffers.
I believe that most security exploits that work by thrashing the stack
to overwrite the return address, allowing arbitrary code execution.
I have now fallen into the habit of declaring temporary buffers as
static char arrays.
Is this a good idea in general?
Followed up on
groups.google.com/group/comp.lang.c/browse_thread/thread/
073b39de9430aec9#

Vivek
Nov 23 '07 #10
rep_movsd <re*******@gmail.comwrites:
On Nov 20, 3:52 am, CBFalconer <cbfalco...@yahoo.comwrote:
>rep_movsd wrote:
Oh well, I guess its best to avoid such "fixes" and make sure I
always know the size of the data that I strcpy or memcpy, in any
case most APIs ( at least WIN32 ones that i know of ) can be made
to return the size of the data that they will return....

Incomprehensible. See the advice in my sig, below.

--
If you want to post a followup via groups.google.com, ensure
you quote enough for the article to make sense. Google is only
an interface to Usenet; it's not Usenet itself. Don't assume
your readers can, or ever will, see any previous articles.
More details at: <http://cfaj.freeshell.org/google/>

--
Posted via a free Usenet account fromhttp://www.teranews.com

Sorry, here is my original query and google groups thread link
Oh dear. Prepare for more of his signature advice because you forgot to
snip his double signature.
>
rep_movsd wrote:
>Hi
>I program primarily in C++ , but once in a while one is forced to use
the odd strcpy or call API functions that dump results into char*
buffers.
>I believe that most security exploits that work by thrashing the stack
to overwrite the return address, allowing arbitrary code execution.
I have now fallen into the habit of declaring temporary buffers as
static char arrays.
>Is this a good idea in general?

Followed up on
groups.google.com/group/comp.lang.c/browse_thread/thread/
073b39de9430aec9#

Vivek
Nov 23 '07 #11
Richard wrote:
[21 lines deleted]
>
Oh dear. Prepare for more of his signature advice because you forgot to
snip his double signature.
[16 lines deleted]

Richard, did you really need to quote the entire article to say that?
(Hint: No, you didn't.) (Hint: This criticism is from someone with
a valid signature; perhaps you'll pay attention to it.)

Are you incapable of trimming quoted text?

You said recently that you had killfiled CBFalconer. I had hoped
this would mean we wouldn't see any more complaints from you about
his signature(s).

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 23 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5066
by: Vaca Louca | last post by:
Hello, My setup: Debian sarge on dual Pentium 4. g++ 3.3.5-3. (the other system is Windows XP with MS Visual Studio .NET 2003) I have an auto_array<T> template (based on a template taken from...
7
6115
by: Jim Showalter | last post by:
I always thought that it is safe for a function to return a pointer to static storage. And the following code does compile quietly with: gcc -pedantic -Wall -o foo foo.c #include <stdio.h> ...
12
2338
by: Michael B Allen | last post by:
Is it legit to modify static data like the following code? #include <stdlib.h> #include <stdio.h> struct tbl { int i; char *s; };
28
2671
by: Merrill & Michele | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char *p; p=malloc(4); strcpy(p, "tja"); printf("%s\n", p); free(p); return 0;
33
3118
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
16
3232
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is...
15
2450
by: rEvolution27 | last post by:
I'm a c++ newbie here, trying out some stuff and when I try to compile this: void create() { char name; cout << "Creating a new timetable /n Please type a name for this timetable"; cin >name;...
2
1749
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm using a COM DLL in my C# application (a single process). This COM DLL generates a data array, and by using the interop DLL (generated by VS/TlbImp.exe), I'm getting this array to a safe Array...
13
2211
by: arnuld | last post by:
this does not work, i know there is some problem in the "for loop" of "print_arr" function. i am not able to correct the weired results i am getting. i have no compile time error, it is only...
0
7162
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7527
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3223
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.