Skip to content

Commit 4a6cca2

Browse files
committed
linux,decree: fixed thread detection
- Each command now takes a task struct to get the pid/tid of the caller - Thread switching detection - Thread/process termination detection
1 parent 6f83530 commit 4a6cca2

File tree

24 files changed

+231
-216
lines changed

24 files changed

+231
-216
lines changed

decree-cgc-cfe/arch/x86/kernel/process_32.c

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
#include <asm/debugreg.h>
5757
#include <asm/switch_to.h>
5858

59+
#include <s2e/decree/decree_monitor.h>
60+
5961
asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
6062
asmlinkage void ret_from_kernel_thread(void) __asm__("ret_from_kernel_thread");
6163

@@ -325,6 +327,8 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
325327

326328
this_cpu_write(current_task, next_p);
327329

330+
s2e_decree_task_switch(prev_p, next_p);
331+
328332
return prev_p;
329333
}
330334

decree-cgc-cfe/arch/x86/kernel/process_64.c

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
#include <asm/debugreg.h>
5151
#include <asm/switch_to.h>
5252

53+
#include <s2e/decree/decree_monitor.h>
54+
5355
asmlinkage extern void ret_from_fork(void);
5456

5557
asmlinkage DEFINE_PER_CPU(unsigned long, old_rsp);
@@ -427,6 +429,8 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
427429
task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV))
428430
__switch_to_xtra(prev_p, next_p, tss);
429431

432+
s2e_decree_task_switch(prev_p, next_p);
433+
430434
return prev_p;
431435
}
432436

