473,769 Members | 7,355 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 2061
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 passwordVerifie d' 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*******@gmai l.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...ma ny others.

Jim
Nov 17 '07 #4
"rep_movsd" <re*******@gmai l.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...@gmai l.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************ *************** *******@w28g200 0hsf.googlegrou ps.com>,
rep_movsd <re*******@gmai l.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....
Incomprehensibl e. See the advice in my sig, below.

--
If you want to post a followup via groups.google.c om, 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...@yah oo.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....

Incomprehensibl e. See the advice in my sig, below.

--
If you want to post a followup via groups.google.c om, 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.co m
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.c om/group/comp.lang.c/browse_thread/thread/
073b39de9430aec 9#

Vivek
Nov 23 '07 #10

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

Similar topics

7
5085
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 the Corona project hosted at SourceForge) which basically wants to implement std::auto_ptr<T> semantics for an array.
7
6146
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> static char *foo (int y) { static char s;
12
2369
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
2708
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
3186
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
3279
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 that I'm not very experienced with pointers, pointers to pointers and the like and I got 4 compiler warnings + I don't completely understand how to build this "compact matrix" (see later).
15
2482
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; ofstream editFile; editFile.open (name, ios::out | ios::app);
2
1760
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 into my C# code (using the marshaling and all that...). My Question is: Does a multiple data arrays is allocated when marshaling the data from the COM component to my C# component. I mean; Does an array is allocated in the COM side and another...
13
2235
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 semantic-bug that is causing the trouble: EXPECTED: january, february, march....december GOT: january, january, january.........january ------------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 10
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.