2011-09-23 12:00:45 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
2012-04-13 17:52:35 +04:00
|
|
|
#include <sys/sendfile.h>
|
2011-09-23 12:00:45 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2011-12-19 21:05:02 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2011-09-23 12:00:45 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/wait.h>
|
2011-12-05 12:07:52 +04:00
|
|
|
#include <sys/resource.h>
|
2011-12-19 21:05:02 +04:00
|
|
|
#include <sys/wait.h>
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
#include "compiler.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include "crtools.h"
|
|
|
|
|
2012-03-06 14:20:00 +04:00
|
|
|
void pr_vma(unsigned int loglevel, const struct vma_area *vma_area)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
|
|
|
if (!vma_area)
|
|
|
|
return;
|
|
|
|
|
2012-04-13 19:44:00 +04:00
|
|
|
print_on_level(loglevel, "s: 0x%16lx e: 0x%16lx l: %8liK p: 0x%8x f: 0x%8x pg: 0x%8lx "
|
|
|
|
"vf: %s st: %s spc: %-8s shmid: 0x%8lx\n",
|
2012-03-02 00:59:59 +04:00
|
|
|
vma_area->vma.start, vma_area->vma.end,
|
|
|
|
KBYTES(vma_area_len(vma_area)),
|
|
|
|
vma_area->vma.prot,
|
|
|
|
vma_area->vma.flags,
|
|
|
|
vma_area->vma.pgoff,
|
|
|
|
vma_area->vm_file_fd < 0 ? "n" : "y",
|
|
|
|
!vma_area->vma.status ? "--" :
|
|
|
|
((vma_area->vma.status & VMA_FILE_PRIVATE) ? "FP" :
|
|
|
|
((vma_area->vma.status & VMA_FILE_SHARED) ? "FS" :
|
|
|
|
((vma_area->vma.status & VMA_ANON_SHARED) ? "AS" :
|
|
|
|
((vma_area->vma.status & VMA_ANON_PRIVATE) ? "AP" : "--")))),
|
|
|
|
!vma_area->vma.status ? "--" :
|
|
|
|
((vma_area->vma.status & VMA_AREA_STACK) ? "stack" :
|
|
|
|
((vma_area->vma.status & VMA_AREA_HEAP) ? "heap" :
|
|
|
|
((vma_area->vma.status & VMA_AREA_VSYSCALL) ? "vsyscall" :
|
2012-03-21 10:12:00 +04:00
|
|
|
((vma_area->vma.status & VMA_AREA_VDSO) ? "vdso" : "n")))),
|
|
|
|
vma_area->vma.shmid);
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 18:28:09 +04:00
|
|
|
int close_safe(int *fd)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
if (*fd > -1) {
|
|
|
|
ret = close(*fd);
|
|
|
|
if (!ret)
|
|
|
|
*fd = -1;
|
|
|
|
else
|
2012-02-01 02:08:04 +04:00
|
|
|
pr_perror("Unable to close fd %d", *fd);
|
2011-09-27 18:28:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-12 19:50:00 +04:00
|
|
|
int reopen_fd_as_safe(char *file, int line, int new_fd, int old_fd, bool allow_reuse_fd)
|
2011-09-23 12:00:45 +04:00
|
|
|
{
|
2012-01-13 13:15:57 +04:00
|
|
|
int tmp;
|
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
if (old_fd != new_fd) {
|
2012-01-13 13:15:57 +04:00
|
|
|
|
|
|
|
if (!allow_reuse_fd) {
|
|
|
|
if (fcntl(new_fd, F_GETFD) != -1 || errno != EBADF) {
|
|
|
|
if (new_fd < 3) {
|
|
|
|
/*
|
2012-02-01 02:08:04 +04:00
|
|
|
* Standard descriptors.
|
2012-01-13 13:15:57 +04:00
|
|
|
*/
|
2012-04-12 19:50:00 +04:00
|
|
|
pr_warn("fd %d already in use (called at %s:%d)\n",
|
|
|
|
new_fd, file, line);
|
2012-01-13 13:15:57 +04:00
|
|
|
} else {
|
2012-04-12 19:50:00 +04:00
|
|
|
pr_err("fd %d already in use (called at %s:%d)\n",
|
|
|
|
new_fd, file, line);
|
2012-01-13 13:15:57 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-01-10 16:41:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = dup2(old_fd, new_fd);
|
2011-09-27 18:39:56 +04:00
|
|
|
if (tmp < 0) {
|
2012-04-12 19:50:00 +04:00
|
|
|
pr_perror("Dup %d -> %d failed (called at %s:%d)",
|
|
|
|
old_fd, new_fd, file, line);
|
2011-09-23 12:00:45 +04:00
|
|
|
return tmp;
|
2011-09-27 18:39:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Just to have error message if failed */
|
|
|
|
close_safe(&old_fd);
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-12-26 21:47:00 +04:00
|
|
|
return 0;
|
2011-09-23 12:00:45 +04:00
|
|
|
}
|
|
|
|
|
2011-12-26 21:48:00 +04:00
|
|
|
int move_img_fd(int *img_fd, int want_fd)
|
|
|
|
{
|
|
|
|
if (*img_fd == want_fd) {
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
tmp = dup(*img_fd);
|
|
|
|
if (tmp < 0) {
|
2012-01-31 15:13:05 +04:00
|
|
|
pr_perror("Can't dup file");
|
2011-12-26 21:48:00 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-10 16:41:00 +04:00
|
|
|
close(*img_fd);
|
|
|
|
|
2011-12-26 21:48:00 +04:00
|
|
|
*img_fd = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-06 21:39:10 +04:00
|
|
|
static int image_dir_fd = -1;
|
|
|
|
|
2012-03-19 15:38:00 +04:00
|
|
|
int open_image(int type, unsigned long flags, ...)
|
2011-12-29 19:56:34 +04:00
|
|
|
{
|
2012-03-19 15:38:00 +04:00
|
|
|
char path[PATH_MAX];
|
|
|
|
va_list args;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(args, flags);
|
|
|
|
vsnprintf(path, PATH_MAX, fdset_template[type].fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (flags & O_EXCL) {
|
|
|
|
ret = unlinkat(image_dir_fd, path, 0);
|
|
|
|
if (ret && errno != ENOENT) {
|
|
|
|
pr_perror("Unable to unlink %s", path);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2011-10-24 13:43:21 +04:00
|
|
|
|
2012-03-19 15:38:00 +04:00
|
|
|
ret = openat(image_dir_fd, path, flags, CR_FD_PERM);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Unable to open %s", path);
|
|
|
|
goto err;
|
|
|
|
}
|
2011-12-29 19:56:34 +04:00
|
|
|
|
2012-03-19 15:38:00 +04:00
|
|
|
if (flags == O_RDONLY) {
|
|
|
|
u32 magic;
|
|
|
|
|
|
|
|
if (read_img(ret, &magic) < 0)
|
|
|
|
goto err;
|
|
|
|
if (magic != fdset_template[type].magic) {
|
|
|
|
pr_err("Magic doesn't match for %s\n", path);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (write_img(ret, &fdset_template[type].magic))
|
|
|
|
goto err;
|
2011-12-29 19:56:34 +04:00
|
|
|
}
|
|
|
|
|
2012-03-19 15:38:00 +04:00
|
|
|
return ret;
|
|
|
|
err:
|
|
|
|
return -1;
|
2011-10-24 13:43:21 +04:00
|
|
|
}
|
2012-01-12 23:50:45 +04:00
|
|
|
|
2012-03-16 17:24:00 +04:00
|
|
|
int open_image_dir(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
image_dir_fd = get_service_fd(IMG_FD_OFF);
|
|
|
|
if (image_dir_fd < 0) {
|
|
|
|
pr_perror("Can't get image fd");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(".", O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
pr_perror("Can't open cwd");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_info("Image dir fd is %d\n", image_dir_fd);
|
|
|
|
|
|
|
|
return reopen_fd_as(image_dir_fd, fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_image_dir(void)
|
|
|
|
{
|
|
|
|
close(image_dir_fd);
|
|
|
|
image_dir_fd = -1;
|
|
|
|
}
|
|
|
|
|
2012-02-17 01:39:36 +04:00
|
|
|
static pid_t open_proc_pid = 0;
|
|
|
|
static int open_proc_fd = -1;
|
|
|
|
|
|
|
|
int close_pid_proc(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (open_proc_fd >= 0)
|
|
|
|
ret = close(open_proc_fd);
|
|
|
|
|
|
|
|
open_proc_fd = -1;
|
|
|
|
open_proc_pid = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int open_pid_proc(pid_t pid)
|
2012-01-12 23:50:45 +04:00
|
|
|
{
|
|
|
|
char path[18];
|
|
|
|
int fd;
|
|
|
|
|
2012-02-17 01:39:36 +04:00
|
|
|
if (pid == open_proc_pid)
|
|
|
|
return open_proc_fd;
|
|
|
|
|
|
|
|
close_pid_proc();
|
2012-01-12 23:50:45 +04:00
|
|
|
sprintf(path, "/proc/%d", pid);
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2012-01-31 15:13:05 +04:00
|
|
|
pr_perror("Can't open %s", path);
|
2012-02-17 01:39:36 +04:00
|
|
|
else {
|
|
|
|
open_proc_fd = fd;
|
|
|
|
open_proc_pid = pid;
|
|
|
|
}
|
|
|
|
|
2012-01-12 23:50:45 +04:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2012-02-17 01:39:36 +04:00
|
|
|
int do_open_proc(pid_t pid, int flags, const char *fmt, ...)
|
2012-01-12 23:50:45 +04:00
|
|
|
{
|
2012-02-17 01:39:35 +04:00
|
|
|
char path[128];
|
|
|
|
va_list args;
|
2012-02-17 01:39:36 +04:00
|
|
|
int dirfd = open_pid_proc(pid);
|
|
|
|
|
|
|
|
if (dirfd < 0)
|
|
|
|
return -1;
|
2012-01-12 23:50:45 +04:00
|
|
|
|
2012-02-17 01:39:35 +04:00
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(path, sizeof(path), fmt, args);
|
|
|
|
va_end(args);
|
2012-01-12 23:50:45 +04:00
|
|
|
|
2012-02-17 01:39:35 +04:00
|
|
|
return openat(dirfd, path, flags);
|
2012-01-12 23:50:45 +04:00
|
|
|
}
|
2012-03-16 17:21:00 +04:00
|
|
|
|
|
|
|
int get_service_fd(int type)
|
|
|
|
{
|
|
|
|
struct rlimit rlimit;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Service FDs are thouse that most likely won't
|
|
|
|
* conflict with any 'real-life' ones
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rlimit)) {
|
|
|
|
pr_perror("Can't get rlimit");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rlimit.rlim_cur - type;
|
|
|
|
}
|
2012-04-13 17:52:35 +04:00
|
|
|
|
|
|
|
int copy_file(int fd_in, int fd_out, size_t bytes)
|
|
|
|
{
|
|
|
|
ssize_t written = 0;
|
|
|
|
size_t chunk = bytes ? bytes : 4096;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = sendfile(fd_out, fd_in, NULL, chunk);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_perror("Can't send data to ghost file");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
if (bytes && (written != bytes)) {
|
|
|
|
pr_err("Ghost file size mismatch %lu/%lu\n",
|
|
|
|
written, bytes);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
written += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|