Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm interested in expanding my knowledge of security issues: things like buffer overflows, format string vulnerabilities, etc. I'd like to be able to go through a language and understand its security implications and pitfalls to get a better understanding of secure coding practices in general.

Are there certain languages that are more conducive to a learning experiment like this? Are there languages, due to abstractions or complexity, I should avoid? Where should I start?

share|improve this question

migrated from stackoverflow.com Aug 30 '11 at 22:29

This question came from our site for professional and enthusiast programmers.

    
Hi Atul, I've rewritten your question to better match our guidelines for constructive questions: asking "Which language should I choose?" isn't something that's on-topic, but a general question about how to select a language for learning secure coding practices is fine. –  user8 Aug 31 '11 at 8:54
    
Thanks Mark! Even though this version appears to be more generalized and doesn't take into account the fact that the OP is familiar with HLLs but if it matches constructive question guidelines, then I'm happy to keep it like this. Thanks again. :) –  Atul Goyal Aug 31 '11 at 10:07

4 Answers 4

up vote 6 down vote accepted

To gain a better understanding of what languages like Java buy you in terms of security and memory management, work in C. The only thing that will drive the point home any deeper will be working in assembler¹. While it's nothing like what Java provides, C++ gives you too many tools to manage the kind of complexity that causes these problems, and the temptation to use them will be too great.

Memory management in C is very labor intensive compared to Java and C++. You don't have any sort of automatic garbage collection, you don't have explicit destructors to free up resources when an object goes out of scope, so you have to manually track every memory allocation and match it up with a corresponding deallocation. Arrays don't know how big they are and there's no bounds checking, so you have to track all those buffer sizes yourself and enforce those limits manually. C doesn't provide much in the way of exception handling (signals and setjmp/longjmp are the extent of it, and that's not very much at all), so you have to be careful about how you handle error conditions.

If you really want to appreciate what Java (or even C++) buy you, implement a C library that mimics the behavior of the Java String class.


¹ The best of both worlds would be to work in C and have your compiler write the generated assembly to an output file and study that.

share|improve this answer

Modern C++ style has a number of constructs that help mitigate security problems. std::vector and string in particular.

So, I would recommend C.

Or, for super-fun times, you could write some assembly programs.

share|improve this answer
    
+1 for the assembler. If you wouldn't have mentioned it I would have posted a new answer :) –  Ioan Paul Pirau Aug 30 '11 at 21:38
    
Using the same arguments, I arrive at the opposite conclusion. –  Konrad Rudolph Aug 31 '11 at 8:44
    
I think Konrad is pointing out that C is not C++. –  Steve314 Aug 31 '11 at 9:37
    
@Konrad: he's looking to experiment with understanding how security holes work. C/asm are - imo - the easiest languages to create security holes. –  Paul Nathan Aug 31 '11 at 17:00

The key thing to understand is that most security issues are inherently low-level. C++ is roughly a superset of C, but to really be forced to understand how low-level things work, you need to at least dabble in straight C, or write inherently low-level code (things like memory allocators or string classes) in C++. If you just learn C++ coming from a background of even higher level languages, chances are you'll try to program in a Java-like style that you're already comfortable with, using lots of objects and never touch the lower-level features inherited from C, which are the important ones if you're trying to learn about security.

share|improve this answer
    
"most security issues are inherently low-level" - I don't think that's right. Standard estimates are that about 50% of security vulnerabilities are due to bugs (problems in the implementation/code), and 50% are due to flaws (problems in the design). –  D.W. Nov 7 '11 at 0:42

There's no reason to pick C over C++. Pretty much anything C can do, C++ can do, but better. Things like automatic resource management and templates are irreplacable tools. Having to learn to free() is really irrelevant. If you write C code, you still don't have to write your own malloc() or anything like that.

There's nothing language-specific about security.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.