473,624 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

security coding guidelines for C/C++

I am Aravind.Could someone provide me with a list of specific
guidelines for secure programming in C/C++?.I would like to use those
guidelines for developing a security application to deal with issues
like buffer overflows,memor y leaks,user input validation etc....
Aravind
Nov 14 '05 #1
8 1876
Aravind wrote:

I am Aravind.Could someone provide me with a list of specific
guidelines for secure programming in C/C++?.I would like to use those
guidelines for developing a security application to deal with issues
like buffer overflows,memor y leaks,user input validation etc....


No. Nobody here ever heard of the language C/C++. On the other
hand, the C++ group is down the hall to the right, and we do deal
with C here.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nov 14 '05 #2

"Aravind" <ar*********@ya hoo.com> wrote in message

I am Aravind.Could someone provide me with a list of specific
guidelines for secure programming in C/C++?.
I would like to use those guidelines for developing a security
application to deal with issues like buffer overflows,memor y
leaks,user input validation etc....

Security is a real problem for C programs, and it is not easy to write tools
to check for it.
The worst problem is when user input overflows an "auto" (stack) array, on
systems where this corrupts the reurn stack. An attacker can use this to
induce a jump to a location of his choosing, and thus introduce malicious
code.
It is also possible to oveflow the stack. For instance the code

double eval( char *expr)
{
...
if(*expr == '9')
temp = eval(expr+1);
...
}

can be caused to crash by inputting a huge number of open parentheses.

You simply have to be careful to call malloc() with the right size, not
overstep the array, check the return value, and free memory after you have
done with it. The good news is that there is little the user can do to wreck
things here. (To test, a good technique is to provide a version of malloc()
that fails periodically).

For user input, be aware that the user can type anything, and assume he is
trying to wreck your program and has a copy of the source.
Nov 14 '05 #3
On Mon, 24 May 2004 21:00:08 +0100, Malcolm wrote:

"Aravind" <ar*********@ya hoo.com> wrote in message

I am Aravind.Could someone provide me with a list of specific
guidelines for secure programming in C/C++?.
I would like to use those guidelines for developing a security
application to deal with issues like buffer overflows,memor y
leaks,user input validation etc....
Security is a real problem for C programs, and it is not easy to write tools
to check for it.
The worst problem is when user input overflows an "auto" (stack) array, on
systems where this corrupts the reurn stack. An attacker can use this to
induce a jump to a location of his choosing, and thus introduce malicious
code.


This is largely obviated by either fgets() or the much better
fgetc()/realloc() method, or something very similar adapted to your
specific input environment.

For a good example of the fgetc()/realloc() method, see the fggets()
implementation by CBFalconer:

http://cbfalconer.home.att.net/download/ggets.zip
It is also possible to oveflow the stack. For instance the code

double eval( char *expr)
{
...
if(*expr == '9')
temp = eval(expr+1);
...
}

can be caused to crash by inputting a huge number of open parentheses.
Or, in your case, a large number of nines. ;-)

This is probably the most serious problem if you fail to plan ahead.
Scanning the string beforehand to see if all of the parens match and if
the nesting is too deep could be valuable, or you could give eval a static
int that says how many layers deep the recursion has gone. If the int gets
too big, you return badly.

Of course, how deep is too deep is somewhat up in the air. Do any of the
standards specify the minimum recursion depth an implementation must
support?

You simply have to be careful to call malloc() with the right size, not
overstep the array, check the return value, and free memory after you have
done with it. The good news is that there is little the user can do to wreck
things here.
And, of course, always #include <stdlib.h> and never cast its return value.
(To test, a good technique is to provide a version of malloc()
that fails periodically).
That, or begin developing big programs for small machines. ;-)

For user input, be aware that the user can type anything, and assume he is
trying to wreck your program and has a copy of the source.


Indeed. This is the best possible route to defensive programming, and I
wish more Microsoft programmers (for example) thought this way.

--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.

Nov 14 '05 #4
In article <pa************ *************** *@sig.now>,
August Derleth <se*@sig.now> wrote:
On Mon, 24 May 2004 21:00:08 +0100, Malcolm wrote:

For user input, be aware that the user can type anything, and assume he is
trying to wreck your program and has a copy of the source.


Indeed. This is the best possible route to defensive programming, and I
wish more Microsoft programmers (for example) thought this way.


I think it is too weak.

For input, assume the input doesn't come from a user, but from a
completely unscrupulous attacker who is paid serious money to cause as
much damage as possible.

Assume that the attacker has both the source code and the assembler
code; the source code is useful to find where your program has undefined
behaviour according to the C Standard, the assembler code tells the
attacker what your implementation actually does in the case of undefined
behavior.

