Add mount namespace mode

This commit is contained in:
topjohnwu 2017-06-08 22:56:21 +08:00
parent 201e32d4c4
commit 0b4baad78b
5 changed files with 21 additions and 10 deletions

@ -1 +1 @@
Subproject commit bc576a9659651703dc4cd4eaebaa9293381a2ac8
Subproject commit d3ff482c9b46f58626fdcb6289ffa0cefbf2afcc

View File

@ -56,7 +56,6 @@ int hide_daemon() {
// When an error occurs, report its failure to main process
err_handler = hide_daemon_err;
int fd;
char buffer[4096], cache_block[256], *line;
struct vector mount_list;
@ -73,13 +72,8 @@ int hide_daemon() {
manage_selinux();
relink_sbin();
snprintf(buffer, sizeof(buffer), "/proc/%d/ns/mnt", pid);
if(access(buffer, F_OK) == -1) continue; // Maybe process died..
fd = xopen(buffer, O_RDONLY);
// Switch to its namespace
xsetns(fd, 0);
close(fd);
if (switch_mnt_ns(pid))
continue;
snprintf(buffer, sizeof(buffer), "/proc/%d/mounts", pid);
vec_init(&mount_list);

2
jni/su

@ -1 +1 @@
Subproject commit 38cc9dfec88bca246725d79157657e43e9363425
Subproject commit 1bbf96a0a7a942798df58883a376b8024d0fae7f

View File

@ -1,6 +1,7 @@
/* misc.c - Store all functions that are unable to be catagorized clearly
*/
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
@ -10,6 +11,7 @@
#include <pwd.h>
#include <signal.h>
#include <errno.h>
#include <sched.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
@ -457,3 +459,17 @@ int resize_img(const char *img, int size) {
snprintf(buffer, PATH_MAX, "e2fsck -yf %s; resize2fs %s %dM;", img, img, size);
return system(buffer);
}
int switch_mnt_ns(int pid) {
char mnt[32];
snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid);
if(access(mnt, R_OK) == -1) return 1; // Maybe process died..
int fd, ret;
fd = xopen(mnt, O_RDONLY);
if (fd < 0) return 1;
// Switch to its namespace
ret = setns(fd, 0);
close(fd);
return ret;
}

View File

@ -94,5 +94,6 @@ void get_client_cred(int fd, struct ucred *cred);
int create_img(const char *img, int size);
int get_img_size(const char *img, int *used, int *total);
int resize_img(const char *img, int size);
int switch_mnt_ns(int pid);
#endif