News for hackers. Stuff that matters.
Re:Printable True Bugs Wait Posters by danielweber on Friday January 31, 2014 @08:29AM (#7156527)
Almost every one has a n version you should use, and their man pages will tell you that.

strcpy -> strncpy (or strlcpy on BSD)

strcat -> strncat (or strlcat on BSD)

sprintf -> snprintf (but still watch out for printf format attacks)

gets -> something else entirely. The man page says "programs should NEVER use gets()".

Re:Printable True Bugs Wait Posters by tptacek on Friday January 31, 2014 @08:34AM (#7156546)
They told me if I used the n version I'd be safe. They told me the risks were tiny. That all we had to do was practice safe counting.

They told me that... ˚sob˚ but they were wrong.

Re:Printable True Bugs Wait Posters by fromdoon on Friday January 31, 2014 @09:52AM (#7156891)
Hi, Is there any resource on Web or even any book that documents all the risks. I had once seen a talk given by you .. was good !! Any other such resources out there ??
Re:Printable True Bugs Wait Posters by tptacek on Friday January 31, 2014 @12:45PM (#7157898)
I like _The Art of Software Security Assessment_ for this.
Re:Printable True Bugs Wait Posters by _telemachus on Friday January 31, 2014 @10:52AM (#7157247)
Good for laughs. Thanks. Now the serious follow up: Ok, what else should someone do?

Programs (often) handle text. Apparently, that pretty much means you're fucked. So what is a reasonable way to write such programs?

Re:Printable True Bugs Wait Posters by cube13 on Friday January 31, 2014 @11:19AM (#7157386)
Make damned sure you know what you're doing. That means making sure you have enough memory allocated to avoid overflows, and that any input is sanitized before putting it down. Meaning, if you're using a function that's expecting a null terminated string, make SURE it's null terminated before copying. Or that you know the exact length to pass into a length specified function.

The problem isn't necessarily the functions themselves, it's coders who make assumptions that don't pan out to be true.

Re:Printable True Bugs Wait Posters by thinkpad20 on Friday January 31, 2014 @11:24AM (#7157410)
Do all of your string work with these guys:

    struct string {
      char *str;
      unsigned length;
    }
Re:Printable True Bugs Wait Posters by _telemachus on Friday January 31, 2014 @11:54AM (#7157572)
Let's assume your string struct is solid. Then does that mean you can safely use it with `printf, fprintf, sprintf` (e.g. printf("%s", string->value)? Or must you also write custom versions of those functions? How deep does this rabbit hole go?
Re:Printable True Bugs Wait Posters by thinkpad20 on Friday January 31, 2014 @12:10PM (#7157695)
You don't have to write custom versions of any of those functions; just use the char pointer in the struct instead of a bare char pointer. Keeping track of the length of your strings gives an easy way to provide the 'n' in all of those 'n' functions, and has other advantages besides. But the use of such a struct in and of itself, of course, provides no guarantees of safety. There is no such thing in C anyway :)
Re:Printable True Bugs Wait Posters by dllthomas on Friday January 31, 2014 @03:58PM (#7159141)
You'd probably want

    "%.*s"
or possibly even

    "%*.*s"
if you want it space padded. In principle you can bound your space usage and avoid an snprintf with such constructs; in practice, it's probably better to still use snprintf (if you're using standard-library string functions at all).
Re:Printable True Bugs Wait Posters by dllthomas on Friday January 31, 2014 @11:18AM (#7157380)
If you're able, don't treat text as zero-terminated char arrays. Ideally, you'd have a well fleshed-out library for encoding-aware ropes.
Re:Printable True Bugs Wait Posters by Arnor on Friday January 31, 2014 @11:51AM (#7157552)
Which libraries do you recommend?
Re:Printable True Bugs Wait Posters by dllthomas on Friday January 31, 2014 @12:06PM (#7157657)
Unfortunately, I don't know of one. My recent C work has worked with text only in a very limited capacity (parsing and building packets in an ascii format - for the later, vectorized write buffers are a poor-man's ropes).

Edited to add:

Apparently there is this: http://www.hpl.hp.com/personal/Hans_Boehm/gc/gc_source/cordh...

Re:Printable True Bugs Wait Posters by oofabz on Friday January 31, 2014 @05:17PM (#7159550)
I recommend bstring for length-prefixed strings in C:

http://bstring.sourceforge.net/

Re:Printable True Bugs Wait Posters by clarry on Sunday February 02, 2014 @04:34AM (#7165453)
Bstring relies on undefined behavior for security. Don't use it if you care about security.
Re:Printable True Bugs Wait Posters by ledbettj on Friday January 31, 2014 @09:13AM (#7156670)
Keeping in mind that strncpy has it's own problems, like not null terminating the buffer if it would overflow.
Re:Printable True Bugs Wait Posters by Nursie on Friday January 31, 2014 @09:24AM (#7156723)
I guess the whole point is don't rely on null terminated strings in your code.
Re:Printable True Bugs Wait Posters by lawl on Friday January 31, 2014 @09:29AM (#7156754)
This is why you should use strlcpy()!
Re:Printable True Bugs Wait Posters by santosha on Friday January 31, 2014 @09:32AM (#7156776)
The point of the n is that you specify the maximum length that it's allowed to copy. It's slightly harder to mess that up than with regular strcpy.
Re:Printable True Bugs Wait Posters by tobiasu on Friday January 31, 2014 @10:49AM (#7157227)
The n in strncpy describes to what size the destination buffer (not string) should be padded with '\0'.

This is useful for copying a string into a fixed size buffer that is sent over the network, to give an example. It's not what the programmer generally means when using strncpy.

Many programmers are surprised when they learn that strncpy() really writes 1M-strlen("abc") zeros in the 1M char array every time it's called...

Re:Printable True Bugs Wait Posters by dllthomas on Friday January 31, 2014 @04:09PM (#7159198)
"The n in strncpy describes to what size the destination buffer (not string) should be padded with '\0'."

This is false.

From the man page: "The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated."

And the following code prints foo:ar on my system.

    char buffer[10];
    strcpy(buffer, "foobar");
    strcpy(buffer, "foo");
    printf("%s:%s\n", buffer, buffer+4);
Edited to add: huh, scratch that. Obvious error in above test :-P. Testing it with strncat like I had meant to, it seems it is in fact padded, not just (possibly) terminated. Interesting, and very worth knowing if you are trying to move a probably small string to a large buffer under time pressure.
Re:Printable True Bugs Wait Posters by tobiasu on Friday January 31, 2014 @05:45PM (#7159687)
What is false? I'm not talking about the source string not being terminated when it's too long. That's obvious and there are plenty posts about it in this thread.

Not sure what you want to say with the example code. Maybe swap it for strncpy and strlcpy and see whether that matches your expectations?

Re:Printable True Bugs Wait Posters by dllthomas on Friday January 31, 2014 @06:02PM (#7159754)
Yeah, my bad, had meant to use strncpy. My contention had been that it only zeroes the first following character (if that). On closer reading of the man page, it is in fact clear.
Re:Printable True Bugs Wait Posters by vezzy-fnord on Friday January 31, 2014 @10:15AM (#7157021)
The alternative to gets() is fgets(). In fact, I'm pretty sure the gets() function is completely deprecated in the C11 standard.

However, despite the n functions being generally safer, you're still propagating misinformation by touting them as secure alternatives.

The original use of the n functions was to manipulate strings in matters of fixed size arrays. If you don't know what you're doing and just blindly use strncpy() as a strcpy() replacement, you could end up truncating your strings.

OpenBSD's l functions, on the other hand, were specifically designed with security in mind.

Re:Printable True Bugs Wait Posters by cmollis on Friday January 31, 2014 @08:53AM (#7156601)
exactly.