Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 65892 - (toolchain) glibc propolice patch has unwanted side-effects
Summary: (toolchain) glibc propolice patch has unwanted side-effects
Status: RESOLVED TEST-REQUEST
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Library (show other bugs)
Hardware: x86 Linux
: High normal (vote)
Assignee: The Gentoo Linux Hardened Team
URL:
Whiteboard:
Keywords:
: 81364 (view as bug list)
Depends on:
Blocks: 55865 66553 81364
  Show dependency tree
 
Reported: 2004-09-30 01:38 UTC by Erik Scheffers
Modified: 2005-06-03 09:38 UTC (History)
4 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
Example program (test.c,132 bytes, text/plain)
2004-09-30 01:39 UTC, Erik Scheffers
Details
Proposed patch (patch.txt,670 bytes, patch)
2004-09-30 01:40 UTC, Erik Scheffers
Details | Diff
Proposed fix for ssp.c. (ssp.sck-read-once.fix.patch,3.01 KB, patch)
2004-10-16 07:48 UTC, Lorenzo Hernández García-Hierro
Details | Diff
Final patch for uclibc and glibc. (ssp-65892-uclibc-def.patch,1.12 KB, patch)
2004-10-16 08:29 UTC, Lorenzo Hernández García-Hierro
Details | Diff
adds open/close/read and corrects uClibc part (ssp.new.patch,1.26 KB, patch)
2004-10-16 10:26 UTC, Peter S. Mazinger
Details | Diff
modified ssp.c, works with glibc (ssp.c,5.46 KB, text/plain)
2005-01-27 16:24 UTC, Kevin F. Quinn (RETIRED)
Details
Change __guard_setup to use unpublished glibc interfaces so that well-behaved preloading doesn't break. (ssp-nosetupoverload.patch,1.12 KB, patch)
2005-02-19 03:39 UTC, Kevin F. Quinn (RETIRED)
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Erik Scheffers 2004-09-30 01:38:44 UTC
As part of the propolice patch for glibc, a function __guard_setup() is called before the main() function is called. This function has an unwanted side-effect.

For example, see the attached test.c program. This program will give the following results when executed on a system with a glibc with propolice patch:

bash-2.05b$ gcc ./test.c 
bash-2.05b$ ./a.out 
I'm reading something now
Hello there!
I'm reading something now

read() is called before main()!

This is an unwanted side-effect of the implementation of the __guard_setup() function. The __guard_setup() function opens '/dev/urandom' and reads some bytes from it to setup a random guard. This is done by calling the open(), read()
and close() functions, resulting in the unwanted side-effect.

The solution looks simple. Instead of calling open(), read() and close(), the __guard_setup() function should call __open(), __read() and __close().

The attached patch will apply this fix to ssp.c which is part of the propolice patch and contains the __guard_setup() function.


Reproducible: Always
Steps to Reproduce:
1. Compile and run test.c
2.
3.

Actual Results:  
function read() is called twice

Expected Results:  
called it once
Comment 1 Erik Scheffers 2004-09-30 01:39:47 UTC
Created attachment 40757 [details]
Example program
Comment 2 Erik Scheffers 2004-09-30 01:40:57 UTC
Created attachment 40758 [details, diff]
Proposed patch
Comment 3 Joshua Kinard gentoo-dev 2004-09-30 02:48:32 UTC
I ran a quick check of this on x86, sparc64, and o32 mips.  x86 and mips both produced the side-effect of read() getting called first, but sparc64 didn't (and I even compiled it with -fstack-protector).  w/ sparc64, main() gets called, then read().  Rather inteeresting.
Comment 4 Peter S. Mazinger 2004-09-30 04:49:28 UTC
Could the difference between x86/mips and sparc be between the used glibc's (w/ ERANDOM or w/o)?
Comment 5 solar (RETIRED) gentoo-dev 2004-09-30 09:26:43 UTC
This is indeed a good catch. Lets test this patch on some arches before commiting. 