Assume that the attacker's goal is not just to crash your program, but
to take control of your computer, and any undefined behavior in your
programmer could give the attacker the means to achieve this. Just
assume that a successful attacker will force the computer to send _your_
name, address, date of birth, mother's maiden name, social security,
passport, driver license and credit card numbers, bank details and so on
to the attacker, leaving a ghastly amount of child pornography on your
computer in exchange which will be discovered by your
employer/wife/girlfriend, leading to job loss/loss of important body
parts and several years of jailtime. That should keep you motivated, and
motivation is the most important thing for secure programming.
Nov 14 '05 #5
The problems of buffer overflows could soon become less relevant anyway,
with Intel and AMD's new chips at least (Not that I'm suggesting in anyway
that it shouldnt still be protected against!). And NX does require OS
support as well.
http://www.internet-security.ca/inte...-problems.html
What a god send for Microsoft, easier to include NX support in a service
pack than to correct all the 50m lines of code i suppose, it will be
interesting to see which path they take!

The Fat Man

"Aravind" <ar*********@ya hoo.com> wrote in message
news:5e******** *************** **@posting.goog le.com...
I am Aravind.Could someone provide me with a list of specific
guidelines for secure programming in C/C++?.I would like to use those
guidelines for developing a security application to deal with issues
like buffer overflows,memor y leaks,user input validation etc....
Aravind

Nov 14 '05 #6

On Tue, 25 May 2004, Matthew Jakeman wrote:
[re: discussion of security in C/C++ programs]
The problems of buffer overflows could soon become less relevant anyway,
with Intel and AMD's new chips at least (Not that I'm suggesting in anyway
that it shouldnt still be protected against!). And NX does require OS
support as well.
http://www.internet-security.ca/inte...-problems.html
What a god send for Microsoft, easier to include NX support in a service
pack than to correct all the 50m lines of code i suppose, it will be
interesting to see which path they take!


The press release doesn't say much about the technology, but from
what I can gather, it's basically making the stack segment
read-and-write-only, no execution privileges. I thought Intel machines
had segments you couldn't execute from already, like the data segment?
No?
And if it is what I think it is, then it doesn't protect against
an attacker's taking over your machine: it only protects against an
attacker's being able to take over your machine with code he wrote
himself and inserted into the stack frame. A static-buffer overflow
*followed by* a stack-buffer overflow executing a jump into the
overflowed static buffer would be just as devastating as the old kind;
and stack-buffer overflows could still make the computer crash or
execute the wrong code.
So is the "Magic Lamp to end buffer overflow exploits" tagline just
normal media hype, or am I really missing something WRT the capabilities
of the "new" technology?

(Fup-to: comp.programmin g. comp.lang.c will thank you.)

-Arthur
Nov 14 '05 #7
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote:
On Tue, 25 May 2004, Matthew Jakeman wrote:
[re: discussion of security in C/C++ programs]

The problems of buffer overflows could soon become less relevant anyway,
with Intel and AMD's new chips at least (Not that I'm suggesting in anyway
that it shouldnt still be protected against!). And NX does require OS
support as well.
http://www.internet-security.ca/inte...-problems.html
What a god send for Microsoft, easier to include NX support in a service
pack than to correct all the 50m lines of code i suppose, it will be
interesting to see which path they take!
The press release doesn't say much about the technology, but from
what I can gather, it's basically making the stack segment
read-and-write-only, no execution privileges. I thought Intel machines
had segments you couldn't execute from already, like the data segment?
No?


No. First of all this is a feature that comes from AMD, and has
already been implemented in the Athlon-64 CPUs. Its called the
No-Execute bit, and its a mode that's just been missing in the x86
architecture before AMD added it to AMD64. The OpenBSD folks have
added it to their architecture in anticipation of this being enabled
in x86s.
And if it is what I think it is, then it doesn't protect against
an attacker's taking over your machine: it only protects against an
attacker's being able to take over your machine with code he wrote
himself and inserted into the stack frame.
Right. But this is a very common problem and exploit. For example
one of the software based X-box mods rely on exactly this flaw in the
initial dashboard background image loader.
[...] A static-buffer overflow
*followed by* a stack-buffer overflow executing a jump into the
overflowed static buffer would be just as devastating as the old kind;
and stack-buffer overflows could still make the computer crash or
execute the wrong code.
Yes, you can still crash the machine. Making it execute code you've
crafted yourself though -- you'll have to rely on other holes.
So is the "Magic Lamp to end buffer overflow exploits" tagline just
normal media hype, or am I really missing something WRT the capabilities
of the "new" technology?
It solves one manifestation of the problem. I've thought about this
problem for a little bit, and I am not sure it will be ended by using
this mechanism -- just that it needs to be a lot more creative. I.e.,
to spurr a program to arbitrary action you have to prime it by forcing
it to jump into *its own* code segment, however with the *parameters*
on the stack may be arbitrarily modified. (So for example if the code
itself has a call in it to turn the NX bit on and off, then you can
call that code with parameters that say turn it off, *then* do the
same exploit, etc.)
(Fup-to: comp.programmin g. comp.lang.c will thank you.)


