Skip to content

Commit

Permalink
Set hostname inside namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
whokilleddb committed Feb 17, 2022
1 parent 244255b commit 2b4a401
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion include/networkns.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ int interface_up(char *ifname, char *ip, char *netmask, short if_flags);
int ns_fd(int pid);
int add_route();
int create_veth(int child_pid);
#endif
#endif
5 changes: 5 additions & 0 deletions include/uts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#ifndef __UTS_NS
#define __UTS_NS
#define HOSTNAME "isolate" // Hostname to be used inside the new namespace
#endif
41 changes: 41 additions & 0 deletions src/isolate.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

// User-defined headers
#include "utils.h"
#include "uts.h"
#include "mountns.h"
#include "userns.h"
#include "networkns.h"
Expand Down Expand Up @@ -49,11 +50,39 @@ void print_isolated_cmd(int argc, char **argv){
fprintf(stdout,"\n");
}


// Set hostname and prepare /etc/hostname
int configure_hostname(){
// Set hostname for new namespace
if (sethostname(HOSTNAME,sizeof(HOSTNAME)) != 0){
fprintf(stderr,"["RED("!")"] Failed to set Hostname with "RED("sethostname()")"\n");
return -1;
}

// Write to /etc/hostname
FILE *fp = fopen("/etc/hostname","w");
if(fp == NULL){
fprintf(stderr,"["RED("!")"] Failed open "RED("/etc/hostname")" for writing\n");
return -1;
}
if(fprintf(fp,"%s\n",HOSTNAME) < 0){
fprintf(stderr,"["RED("!")"] Failed to write to "RED("/etc/hostname")"\n");
fclose(fp);
return -1;
}
if (fclose(fp) != 0){
fprintf(stderr,"["RED("!")"] Failed to close "RED("/etc/hostname")" after writing\n");
return -1;
}
return 0;
}

// Child process to be called to run a command
int cmd_exec(void *arg){
// Send a SIGKILL if the isolated process dies
if (prctl(PR_SET_PDEATHSIG, SIGKILL)<0){
fprintf(stderr,"[" RED("!") "] Cannot Set" RED("prctl()")"\n");
exit(EXIT_FAILURE);
return -1;
}

Expand All @@ -64,25 +93,36 @@ int cmd_exec(void *arg){
char buf[2];
if(read(params->fd[0],buf,2)!= 2){
fprintf(stderr, "[" RED("!") "] Failed to read from pipe while awaiting "RED("'setup done'")" from main");
exit(EXIT_FAILURE);
return -1;
}

// Prepare MOUNT namespace
if (prepare_mountns() != 0){
fprintf(stderr,"[" RED("!") "] Failed to create "RED("MOUNT") " namespace\n");
exit(EXIT_FAILURE);
return -1;
}
fprintf(stdout,"[" GREEN("i") "] Successfully created " GREEN("MOUNT") " namespace\n");

// Set hostname for new namespace
if (configure_hostname() != 0){
fprintf(stderr,"["RED("!")"] Could not set hostname inside new namespace\n");
exit(EXIT_FAILURE);
return -1;
}

// Close reading end of the pipe once done
if(close(params->fd[0])){
fprintf(stderr, "[" RED("!") "] Failed to close pipe\n");
exit(EXIT_FAILURE);
return -1;
}

// Drop superuser privileges
if ((setuid(0)==-1) || setgid(0) == -1){
fprintf(stderr, "[" RED("!") "] Could not set privileges\n");
exit(EXIT_FAILURE);
return -1;
}

Expand All @@ -91,6 +131,7 @@ int cmd_exec(void *arg){

if (execvp(cmd,argv)==-1){
fprintf(stderr,"[" RED("!")"] Cannot execute command in Isolation :(\n");
exit(EXIT_FAILURE);
return -1;
}

Expand Down
6 changes: 4 additions & 2 deletions src/networkns.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ int prepare_networkns(int child_pid){
fprintf(stderr, "["RED("!")"] Could not setup %s interface\n", VETH1);
return -1;
}

// Add route via default gateway inside namespace
if (add_route() != 0){
fprintf(stderr, "["RED("!")"] Failed to add route\n");
return -1;
Expand All @@ -205,8 +207,8 @@ int prepare_networkns(int child_pid){
fprintf(stderr,"["RED("!")"] Failed to move thread back to host namespace\n");
return -1;
}

close(host_fd);
close(child_fd);
return 0;
}
}

0 comments on commit 2b4a401

Please sign in to comment.