Skip to content

Commit 2dd97d3

Browse files
author
Arthur Gautier
committed
Initial commit
Signed-off-by: Arthur Gautier <[email protected]>
0 parents  commit 2dd97d3

17 files changed

+1749
-0
lines changed

.clang-format

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
AlwaysBreakAfterDefinitionReturnType: true
2+
BasedOnStyle: LLVM
3+
IndentWidth: 8
4+
UseTab: Always
5+
BreakBeforeBraces: Linux
6+
AllowShortIfStatementsOnASingleLine: false
7+
IndentCaseLabels: false

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/common.o
2+
/platform/linux/linux.o
3+
/platform/linux/linux_ptrace.o
4+
/reallocarray.o
5+
/setns
6+
/setns.o

Makefile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
CFLAGS += -D_GNU_SOURCE
3+
CFLAGS += -Wall
4+
CFLAGS += -Wextra
5+
CFLAGS += -Werror
6+
CFLAGS += -Wmissing-declarations
7+
8+
OBJS=setns.o
9+
OBJS+=common.o
10+
OBJS+=reallocarray.o
11+
12+
UNAME_S := $(shell uname -s)
13+
ifeq ($(UNAME_S),Linux)
14+
OBJS+=platform/linux/linux_ptrace.o
15+
OBJS+=platform/linux/linux.o
16+
endif
17+
18+
.PHONY: all
19+
all: setns
20+
21+
setns: $(OBJS)
22+
23+
common.o: common.h
24+
setns.o: ptrace.h common.h platform/platform.h $(wildcard platform/*/arch/*.h)
25+
ptrace.o: ptrace.h platform/platform.h $(wildcard platform/*/arch/*.h)
26+
27+
.PHONY: clean
28+
clean:
29+
rm -f setns $(OBJS)
30+
31+
.PHONY: format
32+
format:
33+
clang-format-3.7 -i *.h *.c $(shell find platform -type f -name '*.c' -o -name '*.h')

common.c

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#include "platform/platform.h"
5+
#include "common.h"
6+
#include "reallocarray.h"
7+
8+
static void
9+
_debug(const char *pfx, const char *msg, va_list ap)
10+
{
11+
12+
if (pfx)
13+
fprintf(stderr, "%s", pfx);
14+
vfprintf(stderr, msg, ap);
15+
fprintf(stderr, "\n");
16+
}
17+
18+
void
19+
die(const char *msg, ...)
20+
{
21+
va_list ap;
22+
va_start(ap, msg);
23+
_debug("[!] ", msg, ap);
24+
va_end(ap);
25+
26+
exit(1);
27+
}
28+
29+
void
30+
debug(const char *msg, ...)
31+
{
32+
33+
va_list ap;
34+
35+
va_start(ap, msg);
36+
_debug("[+] ", msg, ap);
37+
va_end(ap);
38+
}
39+
40+
void
41+
error(const char *msg, ...)
42+
{
43+
va_list ap;
44+
va_start(ap, msg);
45+
_debug("[-] ", msg, ap);
46+
va_end(ap);
47+
}
48+
49+
int
50+
fd_array_push(struct fd_array *fda, int fd)
51+
{
52+
int *tmp;
53+
54+
if (fda->n == fda->allocated) {
55+
fda->allocated = fda->allocated ? 2 * fda->allocated : 2;
56+
tmp = xreallocarray(fda->fds, fda->allocated, sizeof *tmp);
57+
if (tmp == NULL) {
58+
free(fda->fds);
59+
fda->fds = NULL;
60+
fda->allocated = 0;
61+
return -1;
62+
}
63+
fda->fds = tmp;
64+
}
65+
fda->fds[fda->n++] = fd;
66+
return 0;
67+
}

common.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#include <stdarg.h>
3+
4+
#define assert_nonzero(expr) \
5+
({ \
6+
typeof(expr) __val = expr; \
7+
if (__val == 0) \
8+
die("Unexpected: %s == 0!\n", #expr); \
9+
__val; \
10+
})
11+
12+
#define __printf __attribute__((format(printf, 1, 2)))
13+
void __printf die(const char *msg, ...) __attribute__((noreturn));
14+
void __printf debug(const char *msg, ...);
15+
void __printf error(const char *msg, ...);

platform/linux/arch/amd64.h

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (C) 2011 by Nelson Elhage
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
#include "x86_common.h"
23+
24+
#define ARCH_HAVE_MULTIPLE_PERSONALITIES
25+
26+
static struct ptrace_personality arch_personality[2] = {
27+
{
28+
offsetof(struct user, regs.rax), offsetof(struct user, regs.rdi),
29+
offsetof(struct user, regs.rsi), offsetof(struct user, regs.rdx),
30+
offsetof(struct user, regs.r10), offsetof(struct user, regs.r8),
31+
offsetof(struct user, regs.r9), offsetof(struct user, regs.rip),
32+
},
33+
{
34+
offsetof(struct user, regs.rax), offsetof(struct user, regs.rbx),
35+
offsetof(struct user, regs.rcx), offsetof(struct user, regs.rdx),
36+
offsetof(struct user, regs.rsi), offsetof(struct user, regs.rdi),
37+
offsetof(struct user, regs.rbp), offsetof(struct user, regs.rip),
38+
},
39+
};
40+
41+
struct x86_personality x86_personality[2] = {
42+
{
43+
offsetof(struct user, regs.orig_rax), offsetof(struct user, regs.rax),
44+
},
45+
{
46+
offsetof(struct user, regs.orig_rax), offsetof(struct user, regs.rax),
47+
},
48+
};
49+
50+
struct syscall_numbers arch_syscall_numbers[2] = {
51+
#include "default-syscalls.h"
52+
{
53+
/*
54+
* These don't seem to be available in any convenient header. We could
55+
* include unistd_32.h, but those definitions would conflict with the
56+
* standard ones. So, let's just hardcode the values for now. Probably
57+
* we should generate this from unistd_32.h during the build process or
58+
* soemthing.
59+
*/
60+
.nr_mmap = 90,
61+
.nr_mmap2 = 192,
62+
.nr_munmap = 91,
63+
.nr_getsid = 147,
64+
.nr_setsid = 66,
65+
.nr_setpgid = 57,
66+
.nr_fork = 2,
67+
.nr_wait4 = 114,
68+
.nr_signal = 48,
69+
.nr_rt_sigaction = 174,
70+
.nr_open = 5,
71+
.nr_close = 6,
72+
.nr_ioctl = 54,
73+
.nr_dup2 = 63,
74+
.nr_socketcall = 102,
75+
.nr_setns = 346,
76+
}};
77+
78+
static int
79+
arch_get_personality(struct ptrace_child *child)
80+
{
81+
unsigned long cs;
82+
83+
cs = ptrace_command(child, PTRACE_PEEKUSER,
84+
offsetof(struct user, regs.cs));
85+
if (child->error)
86+
return -1;
87+
if (cs == 0x23)
88+
child->personality = 1;
89+
return 0;
90+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#define SC(name) .nr_##name = __NR_##name
2+
3+
{
4+
#ifdef __NR_mmap
5+
SC(mmap),
6+
#else
7+
.nr_mmap = -1,
8+
#endif
9+
#ifdef __NR_mmap2
10+
SC(mmap2),
11+
#else
12+
.nr_mmap2 = -1,
13+
#endif
14+
SC(munmap),
15+
SC(getsid),
16+
SC(setsid),
17+
SC(setpgid),
18+
SC(fork),
19+
SC(wait4),
20+
#ifdef __NR_signal
21+
SC(signal),
22+
#else
23+
.nr_signal = -1,
24+
#endif
25+
SC(rt_sigaction),
26+
SC(open),
27+
SC(close),
28+
SC(ioctl),
29+
SC(dup2),
30+
#ifdef __NR_socketcall
31+
SC(socketcall),
32+
#else
33+
SC(socket),
34+
SC(connect),
35+
SC(sendmsg),
36+
#endif
37+
SC(setns),
38+
}
39+
,
40+
41+
#undef SC

platform/linux/arch/x86_common.h

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (C) 2011 by Nelson Elhage
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
23+
struct x86_personality {
24+
size_t orig_ax;
25+
size_t ax;
26+
};
27+
28+
struct x86_personality x86_personality[];
29+
30+
static inline struct x86_personality *
31+
x86_pers(struct ptrace_child *child)
32+
{
33+
return &x86_personality[child->personality];
34+
}
35+
36+
static inline void
37+
arch_fixup_regs(struct ptrace_child *child)
38+
{
39+
struct x86_personality *x86pers = x86_pers(child);
40+
struct ptrace_personality *pers = personality(child);
41+
struct user *user = &child->user;
42+
#define ptr(user, off) ((unsigned long *)((void *)(user) + (off)))
43+
*ptr(user, pers->reg_ip) -= 2;
44+
*ptr(user, x86pers->ax) = *ptr(user, x86pers->orig_ax);
45+
}
46+
47+
static inline int
48+
arch_set_syscall(struct ptrace_child *child, unsigned long sysno)
49+
{
50+
return ptrace_command(child, PTRACE_POKEUSER, x86_pers(child)->orig_ax,
51+
sysno);
52+
}
53+
54+
static inline int
55+
arch_save_syscall(struct ptrace_child *child)
56+
{
57+
child->saved_syscall = *ptr(&child->user, x86_pers(child)->orig_ax);
58+
return 0;
59+
}
60+
61+
static inline int
62+
arch_restore_syscall(__attribute__((unused)) struct ptrace_child *child)
63+
{
64+
return 0;
65+
}
66+
67+
#undef ptr

0 commit comments

Comments
 (0)