-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhang.asm
129 lines (101 loc) · 3.11 KB
/
hang.asm
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
119
120
121
122
123
124
125
126
127
128
129
; The MIT License (MIT)
;
; Copyright (c) 2017 Nathan Osman
;
; 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.
%use smartalign
%define sys_write 0x01
%define sys_rt_sigaction 0x0d
%define sys_rt_sigreturn 0x0f
%define sys_pause 0x22
%define sys_exit 0x3c
%define SA_RESTORER 0x04000000
%define SIGTERM 0x0f
%define STDOUT 0x01
; Definition of sigaction struct for sys_rt_sigaction
struc sigaction
.sa_handler resq 1
.sa_flags resq 1
.sa_restorer resq 1
.sa_mask resq 1
endstruc
section .data
align 16
; Message shown when a syscall fails
error_msg db 'syscall error', 0x0a
error_msg_len equ $ - error_msg
; Message shown when SIGTERM is received
sigterm_msg db 'SIGTERM received', 0x0a
sigterm_msg_len equ $ - sigterm_msg
align 16
act:
istruc sigaction
at sigaction.sa_handler, dq handler
at sigaction.sa_flags, dq SA_RESTORER
at sigaction.sa_restorer, dq restorer
iend
section .text
global _start
align 16
_start:
; Set the handler
xor edx, edx; rdx=0
lea eax, [rdx+sys_rt_sigaction]
lea edi, [rdx+SIGTERM]
mov esi, act
mov ebp, esi ; save offset into data section
lea r10d,[rdx+0x08]
syscall
; Ensure the syscall succeeded
mov ebx, eax ; save syscall return
test eax, eax
jnz error
; Pause until a signal is received
xor eax, eax
mov al, sys_pause
syscall
exit:
; Terminate the application gracefully
xor eax, eax
mov al, sys_exit
mov edi, ebx ; ebx=0 -> syscall successfull
syscall
handler:
; Display a message
xor eax, eax
lea esi, [rbp-(act-sigterm_msg)] ; offset to sigterm_msg from act
lea edx, [rax+sigterm_msg_len]
mov al, sys_write
mov edi, eax ; set edi=1 STDOUT
syscall
ret
restorer:
; return from the signal handler
xor eax, eax
mov al, sys_rt_sigreturn
syscall
error:
; Display an error message
xor eax, eax
lea esi, [rbp-(act-error_msg)] ;offset to error_msg from act
lea edx, [rax+error_msg_len]
mov al, sys_write
mov edi, eax; edi=1 STDOUT
syscall
jmp exit