Bug 12782 - POSIX strerror_r quality of implementation
Summary: POSIX strerror_r quality of implementation
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: 2.13
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on: 12204
Blocks:
  Show dependency tree / graph
 
Reported: 2011-05-18 23:33 UTC by Eric Blake
Modified: 2014-06-27 13:20 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
Project(s) to access:
ssh public key:
fweimer: security-


Attachments
proposed patch (826 bytes, patch)
2011-05-18 23:35 UTC, Eric Blake
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Blake 2011-05-18 23:33:15 UTC
Although POSIX permits 'buf' to remain untouched on error, it does not require this; in fact, it recommends that buf still be populated insofar as possible for a better quality of implementation (since applications are likely to use the contents of buffer without checking for errors).  Furthermore, this is more in line with what glibc does for GNU strerror_r (a useful result at all costs, even if the result had to be truncated):

XSH line 63300:

"Similarly, implementations are encouraged to have strerror_r( ) return [EINVAL] and put a string like "unknown error" or "error number xxx" in the buffer pointed to by strerrbuf when the value of errnum is not a valid error number."

and http://austingroupbugs.net/view.php?id=398:

"Additionally, implementations are encouraged to NUL-terminate strerrbuf
when failing with ERANGE for any size other than buflen of zero."

If you use the example program of http://sourceware.org/bugzilla/show_bug.cgi?id=12204, then I argue that the results on glibc should be:

result: failure 22 Unknown error 18446744073709551614
result: failure 34 Pe

rather than Bruno's claim of

result: failure 22 ______
result: failure 34 ______

Additionally, that bug showed prior art of other platforms that (almost) have this QoI issue resolved according to POSIX recommendations:

FreeBSD 6.4 (albeit buggy for returning EINVAL on 0), AIX 6.1 (albeit buggy for not returning ERANGE on short buffer), Tru64 5.1 (albeit buggy for wrong return values).
Comment 1 Eric Blake 2011-05-18 23:35:55 UTC
Created attachment 5736 [details]
proposed patch
Comment 2 Ulrich Drepper 2011-05-21 16:12:46 UTC
I've applied a simpler patch.
Comment 3 Bruno Haible 2011-05-21 17:21:18 UTC
The new code at
http://sourceware.org/git/?p=glibc.git;a=blob;f=string/xpg-strerror.c;h=00256c3dac2591e99b504c97b05945e27aefecce;hb=7ea72f99966a65a56aedba817ee2413ff9b1f23c
has three flaws:

- It overwrites buf[0] when buflen == 0. This is an out-of-bounds access.

- If errnum is invalid, it wastes time calling __strerror_r and strlen
  before then returning EINVAL and ignoring the result of __strerror_r.

- It includes <libintl.h> and <locale.h> and defines dgettext(), although
  these are not needed any more, because the localization is now done by
  __strerror_r.
Comment 4 Bruno Haible 2012-06-30 16:11:50 UTC
The first and third reported flaw were fixed by Ulrich in
http://sourceware.org/git/?p=glibc.git;a=commitdiff;h=adcd5c15d2a37794d021104160b425ff61f88219

For the second one, see bug 14317.