public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* Re: unable to attach to setuid program that as reverted it privilege
@ 2008-04-14 13:46 Reynolds, Brandon
  2008-04-14 16:32 ` Michael Potter
  2008-04-14 16:45 ` Tavis Ormandy
  0 siblings, 2 replies; 14+ messages in thread
From: Reynolds, Brandon @ 2008-04-14 13:46 UTC (permalink / raw)
  To: pottmi; +Cc: gdb


I can't seem to get this to work.

I have a setuid executable with the following code:
-----------------------------------------------------------------------------
    /* keep root capabilities as we transition to the regular user */
    prctl( PR_SET_KEEPCAPS, 1, 0, 0, 0 );

    /* switch back to user that ran us */
    setuid( p->pw_uid );
    setgid( p->pw_gid );

    /* drop all privs except CAP_SYS_NICE */
    if (cap_set_proc((cap = cap_from_text( "CAP_SYS_NICE+pe" ))) < 0)
    {
        perror( "cap_set_proc: failed to drop privs, aborting" );
        exit( 1 );
    }
    cap_free(cap);
-----------------------------------------------------------------------------

> cat /proc/sys/kernel/suid_dumpable
2

> uname -a
Linux sgi2 2.6.16.46-0.12-default #1 SMP Thu May 17 14:00:09 UTC 2007 ia64 ia64 ia64 GNU/Linux

FYI, the man 5 proc says:

/proc/sys/fs/suid_dumpable (since Linux 2.6.13)
              The value in this file determines whether core dump files are produced for set-user-ID or otherâ
              wise protected/tainted binaries.  Three different integer values can be specified:

              0 (default) This provides the traditional (pre-Linux 2.6.13) behaviour.  A core dump will not be
              produced for a process which has changed credentials (by calling seteuid(2), setgid(2), or simiâ
              lar,  or  by executing a set-user-ID or set-group-ID program) or whose binary does not have read
              permission enabled.

              1 ("debug") All processes dump core when possible.  The core dump is owned by  the  file  system
              user  ID  of the dumping process and no security is applied.  This is intended for system debugâ
              ging situations only.  Ptrace is unchecked.

              2 ("suidsafe") Any binary which normally would not be dumped (see "0" above) is dumped  readable
              by  root only.  This allows the user to remove the core dump file but not to read it.  For secuâ
              rity reasons core dumps in this mode will not overwrite one another or other files.   This  mode
              is appropriate when administrators are attempting to debug problems in a normal environment.

> This is documented as allowing core files to be created for setuid
> programs.  What I am using it for is to allow gdb run as a non-root
> user to connect to setuid programs that have _permanently_ given up
> their root privilege.  Without suid_dumpable enabled, gdb will fail
> with a EPERM error even tho the target program is no longer running as
> root and can not reacquire root privilege ( a good default behavior ).

What did you mean by "_permanently_"?

-Brandon

^ permalink raw reply	[flat|nested] 14+ messages in thread
* unable to attach to setuid program that as reverted it privilege
@ 2008-01-22 20:00 Michael Potter
  2008-01-22 20:09 ` Daniel Jacobowitz
  2008-01-23 17:52 ` Mark Kettenis
  0 siblings, 2 replies; 14+ messages in thread
From: Michael Potter @ 2008-01-22 20:00 UTC (permalink / raw)
  To: gdb

Gdb Crew,

I get this error when attaching to a setuid program that has
_given_up_ its root privilege setuid(getuid()):

---------------------
x~> gdb -p 19484
GNU gdb 6.5
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for
details.
This GDB was configured as "i586-suse-linux".
Attaching to process 19484
ptrace: Operation not permitted.            <<<<<=================

--------------------
I will speculate that the logic behind that is even tho the program
does not have root privilege now, it could have something in left over
in memory from when it did have root privilege.  I think this is a
good default behavior, but I am hopeful that some clever programmer
has found a way to change their program such that gdb can attach to
it.

My program _only_ uses root privilege to be able to be able to do a
oneway switch to a non-privileged user.  I believe that any hole I am
opening up is much smaller than my next alternative which is to allow
the programmers to run gdb as root so they can attach to the program.

Suggestions on alternatives such as a way to switch users without root
privileges
are welcome.

Thanks,

For those of you who may want to reproduce this feature here are some
cut and paste ready instructions:
1) mkdir -p tmp/setuidtest;cd tmp/setuidtest
2) vim iamsetuidroot.c   # paste in the following:
#include <stdio.h>
#include <stdlib.h>

int main(int arc, char *argv[])
{
   if (geteuid() != 0)
   {
      printf("this program must be run setuid root, not %d\n",
geteuid());
      exit(1);
   }
   if (getuid() == 0)
   {
      printf("this program must not be run as root\n");
      exit(1);
   }

   setuid(getuid());

   if (!setuid(0))
   {
      /* we want setuid to fail to be able to return to root */
      printf("this program was able to revert to root\n");
      exit(1);
   }

   printf("before the sleep %d\n", getpid());

   sleep(60);

   printf("after the sleep\n");

   exit(0);

}

3) cc -o iamsetuidroot iamsetuidroot.c
4) sudo chown root iamsetuidroot
5) sudo chmod u+s iamsetuidroot
6) ./iamsetuidroot   # observe pid in output.
7) gdb -p $thePid    # in another window

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2008-04-14 16:45 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-14 13:46 unable to attach to setuid program that as reverted it privilege Reynolds, Brandon
2008-04-14 16:32 ` Michael Potter
2008-04-14 16:45 ` Tavis Ormandy
2008-04-15  1:02   ` Reynolds, Brandon
2008-04-15  1:24     ` Michael Potter
  -- strict thread matches above, loose matches on Subject: below --
2008-01-22 20:00 Michael Potter
2008-01-22 20:09 ` Daniel Jacobowitz
2008-01-22 20:24   ` Michael Potter
2008-01-23 17:52 ` Mark Kettenis
2008-01-23 18:48   ` Michael Potter
2008-01-23 20:26     ` Michael Potter
2008-01-23 20:42     ` Andreas Schwab
2008-01-24  5:05       ` Michael Potter
2008-01-24  9:18         ` Andreas Schwab

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).