decree-cgc-cfe/arch/x86/mm/fault.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ force_sig_info_fault(int si_signo, int si_code, unsigned long address,
177177

178178
if (s2e_decree_monitor_enabled) {
179179
s2e_printf("SEGFAULT at 0x%lx\n", task_pt_regs(tsk)->ip);
180-
s2e_decree_segfault(current->pid, current->comm, task_pt_regs(tsk)->ip, address, fault);
180+
s2e_decree_segfault(current, current->comm, task_pt_regs(tsk)->ip, address, fault);
181181
}
182182

183183
info.si_signo = si_signo;

decree-cgc-cfe/fs/binfmt_cgc.c

+31-32
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ static int load_cgcos_binary(struct linux_binprm *bprm)
241241
struct cgc_params pars;
242242

243243
if (s2e_decree_monitor_enabled) {
244-
s2e_decree_process_load(current->pid, bprm->interp);
244+
s2e_decree_process_load(current, bprm->interp);
245245
}
246246

247247
memset(&pars, 0, sizeof(pars));
@@ -561,9 +561,9 @@ static int load_cgcos_binary(struct linux_binprm *bprm)
561561
ret = 0;
562562
out:
563563
if (ret == 0 && s2e_decree_monitor_enabled) {
564-
s2e_decree_module_load(bprm->interp, current->pid, hdr.c_entry,
564+
s2e_decree_module_load(current, bprm->interp, hdr.c_entry,
565565
elf_phdr, elf_phdr_size);
566-
s2e_decree_update_memory_map(current->pid, current->comm,
566+
s2e_decree_update_memory_map(current, current->comm,
567567
current->mm);
568568
}
569569
if (phdrs)
@@ -594,7 +594,7 @@ static void s2e_decree_set_args(int *skip_rng)
594594
params.cgc_seed_ptr = (uintptr_t)current->cgc_seed;
595595
params.cgc_seed_len = current->cgc_seed_len;
596596

597-
s2e_decree_do_set_args(current->pid, current->comm, &params);
597+
s2e_decree_do_set_args(current, current->comm, &params);
598598

599599
/* Write back new param values */
600600
current->cgc_max_transmit = params.cgc_max_transmit;
@@ -1705,13 +1705,13 @@ long s2e_copy_to_user(void __user *to, const void *from, long n)
17051705
{
17061706
long ret;
17071707
if (s2e_decree_monitor_enabled) {
1708-
s2e_decree_copy_to_user(current->pid, current->comm, to, from,
1709-
n, 0, 0);
1708+
s2e_decree_copy_to_user(current, current->comm, to, from, n, 0,
1709+
0);
17101710
}
17111711
ret = copy_to_user(to, from, n);
17121712
if (s2e_decree_monitor_enabled) {
1713-
s2e_decree_copy_to_user(current->pid, current->comm, to, from,
1714-
n, 1, ret);
1713+
s2e_decree_copy_to_user(current, current->comm, to, from, n, 1,
1714+
ret);
17151715
}
17161716
return ret;
17171717
}
@@ -1746,13 +1746,13 @@ static int asmlinkage cgcos_fdwait(int nfds, fd_set __user *readfds,
17461746
if (s2e_decree_monitor_enabled) {
17471747
invoke_orig = 1;
17481748
if (timeout != NULL) {
1749-
res = s2e_decree_waitfds(current->pid, current->comm,
1750-
nfds, true, to->tv_sec,
1751-
to->tv_nsec, &invoke_orig);
1749+
res = s2e_decree_waitfds(current, current->comm, nfds,
1750+
true, to->tv_sec, to->tv_nsec,
1751+
&invoke_orig);
17521752
} else {
1753-
res = s2e_decree_waitfds(current->pid, current->comm,
1754-
nfds, false, to->tv_sec,
1755-
to->tv_nsec, &invoke_orig);
1753+
res = s2e_decree_waitfds(current, current->comm, nfds,
1754+
false, to->tv_sec, to->tv_nsec,
1755+
&invoke_orig);
17561756
}
17571757
if (invoke_orig) {
17581758
res = core_sys_select(nfds, readfds, writefds, NULL,
@@ -1786,8 +1786,8 @@ static int asmlinkage cgcos_allocate(unsigned long len, unsigned long exec,
17861786
return (-EFAULT);
17871787

17881788
if (s2e_decree_monitor_enabled) {
1789-
s2e_decree_handle_symbolic_allocate_size(current->pid,
1790-
current->comm, &len);
1789+
s2e_decree_handle_symbolic_allocate_size(current, current->comm,
1790+
&len);
17911791
}
17921792

17931793
res = vm_mmap(NULL, 0, len, prot, MAP_ANON | MAP_PRIVATE, 0);
@@ -1798,7 +1798,7 @@ static int asmlinkage cgcos_allocate(unsigned long len, unsigned long exec,
17981798
return (-EFAULT);
17991799
}
18001800
if (s2e_decree_monitor_enabled) {
1801-
s2e_decree_update_memory_map(current->pid, current->comm,
1801+
s2e_decree_update_memory_map(current, current->comm,
18021802
current->mm);
18031803
}
18041804
return (0);
@@ -1817,8 +1817,8 @@ int asmlinkage cgcos_random(char __user *buf, size_t count,
18171817
return (-EFAULT);
18181818

18191819
if (s2e_decree_monitor_enabled) {
1820-
s2e_decree_handle_symbolic_random_buffer(
1821-
current->pid, current->comm, (void **)&buf, &count);
1820+
s2e_decree_handle_symbolic_random_buffer(current, current->comm,
1821+
(void **)&buf, &count);
18221822
}
18231823

18241824
for (i = 0; i < count; i += sizeof(randval)) {
@@ -1839,7 +1839,7 @@ int asmlinkage cgcos_random(char __user *buf, size_t count,
18391839
if (s2e_decree_monitor_enabled) {
18401840
// either replace everything with symbolic data, or make values
18411841
// concolic
1842-
s2e_decree_random(current->pid, current->comm, buf, count);
1842+
s2e_decree_random(current, current->comm, buf, count);
18431843
}
18441844

18451845
if (rnd_out != NULL &&
@@ -1855,8 +1855,8 @@ static int asmlinkage cgcos_deallocate(unsigned long ptr, size_t len)
18551855
ptr >= (CGC_MAGIC_PAGE + PAGE_SIZE)) {
18561856
int res = vm_munmap(ptr, len);
18571857
if (res == 0 && s2e_decree_monitor_enabled) {
1858-
s2e_decree_update_memory_map(
1859-
current->pid, current->comm, current->mm);
1858+
s2e_decree_update_memory_map(current, current->comm,
1859+
current->mm);
18601860
}
18611861
return res;
18621862
}
@@ -1880,7 +1880,7 @@ int asmlinkage cgcos_transmit(int fd, char __user *buf, size_t count,
18801880

18811881
if (s2e_decree_monitor_enabled) {
18821882
s2e_decree_handle_symbolic_transmit_buffer(
1883-
current->pid, current->comm, (void **)&buf, &count);
1883+
current, current->comm, (void **)&buf, &count);
18841884
}
18851885

18861886
if (count != 0) {
@@ -1891,8 +1891,8 @@ int asmlinkage cgcos_transmit(int fd, char __user *buf, size_t count,
18911891

18921892
if (s2e_decree_monitor_enabled) {
18931893
// res becomes symbolic if count_orig was symbolic
1894-
s2e_decree_write_data(current->pid, current->comm, fd,
1895-
buf, &res, &count_orig);
1894+
s2e_decree_write_data(current, current->comm, fd, buf,
1895+
&res, &count_orig);
18961896
}
18971897
}
18981898

@@ -1917,7 +1917,7 @@ int asmlinkage cgcos_receive(int fd, char __user *buf, size_t count,
19171917
count = current->cgc_max_receive;
19181918

19191919
if (s2e_decree_monitor_enabled) {
1920-
invoke_orig = s2e_get_cfg_bool(current->pid, current->comm,
1920+
invoke_orig = s2e_get_cfg_bool(current, current->comm,
19211921
"invokeOriginalSyscalls");
19221922
}
19231923

@@ -1929,16 +1929,15 @@ int asmlinkage cgcos_receive(int fd, char __user *buf, size_t count,
19291929
}
19301930

19311931
if (s2e_decree_monitor_enabled) {
1932-
s2e_decree_read_data_post(current->pid,
1933-
current->comm, fd,
1934-
buf, res);
1932+
s2e_decree_read_data_post(
1933+
current, current->comm, fd, buf, res);
19351934
}
19361935
}
19371936
} else {
19381937
size_t count_orig = count; // remember original symbolic size
19391938

19401939
s2e_decree_handle_symbolic_receive_buffer(
1941-
current->pid, current->comm, (void **)&buf, &count);
1940+
current, current->comm, (void **)&buf, &count);
19421941

19431942
if (count != 0) {
19441943
void *kbuf;
@@ -1951,8 +1950,8 @@ int asmlinkage cgcos_receive(int fd, char __user *buf, size_t count,
19511950
}
19521951

19531952
// res becomes symbolic if count_orig was symbolic
1954-
s2e_decree_read_data(current->pid, current->comm, fd,
1955-
kbuf, count, &count_orig, &res);
1953+
s2e_decree_read_data(current, current->comm, fd, kbuf,
1954+
count, &count_orig, &res);
19561955

19571956
if (s2e_copy_to_user(buf, kbuf, count)) {
19581957
kfree(kbuf);

decree-cgc-cfe/kernel/panic.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void panic(const char *fmt, ...)
105105
va_end(args);
106106

107107
if (s2e_decree_monitor_enabled) {
108-
s2e_decree_kernel_panic(buf, sizeof(buf));
108+
s2e_decree_kernel_panic(current, buf, sizeof(buf));
109109
}
110110

111111

decree-cgc-cfe/kernel/s2e/s2e.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@
3030

3131
static int __init s2e_init(void)
3232
{
33-
size_t task_struct_pid_offset = offsetof(struct task_struct, pid);
34-
3533
/* Check if the DecreeMonitor plugin is enabled */
3634
if (boot_cpu_has(X86_FEATURE_S2E) && s2e_plugin_loaded("DecreeMonitor")) {
3735
s2e_decree_monitor_enabled = 1;
3836
}
3937

4038
/* Send addresses and offsets to the DecreeMonitor plugin */
4139
if (s2e_decree_monitor_enabled) {
42-
s2e_decree_init(PAGE_OFFSET, __START_KERNEL, task_struct_pid_offset);
40+
s2e_decree_init(current, PAGE_OFFSET, __START_KERNEL);
4341
}
4442

4543
return 0;

decree-cgc-cfe/kernel/sched/core.c

+1
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
21582158
switch_to(prev, next, prev);
21592159

21602160
barrier();
2161+
21612162
/*
21622163
* this_rq must be evaluated again because prev may have moved
21632164
* CPUs since it called schedule(), thus the 'rq' on its stack

decree-cgc-cfe/mm/memory.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3823,7 +3823,7 @@ int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
38233823
}
38243824

38253825
if (s2e_decree_monitor_enabled && current->personality == PER_CGCOS) {
3826-
s2e_decree_update_memory_map(current->pid, current->comm, current->mm);
3826+
s2e_decree_update_memory_map(current, current->comm, current->mm);
38273827
}
38283828

38293829
return ret;

include/s2e/decree/commands.h

+9-12
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,21 @@
55
///
66
/// Permission is hereby granted, free of charge, to any person obtaining a copy
77
/// of this software and associated documentation files (the "Software"), to
8-
/// deal
9-
/// in the Software without restriction, including without limitation the rights
8+
/// deal in the Software without restriction, including without limitation the rights
109
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1110
/// copies of the Software, and to permit persons to whom the Software is
1211
/// furnished to do so, subject to the following conditions:
1312
///
1413
/// The above copyright notice and this permission notice shall be included in
15-
/// all
16-
/// copies or substantial portions of the Software.
14+
/// all copies or substantial portions of the Software.
1715
///
1816
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1917
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2018
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2119
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2220
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23-
/// FROM,
24-
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25-
/// THE
26-
/// SOFTWARE.
21+
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
/// THE SOFTWARE.
2723

2824
#ifndef S2E_DECREE_COMMANDS_H
2925
#define S2E_DECREE_COMMANDS_H
@@ -40,7 +36,7 @@
4036
extern "C" {
4137
#endif
4238

43-
#define S2E_DECREEMON_COMMAND_VERSION 0x201903202239ULL // date +%Y%m%d%H%M
39+
#define S2E_DECREEMON_COMMAND_VERSION 0x202301082207ULL // date +%Y%m%d%H%M
4440

4541
enum S2E_DECREEMON_COMMANDS {
4642
DECREE_SEGFAULT,
@@ -62,7 +58,8 @@ enum S2E_DECREEMON_COMMANDS {
6258
DECREE_SET_CB_PARAMS,
6359
DECREE_INIT,
6460
DECREE_KERNEL_PANIC,
65-
DECREE_MODULE_LOAD
61+
DECREE_MODULE_LOAD,
62+
DECREE_TASK_SWITCH
6663
};
6764

6865
struct S2E_DECREEMON_COMMAND_READ_DATA {
@@ -179,7 +176,6 @@ struct S2E_DECREEMON_VMA {
179176
struct S2E_DECREEMON_COMMAND_INIT {
180177
uint64_t page_offset;
181178
uint64_t start_kernel;
182-
uint64_t task_struct_pid_offset;
183179
} __attribute__((packed));
184180

185181
struct S2E_DECREEMON_COMMAND_KERNEL_PANIC {
@@ -190,7 +186,7 @@ struct S2E_DECREEMON_COMMAND_KERNEL_PANIC {
190186
struct S2E_DECREEMON_COMMAND {
191187
uint64_t version;
192188
enum S2E_DECREEMON_COMMANDS Command;
193-
uint64_t currentPid;
189+
struct S2E_LINUXMON_TASK CurrentTask;
194190
union {
195191
struct S2E_LINUXMON_COMMAND_PROCESS_LOAD ProcessLoad;
196192
struct S2E_LINUXMON_COMMAND_MODULE_LOAD ModuleLoad;
@@ -208,6 +204,7 @@ struct S2E_DECREEMON_COMMAND {
208204
struct S2E_DECREEMON_COMMAND_SET_CB_PARAMS CbParams;
209205
struct S2E_DECREEMON_COMMAND_INIT Init;
210206
struct S2E_DECREEMON_COMMAND_KERNEL_PANIC Panic;
207+
struct S2E_LINUXMON_COMMAND_TASK_SWITCH TaskSwitch;
211208
};
212209
char currentName[32]; // not NULL terminated
213210
} __attribute__((packed));

0 commit comments

Comments
 (0)