linux-um archives
 help / color / mirror / Atom feed
From: "Anthony Brock" <brocka@sterlingcgi.com>
To: user-mode-linux-devel@lists.sourceforge.net
Cc: Blaisorblade <blaisorblade@yahoo.it>
Subject: RE: [uml-devel] Re: pcap cross-linking [PATCH]
Date: Sun, 25 Dec 2005 23:47:56 -0800	[thread overview]
Message-ID: <LEEIJGBJEEABAGOFFGAHMEINDDAA.brocka@sterlingcgi.com> (raw)
In-Reply-To: <200512231711.27326.blaisorblade@yahoo.it>

[-- Attachment #1: Type: text/plain, Size: 837 bytes --]

> On Friday December 23, 2005 at 8:11 AM, Blaisorblade wrote:
> Only one minor complaint: you do the following sequence (excluding error
> paths):
>
> 	chdir(chroot_dir);
> 	chroot(chroot_dir);
>
> this will fail needlessly if a relative path is used.

Thanks! This hasn't been an issue since we always use full paths in our
environment. However, it's definitely a bug. I'm attaching a version with
your suggested change.

> Replacing the chroot with chroot(".") would be better, IMHO.
>
> One question: would
> 	chroot(chroot_dir);
> 	chdir("/");
> work equally well?
> I've not seen it used so I wonder (a bit) if there can be some hidden bug.

I'm more comfortable creating a chroot where the "jailed" process never has
an external file reference. However, I don't see why this wouldn't work.
Logically, it should be identical.

Tony

[-- Attachment #2: chrootuidgid.c --]
[-- Type: application/octet-stream, Size: 3553 bytes --]

#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/param.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

#define TMP_PATH "/dev/shm"
#define PATH "PATH"
int main(int ac, char **av)
{
	char *param[ac+1];
	char *uname = NULL;
	char *chroot_dir = NULL;
	char *path = NULL;
	struct passwd *userinfo;
	int c, i;
	int nice_val = 0;
	uid_t uid = 0;
	gid_t gid = 0;

	opterr = 0;

	while ((c = getopt(ac, av, "+u:n:p:")) != -1)
		switch (c)
		{
			case 'u':
				uname = optarg;
				break;
			case 'n':
				nice_val = atoi(optarg);
				break;
			case 'p':
				chroot_dir = optarg;
				break;
			case '?':
				if (isprint (optopt))
					fprintf (stderr, "Unknown option `-%c'.\n", optopt);
				else
					fprintf (stderr,
						 "Unknown option character `\\x%x'.\n",
						  optopt);
				return 1;
			default:
				abort();
		}

	if ((uname != NULL) && (chroot_dir != NULL))
	{
		if ((userinfo = getpwnam(uname)) != NULL)
		{
			uid = userinfo->pw_uid;
			gid = userinfo->pw_gid;

			// Adjust the process priority by the requested "nice" value
			if(nice(nice_val) == -1)
			{
				fprintf(stderr, "error changing 'nice' value to %i: %s\n", nice_val, strerror(errno));
				return 1;
			}

			// Abort if we were passed user "root"
			if (uid == 0)
			{
				fprintf (stderr, "Specified account must NOT be root!\n");
				return 1;
			}

			// Clear any existing environment
			path = getenv(PATH);
			if (clearenv())
			{
				fprintf(stderr, "WARNING: unable to clear environment!\n");
			}

			// Setup the command environment
			setenv("HOME", userinfo->pw_dir, 1);
			setenv(PATH, path, 1);
			setenv("TMP", TMP_PATH, 1);
			setenv("USER", userinfo->pw_name, 1);

			// Our first parameter should be the command we're to execute
			// Therefore, we assign this to element '0' of our parameters to pass
			for (i = 0, c = optind; c < ac; i++, c++)
			{
				param[i] = av[c];
			}

			// Abort if we have not other parameters
			if (i == 0)
			{
				fprintf (stderr, "You must specify a command to execute!\n");
				return 1;
			}

			// The final element in the array MUST be a NULL pointer
			param[i] = NULL;

			// First, we need to CHDIR to the CHROOT directory
			if(chdir(chroot_dir) == -1)
			{
				fprintf(stderr, "chdir to '%s' failed: %s\n", chroot_dir, strerror(errno));
				return 3;
			}

			// Next, we need to CHROOT ourselves (while we're still root)
			if(chroot(".") == -1)
			{
				fprintf(stderr, "chroot to '%s' failed: %s\n", chroot_dir, strerror(errno));
				return 3;
			}

			// Now CHDIR to the accounts home directory. This is a non-fatal error.
			if(chdir(userinfo->pw_dir) == -1)
			{
				fprintf(stderr, "WARNING: chdir to home directory '%s' failed: %s\n", userinfo->pw_dir, strerror(errno));
			}

			if(setregid(gid, gid) == -1)
			{
				fprintf(stderr, "setregid to gid %i failed: %s\n", gid, strerror(errno));
				return 3;
			}

			if(setreuid(uid, uid) == -1)
			{
				fprintf(stderr, "setreuid to uid %i failed: %s\n", uid, strerror(errno));
				return 3;
			}

			if(execvp(param[0], param) == -1)
			{
				fprintf(stderr, "exec for '%s' failed: %s\n", param[0], strerror(errno));
				return 4;
			}
		}
		else
		{
			fprintf(stderr, "User '%s' not found\n", uname);
			return 2;
		}
	}
	else
	{
		fprintf(stderr, "You must specify a valid account with the '-u' option and a valid chroot path with the '-p' option!\n");
		return 1;
	}
}

  reply	other threads:[~2005-12-26  7:48 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-18 19:03 [uml-devel] pcap cross-linking Antoine Martin
2005-12-19 16:16 ` [uml-devel] " Blaisorblade
2005-12-19 18:33   ` [uml-devel] Re: pcap cross-linking [PATCH] Antoine Martin
2005-12-19 19:27     ` Blaisorblade
2005-12-19 21:47       ` Antoine Martin
2005-12-20 14:23         ` Blaisorblade
2005-12-20 16:25           ` Antoine Martin
2005-12-20 19:24             ` Blaisorblade
2005-12-20 20:01     ` Rob Landley
2005-12-20 20:24       ` Antoine Martin
2005-12-20 20:43       ` Blaisorblade
2005-12-21 18:13         ` Blaisorblade
2005-12-22 17:57           ` Anthony Brock
2005-12-23 16:11             ` Blaisorblade
2005-12-26  7:47               ` Anthony Brock [this message]
2005-12-29 20:12               ` Rob Landley
2006-01-01 18:51                 ` Blaisorblade
2006-01-01 21:01                   ` Rob Landley
2006-01-02 20:10                     ` Blaisorblade
2005-12-19 18:39   ` [uml-devel] Re: pcap cross-linking Blaisorblade

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=LEEIJGBJEEABAGOFFGAHMEINDDAA.brocka@sterlingcgi.com \
    --to=brocka@sterlingcgi.com \
    --cc=blaisorblade@yahoo.it \
    --cc=user-mode-linux-devel@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox