-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsig1.c
More file actions
118 lines (91 loc) · 1.91 KB
/
sig1.c
File metadata and controls
118 lines (91 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Example of using sigaction() to setup a signal handler with 3 arguments
* including siginfo_t.
*/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdint.h>
struct stackt {
char* ss_sp;
uint64_t ss_size;
int32_t ss_flags;
char pad_cgo_0[4];
};
struct ucontext {
int32_t uc_onstack;
uint32_t uc_sigmask;
struct stackt uc_stack;
struct ucontext* uc_link;
uint64_t uc_mcsize;
struct mcontext64* uc_mcontext;
};
struct exceptionstate64 {
uint16_t trapno;
uint16_t cpu;
uint32_t err;
uint64_t faultvaddr;
};
struct regs64 {
uint64_t rax;
uint64_t rbx;
uint64_t rcx;
uint64_t rdx;
uint64_t rdi;
uint64_t rsi;
uint64_t rbp;
uint64_t rsp;
uint64_t r8;
uint64_t r9;
uint64_t r10;
uint64_t r11;
uint64_t r12;
uint64_t r13;
uint64_t r14;
uint64_t r15;
uint64_t rip;
uint64_t rflags;
uint64_t cs;
uint64_t fs;
uint64_t gs;
};
struct mcontext64 {
struct exceptionstate64 es;
struct regs64 ss;
// fs floatstate64
// pad_cgo_0 [4]byte
};
void hi()
{
printf("hi\n");
}
static void hdl (int sig, siginfo_t *siginfo, void* context)
{
printf ("Sending PID: %ld, UID: %ld\n",
(long)siginfo->si_pid, (long)siginfo->si_uid);
struct ucontext* uctx = (struct ucontext*)context;
struct regs64* reg = &(uctx->uc_mcontext->ss);
printf("%llu\n", reg->rip);
uint64_t pc = reg->rip;
uint64_t sp = reg->rsp;
sp -= sizeof(uint64_t);
*((uint64_t*)sp) = pc;
reg->rsp = sp;
reg->rip = hi;
}
int main (int argc, char *argv[])
{
struct sigaction act;
memset (&act, '\0', sizeof(act));
/* Use the sa_sigaction field because the handles has two additional parameters */
act.sa_sigaction = &hdl;
/* The SA_SIGINFO flag tells sigaction() to use the sa_sigaction field, not sa_handler. */
act.sa_flags = SA_ONSTACK | SA_SIGINFO ;
if (sigaction(SIGINT, &act, NULL) < 0) {
perror ("sigaction");
return 1;
}
while (1)
{}
return 0;
}