public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "David A. Wheeler" <dwheeler@dwheeler.com>
To: "libc-alpha" <libc-alpha@sourceware.org>
Subject: Re: Implement C11 annex K?
Date: Thu, 21 Aug 2014 22:45:00 -0000	[thread overview]
Message-ID: <E1XKb6n-0005Nu-Ap@rmm6prod02.runbox.com> (raw)
In-Reply-To: <53F1B352.3010207@cs.ucla.edu>

On Mon, 18 Aug 2014 01:03:30 -0700, Paul Eggert <eggert@cs.ucla.edu> wrote:
> OpenSSH's authors have strongly 
> advocated strlcpy and have much invested in it over the years.  Even if 
> they conceded that strlcpy is not that helpful now (admittedly 
> unlikely), inertia would probably induce them to keep it.

There are now *hundreds* of packages that use strlcpy/strlcat.
It is NOT just the original OpenSSH authors that use them.
Every package has to keep re-implementing them, typically in less-efficient
portable ways, because glibc fails to include them.

Here's a list put together by deraadt, there are probably others:
http://marc.info/?l=openbsd-tech&m=138733933417096&w=2

I'd like to convince you to think about *risk*.  There's no doubt that it
is *possible* to write secure software using only strcpy, strlen, etc.
But while that is *true*, it is also *irrelevant*.

The probability of making a mistake using routines like strcpy is very high.
This is amply demonstrated by the continuing march of buffer overflows.
We need alternatives, ones that automatically *prevent* buffer overflows
and can be easily applied.

So with that, let me comment on the comments...


> > addrmatch.c:321:
> > ... The one-line snprintf version is this horror:
> 
> That's because you wrote it in a horrible way.  This is better:
> 
>     if (snprintf(addrbuf, sizeof addrbuf, "%s", p) >= sizeof addrbuf)
>       return -1;

The spec says snprintf can return <0, which this code fails to handle.
It's still not better, anyway; it sure isn't obvious that this is a string copy.
But to continue...

> Though I wouldn't use snprintf here, as the following distinguishes the 
> check from the action more clearly:
> 
>     if (strlen(p) >= sizeof addrbuf)
>       return -1;
>     strcpy(addrbuf, p);

No. That wastes a lot of *people* time and is dangerous during maintenance.
In many projects, every strcpy() will (correctly) set off warning bells
requiring multiple people to *prove* that it can't exceed the buffer overflows.
And it's not just during writing the initial code.  It's also easy to turn this kind
of code into a vulnerability when the code is maintained
(which is one reason this kind of code wastes a lot of people-time),
so every time the function is changed, people will have to re-check it, and
over time this can get painful.

So every strcpy(), even if it's safe, increases *development* and *maintenance* time.
Developer and reviewer time is often MORE important than execution time
(even in C code, only some paths usually matter).
It's not TOO hard in this case to show that the strcpy cannot be
exceeded, sure, but that's not the end.

A lot of developers who care about secure code are trying to *eliminate*
the use of strcpy and functions like it, because it's just too easy to make a
disastrous mistake. Use strcpy() where it's provably safe and on a path where
performance is very important, sure.  But it should NOT be used elsewhere.
Telling people "don't make any mistakes" hasn't worked so far, and it won't in the future.

So NO, this is NOT better.  This is WORSE.  The call to strcpy() is
*faster*, but it's only justifiable if it's on a fast path where the speed matters.

> Regardless of the form one prefers, the use of strlcpy here does not fix 
> any bugs or make the code significantly clearer, compared to using 
> standard functions.

The use of strlcpy *does* help, quite substantially.

It eliminates the risk that a later modification will cause a buffer overflow;
at worst it becomes a truncation instead.  That *matters*.

So I reach radically different conclusions here.  I think that
pattern will continue... :-).



> > auth.c:486:
> >  strlcpy(buf, cp, sizeof(buf));
> >  ... So.. do you really believe that MAXPATHLEN really is the max length?
> 
> It's not a matter of belief.  It's obvious from the code that sets 'cp', 
> four lines earlier.

