Skip to content

Instantly share code, notes, and snippets.

@sreehax
Last active July 10, 2017 00:02
Show Gist options
  • Save sreehax/c555f3342ee31ebab3bb9e4fe7ca10cb to your computer and use it in GitHub Desktop.
Save sreehax/c555f3342ee31ebab3bb9e4fe7ca10cb to your computer and use it in GitHub Desktop.
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
//Written by sreehari. Please do not rob my code!
/*
Copyright 2017 Sreehari
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
int main(int argc, char **argv) {
int uid = getuid();
if(0 != uid) {
printf("Error! You must be root to run chroot!\n");
exit(1);
}
int arg_c = argc -1;
if(arg_c < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
printf("Usage: chroot [rew root] [full command]\n");
exit(1);
}
char *newroot = argv[1];
char *newcommand = argv[2];
int newroot_status = chdir(newroot); //Attempt to prevent breaking out!
if(newroot_status == -1) {
printf("Error cannot chdir to %s\n", newroot);
exit(1);
}
int chroot_status = chroot(newroot);
if(chroot_status == -1) {
printf("Error cannot chroot!\n");
}
int j;
char *arg_v[arg_c-2];
for(j=2;j<arg_c;j++) {
arg_v[j-2] = argv[j];
}
execv(newcommand, arg_v);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment