Slashdot Mirror


Secure Programming in GNU/Linux Systems: Part I

LNXPhreak writes "A new article on OSWeekly.com discusses secure programming in GPU/Linux systems in terms of programming talent and requirements. Quote: "A "secure program" is an application that sits on a security boundary, taking input from a source that does not have the same access rights as the program. Such programs include application programs used as viewers of remote data, web applications (including CGI scripts), network servers, and setuid/setgid programs."

20 of 64 comments (clear)

  1. Unsafe Languages? by (1+-sqrt(5))*(2**-1) · · Score: 3, Insightful
    In spite of myself, I went into TFA with cautious optimism; the author lost a lot of credibility by me, however, when he averred:
    C is an unsafe language, and the standard C library string functions are unsafe.
    I'd counterplead (to borrow a gun-canard): languages are not unsafe, but programmers are.
    1. Re:Unsafe Languages? by gowen · · Score: 5, Insightful

      Well that's strictly true, but you know what he's saying.
      The string handling functions of the standard C library make it very easy to write unsafe code.

      Similarly, a car with very bad brakes isn't intrinsically unsafe, as long as the cautious driver is prepared to take alternative precautions on ever bend. But if the driver makes an error (and everyone, yes, even you, will eventually make an error), he's going to end up wrapped around a tree.

      Even though other cars aren't safe in the hands of a bad driver, given that you will make mistakes, you'll have far fewer horrific crashes in a car with ABS.

      --
      Athletic Scholarships to universities make as much sense as academic scholarships to sports teams.
    2. Re:Unsafe Languages? by slavemowgli · · Score: 4, Insightful

      He does have a point, though. It *is* possible to use the standard C library string functions in a safe manner, but it's difficult and error-prone - and if you make just one mistake, it might well be enough to open an exploitable hole in your program.

      It's important to realise that programmers aren't perfect. "unsafe" programmers, as you call them, are not something you'll ever be able to get rid of; your best bet is thus to give them tools that make it as easy as possible to write safe code and that will mitigate the impact when things *do* go wrong - in other words, tools that will fail in a defined and safe manner.

      One of the problems with C (actually, one of its strengths, too, of course, depending your point of view) is that it's really only optimised for speed, and a lot of compromises were and still are made there. Similarly, another problem is that C is intended as a very low-level language - a hardware-independent macro-assembler with automatic register allocation, if you will. This makes it very suitable for certain low-level tasks (like OS kernel programming), but it also creates problems when you move higher up in the level hierarchy, away from the bare metal.

      C does have its place, but if you want to develop an application that's not closely tied to the hardware etc., C probably isn't as ideal a choice as you might think - and at the very least, if you *do* decide to use it after all, you should be aware of its weaknesses and pitfalls so you can avoid them.

      It's just like with guns. Guns can be handled in a safe manner, but that doesn't mean you should give everyone a loaded and unlocked gun who's never touched one before in his life - chances are that accidents *will* happen. And while you can say that it's handler's fault, not the gun's, well... accidents still will happen. It's better to get off of one's high horse and try to minimise the number of actual incidents (and the severity of those incidents that do happen), since that's what counts in the end.

      --
      quidquid latine dictum sit altum videtur.
    3. Re:Unsafe Languages? by miyako · · Score: 4, Insightful

      I completely agree with you're point, and just thought I would pose a random sort of thought I had.
      The biggest problem with C and C++ is that it's very easy for novice programmers to create insecure programs because, for the most part, they don't know how to properly work with pointers. Now days it seems to me that much fewer people learn C/C++ than used to just a few years ago. In highschool in my CS pretty much everyone who knew any programming knew C or C++. Back then, and that was only a few years ago mind, it seemed rare to me to know anyone who knew, e.g. perl or Java, and didn't know C or (more often) C++.
      Now, this semester I graduate with a degree in Computer Information Systems, and of the 20 or 30 people I know off hand that are within a semester or two of me, I'm the only one who is competent with C++. There are a couple of other people who vaguely know it, and could probably recognize the syntax, but couldn't write anything useful in it.
      At my school, the programming languages that are explicitly required are Java, VB.NET, COBOL, and just enough C# to do some stuff in ASP.NET- which is required for the web class. They teach a couple of assembly classes, and a couple of C++ classes that are required for the CET majors and offered as an elective to the CIS majors. I'm the *ONLY* person in the 4 years I've been at school I know who is a CIS major who took these classes.
      So now, a lot of people who call themselves programmers are graduating and, quite literally, have no idea what a pointer is. So, this makes me wonder. Are C and C++ safer because most of the bad programmers are now working in "safe" languages like Java, or are they less safe because people don't learn about pointers early on?
      Personally, I never understood why people have such trouble with understanding pointers, but then it may be because in highschool I was lucky enough to have an absolutely fantastic computer science teacher who instilled the basic ideas in us about how the compiler works and how variables work, and what memory addresses are, etc. from our first programming class.
      Anyway, there wasn't much of a point to all that (it's 4:20am right now, and I should have been in bed 4 or 5 hours ago, so just be thankful that it's coherent (if indeed it is)) but maybe it'll stimulate discussion anyway.

      --
      Famous Last Words: "hmm...wikipedia says it's edible"
    4. Re:Unsafe Languages? by EsbenMoseHansen · · Score: 3, Informative

      Actually, strncpy is almost as bad as strcpy, with the added bonus of being inefficient. Use strlcpy. strncpy is a leftover from an early database format, and should almost never be used.

      --
      Religion is regarded by the common people as true, by the wise as false, and by rulers as useful.
    5. Re:Unsafe Languages? by UlfJack · · Score: 2, Insightful

      How good a programmer are you? Good? Very good? Excellent? Even Perfect?

      No doubt you are not perfect, you are human after all. Now, I've never seen a Java programmer write code that could be exploited with a buffer overflow exploit. But I've seen countless buffer overflow bugs in C software. Just subscribe to one of the numerous security mailing lists.

      Yes, you can write unsafe software in Java. No doubt about that. But a language where you cannot use the standard library (string functions!!!) for security related software is at best not well designed.

    6. Re:Unsafe Languages? by 4D6963 · · Score: 2, Interesting
      I never understood why people have such trouble with understanding pointers

      I've been both in the position of one of the students who had the thougest times understanding pointers in Pascal, and when I started skipping college and studying C at home I started understanding them really well.

      Personally, what prevented me from understanding them back when I was in college was their nature, I didn't understand how it worked. When I started working with C and with heavily sending and returning arrays and values from functions, it only made sense to me when I understood that a pointer was an address, and that for example when you want to send a variable to a function and make it modify it, you have to send its address so the function doesn't have to return a value, but can modify the contents of the indicated address.

      Once I understood that pointers suddenly went from mysterious to easy and actually people who tell me about pointers when I say I program in C tell me shit like "so you like pointers, huh?" well I actually do now. I think that pointers are only hard to understand (at least in my case) when the language you're in tries to make abstraction of what a pointer really is (like Pascal) as I think one of the strength of C is that it acts more closely to the reality of how things really get done (in a fashion closer to assembly code, although it's portable) and thus makes it alot clearer. (Disclaimer : I've only been doing C for about a year and most of what I work on is DSP programs, thus I rarely have to deal with strings, otherwise I wouldn't like C that much)

      --
      You just got troll'd!
    7. Re:Unsafe Languages? by Anonymous Coward · · Score: 2, Informative

      Yep. strncpy() only null-terminates the string if the null-character fits in the buffer. In addition, it null-pads the entire buffer, which can lead to horrible performance.

      Use strlcpy().

    8. Re:Unsafe Languages? by master_p · · Score: 2, Insightful

      Actually, you are wrong: a programming language is as unsafe as the most "unsafe" programmer of a project is. In other words, you may have a team of C experts that write the most secure C, but it only takes one minor error from a not-so-experienced programmer or from an experienced programmer in a bad day to bring down whole systems.

      So although your argument seems correct on the surface, it actually is not true: a programming language shall enforce safety.

    9. Re:Unsafe Languages? by squiggleslash · · Score: 2, Interesting
      The GP didn't mention Python.

      Personally I think he's wrong but not for the reasons you mention. What's needed is a "managed" language, not a "modern" language. Managed languages have existed almost since high level languages were invented, indeed, for the most part, we did a bad (if necessary at the time) thing during the late sixties to the mid-nineties when languages started to allow us to do unmanaged things.

      Examples of managed languages include Java and C#, Pascal and the Modula series of languages (and Oberon, I guess), not to mention a great many interpreted languages like Python (as you mentioned), BASIC, LISP and its derivatives, SmallTalk, et al.

      The first group of languages has performance comparable to C, C++, and Objective C. Some are cruder than others ("classic" Pascal is a PITA to work with, mainstream 1980s versions had to massively extend it to get it to work, frequently ending up discarding management along the way), others, like Java, currently suffer from the way they're currently packaged (but see GCJ), but all essentially prevent you from making basic mistakes and provide a level of code security that, arguably, makes hardware memory protection obsolete.

      I'm not going to argue they're perfect. Java suffers from its designers obsessions with discouraging the use of native APIs in the name of half-arsed "portability", and both it and C# require substantial run-times for much the same reason. At the same time, when you use either, the level of security you get is much, much, better, with it being much more difficult for a bug in one piece of code to corrupt another in an exploitable way.

      I love C, I really do. I've programmed it since the late eighties. I've amazed myself with some of the stuff I've done. But, with hindsight, while we probably couldn't have gotten here today without the experimentation and lack of restrictions it did for us, it's also the case we've had to end up with vastly inefficient computer and operating system designs and our current problems with security are manifested in our decisions to base our entire computer infrastructures on that language. Much as I hated it at University, with hindsight I think Modula 2 and its object oriented successors would have been a better choice. And now that Java and C# have momentum, it's time to jump on board with both feet.

      --
      You are not alone. This is not normal. None of this is normal.
    10. Re:Unsafe Languages? by Kjella · · Score: 2, Interesting

      Consider C/C++ something like downhill skis. If you know what you're doing, you can go incredibly fast on racing skis. Hell, you'd be pretty quick on normal skies too but that translates well to novice/pro programmers.

      But if you don't know what you're doing, you are a lot more likely to hurt yourself badly on racing skis. Why? Because they're only designed to give you the minimum of control you need to get down in one piece. They will not forgive you the slightest mistake, instead you will crash.

      So to if a newbies comes in thinking he'll buy racing stuff because they're so flashy and cool and expensive (and he's got the cash), if you have any conscience you'll advice against it. You'll tell him that using this equipment is unsafe.

      C/C++ pointers work the same. Most of the time you don't need that extra bit of speed, 99% of the time it's just about reaching the goal. 99% of the time you're just being unnecessarily unsafe. Personally I wrap most things in a library instead of using another language, I don't deal with zero-termination or buffer overflows.

      If that really gives inadequate performance, try redesigning what you're trying to do or the method you're doing it with. Dancing around with pointer arithmetic instead of wrapper classes with offset checks is really a last-ditch resort for me, right before I would seek someone who knows assembler.

      --
      Live today, because you never know what tomorrow brings
    11. Re:Unsafe Languages? by zootm · · Score: 2, Informative

      Strictly the distinction usually made between "safe" and "unsafe" languages is the number of things that can be detected at compile-time, rather than runtime. There are whole classes of errors which, in modern "safe" languages, just cannot happen, if the program compiles. This verifiability means that more potential problems are spotted before the code is run, and obviously, the earlier these things are caught, the better. People often complain that there is a loss of direct "power" from such things, and to a degree they're right.

      But C allows you to directly manipulate pointers to memory. This operation is deemed unsafe because it's not possible to systematically verify that that memory will behave the way that you anticipate. It allows you to (easily) take advantage of language implementation issues that should not be exposed to the programmer, let alone used.

      It's not generally a strict term (the CLI gives it a strict meaning, but that's basically restricted to the CLI), but in general, although unsafe programmers exist all over the place, there's not a single 100% safe programmer in the world. We all make mistakes, and making languages which catch more of these at as early a stage as possible can only be a good thing. If you have a specific reason not to use them, that's usually fair enough, but this doesn't mean that they're not better for tasks where there's not a need for the higher degree of control available from these "unsafe" languages.

    12. Re:Unsafe Languages? by mrsbrisby · · Score: 2, Interesting

      The string handling functions of the standard C library make it very easy to write unsafe code.

      So what?

      Secure programming in C tends to avoid using the string handling functions as provided by ANSI and POSIX.

      Secure programming in C++ tends to avoid IOS for loss of atomicity.

      Secure programming in Perl tends to avoid iterpolation of user-data.

      Secure programming in any language tends to involve avoiding the mechanisms that are difficult to verify correctness with.

      Indeed: the most secure programming languages are the ones where correctness is easiest to verify- to do so in Java or Perl means verifying the VM and runtime, and to do so in C++ means finding that rare soul that actually understands what kind of nonsense the compiler is doing behind your back.

      That is to say that C is one of the best languages to use for secure software- not the worst- simply because it's easier to verify correctness on the whole, than with other languages and runtimes.

      The example of the image viewer that reads from an untrustworthy source is an excellent one- the Image viewer can be written in any language they like- and could have dozens of bugs and still not be a security risk if the application were run with a restricted uid and had no file or network I/O access.

      This could be done by splitting the application into several parts- one that represents the file I/O, one that represents decoding, and one that blits to the screen. The part that represents decoding is probably the hardest to verify, so it would be limited to pure CPU operations, reading bits from an existing pipe (from the file I/O part) and writing bits out to another existing pipe (representing the blitter).

      This approach works well, and believe it or not, in a world of increasingly multi-core and multi-processor systems, it's actually faster.

    13. Re:Unsafe Languages? by try_anything · · Score: 2, Insightful

      Exactly. Article writer has no knowledge in the area.

      Or perhaps you're missing the point.

      The article writer isn't saying that a smart, conscientious C programmer can't copy strings correctly 98% of the time. He's saying it isn't good enough to meet that standard, and most languages do better. Even C++ allows you to simply write string s2 = s1.

      To do the same thing in C, the programmer must know or check the length of s1, know or check the length of s2 (actually, know or check whether s2 points to any memory at all), possibly reallocate memory for s2, and copy exactly the right number of characters from s1 to s2. There are many possibilities for a typo or a lack of caffeine to open a security hole.

      this problem of stupid/lazy programmer

      No one denies that requiring several lines of finicky code to perform a simple operation separates the mental weenies from mental he-men. If your ego is the biggest thing on the line when you create software, by all means, keep using C for everything.

    14. Re:Unsafe Languages? by ufnoise · · Score: 2, Informative

      C is an unsafe language, and the standard C library string functions are unsafe.

      "Secure Programming Cookbook for C and C++ : Recipes for Cryptography, Authentication, Input Validation & More"
      by John Viega, Matt Messier.

      This is an excellent book which discusses many of the issues with writing safe code. They present safe versions of many of the standard C library functions.

      In addition, the C++ STL string classes are very well written for doing string handling in a safe manner.

  2. Re:Janitors need training/education by Solra+Bizna · · Score: 2, Funny

    Clearly, a variety of Linux tailored to drive some state-of-the-art software-controlled video card. :P

    -:sigma.SB

    --
    WARN
    THERE IS ANOTHER SYSTEM
  3. Secure Programming... by ltning · · Score: 5, Funny

    ...in GNU/Linux systems: 1500 pages, 3 volumes. ...in Windows systems: Two words: "You don't".

    --
    Love over Gold.
  4. Clash of the Cliches ... by Zero__Kelvin · · Score: 3, Insightful

    Actually, C is an unsafe language, and guns are dangerous, even in the right hands. The definition of "The Right Hands(tm)" is: "Someone who knows how intrinsically dangerous they are by nature."

    While we are throwing cliches around, I will toss this one out there: "The right tool for the right job."

    You wouldn't want to protect yourself from an attacker with a sponge, even though they are soft and fuzzy and safe. You wouldn't want to go deer hunting with a compiler. The problem is this: A gun in the hands of a person who doesn't understand why people think they are dangerous, is very dangerous!

    C is not a safe language ... it is a powerful one. One can write secure code with C, just as one can shoot oneself in the foot with it. You significantly minimize the risk of shooting yourself in the foot when you take a course that helps you understand how and why the gun is dangerous, and the methods you can use to mitigate the risk.

    Here is the problem, though: You don't need a C permit to write code using the C language." As a result, there are far too many people out there haphazardly swinging the barrel of their C compiler around thinking C saftey is a matter of pointers. You can be as careful as you want about where you point your gun when it is in your hands, but if you believe that gun safety begins and ends there, someone is more than likely going to get hurt. It is not enough to be careful where you point your C compilers barrel. You need to lock it up when you are not using it ....

    --
    Guns don't kill people; Physics kills people! - John Lithgow as Dick Solomon on Third Rock From The Sun
  5. Unsafe languages (and implementation details) by joel.neely · · Score: 4, Insightful

    The use of unconstrained pointers and casting (don't forget that in C this includes arrays!), combined with allocation of local data in the same stack that contains state information (registers to be restored upon function return) is at the heart of a large portion (most?) of the common security vulnerabilities on PCs.

    Some large-ish number of years ago I saw an article in which the author (don't recall the name offhand, sorry) asserted that raw pointers were the data equivalent of GOTOs. Both are potentially useful as under-the-hood implementation mechanisms, but entirely too easy to abuse for them to be exposed in a high-level-language.

  6. Re:Rule 1 by DrSkwid · · Score: 2, Interesting
    --
    There are places where the networks are not touching,and there are places where they are-Boeing's Lori Gunter