(I was actually complaining about MAXPATHLEN nonsense in the standard,
and not really about the subject-at-hand, so let's skip that.)

>  Worse, this use of strlcpy has undefined behavior 
> when cp points into buf.

I don't think so.  strlcpy is required to copy the source left-to-right, since it stops at the \0,
and it's copying to the beginning of "buf", so I don't see an undefined behavior.


>  A fix would be:
> 
>     memmove(buf, cp, strlen(cp) + 1);

That's not a fix.  That's horrific.

That way of using memmove checks the length of the *source*,
and copies it, and it fails to do *any* checks that the buffer has enough length.
That kind of construct is *BEGGING* to be part of a buffer overflow.

It's true that in *this particular case* it won't overflow a buffer, as long as
nothing ever changes in the rest of the code surrounding it.
But anyone who keeps writing code this way, using functions
that do not *always* check the destination length, will eventually make a mistake.
Actually, in my experience, they'll make dozens of them.

We need simple mechanisms that people can use *every time* that
prevent buffer overflows.  Ones that don't require deep analysis and
deep re-analysis every time someone changes a line of code.

Most code in most C programs is *not* on a speed-critical path, but is instead
doing setup, special-case error handling, etc.  It should be *easy* to write
code that obviously cannot ever cause a buffer overflow, when it's
okay to spend a few extra cycles to do it.


> > authfd.c:107:
> >  strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
> > ... Truncation isn't checked... but it's not clear what else you
> >  could do when truncation occurs.
> 
> No, it's quite clear.  You could return -1, which is what strlcpy's 
> caller is supposed to do on failure.  Here, strlcpy might be 
> contributing to a bug, and it certainly isn't helping: a programmer who 
> had used strlen + strcpy would likely have done better here and returned 
> -1 on overlong inputs.

Or, a programmer using strcpy could just allow a buffer overflow, the usual result :-).

I agree that returning -1 would be a good idea. But note that even when
you don't check the return, which you could call a mistake, the usual result
of strlcpy is much safer - it is merely truncation (which often isn't exploitable).
A common result of strcpy misuse is a CVE id :-).

But let's go with your thought.  Changing this to return -1 is also easy:

if (strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path)) >= sizeof(sunaddr.sun_path))
  return -1;
 

> > auth-pam.c:742:
> >   mlen = strlen(msg);
> >   ...
> >   len = plen + mlen + 1;
> >   **prompts = xrealloc(**prompts, 1, len);
> >   strlcpy(**prompts + plen, msg, len - plen);
> >   plen += mlen;
> > ....
> >   Advantage strlcpy, due to a philosophical preference
> 
> I'm afraid that veers too closely to "I like strlcpy because I like 
> strlcpy".  strlcpy does not fix any bugs here compared to strcpy, and 
> this was the point I originally made.  And strcpy would be simpler here.

No. strlcpy/strlcat, and other routines like strcpy_s, have the advantage
that they significantly reduce the likelihood that the inevitable programming
mistakes will become a serious vulnerability.

It's a *risk* decision.  C should have routines that give you the fastest
possible routines when you need them... but it should also have some
less-risky functions for common cases.