Peter does the same problem shows up on uClibc environments?
Comment 6 Peter S. Mazinger 2004-09-30 12:23:10 UTC
It reads twice on uclibc independently of ssp[-all] enabled or disabled (tested x86 gentoo and my env)
ERANDOM is not "ported" to uclibc, so that does not influence the case.
Comment 7 solar (RETIRED) gentoo-dev 2004-09-30 13:31:37 UTC
My concern here is that some other non x86 arches (sparc/mips/few others maybe) do not accept __open(), __close(), __read() with the __ prefix for symbol resolution. 

I can't recall when it's hit me before but it seems as one or two of these arches wanted _ vs __ and another wanted no _ or __

We have to do some testing before we can make this final. As we don't want to break up anybody's system.
Comment 8 Peter S. Mazinger 2004-09-30 13:42:14 UTC
I have done testing w/ libssp (it is not in glibc), the behaviour is the same (less the non-ssp case, but this is normal). I have then rebuilt the libssp adding __, and it works correctly now
Comment 9 solar (RETIRED) gentoo-dev 2004-09-30 13:54:27 UTC
So x86/ppc are good?

Ok that leaves sparc{32,64}/amd64/arm/mips/hppa that we need to get confirmed reports from.
Comment 10 Travis Tilley (RETIRED) gentoo-dev 2004-09-30 17:07:39 UTC
good to go for amd64...
Comment 11 solar (RETIRED) gentoo-dev 2004-10-02 15:48:46 UTC
Peter S. Mazinger  and myself have been in contact about how this bug also relates to uClibc. In one of our mails to each other we bring up that there are quite a few more libc and syscalls used within the ssp.c

The complete list includes.

socket(),
sendto(),

getpid(),

kill(),
sigdelset(),
sigprocmask(),
sigfillset(),
sigaction(),

strcpy(),
strncat(),
strlen(),
strncpy(),
gettimeofday(),

Erik Scheffers would you mind doing a test run with all of the appropriate libc internal functions on a native glibc host. 
A Proposed patch#2 would be really appreciated.
Comment 12 Lorenzo Hernández García-Hierro 2004-10-16 07:48:40 UTC
Created attachment 41960 [details, diff]
Proposed fix for ssp.c.

I haven't tested it but i hope it will work without pain, but still needs
testing.
I don't have time by now to test it further, pleas take care of that asap.
Comment 13 Peter S. Mazinger 2004-10-16 08:05:29 UTC
Could we generalize it so that it will also work on uClibc
like
#ifdef __UCLIBC__
#define somefunc __libc_somefunc
#else
#define somefunc __somefunc
#endif
Comment 14 Lorenzo Hernández García-Hierro 2004-10-16 08:12:00 UTC
That will be also a good alternative to fix them, than replacing directly the calls, and just  redefining their names.
I'm starting another proposed patch ;-)
Thanks.
Comment 15 Lorenzo Hernández García-Hierro 2004-10-16 08:29:17 UTC
Created attachment 41964 [details, diff]
Final patch for uclibc and glibc.

Added the new patch with the additions that Peter proposed in his last
comment.Seems much more smart than replacing each call and appending the __
prefix, also it makes the fix available for Uclibc.

Cheers.
Comment 16 Peter S. Mazinger 2004-10-16 10:26:12 UTC
Created attachment 41970 [details, diff]
adds open/close/read and corrects uClibc part
Comment 17 Kevin F. Quinn (RETIRED) gentoo-dev 2005-01-27 12:01:42 UTC
The attached patch fails for me.  See bug #66553 for an alternative patch, one that patches each line rather than trying the #define trick (I guess that won't work with uclibc).

I think care has to be taken with these names; depending on architecture some are already #defined.  Some of the proposed targets don't exist - __strcpy, __strncat, __strlen and __strncpy don't seem to.  I don't think these need to be #defined on glibc anyway, as there's some "libc_hidden_proto" thingy although I'm not sure what it does :)  Similarly sigdelset and sigfillset, although the __ variants of these do exist.

sigaction is both a function and a struct name so that #define doesn't work either - needs to be of the form:

  #define sigaction(a,b,c) __sigaction(a,b,c)

