home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / question / 10130 < prev    next >
Encoding:
Text File  |  1992-08-17  |  4.0 KB  |  133 lines

  1. Path: sparky!uunet!gatech!bloom-beacon!eru.mt.luth.se!lunic!sunic!aun.uninett.no!nuug!ifi.uio.no!gisle
  2. From: gisle@ifi.uio.no (Gisle Hannemyr)
  3. Newsgroups: comp.unix.questions
  4. Subject: Summary: Protecting against SIGKILL (kill -9).
  5. Message-ID: <GISLE.92Aug17183354@gyda.ifi.uio.no>
  6. Date: 17 Aug 92 17:33:53 GMT
  7. Sender: gisle@ifi.uio.no (Gisle Hannemyr)
  8. Organization: gisle@ifi.uio.no
  9. Lines: 120
  10. Nntp-Posting-Host: gyda.ifi.uio.no
  11. Originator: gisle@gyda.ifi.uio.no
  12.  
  13. Some time ago I posted the following question to comp.unix questions:
  14.  
  15. > Is there a way to protect a process from being killed by the user
  16. > who started it in a hostile environment.  I.e. is it possible to
  17. > create a program that can be started by anyone, but only killed
  18. > by root.
  19.  
  20. I received responses from the following 17 programmers:
  21.  
  22.     graham@castle.edinburgh.ac.uk
  23.     leo@ine.philips.nl
  24.     warren@itexjct.jct.ac.il (Warren Burstein)
  25.     cedman@714-725-3177.nts.uci.edu (Carl Edman)
  26.     Barry Margolin <barmar@Think.COM>
  27.     dvsc-a@minster.york.ac.uk
  28.     hello@cs.utwente.nl (Ronald Hello)
  29.     <rthomas@hakatac.almanac.bc.ca>
  30.     Robin Pickering <rob@inmos.co.uk>
  31.     matthews@oberon.umd.edu (Mike Matthews)
  32.     bof@midget.saar.de (John Bof)
  33.     amf@amfent.gwinnett.com (Andy Feibus)
  34.     Paul Foster <pfoster@gucis.cit.gu.edu.au>
  35.     fazc016@hq.dla.mil (Don Costello)
  36.     baur@mdcbbs.com
  37.     bjst@sth.frontec.se (Bjorn Stenberg)
  38.     bar@bbma.uucp (Joachim Bartsch)
  39.  
  40. Thank you very much!
  41.  
  42. Most noted this could be accomplished by having both the real and
  43. effective user ID of the process set to a different uid than the
  44. invoking user.  How you actually do this depends on what flavour
  45. of Unix you are running.
  46.  
  47. Below is the simplest method that worked on SunOS 4.1.1:
  48.  
  49. 1) Install signal handlers for all trappable signals.  This
  50.    only leaves SIGKILL to be dealt with specially.
  51. 2) Make the program call setuid(3) to set the real user ID equal to
  52.    the effective user ID.
  53. 3) Install the program setuid to user "daemon" (this will make
  54.    it run with the effective user id "daemon").
  55.  
  56. -----cut here-----------------------------------------------------------
  57. /* batman.c
  58. +-----------------------------------------------------------------------
  59. | Abstract:
  60. |    Example of program that can't be killed by invoking user.
  61. |    If installed setuid, this program can only be terminated by root
  62. |    sending SIGTERM ( # kill -TERM batman ).
  63. |
  64. | Environment:
  65. |    Tested on SunOS 4.1.1.  Portability unknown.
  66. |
  67. | Compilation:
  68. |    # gcc -o batman batman.c
  69. |    # chown daemon.daemon batman
  70. |    # chmod 4755 batman
  71. +---------------------------------------------------------------------*/
  72.  
  73. #include <stdio.h>
  74. #include <signal.h>
  75.  
  76. int signalhandler(int signo, int code, struct sigcontext *scp, char *addr)
  77. {
  78.     signal(signo, SIG_IGN); /* disable */
  79.     switch (signo) {
  80.       case SIGHUP:
  81.     fputs("Hup!!\n", stderr);
  82.     break;
  83.       case SIGINT:
  84.     fputs("Ouch!\n", stderr);
  85.     break;
  86.       case SIGQUIT:
  87.     fputs("It hurts!\n", stderr);
  88.     break;
  89.       default:
  90.     fprintf(stderr,"*** Unexpected SIG: %d %d\n",signo, code);
  91.     } /* switch */
  92.     (void) signal(signo, signalhandler);
  93.     return (0);
  94. } /* signalhandler */
  95.  
  96.  
  97. main()
  98. {
  99.     int uid, eid, oid;
  100.     char buffer[3];
  101.  
  102.     oid = getuid();
  103.     eid = geteuid();
  104.     if (setuid(eid)) fputs("*** setuid failed\n", stderr);
  105.     uid = getuid();
  106.     eid = geteuid();
  107.  
  108.     signal(SIGHUP,  signalhandler);    /* install handler */
  109.     signal(SIGINT,  signalhandler);    /* install handler */
  110.     signal(SIGQUIT, signalhandler);    /* install handler */
  111.  
  112.     printf("Started by user %d, \
  113. now real id = %d, effective id = %d\n", oid, uid, eid);
  114.  
  115.     fputs("Hah Joker -- can you kill me?\n", stdout);
  116.     for (;;) {
  117.     sleep(5);
  118.     fputs("... I'm waiting for you ...\n", stderr);
  119.     } /* forever */
  120.     /* NOTREACHED */
  121. } /* main */
  122.  
  123. /* EOF */
  124. --
  125. Disclaimer: My employer seldom even LISTENS to my opinion.
  126.  
  127. - gisle hannemyr  (Norsk Regnesentral)
  128.   OSI:   C=no;PRMD=uninett;O=nr;S=Hannemyr;G=Gisle (X.400 SA format)
  129.          gisle.hannemyr@nr.no                      (RFC-822  format)
  130.   Inet:  gisle@ifi.uio.no
  131.   UUCP:  ...!mcsun!ifi!gisle
  132. ------------------------------------------------
  133.