--- David A. Wheeler


  parent reply	other threads:[~2014-08-21 22:45 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1407616492.31098.ezmlm@sourceware.org>
2014-08-09 20:52 ` David A. Wheeler
2014-08-10  7:52   ` Andreas Jaeger
2014-08-10 15:03     ` Adhemerval Zanella
2014-08-11 15:32       ` Joseph S. Myers
2014-08-11 15:52         ` Paul Eggert
2014-08-11 16:06           ` Joseph S. Myers
2014-08-11 15:56         ` David A. Wheeler
2014-08-12  4:23       ` Rich Felker
     [not found]         ` <3565dfa0-060c-46b9-b08c-6edc4eaa1179@email.android.com>
2014-08-12 21:00           ` Rich Felker
     [not found]             ` <d4ae8119-f629-4235-8981-dd2ccc220fea@email.android.com>
2014-08-12 22:08               ` Rich Felker
2014-08-12 23:15                 ` David A. Wheeler
2014-08-12 23:48                   ` dalias
2014-08-13 19:23                     ` David A. Wheeler
2014-08-13 19:44                       ` Adhemerval Zanella
2014-08-13 19:45                         ` Adhemerval Zanella
2014-08-13 20:49                         ` Rich Felker
2014-08-13 20:41                       ` dalias
2014-08-13 20:55                       ` Joseph S. Myers
2014-08-13 21:25                         ` Paul Eggert
2014-08-13 21:35                           ` Rich Felker
2014-08-13 22:46                             ` Tolga Dalman
2014-08-13 23:59                               ` Russ Allbery
2014-08-14  0:55                                 ` Joseph S. Myers
2014-08-14  1:01                                   ` Russ Allbery
2014-08-14  2:25                                 ` Rich Felker
2014-08-14  5:25                                   ` Russ Allbery
2014-08-14  5:46                                     ` Rich Felker
2014-08-14  6:15                                       ` Russ Allbery
2014-08-14  9:55                                         ` Florian Weimer
2014-08-14 10:02                                           ` Andreas Schwab
2014-08-14 10:06                                             ` Florian Weimer
2014-08-14 10:13                                               ` Andreas Schwab
2014-08-14 16:26                                                 ` Rich Felker
2014-08-14 16:53                                                   ` Andreas Schwab
2014-08-14 17:04                                                     ` Rich Felker
2014-08-18  7:31                                                       ` Andreas Schwab
2014-08-18 19:20                                                         ` Rich Felker
2014-08-14 15:20                                         ` Paul Eggert
2014-08-14 17:20                                           ` Russ Allbery
2014-08-14 17:46                                           ` Rich Felker
2014-08-15  7:51                                             ` Florian Weimer
2014-08-14  6:08                                 ` Paul Eggert
2014-08-15 14:25                                   ` David A. Wheeler
2014-08-15 15:36                                     ` Paul Eggert
2014-08-15 16:14                                       ` David A. Wheeler
2014-08-15 16:39                                         ` Rich Felker
2014-08-15 22:01                                           ` David A. Wheeler
2014-08-16  2:19                                             ` Rich Felker
2014-08-16  2:26                                             ` Russ Allbery
2014-08-16  2:49                                               ` Rich Felker
2014-08-16  3:03                                                 ` Russ Allbery
2014-08-15 22:04                                         ` Paul Eggert
2014-08-15 22:25                                           ` David A. Wheeler
2014-08-15 22:43                                             ` Adhemerval Zanella
2014-08-16  4:41                                               ` David A. Wheeler
2014-08-16  5:01                                                 ` Rich Felker
2014-08-17 18:03                                                   ` David A. Wheeler
2014-08-17 19:05                                                     ` dalias
2014-08-17 20:33                                                       ` David A. Wheeler
2014-08-17 23:25                                                         ` Rich Felker
2014-08-18  0:59                                                           ` David A. Wheeler
2014-08-18  0:15                                           ` David A. Wheeler
2014-08-18  8:03                                             ` Paul Eggert
2014-08-18 19:22                                               ` Rich Felker
2014-08-21 22:45                                               ` David A. Wheeler [this message]
2014-08-22  0:37                                                 ` Rich Felker
2014-08-22  1:39                                                 ` William Park
2014-08-22  1:53                                                   ` Jonathan Nieder
2014-08-22  4:36                                                     ` William Park
2014-08-22  2:32                                                 ` Paul Eggert
2014-08-22  2:51                                                   ` Rich Felker
2014-09-08 23:21                                                   ` David A. Wheeler
2014-09-09  3:34                                                     ` Paul Eggert
2014-08-13 22:20                         ` Time to add strlcpy/strlcat FINALLY David A. Wheeler
2014-08-14  1:09                           ` Paul Eggert
2014-08-14  2:34                             ` Rich Felker
2014-08-14  3:02                             ` William Park
2014-08-14 13:01                               ` Mike Frysinger
2014-08-15 10:37                                 ` Michael Kerrisk (man-pages)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1XKb6n-0005Nu-Ap@rmm6prod02.runbox.com \
    --to=dwheeler@dwheeler.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).