euglug.org

[Eug-lug] setuid programs

작성자

카테고리:



Bob MillerFri Feb 18 15:22:37 PST 2005

Jim Beard wrote:

> The C code looks like this:
> 
> #include <unistd.h>
> 
> int main () {
>   execl("/bin/chown", "-R server /var/cvs/repository");
>   execl("/bin/chgrp", "-R", "server", "/var/cvs/repository");
>   execl("/bin/chmod", "-R o+w /var/cvs/repository");
> }
> 
> Anyone know why this isn't working?

That program has a remarkable bug density.

1. execl() doesn't return.  You need to fork before calling execl().
   Or, better, call system().

2. execl()'s parameter list must be terminated by a NULL.

3. You've left out the first argument in the argument list.
   argv[0] is the name of the program.  So, instead of this,

	execl("/bin/chgrp", "-R", "server", "/var/cvs/repository");

    you want this, with "/bin/chgrp" twice.

	execl("/bin/chgrp", "/bin/chgrp", "-R", "server",
		"/var/cvs/repository", NULL);

4. In the chown and chmod instances, you need to separate the program's
   arguments into separate C arguments.  You did this correctly
   for the chgrp instance.  (Note, if you use system(), you
   pass the whole command as a single string.)

5. You never checked execl's return for an error.  Since execl()
   doesn't return if it works, ANY return is an error.  But print or
   log a message so you have a chance of finding out what's going on.

6. You can combine the chgrp pass into chown with this syntax.

	# chown -R user:group directory

   In C, that'd be...

	execl("/bin/chown",
	      "/bin/chown", "-R", "server:server",
	      "/var/cvs/repository", NULL);

Here's a revised version.  I'm not going to compile this, let alone
test it, so it probably doesn't work.  Caveat emptor.

	#include <stdlib.h>
	#include <syslog.h>

	void exec_or_log(const char *command)
	{
	    int status;

	    status = system(command);
	    if (status != 0) {
		openlog("mycommand", 0, LOG_DAEMON);
		syslog(LOG_ERR,
			 "command \"%s\" failed, exit status %d\n", status);
		closelog();
	    }
	}

	int main()
	{
	    int status;

	    exec_or_log("/bin/chown -R server:server /var/cvs/repository");
	    exec_or_log("/bin/chmod -R o+w /var/cvs/repository");
	    return 0;
	}

Even better would be to capture the commands' standard error and log
them too.

> Anyone have a better solution to my problem?

Take a look at this CVS info page.  It says that your ,v files should
not be writable.

	$ info '(cvs)File permissions'

There are two CVS books on line, as well.

	http://cvsbook.red-bean.com/
	https://www.cvshome.org/docs/manual/

-- 
Bob Miller                              K<bob>
kbobsoft software consulting
http://kbobsoft.com                     kbob at jogger-egg.com