General Unix/C question

Leo de Wit leo at philmds.UUCP
Thu Sep 8 02:50:10 AEST 1988


In article <641 at jura.tcom.stc.co.uk> john at tcom.stc.co.uk (John Blair) writes:
|
|Below is the program I use to run a unix command under the userid of the
|program owner.
|However when the command passed is Make with parameters and 
|stdout and stderr file redirection I sometimes get segmentation
|problems and core dumped.
|
|If I "su" as the userid of the program owner
|and execute the same Make command I do not get segmentation problems.
|What is wrong with the C program?
|
|/* note, this must run with set uid on execution */
|main(argc,argv)
| int argc;
| char * argv[];
|{
| setuid(geteuid());
| setgid(getegid());
| execvp(argv[1],argv+1);
|}

Two things: 1) the arg count should be checked to be >= 2 (I don't think
execvp likes a null parameter).
            2) when you start the program the input/output redirection is
done, perhaps by your shell. This means that these input/output streams
must be readable/writable by the current uid/gid; when you setuid &
setgid the process may not be allowed anymore to write these
descriptors.  Suggestion: either explicitly allow access to the
stdout/stderr (e.g.  create a file, chmod to correct permissions and do
redirection by appending) or do a fchmod(fileno(stdin),mode) in the
program; this you will have to do probably in a forked child which has
reset the euid/egid to the original value, as only a file owner can
change the mode (fork the child before the setuid/setgid, otherwise you
cannot it change it back anymore).

Hope this helps -
                      Leo.



More information about the Comp.unix.questions mailing list