From 37eada407e6f9731eb2e5c1920e53b8e2c0abc3d Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 29 Jul 2012 23:53:56 +0200 Subject: [PATCH] Box: Re-open /proc/$PID/mem on exec This is necessary on recent kernels, because the fd is associated with a mm_struct, which changes upon exec(). --- box/box.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/box/box.c b/box/box.c index 71ec68c..7fe08a7 100644 --- a/box/box.c +++ b/box/box.c @@ -623,21 +623,30 @@ struct syscall_args { struct user user; }; +static int user_mem_fd; + static int read_user_mem(arg_t addr, char *buf, int len) { - static int mem_fd; - - if (!mem_fd) + if (!user_mem_fd) { char memname[64]; sprintf(memname, "/proc/%d/mem", (int) box_pid); - mem_fd = open(memname, O_RDONLY); - if (mem_fd < 0) + user_mem_fd = open(memname, O_RDONLY); + if (user_mem_fd < 0) die("open(%s): %m", memname); } - if (lseek64(mem_fd, addr, SEEK_SET) < 0) + if (lseek64(user_mem_fd, addr, SEEK_SET) < 0) die("lseek64(mem): %m"); - return read(mem_fd, buf, len); + return read(user_mem_fd, buf, len); +} + +static void close_user_mem(void) +{ + if (user_mem_fd) + { + close(user_mem_fd); + user_mem_fd = 0; + } } #ifdef CONFIG_BOX_KERNEL_AMD64 @@ -1109,7 +1118,10 @@ boxkeeper(void) { msg("[master] "); if (sys == NATIVE_NR_execve) - exec_seen = 1; + { + exec_seen = 1; + close_user_mem(); + } } else if ((act = valid_syscall(&a)) >= 0) { -- 2.39.2