Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Gautier <[email protected]>
  • Loading branch information
Arthur Gautier committed Sep 28, 2015
0 parents commit 2dd97d3
Show file tree
Hide file tree
Showing 17 changed files with 1,749 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
AlwaysBreakAfterDefinitionReturnType: true
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: Always
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/common.o
/platform/linux/linux.o
/platform/linux/linux_ptrace.o
/reallocarray.o
/setns
/setns.o
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

CFLAGS += -D_GNU_SOURCE
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -Werror
CFLAGS += -Wmissing-declarations

OBJS=setns.o
OBJS+=common.o
OBJS+=reallocarray.o

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
OBJS+=platform/linux/linux_ptrace.o
OBJS+=platform/linux/linux.o
endif

.PHONY: all
all: setns

setns: $(OBJS)

common.o: common.h
setns.o: ptrace.h common.h platform/platform.h $(wildcard platform/*/arch/*.h)
ptrace.o: ptrace.h platform/platform.h $(wildcard platform/*/arch/*.h)

.PHONY: clean
clean:
rm -f setns $(OBJS)

.PHONY: format
format:
clang-format-3.7 -i *.h *.c $(shell find platform -type f -name '*.c' -o -name '*.h')
67 changes: 67 additions & 0 deletions common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdlib.h>

#include "platform/platform.h"
#include "common.h"
#include "reallocarray.h"

static void
_debug(const char *pfx, const char *msg, va_list ap)
{

if (pfx)
fprintf(stderr, "%s", pfx);
vfprintf(stderr, msg, ap);
fprintf(stderr, "\n");
}

void
die(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
_debug("[!] ", msg, ap);
va_end(ap);

exit(1);
}

void
debug(const char *msg, ...)
{

va_list ap;

va_start(ap, msg);
_debug("[+] ", msg, ap);
va_end(ap);
}

void
error(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
_debug("[-] ", msg, ap);
va_end(ap);
}

int
fd_array_push(struct fd_array *fda, int fd)
{
int *tmp;

if (fda->n == fda->allocated) {
fda->allocated = fda->allocated ? 2 * fda->allocated : 2;
tmp = xreallocarray(fda->fds, fda->allocated, sizeof *tmp);
if (tmp == NULL) {
free(fda->fds);
fda->fds = NULL;
fda->allocated = 0;
return -1;
}
fda->fds = tmp;
}
fda->fds[fda->n++] = fd;
return 0;
}
15 changes: 15 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#include <stdarg.h>

#define assert_nonzero(expr) \
({ \
typeof(expr) __val = expr; \
if (__val == 0) \
die("Unexpected: %s == 0!\n", #expr); \
__val; \
})

#define __printf __attribute__((format(printf, 1, 2)))
void __printf die(const char *msg, ...) __attribute__((noreturn));
void __printf debug(const char *msg, ...);
void __printf error(const char *msg, ...);
90 changes: 90 additions & 0 deletions platform/linux/arch/amd64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2011 by Nelson Elhage
*
* 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.
*/
#include "x86_common.h"

#define ARCH_HAVE_MULTIPLE_PERSONALITIES

static struct ptrace_personality arch_personality[2] = {
{
offsetof(struct user, regs.rax), offsetof(struct user, regs.rdi),
offsetof(struct user, regs.rsi), offsetof(struct user, regs.rdx),
offsetof(struct user, regs.r10), offsetof(struct user, regs.r8),
offsetof(struct user, regs.r9), offsetof(struct user, regs.rip),
},
{
offsetof(struct user, regs.rax), offsetof(struct user, regs.rbx),
offsetof(struct user, regs.rcx), offsetof(struct user, regs.rdx),
offsetof(struct user, regs.rsi), offsetof(struct user, regs.rdi),
offsetof(struct user, regs.rbp), offsetof(struct user, regs.rip),
},
};

struct x86_personality x86_personality[2] = {
{
offsetof(struct user, regs.orig_rax), offsetof(struct user, regs.rax),
},
{
offsetof(struct user, regs.orig_rax), offsetof(struct user, regs.rax),
},
};

struct syscall_numbers arch_syscall_numbers[2] = {
#include "default-syscalls.h"
{
/*
* These don't seem to be available in any convenient header. We could
* include unistd_32.h, but those definitions would conflict with the
* standard ones. So, let's just hardcode the values for now. Probably
* we should generate this from unistd_32.h during the build process or
* soemthing.
*/
.nr_mmap = 90,
.nr_mmap2 = 192,
.nr_munmap = 91,
.nr_getsid = 147,
.nr_setsid = 66,
.nr_setpgid = 57,
.nr_fork = 2,
.nr_wait4 = 114,
.nr_signal = 48,
.nr_rt_sigaction = 174,
.nr_open = 5,
.nr_close = 6,
.nr_ioctl = 54,
.nr_dup2 = 63,
.nr_socketcall = 102,
.nr_setns = 346,
}};

static int
arch_get_personality(struct ptrace_child *child)
{
unsigned long cs;

cs = ptrace_command(child, PTRACE_PEEKUSER,
offsetof(struct user, regs.cs));
if (child->error)
return -1;
if (cs == 0x23)
child->personality = 1;
return 0;
}
41 changes: 41 additions & 0 deletions platform/linux/arch/default-syscalls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#define SC(name) .nr_##name = __NR_##name

{
#ifdef __NR_mmap
SC(mmap),
#else
.nr_mmap = -1,
#endif
#ifdef __NR_mmap2
SC(mmap2),
#else
.nr_mmap2 = -1,
#endif
SC(munmap),
SC(getsid),
SC(setsid),
SC(setpgid),
SC(fork),
SC(wait4),
#ifdef __NR_signal
SC(signal),
#else
.nr_signal = -1,
#endif
SC(rt_sigaction),
SC(open),
SC(close),
SC(ioctl),
SC(dup2),
#ifdef __NR_socketcall
SC(socketcall),
#else
SC(socket),
SC(connect),
SC(sendmsg),
#endif
SC(setns),
}
,

#undef SC
67 changes: 67 additions & 0 deletions platform/linux/arch/x86_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2011 by Nelson Elhage
*
* 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.
*/

struct x86_personality {
size_t orig_ax;
size_t ax;
};

struct x86_personality x86_personality[];

static inline struct x86_personality *
x86_pers(struct ptrace_child *child)
{
return &x86_personality[child->personality];
}

static inline void
arch_fixup_regs(struct ptrace_child *child)
{
struct x86_personality *x86pers = x86_pers(child);
struct ptrace_personality *pers = personality(child);
struct user *user = &child->user;
#define ptr(user, off) ((unsigned long *)((void *)(user) + (off)))
*ptr(user, pers->reg_ip) -= 2;
*ptr(user, x86pers->ax) = *ptr(user, x86pers->orig_ax);
}

static inline int
arch_set_syscall(struct ptrace_child *child, unsigned long sysno)
{
return ptrace_command(child, PTRACE_POKEUSER, x86_pers(child)->orig_ax,
sysno);
}

static inline int
arch_save_syscall(struct ptrace_child *child)
{
child->saved_syscall = *ptr(&child->user, x86_pers(child)->orig_ax);
return 0;
}

static inline int
arch_restore_syscall(__attribute__((unused)) struct ptrace_child *child)
{
return 0;
}

#undef ptr
Loading

0 comments on commit 2dd97d3

Please sign in to comment.