home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / question / 9411 < prev    next >
Encoding:
Text File  |  1992-07-25  |  3.8 KB  |  124 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!sdd.hp.com!elroy.jpl.nasa.gov!ucla-cs!ucla-ma!luna!zinar
  2. From: zinar@luna.math.ucla.edu (Richard Zinar)
  3. Newsgroups: comp.unix.questions
  4. Subject: Setgid confusion
  5. Message-ID: <1992Jul24.212249.22921@math.ucla.edu>
  6. Date: 24 Jul 92 21:22:49 GMT
  7. Sender: news@math.ucla.edu
  8. Organization: UCLA Mathematics Department
  9. Lines: 113
  10.  
  11. Hello ...
  12.  
  13.   We're experiencing a problem with a locally developed piece of
  14.   code which makes use of the "setuid" and "setgid" features of UNIX.
  15.   I've enclosed a program which recreates the problem.  The executable
  16.   program has the following settings:
  17.  
  18.         owner:  submit (uid 56)
  19.         group:  turnin (gid 22)
  20.         perms:  6711 (-rws--s--x)
  21.  
  22.   and is run with the current directory having:
  23.  
  24.         owner:   p10aaedh (uid 3145)
  25.         group:   student  (gid 20)
  26.         perms:   705 (drwx---r-x)
  27.  
  28.   The getwd() call fails, and produces the message "can't stat .", 
  29.   and the following stat () call also fails.
  30.  
  31.   The program is running on SunOS 4.1.1 on a SparcStation 2.
  32.   The program worked successfully on our old machines running SunOS 3.5,
  33.   but reviewing the documentation in "System Services Overview" didn't
  34.   shed any light on why it's no longer working.  The printed real and
  35.   effective UIDs and GIDs have the values I would expect to see, but 
  36.   it doesn't seem to affect the outcome of the getwd() call.
  37.  
  38.   The file system in which the directory passed to stat() is
  39.   located has mount options "rw,quota,grpid" (and "suid" is documented
  40.   as a default option).  Note that the program is NOT trying to access
  41.   an NFS-mounted file as root (or so I believe, anyway).
  42.  
  43. The program follows:
  44.  
  45.    #include <sys/param.h>
  46.    #include <sys/types.h>
  47.    #include <sys/stat.h>
  48.    #include <stdio.h>
  49.    #include <errno.h>
  50.    
  51.    main ()
  52.    {
  53.       char         pname[MAXPATHLEN];
  54.       struct stat statbuf;
  55.    
  56.       int  real_uid, eff_uid;
  57.       int  real_gid, eff_gid;
  58.    
  59.       extern char *getwd ();
  60.    
  61.    
  62.       real_uid = getuid ();
  63.       real_gid = getgid ();
  64.       eff_uid  = geteuid ();
  65.       eff_gid  = getegid ();
  66.    
  67.       printf ("Real uid: %d\n", real_uid);
  68.       printf ("Real gid: %d\n", real_gid);
  69.       printf ("Effective uid: %d\n", eff_uid);
  70.       printf ("Effective gid: %d\n", eff_gid);
  71.    
  72.       setgid (getegid ());
  73.       setuid (geteuid ());
  74.    
  75.       real_uid = getuid ();
  76.       real_gid = getgid ();
  77.       eff_uid  = geteuid ();
  78.       eff_gid  = getegid ();
  79.    
  80.       printf ("Real uid: %d\n", real_uid);
  81.       printf ("Real gid: %d\n", real_gid);
  82.       printf ("Effective uid: %d\n", eff_uid);
  83.       printf ("Effective gid: %d\n", eff_gid);
  84.    
  85.       if ( getwd (pname) == (char *) 0 )
  86.          printf ("Error: %s\n", pname);
  87.       else
  88.          printf ("Current directory: %s\n", pname);
  89.    
  90.       if ( stat (".", &statbuf) != 0 )
  91.          perror ("Stat call failed");
  92.       else  {
  93.          printf ("Current directory's uid: %d\n", statbuf.st_uid);
  94.          printf ("Current directory's gid: %d\n", statbuf.st_gid);
  95.       }
  96.    
  97.       exit (0);
  98.    }
  99.  
  100.  
  101. The  program output is as follows:
  102.  
  103.    Real uid: 3145
  104.    Real gid: 20
  105.    Effective uid: 56
  106.    Effective gid: 22
  107.    Real uid: 56
  108.    Real gid: 22
  109.    Effective uid: 56
  110.    Effective gid: 22
  111.    Error: getwd: can't stat .
  112.    Stat call failed: Permission denied
  113.  
  114.    In an edited version of the above test program, I used the getgroups()
  115.    system call to check the supplementary group ID(s) after the setuid and
  116.    setgid calls had been made, and found that it contained only
  117.    gid 20 (student), and not gid 22 (turnin), which seemed surprising.
  118.    Any insights into this behaviour would be greatly appreciated ...
  119.  
  120.                            Richard Zinar
  121.                                       Computing Support Staff
  122.                                       UCLA Mathematics Department
  123.                                       Email: zinar@math.ucla.edu
  124.