Its not off topic for comp.lang.c. Remember C, and C alone is the
language that creates this problem.

--
Paul Hsieh
http://www.pobox.com/~qed/
Nov 14 '05 #8

In article <79************ **************@ posting.google. com>, qe*@pobox.com (Paul Hsieh) writes:
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote:
So is the "Magic Lamp to end buffer overflow exploits" tagline just
normal media hype, or am I really missing something WRT the capabilities
of the "new" technology?


It solves one manifestation of the problem. I've thought about this
problem for a little bit, and I am not sure it will be ended by using
this mechanism -- just that it needs to be a lot more creative.


I largely agree with what Paul had to say in his post, but I'd like
to point out that while return-into-program exploits no doubt are
more difficult than return-into-buffer ones, they're well-understood,
there's some good technical information on them and practical advice
on writing them available, and they've been found "in the wild".
(See eg the Bugtraq archives, or past issues of Phrack, for more
information.)

In other words, attackers already know how to exploit return-into-
program buffer overflow holes, so patching return-into-buffer (with a
non-exec page bit) really just eliminates some of the low-hanging
fruit.
(Fup-to: comp.programmin g. comp.lang.c will thank you.)


Its not off topic for comp.lang.c. Remember C, and C alone is the
language that creates this problem.


C programs may be the worst offenders, and language features have
certainly contributed to that. But it's hardly alone. For one thing,
there are plenty of poorly-written C++ programs that suffer from the
same problems.

--
Michael Wojcik mi************@ microfocus.com

Although he was an outsider, and excluded from their rites, they were
always particularly charming to him at this time; he and his household
received small courtesies and presents, just because he was outside.
-- E M Forster
Nov 14 '05 #9

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

Similar topics

3
2081
by: Neo | last post by:
I have a usercontrol that I am trying to use as an ActiveX Control on a WebPage, however it refereneces an Assembly that whenever the first call is made to anything in the Assembly a Security Exception is throw. I am having a lot of trouble debugging this, because I cannot catch the exception in my code. However if I could run the control under the Internet Zone Security settings I could probably find this easily. Does anyone know how to...
27
3470
by: Stuart Gerchick | last post by:
C++ Coding Standards : 101 Rules, Guidelines, and Best Practices by Herb Sutter, Andrei Alexandrescu is now a month or so away from release. What is people's opinion on this...is it going to be a seminal work or lackluster http://www.gotw.ca/resources/clcm.htm for info about ]
39
3485
by: Patrick | last post by:
The c# code style guide that I follow suggests that class variables (fields) be coded with camel casing, like this: int recordId; string name; It also suggests that variables within methods and method parameters use camel casing, like this: void SetName(int id, string newName)
3
2406
by: James Radke | last post by:
Hello, I have an asp.net application (using vb.net codebehind), that is calling some older c++ dlls. These dlls require the use of the c++ Runtime which is in the windows/System32 directories. What is the best way to get access to these directories for the web application? Add the security for IUSR_<system name> to the System32 directory? Is there a better method?
1
2304
by: Scott Meddows | last post by:
Does anyone know of a document that Microsoft produces that has the suggested coding standards for naming tables in a Database, fields in a database, objects in a VB project and so on? Maybe one for SQL and one for VB? Thanks!
4
5327
by: Dotnetjunky | last post by:
Hi, So far, I've found tons of documents describing recommended coding standards for C#, but not a single piece on VB.NET yet. Anybody here knows such a coding standards guideline on VB.NET and minds sharing with us ? Thanks in advance.
3
1399
by: Dan Schaertel | last post by:
Does anybody have a good VB coding standards documnet? I need to generate one and I am looking for examples. Thanks email: dschaertel@hotmail.com
60
5017
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this. consistently may make the code easier to understand for someone else, etc. Using "this." makes it immediately clear, for example, that the variable being referred to is a member of the same class and is not declared in the method as a local variable. ...
12
2971
by: Robert Seacord | last post by:
What functions, if added to the C1X standard, would make the C language more secure and why? Here are a couple of my suggestions: sigaction() - only secure way to set signal persistence clearenv() - important, platform-dependent capability that shouldn't be left to each individual programmer mkstemp() - allows a secure directory to be specified. this feature is lacking from both C99 and TR 24731
0
8173
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
8679
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
8621
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...
1
8335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8475
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...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
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...
2
1482
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.