Lastly, is there any reason not to fixup write (e.g. #define write __write)?
Comment 18 Kevin F. Quinn (RETIRED) gentoo-dev 2005-01-27 16:24:24 UTC
Created attachment 49704 [details]
modified ssp.c, works with glibc

ssp.c updated inline with my previous comment for review (glibc part - I don't
use uclibc so have no comment on that).

Plonked it in sys-libs/glibc/files/2.3.4/ssp.c and changed ebuild to fetch from
files/2.3.4 instead of files/2.3.3.
Comment 19 Jeremy Huddleston (RETIRED) gentoo-dev 2005-02-18 14:52:07 UTC
solar: what're your comments on this modified ssp.c?  Should I throw it into the next glibc?
Comment 20 solar (RETIRED) gentoo-dev 2005-02-18 15:36:08 UTC
I would not merge this code as is.
dietlibc/uClibc are not longer needed so this update is quite a tad more complex 
than need be. Each of the other libcs have unique own versions of these functions.
Comment 21 Kevin F. Quinn (RETIRED) gentoo-dev 2005-02-18 16:51:44 UTC
For info, pappy reckoned that only the __guard_setup needed to change, rather than all references as we have done here.  From the point of view of fixing the side-effects of the _guard_setup he's right, however I'm worried that leaving references to public weak symbols in __stack_smash_handler provides a way for attackers to get around ssp.

Ideally there would be a way of forcing the linker to resolve the references from an object within the library at link time, rather than leaving them to be resolved until load time.  However I don't see any way to tell the linker to fully resolve one object in a library.  Symbol visibility in glibc seems to be in quite a mess, frankly.

The only other alternative is to change the calls; perhaps the patch Malte provided on bug #66553 will do, although there are some holes in that too, I think.  We could fix just the __guard_setup and worry about public weak symbols later, in order to get a resolution of these bugs now if the plan is to make a new glibc release soon.

A last note; the ssp.c in dietlibc and uclibc also have the same problems, unless they've changed since I last synced.
Comment 22 Kevin F. Quinn (RETIRED) gentoo-dev 2005-02-19 03:39:21 UTC
Created attachment 51578 [details, diff]
Change __guard_setup to use unpublished glibc interfaces so that well-behaved preloading doesn't break.

I've discovered that changing open() to __open() etc doesn't solve the problem
of overloading per se; i.e. it's possibleto LD_PRELOAD many of the __*()
functions since they are also WEAK.  My conclusion is that fixing the "use
preload to alter the behaviour of ssp" issue (assuming it is a problem - no-one
has yet said it isn't), is more involved than I thought, and is a topic for
another day.

So in the interest of resolving these bugs, the attached patch solves the
startup side-effect problems and nothing else.	I think this is a sensible
approach.
Comment 23 solar (RETIRED) gentoo-dev 2005-02-19 04:09:07 UTC
I agree with comment #22 and support that patch.
Comment 24 Jeremy Huddleston (RETIRED) gentoo-dev 2005-02-19 12:43:01 UTC
alright, I've committed comment #22's changes that in the 1.3 patch tarball in glibc-20050125-r1.
Comment 25 solar (RETIRED) gentoo-dev 2005-02-19 13:41:50 UTC
Thank you Jeremy.

Users from bug #66553 and bug #81364 please test and see if this solves your problems. changing resolution to TEST-REQUEST
Comment 26 SpanKY gentoo-dev 2005-02-19 13:43:58 UTC
*** Bug 81364 has been marked as a duplicate of this bug. ***
Comment 27 Kevin F. Quinn (RETIRED) gentoo-dev 2005-02-27 23:51:50 UTC
FWIW, and for anyone who like me didn't know, the loader ignores LD_LIBRARY_PATH and restricts LD_PRELOAD to suid libraries when an executable is suid.  Thus on a well-configured system, an unpriviledged attacker can't use preloading to inject malicious code into a process built from an suid executable (ref. man ld.so).

The upshot is that although the stack protector could be thwarted on normal binaries through use of preloading, that's of little use to an attacker as it doesn't give him anything he doesn't already have (i.e. unpriviledged access to the system).