Re: Privilege-escalation attacks on NT-based Windows are unfixable

From: David Thompson (
Date: 08/28/02

From: "David Thompson" <>
Date: Wed, 28 Aug 2002 20:15:40 GMT

Alun Jones <> wrote :
> In article <3D67D8A0.7060208@>, Edward Elliott <nobody@>
> wrote:
> >Alun Jones wrote:
> >> Are you really telling me that you wouldn't find, in thousands of software
> >> houses, code that read "inline void str_no_n_cpy(char *dest, const char
> >
> >> { while (*src) { *dest++=*src++; } *dest=0; }"?
> >
> >Yes that's my claim. Why bother writing a non-standard function when
> >you can just as easily use strncpy? strncpy(dst, src, dst_len) is best,
> >but I bet you'd also see strncpy(dst, src, strlen(dst)) and even
> >strncpy(dst, src, strlen(src)). The last two are slower but easier to
> >code. The last won't prevent buffer overflows, but if all three forms
> >were used with equal frequency, that'd be a 2/3 reduction in buffer
> >overflows as a result of strcpy.
> The last certainly won't prevent buffer overflows - in fact, it devolves to
> the same effect as the code example I posted - and the second-to-last is just
> plain wrong.
The middle (second-from-last or -from-first) is almost always wrong,
and sometimes, if the buffer didn't already contain a properly terminated
string, catastrophically unsafe. The third and last is NOT the same as
the code you posted (which IS the same as strcpy()) -- with those arguments
strncpy will always reach (and stop because of) the specified limit in which
case it does NOT write a null terminator, which means if you subsequently
use (read) that string you get at best wrong results and quite possibly a crash.

The first is safe in itself if dst_len is correct (which is sometimes difficult)
but again does not terminate the string if it hits the limit, and conversely
will pad out the whole buffer if the source is shorter than the limit, which
for a large buffer and on-average short data may be horrendously slow.

The comp.lang.c preferred (as in least disliked) approach is
  dst[0] = '\0'; strncat(dst, src, dst_size -1); /* or equivalent */

- David.Thompson 1 now at