Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chroot + execvp + bash

Tags:

bash

chroot

Update

Got it! See my solution (fifth comment)

Here is my problem:

I have created a small binary called "jail" and in /etc/password I have made it the default shell for a test user.

Here is the -- simplified -- source code:

#define HOME "/home/user"
#define SHELL "/bin/bash"
...
if(chdir(HOME) || chroot(HOME)) return -1;
...
char *shellargv[] = { SHELL, "-login", "-rcfile", "/bin/myscript", 0 };
execvp(SHELL, shellargv);

Well, no matter how hard I try, it seems that, when my test user logs in, /bin/myscript will never be sourced. Similarly, if I drop a .bashrc file in user's home directory, it will be ignored as well.

Why would bash snob these guys?

--

Some precisions, not necessarily relevant, but to clear out some of the points made in the comments:

  • The 'jail' binary is actually suid, thus allowing it to chroot() successfully.
  • I have used 'ln' to make the appropriate binaries available - my jail cell is nicely padded :)
  • The issue does not seem to be with chrooting the user...something else is remiss.
like image 844
Fusion Avatar asked Oct 31 '08 18:10

Fusion


3 Answers

As Jason C says, the exec'ed shell isn't interactive.

His solution will force the shell to be interactive if it accepts -i to mean that (and bash does):

char *shellargv[] = { SHELL, "-i", "-login", ... };
execvp(SHELL, shellargv);

I want to add, though, that traditionally a shell will act as a login shell if ARGV[0] begins with a dash.

char *shellargv[] = {"-"SHELL, "-i", ...};
execvp(SHELL, shellargv);

Usually, though, Bash will autodetect whether it should run interactively or not. Its failure to in your case may be because of missing /dev/* nodes.

like image 196
ephemient Avatar answered Oct 30 '22 03:10

ephemient


The shell isn't interactive. Try adding -i to the list of arguments.

like image 42
dexedrine Avatar answered Oct 30 '22 05:10

dexedrine


I can identify with wanting to do this yourself, but if you haven't already, check out jail chroot project and jailkit for some drop in tools to create a jail shell.

like image 44
Damon Snyder Avatar answered Oct 30 '22 03:10

Damon Snyder