From 0c02286ad78fc0d2d26c3f2cb585ecafd4d52da9 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 21 Jun 2016 18:08:26 +0100 Subject: Cleanup and noting of code to prepare it for work --- .sh.s.swp | Bin 0 -> 1024 bytes makefile | 2 +- sh | Bin 0 -> 3768 bytes sh.asm | 413 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sh.o | Bin 0 -> 4048 bytes sh.s | 388 ---------------------------------------------------------- 6 files changed, 414 insertions(+), 389 deletions(-) create mode 100644 .sh.s.swp create mode 100755 sh create mode 100644 sh.asm create mode 100644 sh.o delete mode 100644 sh.s diff --git a/.sh.s.swp b/.sh.s.swp new file mode 100644 index 0000000..9c0da39 Binary files /dev/null and b/.sh.s.swp differ diff --git a/makefile b/makefile index 04a37e8..3725a30 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,5 @@ all: - nasm sh.s -f elf64 -o sh.o + nasm sh.asm -f elf64 -o sh.o ld sh.o -o sh clean: rm -f sh.o sh diff --git a/sh b/sh new file mode 100755 index 0000000..b8aae89 Binary files /dev/null and b/sh differ diff --git a/sh.asm b/sh.asm new file mode 100644 index 0000000..123d772 --- /dev/null +++ b/sh.asm @@ -0,0 +1,413 @@ +section .data + sigs: dd 0x2 + cpid: dd 0x0 + eol: db `\n` + msg: db "Welcome to deadbeef shell", 0x21, `\n` + prompt: db "[0xdeadbeef]", 0x20 + nfe: db "Error: program not found", 0x2e, `\n` + env: db "/etc/environment", 0x0 + boem: db "Error: input overflows buffer", 0x2e, `\n` + + ;Syscall constants + sys_read equ 0x00 + sys_write equ 0x01 + sys_open equ 0x02 + sys_close equ 0x03 + stub_fork equ 0x39 + stub_execve equ 0x3b + sys_exit equ 0x3c + sys_wait4 equ 0x3d + sys_kill equ 0x3e + + +section .text + global _start + +_start: + ; TODO: handle interrupt signal + + push rbp + mov rbp, rsp + sub rsp, 0x110 + mov r15, rsp + sub rsp, 0x110 + mov r9, rsp + sub rsp, 0x110 + mov rbx, rsp + + mov rax, sys_write + mov rdi, 0x1 + mov rsi, msg + mov rdx, 0x1b + syscall + + jmp _parse_path + +_rloop: + mov rax, sys_write + mov rdi, 0x1 + mov rsi, prompt + mov rdx, 0xd + syscall + + mov r8, -0x1 + mov r12, _rloopr + jmp _bzero + +_rloopr: + mov rax, sys_read + mov rdi, 0x0 + mov rsi, r15 + mov rdx, 0xff + syscall + + ; check for _quit + mov r12, _quit + cmp byte [r15], 0x0 + je _weol + + ; call _parse + jmp _parse + +_exec: + ; fork so we don't hurt ourselves + mov rax, stub_fork + syscall + mov dword [cpid], eax + cmp dword [cpid], 0x0 + jne _wait4_it + + ; now that we know what to execute, do so + mov rax, stub_execve + mov rdi, r13 + mov rsi, r14 + mov rdx, 0x0 + syscall + + ; and now kill the child process + jmp _quit + +_wait4_it: + ;This function is obsolete, replace with 247 (sys_waitid) + mov rdi, rax + mov rax, sys_wait4 + xor rsi, rsi ; null status pointer + xor rdx, rdx + xor rcx, rcx ; null rusage pointer + syscall + + mov dword [cpid], -0x1 + jmp _rloop + +; input: none +; output: r9 (path elements), r10 (length) +; clobbered: r8, r12 +; other: r15 (buffer) +_parse_path: + mov rax, sys_open + mov rdi, env + mov rsi, 0x0 + syscall + cmp rax, 0x0 + jl _quit + + mov r8, -0x1 + mov r12, _parse_path2 + jmp _bzero + +_parse_path2: + mov rdi, rax + mov rax, sys_read + mov rsi, r9 + mov rdx, 0xff + syscall + cmp rax, 0x0 + jl _quit + mov r8, r9 + dec r8 + +_pathfinder: + inc r8 + xor rax, rax + mov byte al, [r8] + cmp al, `P` + jne _pathfinder + mov byte al, [r8+1] + cmp al, `A` + jne _pathfinder + mov byte al, [r8+2] + cmp al, `T` + jne _pathfinder + mov byte al, [r8+3] + cmp al, `H` + jne _pathfinder + mov byte al, [r8+4] + cmp al, `=` + jne _pathfinder + mov byte al, [r8+5] + cmp al, `"` + jne _pathfinder + cmp al, 0x0 + je _quit + add r8, 0x6 + mov r10, 0x1 + push 0x0 + push r8 + dec r8 + +_pathender: + inc r8 + mov al, [r8] + cmp al, `:` + je _pathender1 + cmp al, `"` + jne _pathender + mov byte [r8], 0x0 + mov r9, rsp + jmp _rloop + +_pathender1: + inc r10 + mov byte [r8], 0x0 + lea rcx, [r8+0x1] + push rcx + jmp _pathender + +;Writes 0s in buffer r15 until r8 is 255 +;input: r8 (iterator), r15 (buffer), r12 (return address) +;output: none +_bzero: + inc r8 + mov byte [r8+r15], 0x0 + cmp r8, 0xff + jle _bzero + jmp r12 + +;input: r15 (buffer) +;output: r13 (path), r14 (arguments) +;clobbered: r8, rax, rbx, rcx +_parse: ; needs special commands: cd exit export eval + ;syscall 80 is chdir, so do that first, it's easiest + ;It takes a path string. + ;also needs pipes (|, >, <) + push 0x0 + xor r13, r13 + jmp _strlen + +;r8 holds strlen so we start at the end and parse backwards +; go through and sub/push +_parse1: + cmp byte [r15+r8], ` ` + je _subzp + cmp byte [r15+r8], `\n` + je _subz + cmp byte [r15+r8], `/` + je _sabsf + cmp byte [r15+r8], `.` + je _sabsf + +;Controls the parsing of the directory, keeps parsing each bit of the dir until +;the end of the string or directory name +_parse1r: + dec r8 + cmp r8, 0x0 + jl _parse2 + jmp _parse1 + +_sabsf: + cmp r8, 0x1 + jg _parse1r + mov r13, 0x1 + jmp _parse1r + +_subz: + mov byte [r8+r15], 0x0 + jmp _parse1 + +;Splits at the current position and puts the address of the argument on the stack +_subzp: + mov byte [r8+r15], 0x0 + lea rax, [r8+r15+0x1] + push rax + jmp _parse1 + +_parse2: + push rbx + mov r14, rsp + cmp r13, 0x0 + jg _parse7 + mov r8, -0x1 + +_parse3: + inc r8 + cmp r8, r10 + je _parse4 + mov r13, r15 + mov rcx, [r9+r8*8] + push r8 + jmp _concat + +_parse4: ; not found + mov rax, sys_write + mov rdi, 0x1 + mov rsi, nfe + mov rdx, 0x1a + syscall + jmp _rloop + +; Checks that the program rbx exists before doing anything, this is hacky, slow +; and should therefore be replaced by a version using sys_access with flag X_OK +_parse5: + pop r8 + + mov rax, sys_open + mov rdi, rbx + xor rsi, rsi + xor rdx, rdx + syscall + + ;Check if it was opened successfully to check if it exists + cmp rax, 0x0 + jl _parse3 + + mov rdi, rax + mov rax, sys_close + syscall + +; We'll fall through to here if the file is accessible, therefore we should execute it +_parse6: + mov r13, rbx + jmp _exec + +_parse7: ; absolute path + mov rax, sys_open + mov rdi, r15 + xor rsi, rsi + xor rdx, rdx + syscall + + cmp rax, 0x0 + jl _parse4 + + mov rdi, rax + mov rax, sys_close + syscall + + mov r13, r15 + jmp _exec + + +;input: r12 (return address), r15 (buffer) +;output: r8 (length) +_strlen: + xor r8, r8 + +_strlen1: + cmp byte [r8+r15], 0x0 + je _parse1 + cmp byte [r8+r15], 0xa + je _parse1 + inc r8 + jmp _strlen1 + +; input: rcx (a), r13 (b) +; output: rbx (string) +; clobbered: r8, r11 +_concat: + mov r8, -0x1 + jmp _jlen1 + +_jlen1: + inc r8 + cmp byte [r8+rcx], 0x0 + je _jlen02 + cmp byte [r8+rcx], 0xa + je _jlen02 + jmp _jlen1 + +_jlen02: + dec r8 + mov r11, -0x1 + +_jlen2: + inc r8 + inc r11 + cmp byte [r11+r13], 0x0 + je _treg + cmp byte [r11+r13], 0xa + je _treg + jmp _jlen2 + +_treg: + cmp r8, 0xff + jg _boe + xchg r15, rbx + mov r8, -0x1 + mov r12, _jrsinc + jmp _bzero + +_jrsinc: + xchg r15, rbx + mov r8, -0x1 + mov r11, -0x1 + +_join1: + inc r8 + cmp byte [r8+rcx], 0x0 + je _join02 + cmp byte [r8+rcx], 0xa + je _join02 + mov al, [r8+rcx] + mov [r8+rbx], al + jmp _join1 + +_join02: + mov byte [r8+rbx], `/` +_join2: + inc r8 + inc r11 + cmp byte [r11+r13], 0x0 + je _parse5 + cmp byte [r11+r13], `\n` + je _parse5 + mov al, [r11+r13] + mov [r8+rbx], al + jmp _join2 + +_boe: + mov rax, sys_write + mov rdi, 0x1 + mov rsi, boem + mov rdx, 0x1f + jmp _quit + +; interrupt signal handler +_sigint: + cmp dword [cpid], 0x0 + jl _sigint_nc + +__sigint_c: ; kill the child + mov rax, sys_kill + mov rdi, [cpid] + mov rsi, 0x2 + syscall + +_sigint_nc: + mov r12, _rloop + jmp _weol + +; input: r12 (return address) +; output: none +; Writes end of line to the terminal +_weol: + mov rax, sys_write + mov rdi, 0x1 + mov rsi, eol + mov rdx, 0x1 + syscall + jmp r12 + +_quit: + mov rax, sys_exit + mov rdi, 0x0 + syscall diff --git a/sh.o b/sh.o new file mode 100644 index 0000000..164fa55 Binary files /dev/null and b/sh.o differ diff --git a/sh.s b/sh.s deleted file mode 100644 index edc6710..0000000 --- a/sh.s +++ /dev/null @@ -1,388 +0,0 @@ -section .text - global _start - -_start: - ; TODO: handle interrupt signal - - push rbp - mov rbp, rsp - sub rsp, 0x110 - mov r15, rsp - sub rsp, 0x110 - mov r9, rsp - sub rsp, 0x110 - mov rbx, rsp - - mov rax, 0x1 - mov rdi, 0x1 - mov rsi, msg - mov rdx, 0x1b - syscall - - jmp parse_path - -rloop: - mov rax, 0x1 - mov rdi, 0x1 - mov rsi, prompt - mov rdx, 0xd - syscall - - mov r8, -0x1 - mov r12, rloopr - jmp bzero - -rloopr: - mov rax, 0x0 - mov rdi, 0x0 - mov rsi, r15 - mov rdx, 0xff - syscall - - ; check for quit - cmp byte [r15], 0x0 - mov r12, quit - je weol - - ; call parse - jmp parse - -exec: - ; fork so we don't hurt ourselves - mov rax, 0x39 - syscall - mov dword [cpid], eax - cmp dword [cpid], 0x0 - jne wait4_it - - ; now that we know what to execute, do so - mov rax, 0x3b - mov rdi, r13 - mov rsi, r14 - mov rdx, 0x0 - syscall - - ; and now kill the child process - jmp quit - -wait4_it: - mov rdi, rax - mov rax, 0x3d - xor rsi, rsi ; null status pointer - xor rdx, rdx - xor rcx, rcx ; null rusage pointer - syscall - - mov dword [cpid], -0x1 - jmp rloop - -; input: none -; output: r9 (path elements), r10 (length) -; clobbered: r8, r12 -; other: r15 (buffer) -parse_path: - mov rax, 0x2 - mov rdi, env - mov rsi, 0x0 - syscall - cmp rax, 0x0 - jl quit - - mov r8, -0x1 - mov r12, parse_path2 - jmp bzero - -parse_path2: - mov rdi, rax - mov rax, 0x0 - mov rsi, r9 - mov rdx, 0xff - syscall - cmp rax, 0x0 - jl quit - mov r8, r9 - dec r8 - -pathfinder: - inc r8 - xor rax, rax - mov byte al, [r8] - cmp al, 0x50 - jne pathfinder - mov byte al, [r8+1] - cmp al, 0x41 - jne pathfinder - mov byte al, [r8+2] - cmp al, 0x54 - jne pathfinder - mov byte al, [r8+3] - cmp al, 0x48 - jne pathfinder - mov byte al, [r8+4] - cmp al, 0x3d - jne pathfinder - mov byte al, [r8+5] - cmp al, 0x22 - jne pathfinder - cmp al, 0x0 - je quit - add r8, 0x6 - mov r10, 0x1 - push 0x0 - push r8 - dec r8 - -pathender: - inc r8 - mov al, [r8] - cmp al, 0x3a - je pathender1 - cmp al, 0x22 - jne pathender - mov byte [r8], 0x0 - mov r9, rsp - jmp rloop - -pathender1: - inc r10 - mov byte [r8], 0x0 - lea rcx, [r8+0x1] - push rcx - jmp pathender - -;input: r8 (iterator), r15 (buffer), r12 (return address) -;output: none -bzero: - inc r8 - mov byte [r8+r15], 0x0 - cmp r8, 0xff - jle bzero - jmp r12 - -;input: r15 (buffer) -;output: r13 (path), r14 (arguments) -;clobbered: r8, rax, rbx, rcx -parse: ; needs special commands: cd exit export echo eval pwd - ;also needs pipes (|, >, <) - push 0x0 - xor r13, r13 - jmp strlen - -; go through and sub/push -parse1: - cmp byte [r8+r15], 0x20 - je subzp - cmp byte [r8+r15], 0x0a - je subz - cmp byte [r8+r15], 0x2f - je sabsf - cmp byte [r8+r15], 0x2e - je sabsf - -parse1r: - dec r8 - cmp r8, 0x0 - jl parse2 - jmp parse1 - -sabsf: - cmp r8, 0x1 - jg parse1r - mov r13, 0x1 - jmp parse1r - -subz: - mov byte [r8+r15], 0x0 - jmp parse1 - -subzp: - mov byte [r8+r15], 0x0 - lea rax, [r8+r15+0x1] - push rax - jmp parse1 - -parse2: - push rbx - mov r14, rsp - cmp r13, 0x0 - jg parse7 - mov r8, -0x1 - -parse3: - inc r8 - cmp r8, r10 - je parse4 - mov r13, r15 - mov rcx, [r9+r8*8] - push r8 - jmp concat - -parse4: ; not found - mov rax, 0x1 - mov rdi, 0x1 - mov rsi, nfe - mov rdx, 0x1a - syscall - jmp rloop - -parse5: ; test rbx - pop r8 - - mov rax, 0x2 - mov rdi, rbx - xor rsi, rsi - xor rdx, rdx - syscall - - cmp rax, 0x0 - jl parse3 - - mov rdi, rax - mov rax, 0x3 - syscall - -parse6: ; found it - mov r13, rbx - jmp exec - -parse7: ; absolute path - mov rax, 0x2 - mov rdi, r15 - xor rsi, rsi - xor rdx, rdx - syscall - - cmp rax, 0x0 - jl parse4 - - mov rdi, rax - mov rax, 0x3 - syscall - - mov r13, r15 - jmp exec - - -;input: r12 (return address), r15 (buffer) -;output: r8 (length) -strlen: - xor r8, r8 - -strlen1: - cmp byte [r8+r15], 0x0 - je parse1 - cmp byte [r8+r15], 0xa - je parse1 - inc r8 - jmp strlen1 - -; input: rcx (a), r13 (b) -; output: rbx (string) -; clobbered: r8, r11 -concat: - mov r8, -0x1 - jmp jlen1 - -jlen1: - inc r8 - cmp byte [r8+rcx], 0x0 - je jlen02 - cmp byte [r8+rcx], 0xa - je jlen02 - jmp jlen1 - -jlen02: - dec r8 - mov r11, -0x1 - -jlen2: - inc r8 - inc r11 - cmp byte [r11+r13], 0x0 - je treg - cmp byte [r11+r13], 0xa - je treg - jmp jlen2 - -treg: - cmp r8, 0xff - jg boe - xchg r15, rbx - mov r8, -0x1 - mov r12, rsinc - jmp bzero - -rsinc: - xchg r15, rbx - mov r8, -0x1 - mov r11, -0x1 - -join1: - inc r8 - cmp byte [r8+rcx], 0x0 - je join02 - cmp byte [r8+rcx], 0xa - je join02 - mov al, [r8+rcx] - mov [r8+rbx], al - jmp join1 - -join02: - mov byte [r8+rbx], 0x2f -join2: - inc r8 - inc r11 - cmp byte [r11+r13], 0x0 - je parse5 - cmp byte [r11+r13], 0xa - je parse5 - mov al, [r11+r13] - mov [r8+rbx], al - jmp join2 - -boe: - mov rax, 0x1 - mov rdi, 0x1 - mov rsi, boem - mov rdx, 0x1f - jmp quit - -; interrupt signal handler -sigint: - cmp dword [cpid], 0x0 - jl sigint_nc - -sigint_c: ; kill the child - mov rax, 0x3e - mov rdi, [cpid] - mov rsi, 0x2 - syscall - -sigint_nc: - mov r12, rloop - jmp weol - -; input: r12 (return address) -; output: none -weol: - mov rax, 0x1 - mov rdi, 0x1 - mov rsi, eol - mov rdx, 0x1 - syscall - jmp r12 - -quit: - mov rax, 0x3c - mov rdi, 0x0 - syscall - -section .data - sigs: dd 0x2 - cpid: dd 0x0 - eol: db 0xa - msg: db "Welcome to deadbeef shell", 0x21, 0x0a - prompt: db "[0xdeadbeef]", 0x20 - nfe: db "Error: program not found", 0x2e, 0x0a - env: db "/etc/environment", 0x0 - boem: db "Error: input overflows buffer", 0x2e, 0x0a -- cgit v1.2.3 From 7cc39751094583703bee1e320e8fe8e1939e97f0 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 21 Jun 2016 18:28:23 +0100 Subject: Replaced all the mov *, 0s with xor *,* for speed --- sh.asm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sh.asm b/sh.asm index 123d772..53fe533 100644 --- a/sh.asm +++ b/sh.asm @@ -56,7 +56,7 @@ _rloop: _rloopr: mov rax, sys_read - mov rdi, 0x0 + xor rdi, rdi mov rsi, r15 mov rdx, 0xff syscall @@ -81,7 +81,7 @@ _exec: mov rax, stub_execve mov rdi, r13 mov rsi, r14 - mov rdx, 0x0 + xor rdx, rdx syscall ; and now kill the child process @@ -106,7 +106,7 @@ _wait4_it: _parse_path: mov rax, sys_open mov rdi, env - mov rsi, 0x0 + xor rsi, rsi syscall cmp rax, 0x0 jl _quit @@ -409,5 +409,5 @@ _weol: _quit: mov rax, sys_exit - mov rdi, 0x0 + xor rdi, rdi syscall -- cgit v1.2.3 From 1e521c68bbe4e41b42ac00ff9a0562bf6c5eb15e Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 21 Jun 2016 18:32:42 +0100 Subject: Removed needless use of cmp --- sh | Bin 3768 -> 3760 bytes sh.asm | 1 - sh.o | Bin 4048 -> 4048 bytes 3 files changed, 1 deletion(-) diff --git a/sh b/sh index b8aae89..27a7358 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 53fe533..36e22e6 100644 --- a/sh.asm +++ b/sh.asm @@ -210,7 +210,6 @@ _parse1: ;the end of the string or directory name _parse1r: dec r8 - cmp r8, 0x0 jl _parse2 jmp _parse1 diff --git a/sh.o b/sh.o index 164fa55..cef7bce 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From d850a1b51c2029c092269173b51a14de4c9fa9d1 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 21 Jun 2016 18:55:23 +0100 Subject: Replaced usage of sys_open to check if executable --- sh | Bin 3760 -> 3824 bytes sh.asm | 39 +++++++++++++++++---------------------- sh.o | Bin 4048 -> 4112 bytes 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/sh b/sh index 27a7358..e88dabd 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 36e22e6..951af91 100644 --- a/sh.asm +++ b/sh.asm @@ -8,11 +8,17 @@ section .data env: db "/etc/environment", 0x0 boem: db "Error: input overflows buffer", 0x2e, `\n` + ;Flag indicating ability to execute + X_OK equ 0x1 + + ;Indicates that a file can be accessed in the way specified + F_OK equ 0x0 ;Syscall constants sys_read equ 0x00 sys_write equ 0x01 sys_open equ 0x02 sys_close equ 0x03 + sys_access equ 0x15 stub_fork equ 0x39 stub_execve equ 0x3b sys_exit equ 0x3c @@ -254,44 +260,33 @@ _parse4: ; not found syscall jmp _rloop -; Checks that the program rbx exists before doing anything, this is hacky, slow -; and should therefore be replaced by a version using sys_access with flag X_OK +; Checks that the program rbx can be executed _parse5: pop r8 - mov rax, sys_open - mov rdi, rbx - xor rsi, rsi - xor rdx, rdx + mov rax, sys_access + mov rdi, rbx + mov rsi, X_OK syscall - ;Check if it was opened successfully to check if it exists - cmp rax, 0x0 + ;Check if it can be executed + cmp rax, F_OK jl _parse3 - mov rdi, rax - mov rax, sys_close - syscall - ; We'll fall through to here if the file is accessible, therefore we should execute it _parse6: mov r13, rbx jmp _exec _parse7: ; absolute path - mov rax, sys_open - mov rdi, r15 - xor rsi, rsi - xor rdx, rdx - syscall + mov rax, sys_access + mov rdi, rbx + mov rsi, X_OK + syscall - cmp rax, 0x0 + cmp rax, F_OK jl _parse4 - mov rdi, rax - mov rax, sys_close - syscall - mov r13, r15 jmp _exec diff --git a/sh.o b/sh.o index cef7bce..4bee425 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From 71b9646e8256179f9b0f013d7399bc98a47c4663 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 21 Jun 2016 20:41:31 +0100 Subject: Switched to using sys_waitid since wait4 is deprecated --- sh | Bin 3824 -> 3864 bytes sh.asm | 22 ++++++++++++---------- sh.o | Bin 4112 -> 4176 bytes 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/sh b/sh index e88dabd..33ac2fc 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 951af91..6b19535 100644 --- a/sh.asm +++ b/sh.asm @@ -24,6 +24,7 @@ section .data sys_exit equ 0x3c sys_wait4 equ 0x3d sys_kill equ 0x3e + sys_waitid equ 0xf7 section .text @@ -81,7 +82,7 @@ _exec: syscall mov dword [cpid], eax cmp dword [cpid], 0x0 - jne _wait4_it + jne _wait_for_proc ; now that we know what to execute, do so mov rax, stub_execve @@ -93,14 +94,15 @@ _exec: ; and now kill the child process jmp _quit -_wait4_it: - ;This function is obsolete, replace with 247 (sys_waitid) - mov rdi, rax - mov rax, sys_wait4 - xor rsi, rsi ; null status pointer - xor rdx, rdx - xor rcx, rcx ; null rusage pointer - syscall +;waits for the process to close +_wait_for_proc: + mov rsi, rax + mov rax, sys_waitid + mov rdi, 0 + xor rdx, rdx + xor r10, r8 + xor r8, r8 + syscall mov dword [cpid], -0x1 jmp _rloop @@ -232,7 +234,7 @@ _subz: ;Splits at the current position and puts the address of the argument on the stack _subzp: mov byte [r8+r15], 0x0 - lea rax, [r8+r15+0x1] + lea rax, [r8+r15+1] push rax jmp _parse1 diff --git a/sh.o b/sh.o index 4bee425..c6b7ddf 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From 946aadad12b4b095dce1ba67d824bf7750101134 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 21 Jun 2016 20:50:39 +0100 Subject: Relied on the value of r8 accidentally, oops --- sh | Bin 3864 -> 3872 bytes sh.asm | 5 ++++- sh.o | Bin 4176 -> 4176 bytes 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/sh b/sh index 33ac2fc..7644a0b 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 6b19535..e50d941 100644 --- a/sh.asm +++ b/sh.asm @@ -13,6 +13,9 @@ section .data ;Indicates that a file can be accessed in the way specified F_OK equ 0x0 + + ;option for waitid + P_PGID equ 2 ;Syscall constants sys_read equ 0x00 sys_write equ 0x01 @@ -100,7 +103,7 @@ _wait_for_proc: mov rax, sys_waitid mov rdi, 0 xor rdx, rdx - xor r10, r8 + mov r10, P_PGID xor r8, r8 syscall diff --git a/sh.o b/sh.o index c6b7ddf..0638b78 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From d6f8317ebfbfdc2a5e492fcc5023dd320e41569a Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 21 Jun 2016 21:04:45 +0100 Subject: Fixed bug where string length was being overwritten --- sh | Bin 3872 -> 3904 bytes sh.asm | 7 ++++++- sh.o | Bin 4176 -> 4192 bytes 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/sh b/sh index 7644a0b..32fb4c7 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index e50d941..cdebb94 100644 --- a/sh.asm +++ b/sh.asm @@ -13,7 +13,7 @@ section .data ;Indicates that a file can be accessed in the way specified F_OK equ 0x0 - + ;option for waitid P_PGID equ 2 ;Syscall constants @@ -99,6 +99,8 @@ _exec: ;waits for the process to close _wait_for_proc: + mov r12, r10 ;backup + mov rsi, rax mov rax, sys_waitid mov rdi, 0 @@ -107,6 +109,9 @@ _wait_for_proc: xor r8, r8 syscall + mov r10, r12 ;restore + ;mov r10,30 + mov dword [cpid], -0x1 jmp _rloop diff --git a/sh.o b/sh.o index 0638b78..ff0858c 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From 32110b5a5f404206e2a4fb5b3d9f2ae2a874f29b Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 21 Jun 2016 21:14:24 +0100 Subject: Sped up a push --- sh | Bin 3904 -> 3904 bytes sh.asm | 5 ++--- sh.o | Bin 4192 -> 4192 bytes 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/sh b/sh index 32fb4c7..19f1ea8 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index cdebb94..6a6232c 100644 --- a/sh.asm +++ b/sh.asm @@ -110,7 +110,6 @@ _wait_for_proc: syscall mov r10, r12 ;restore - ;mov r10,30 mov dword [cpid], -0x1 jmp _rloop @@ -206,8 +205,8 @@ _parse: ; needs special commands: cd exit export eval ;syscall 80 is chdir, so do that first, it's easiest ;It takes a path string. ;also needs pipes (|, >, <) - push 0x0 - xor r13, r13 + xor r13, r13 ;for _strlen + push r13 ;push 0 jmp _strlen ;r8 holds strlen so we start at the end and parse backwards diff --git a/sh.o b/sh.o index ff0858c..d9a09f8 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From 49441d8b2043c9ae4381855e73a3880278ae39f0 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sun, 26 Jun 2016 14:24:29 +0100 Subject: Added exit builtin --- sh | Bin 3904 -> 4328 bytes sh.asm | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++------------ sh.o | Bin 4192 -> 4672 bytes 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/sh b/sh index 19f1ea8..2233f3e 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 6a6232c..4770cac 100644 --- a/sh.asm +++ b/sh.asm @@ -7,7 +7,8 @@ section .data nfe: db "Error: program not found", 0x2e, `\n` env: db "/etc/environment", 0x0 boem: db "Error: input overflows buffer", 0x2e, `\n` - + invalid_int_str: db "Error: Invalid integer",`\n` + invalid_int_str_len: equ $-invalid_int_str ;Flag indicating ability to execute X_OK equ 0x1 @@ -53,7 +54,7 @@ _start: jmp _parse_path -_rloop: +_read_loop: mov rax, sys_write mov rdi, 0x1 mov rsi, prompt @@ -61,10 +62,10 @@ _rloop: syscall mov r8, -0x1 - mov r12, _rloopr + mov r12, _read_loopr jmp _bzero -_rloopr: +_read_loopr: mov rax, sys_read xor rdi, rdi mov rsi, r15 @@ -75,6 +76,8 @@ _rloopr: mov r12, _quit cmp byte [r15], 0x0 je _weol + ;Start using r12 to hold the number of args + xor r12,r12 ; call _parse jmp _parse @@ -89,9 +92,9 @@ _exec: ; now that we know what to execute, do so mov rax, stub_execve - mov rdi, r13 - mov rsi, r14 - xor rdx, rdx + mov rdi, r13 ;Filename + mov rsi, r14 ;argv + xor rdx, rdx ;envp syscall ; and now kill the child process @@ -112,7 +115,7 @@ _wait_for_proc: mov r10, r12 ;restore mov dword [cpid], -0x1 - jmp _rloop + jmp _read_loop ; input: none ; output: r9 (path elements), r10 (length) @@ -179,7 +182,7 @@ _pathender: jne _pathender mov byte [r8], 0x0 mov r9, rsp - jmp _rloop + jmp _read_loop _pathender1: inc r10 @@ -243,6 +246,7 @@ _subzp: mov byte [r8+r15], 0x0 lea rax, [r8+r15+1] push rax + inc r12 jmp _parse1 _parse2: @@ -256,6 +260,13 @@ _parse3: inc r8 cmp r8, r10 je _parse4 + ;r15 holds the name that the user entered, so we'll use that for shell builtins + mov rcx,`\0\0\0exit\0` + mov r13, [r15] ; Add exit codes later + shl r13, 24 ;Remove the last character + cmp r13, rcx + je _builtin_exit + ;END OF BUILTINS mov r13, r15 mov rcx, [r9+r8*8] push r8 @@ -267,17 +278,16 @@ _parse4: ; not found mov rsi, nfe mov rdx, 0x1a syscall - jmp _rloop + jmp _read_loop ; Checks that the program rbx can be executed _parse5: - pop r8 - mov rax, sys_access mov rdi, rbx mov rsi, X_OK syscall + pop r8 ;Check if it can be executed cmp rax, F_OK jl _parse3 @@ -396,7 +406,7 @@ __sigint_c: ; kill the child syscall _sigint_nc: - mov r12, _rloop + mov r12, _read_loop jmp _weol ; input: r12 (return address) @@ -414,3 +424,38 @@ _quit: mov rax, sys_exit xor rdi, rdi syscall + +;usage: exit [status] +;exits with the given status or 0 if one hasn't been provided +_builtin_exit: + ;convert first arg to an int if present + ;exit with that code + _string_to_int: + xor rdi, rdi + ;accumulate in rdi since it's also the register used to provide the status + ;to sys_exit + xor rdx, rdx + cmp r12, 0 + jz _string_to_int_end + _string_to_int_loop: + mov dl, [rax] ; Convert this to a number + cmp dl, 0 ;Checks for NULL + jz _string_to_int_end + sub dl, `0` ;sub because we'll need to do this anyway + jl _invalid_int + cmp dl, 9 ;since we've already subtracted we can just compase with 9 + jg _invalid_int + imul rdi,10 + add rdi, rdx + inc rax + jmp _string_to_int_loop + _string_to_int_end: + mov rax, sys_exit + syscall +_invalid_int: + mov rax,sys_write + mov rdi, 0x1 + mov rsi, invalid_int_str + mov rdx, invalid_int_str_len + syscall + jmp _read_loop diff --git a/sh.o b/sh.o index d9a09f8..554e951 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From 040f747ed405dbcb26fe2a30771c4f1c35056fbc Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sun, 26 Jun 2016 14:44:36 +0100 Subject: Added cd --- sh | Bin 4328 -> 4448 bytes sh.asm | 18 ++++++++++++++++-- sh.o | Bin 4672 -> 4784 bytes 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/sh b/sh index 2233f3e..80f47f7 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 4770cac..bc6bea8 100644 --- a/sh.asm +++ b/sh.asm @@ -28,6 +28,7 @@ section .data sys_exit equ 0x3c sys_wait4 equ 0x3d sys_kill equ 0x3e + sys_chdir equ 0x50 sys_waitid equ 0xf7 @@ -262,10 +263,15 @@ _parse3: je _parse4 ;r15 holds the name that the user entered, so we'll use that for shell builtins mov rcx,`\0\0\0exit\0` - mov r13, [r15] ; Add exit codes later - shl r13, 24 ;Remove the last character + mov r13, [r15] + shl r13, 24 ;Remove the last character, because it's an arg not the name of it cmp r13, rcx je _builtin_exit + mov rcx,`\0\0\0\0\0cd\0` + shl r13, 16 ;Remove the extra characters for cd, note how we're doing the + ;longest function names first then the shorter later + cmp r13, rcx + je _builtin_cd ;END OF BUILTINS mov r13, r15 mov rcx, [r9+r8*8] @@ -425,6 +431,14 @@ _quit: xor rdi, rdi syscall +;cd [dir] +;changes to the given directory +_builtin_cd: + ;stub atm + mov rdi,rax + mov rax, sys_chdir + syscall + jmp _read_loop ;usage: exit [status] ;exits with the given status or 0 if one hasn't been provided _builtin_exit: diff --git a/sh.o b/sh.o index 554e951..ebf5bca 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From 875e9b8008f2d3b0d45adf8bfa70e2a9851478bb Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sun, 26 Jun 2016 14:45:25 +0100 Subject: Oops, old comment --- sh.asm | 1 - 1 file changed, 1 deletion(-) diff --git a/sh.asm b/sh.asm index bc6bea8..a239844 100644 --- a/sh.asm +++ b/sh.asm @@ -434,7 +434,6 @@ _quit: ;cd [dir] ;changes to the given directory _builtin_cd: - ;stub atm mov rdi,rax mov rax, sys_chdir syscall -- cgit v1.2.3 From 6fe5395074c753b3915db9c3ca9da544249d5593 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Thu, 14 Jul 2016 22:17:44 +0100 Subject: Added error message for invalid cd attempts --- sh | Bin 4448 -> 4576 bytes sh.asm | 13 +++++++++++-- sh.o | Bin 4784 -> 4928 bytes 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/sh b/sh index 80f47f7..42f7bed 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index a239844..81c7977 100644 --- a/sh.asm +++ b/sh.asm @@ -7,7 +7,9 @@ section .data nfe: db "Error: program not found", 0x2e, `\n` env: db "/etc/environment", 0x0 boem: db "Error: input overflows buffer", 0x2e, `\n` - invalid_int_str: db "Error: Invalid integer",`\n` + no_dir_str: db "cd: Unknown directory",`\n` + no_dir_str_len: equ $-no_dir_str + invalid_int_str: db "exit: Invalid integer",`\n` invalid_int_str_len: equ $-invalid_int_str ;Flag indicating ability to execute X_OK equ 0x1 @@ -431,12 +433,19 @@ _quit: xor rdi, rdi syscall -;cd [dir] +;cd [dir (rax)] ;changes to the given directory _builtin_cd: mov rdi,rax mov rax, sys_chdir syscall + cmp rax, 0 + jz _read_loop + mov rax, sys_write + mov rdi, 0x1 + mov rsi, no_dir_str + mov rdx, no_dir_str_len + syscall jmp _read_loop ;usage: exit [status] ;exits with the given status or 0 if one hasn't been provided diff --git a/sh.o b/sh.o index ebf5bca..d5ee55b 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From 97f8ecb25566b3eb0431f7cdc42e712638ec7c3b Mon Sep 17 00:00:00 2001 From: faissaloo Date: Fri, 15 Jul 2016 17:21:45 +0100 Subject: Speed improvements --- .sh.s.swp | Bin 1024 -> 0 bytes sh.asm | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 .sh.s.swp diff --git a/.sh.s.swp b/.sh.s.swp deleted file mode 100644 index 9c0da39..0000000 Binary files a/.sh.s.swp and /dev/null differ diff --git a/sh.asm b/sh.asm index 81c7977..0ef79d4 100644 --- a/sh.asm +++ b/sh.asm @@ -109,7 +109,7 @@ _wait_for_proc: mov rsi, rax mov rax, sys_waitid - mov rdi, 0 + xor rdi, rdi xor rdx, rdx mov r10, P_PGID xor r8, r8 -- cgit v1.2.3 From b6099ab700d1af16647c1bc8d17f2b4d84bfd338 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Fri, 22 Jul 2016 16:00:38 +0100 Subject: Switched to a faster cpid check, as well as other 0 dependant checks --- sh | Bin 4576 -> 4568 bytes sh.asm | 4 ++-- sh.o | Bin 4928 -> 4912 bytes 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sh b/sh index 42f7bed..291733a 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 0ef79d4..d899a31 100644 --- a/sh.asm +++ b/sh.asm @@ -90,7 +90,7 @@ _exec: mov rax, stub_fork syscall mov dword [cpid], eax - cmp dword [cpid], 0x0 + test eax, eax ;checks if cpid is 0 jne _wait_for_proc ; now that we know what to execute, do so @@ -146,7 +146,7 @@ _parse_path2: jl _quit mov r8, r9 dec r8 - +; Reads the PATH variable _pathfinder: inc r8 xor rax, rax diff --git a/sh.o b/sh.o index d5ee55b..621e66e 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From e255b0dca861700d5b6358db8855b037268be8bb Mon Sep 17 00:00:00 2001 From: faissaloo Date: Fri, 22 Jul 2016 16:02:51 +0100 Subject: Oops, forgot to stage some changes --- sh | Bin 4568 -> 4560 bytes sh.asm | 10 +++++----- sh.o | Bin 4912 -> 4896 bytes 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/sh b/sh index 291733a..946ab75 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index d899a31..edcf859 100644 --- a/sh.asm +++ b/sh.asm @@ -129,7 +129,7 @@ _parse_path: mov rdi, env xor rsi, rsi syscall - cmp rax, 0x0 + test rax, rax ;check if rax is 0 jl _quit mov r8, -0x1 @@ -142,7 +142,7 @@ _parse_path2: mov rsi, r9 mov rdx, 0xff syscall - cmp rax, 0x0 + test rax, rax jl _quit mov r8, r9 dec r8 @@ -168,7 +168,7 @@ _pathfinder: mov byte al, [r8+5] cmp al, `"` jne _pathfinder - cmp al, 0x0 + test al, al ;checks for 0 je _quit add r8, 0x6 mov r10, 0x1 @@ -439,7 +439,7 @@ _builtin_cd: mov rdi,rax mov rax, sys_chdir syscall - cmp rax, 0 + test rax, rax jz _read_loop mov rax, sys_write mov rdi, 0x1 @@ -461,7 +461,7 @@ _builtin_exit: jz _string_to_int_end _string_to_int_loop: mov dl, [rax] ; Convert this to a number - cmp dl, 0 ;Checks for NULL + test dl, dl ;Checks for NULL jz _string_to_int_end sub dl, `0` ;sub because we'll need to do this anyway jl _invalid_int diff --git a/sh.o b/sh.o index 621e66e..e9ab2e1 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From de50b0dfe1cd2dd9ef7e4c02b43dbf129bf977d0 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Fri, 22 Jul 2016 21:01:04 +0100 Subject: Added constants for special file descriptors for readability --- sh | Bin 4560 -> 4664 bytes sh.asm | 20 +++++++++++++------- sh.o | Bin 4896 -> 4992 bytes 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/sh b/sh index 946ab75..42a2402 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index edcf859..b20eaec 100644 --- a/sh.asm +++ b/sh.asm @@ -19,6 +19,12 @@ section .data ;option for waitid P_PGID equ 2 + + ;Special file descriptors + fd_stdin equ 0x00 + fd_stdout equ 0x01 + fd_stderr equ 0x02 + ;Syscall constants sys_read equ 0x00 sys_write equ 0x01 @@ -50,7 +56,7 @@ _start: mov rbx, rsp mov rax, sys_write - mov rdi, 0x1 + mov rdi, fd_stdout mov rsi, msg mov rdx, 0x1b syscall @@ -59,7 +65,7 @@ _start: _read_loop: mov rax, sys_write - mov rdi, 0x1 + mov rdi, fd_stdout mov rsi, prompt mov rdx, 0xd syscall @@ -70,7 +76,7 @@ _read_loop: _read_loopr: mov rax, sys_read - xor rdi, rdi + xor rdi, rdi ;stdin mov rsi, r15 mov rdx, 0xff syscall @@ -282,7 +288,7 @@ _parse3: _parse4: ; not found mov rax, sys_write - mov rdi, 0x1 + mov rdi, fd_stdout mov rsi, nfe mov rdx, 0x1a syscall @@ -422,7 +428,7 @@ _sigint_nc: ; Writes end of line to the terminal _weol: mov rax, sys_write - mov rdi, 0x1 + mov rdi, fd_stdout mov rsi, eol mov rdx, 0x1 syscall @@ -442,7 +448,7 @@ _builtin_cd: test rax, rax jz _read_loop mov rax, sys_write - mov rdi, 0x1 + mov rdi, fd_stdout mov rsi, no_dir_str mov rdx, no_dir_str_len syscall @@ -476,7 +482,7 @@ _builtin_exit: syscall _invalid_int: mov rax,sys_write - mov rdi, 0x1 + mov rdi, fd_stdout mov rsi, invalid_int_str mov rdx, invalid_int_str_len syscall diff --git a/sh.o b/sh.o index e9ab2e1..a03720b 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From 6c812fc1cabb155e0fc41d14332a3859cccc5df4 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Fri, 22 Jul 2016 22:16:11 +0100 Subject: Code cleanup --- sh | Bin 4664 -> 4808 bytes sh.asm | 241 ++++++++++++++++++++++++++++++++++------------------------------- sh.o | Bin 4992 -> 5136 bytes 3 files changed, 125 insertions(+), 116 deletions(-) diff --git a/sh b/sh index 42a2402..6d7cffe 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index b20eaec..474433b 100644 --- a/sh.asm +++ b/sh.asm @@ -1,43 +1,51 @@ section .data - sigs: dd 0x2 - cpid: dd 0x0 - eol: db `\n` - msg: db "Welcome to deadbeef shell", 0x21, `\n` - prompt: db "[0xdeadbeef]", 0x20 - nfe: db "Error: program not found", 0x2e, `\n` - env: db "/etc/environment", 0x0 - boem: db "Error: input overflows buffer", 0x2e, `\n` - no_dir_str: db "cd: Unknown directory",`\n` - no_dir_str_len: equ $-no_dir_str - invalid_int_str: db "exit: Invalid integer",`\n` - invalid_int_str_len: equ $-invalid_int_str - ;Flag indicating ability to execute - X_OK equ 0x1 - - ;Indicates that a file can be accessed in the way specified - F_OK equ 0x0 - - ;option for waitid - P_PGID equ 2 + sigs dd 0x2 + cpid dd 0x0 + eol db `\n` + msg db "Welcome to deadbeef shell!", `\n` + env_str db "/etc/environment", 0x0 + + prompt_str db "[0xdeadbeef] " + prompt_str_len equ $-prompt_str - ;Special file descriptors - fd_stdin equ 0x00 - fd_stdout equ 0x01 - fd_stderr equ 0x02 - - ;Syscall constants - sys_read equ 0x00 - sys_write equ 0x01 - sys_open equ 0x02 - sys_close equ 0x03 - sys_access equ 0x15 - stub_fork equ 0x39 - stub_execve equ 0x3b - sys_exit equ 0x3c - sys_wait4 equ 0x3d - sys_kill equ 0x3e - sys_chdir equ 0x50 - sys_waitid equ 0xf7 + not_func_str db "Error: program not found.", `\n` + not_func_str_len equ $-not_func_str + + boe_str db "Error: input overflows buffer.", `\n` + boe_str_len equ $-boe_str + + no_dir_str db "cd: Unknown directory.",`\n` + no_dir_str_len equ $-no_dir_str + + invalid_int_str db "exit: Invalid integer.",`\n` + invalid_int_str_len equ $-invalid_int_str + ;Flag indicating ability to execute + X_OK equ 0x1 + + ;Indicates that a file can be accessed in the way specified + F_OK equ 0x0 + + ;option for waitid + P_PGID equ 2 + + ;Special file descriptors + fd_stdin equ 0x00 + fd_stdout equ 0x01 + fd_stderr equ 0x02 + + ;Syscall constants + sys_read equ 0x00 + sys_write equ 0x01 + sys_open equ 0x02 + sys_close equ 0x03 + sys_access equ 0x15 + stub_fork equ 0x39 + stub_execve equ 0x3b + sys_exit equ 0x3c + sys_wait4 equ 0x3d + sys_kill equ 0x3e + sys_chdir equ 0x50 + sys_waitid equ 0xf7 section .text @@ -66,8 +74,8 @@ _start: _read_loop: mov rax, sys_write mov rdi, fd_stdout - mov rsi, prompt - mov rdx, 0xd + mov rsi, prompt_str + mov rdx, prompt_str_len syscall mov r8, -0x1 @@ -85,8 +93,8 @@ _read_loopr: mov r12, _quit cmp byte [r15], 0x0 je _weol - ;Start using r12 to hold the number of args - xor r12,r12 + ;Start using r12 to hold the number of args + xor r12,r12 ; call _parse jmp _parse @@ -111,17 +119,17 @@ _exec: ;waits for the process to close _wait_for_proc: - mov r12, r10 ;backup + mov r12, r10 ;backup - mov rsi, rax - mov rax, sys_waitid - xor rdi, rdi - xor rdx, rdx - mov r10, P_PGID - xor r8, r8 - syscall + mov rsi, rax + mov rax, sys_waitid + xor rdi, rdi + xor rdx, rdx + mov r10, P_PGID + xor r8, r8 + syscall - mov r10, r12 ;restore + mov r10, r12 ;restore mov dword [cpid], -0x1 jmp _read_loop @@ -132,7 +140,7 @@ _wait_for_proc: ; other: r15 (buffer) _parse_path: mov rax, sys_open - mov rdi, env + mov rdi, env_str xor rsi, rsi syscall test rax, rax ;check if rax is 0 @@ -213,7 +221,7 @@ _bzero: ;input: r15 (buffer) ;output: r13 (path), r14 (arguments) ;clobbered: r8, rax, rbx, rcx -_parse: ; needs special commands: cd exit export eval +_parse: ;syscall 80 is chdir, so do that first, it's easiest ;It takes a path string. ;also needs pipes (|, >, <) @@ -255,7 +263,7 @@ _subzp: mov byte [r8+r15], 0x0 lea rax, [r8+r15+1] push rax - inc r12 + inc r12 jmp _parse1 _parse2: @@ -269,40 +277,41 @@ _parse3: inc r8 cmp r8, r10 je _parse4 - ;r15 holds the name that the user entered, so we'll use that for shell builtins - mov rcx,`\0\0\0exit\0` - mov r13, [r15] - shl r13, 24 ;Remove the last character, because it's an arg not the name of it - cmp r13, rcx - je _builtin_exit - mov rcx,`\0\0\0\0\0cd\0` - shl r13, 16 ;Remove the extra characters for cd, note how we're doing the - ;longest function names first then the shorter later - cmp r13, rcx - je _builtin_cd - ;END OF BUILTINS + ; Builtins to add: export, eval + ;r15 holds the name that the user entered, so we'll use that for shell builtins + mov rcx,`\0\0\0exit\0` + mov r13, [r15] + shl r13, 24 ;Remove the last character in r13, because it's an arg not the name of it + cmp r13, rcx + je _builtin_exit + mov rcx,`\0\0\0\0\0cd\0` + shl r13, 16 ;Remove the extra characters for cd, note how we're doing the + ;longest function names first then the shorter later + cmp r13, rcx + je _builtin_cd + ;END OF BUILTINS mov r13, r15 mov rcx, [r9+r8*8] push r8 jmp _concat -_parse4: ; not found +_parse4: ; Function not found mov rax, sys_write mov rdi, fd_stdout - mov rsi, nfe - mov rdx, 0x1a + mov rsi, not_func_str + mov rdx, not_func_str_len syscall jmp _read_loop ; Checks that the program rbx can be executed _parse5: - mov rax, sys_access - mov rdi, rbx - mov rsi, X_OK + mov rax, sys_access + mov rdi, rbx + mov rsi, X_OK syscall - pop r8 - ;Check if it can be executed + pop r8 + ;Check if it can be executed cmp rax, F_OK jl _parse3 @@ -312,10 +321,10 @@ _parse6: jmp _exec _parse7: ; absolute path - mov rax, sys_access - mov rdi, rbx - mov rsi, X_OK - syscall + mov rax, sys_access + mov rdi, rbx + mov rsi, X_OK + syscall cmp rax, F_OK jl _parse4 @@ -404,8 +413,8 @@ _join2: _boe: mov rax, sys_write mov rdi, 0x1 - mov rsi, boem - mov rdx, 0x1f + mov rsi, boe_str + mov rdx, boe_str_len jmp _quit ; interrupt signal handler @@ -442,48 +451,48 @@ _quit: ;cd [dir (rax)] ;changes to the given directory _builtin_cd: - mov rdi,rax - mov rax, sys_chdir - syscall - test rax, rax - jz _read_loop - mov rax, sys_write - mov rdi, fd_stdout - mov rsi, no_dir_str - mov rdx, no_dir_str_len - syscall - jmp _read_loop + mov rdi,rax + mov rax, sys_chdir + syscall + test rax, rax + jz _read_loop + mov rax, sys_write + mov rdi, fd_stdout + mov rsi, no_dir_str + mov rdx, no_dir_str_len + syscall + jmp _read_loop ;usage: exit [status] ;exits with the given status or 0 if one hasn't been provided _builtin_exit: - ;convert first arg to an int if present - ;exit with that code - _string_to_int: - xor rdi, rdi - ;accumulate in rdi since it's also the register used to provide the status - ;to sys_exit - xor rdx, rdx - cmp r12, 0 - jz _string_to_int_end - _string_to_int_loop: - mov dl, [rax] ; Convert this to a number - test dl, dl ;Checks for NULL - jz _string_to_int_end - sub dl, `0` ;sub because we'll need to do this anyway - jl _invalid_int - cmp dl, 9 ;since we've already subtracted we can just compase with 9 - jg _invalid_int - imul rdi,10 - add rdi, rdx - inc rax - jmp _string_to_int_loop - _string_to_int_end: + ;convert first arg to an int if present + ;exit with that code + _string_to_int: + xor rdi, rdi + ;accumulate in rdi since it's also the register used to provide the status + ;to sys_exit + xor rdx, rdx + cmp r12, 0 + jz _string_to_int_end + _string_to_int_loop: + mov dl, [rax] ; Convert this to a number + test dl, dl ;Checks for NULL + jz _string_to_int_end + sub dl, `0` ;sub because we'll need to do this anyway + jl _invalid_int + cmp dl, 9 ;since we've already subtracted we can just compase with 9 + jg _invalid_int + imul rdi,10 + add rdi, rdx + inc rax + jmp _string_to_int_loop + _string_to_int_end: mov rax, sys_exit syscall _invalid_int: - mov rax,sys_write + mov rax,sys_write mov rdi, fd_stdout mov rsi, invalid_int_str mov rdx, invalid_int_str_len - syscall - jmp _read_loop + syscall + jmp _read_loop diff --git a/sh.o b/sh.o index a03720b..818a5f6 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From 8d4fcd8fc87cb9a83fc88d2ddbd3fcab82142a97 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Fri, 22 Jul 2016 22:18:04 +0100 Subject: Removed welocem message bloat --- sh | Bin 4808 -> 4704 bytes sh.asm | 9 +-------- sh.o | Bin 5136 -> 5008 bytes 3 files changed, 1 insertion(+), 8 deletions(-) diff --git a/sh b/sh index 6d7cffe..2f2d395 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 474433b..38d47a8 100644 --- a/sh.asm +++ b/sh.asm @@ -2,12 +2,11 @@ section .data sigs dd 0x2 cpid dd 0x0 eol db `\n` - msg db "Welcome to deadbeef shell!", `\n` env_str db "/etc/environment", 0x0 prompt_str db "[0xdeadbeef] " prompt_str_len equ $-prompt_str - + not_func_str db "Error: program not found.", `\n` not_func_str_len equ $-not_func_str @@ -63,12 +62,6 @@ _start: sub rsp, 0x110 mov rbx, rsp - mov rax, sys_write - mov rdi, fd_stdout - mov rsi, msg - mov rdx, 0x1b - syscall - jmp _parse_path _read_loop: diff --git a/sh.o b/sh.o index 818a5f6..37df737 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From ece7bd6dc17ba91deff32a9c121af80bcc1e79f2 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sat, 23 Jul 2016 18:47:54 +0100 Subject: Switched from weird jump system to a normal call + ret --- sh | Bin 4704 -> 4688 bytes sh.asm | 11 ++++------- sh.o | Bin 5008 -> 4912 bytes 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/sh b/sh index 2f2d395..f1a78d3 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 38d47a8..a1dc0be 100644 --- a/sh.asm +++ b/sh.asm @@ -72,8 +72,7 @@ _read_loop: syscall mov r8, -0x1 - mov r12, _read_loopr - jmp _bzero + call _bzero _read_loopr: mov rax, sys_read @@ -140,8 +139,7 @@ _parse_path: jl _quit mov r8, -0x1 - mov r12, _parse_path2 - jmp _bzero + call _bzero _parse_path2: mov rdi, rax @@ -209,7 +207,7 @@ _bzero: mov byte [r8+r15], 0x0 cmp r8, 0xff jle _bzero - jmp r12 + ret ;input: r15 (buffer) ;output: r13 (path), r14 (arguments) @@ -372,8 +370,7 @@ _treg: jg _boe xchg r15, rbx mov r8, -0x1 - mov r12, _jrsinc - jmp _bzero + call _bzero _jrsinc: xchg r15, rbx diff --git a/sh.o b/sh.o index 37df737..d628f83 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From c2f00cb50d8507c83b45798fb287dff407d18cdd Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sat, 23 Jul 2016 19:31:17 +0100 Subject: Path finder now compares a qword at a time for speed --- sh | Bin 4688 -> 4664 bytes sh.asm | 32 +++++++++++--------------------- sh.o | Bin 4912 -> 4880 bytes 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/sh b/sh index f1a78d3..c41dcb8 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index a1dc0be..0c5d707 100644 --- a/sh.asm +++ b/sh.asm @@ -154,27 +154,17 @@ _parse_path2: ; Reads the PATH variable _pathfinder: inc r8 - xor rax, rax - mov byte al, [r8] - cmp al, `P` - jne _pathfinder - mov byte al, [r8+1] - cmp al, `A` - jne _pathfinder - mov byte al, [r8+2] - cmp al, `T` - jne _pathfinder - mov byte al, [r8+3] - cmp al, `H` - jne _pathfinder - mov byte al, [r8+4] - cmp al, `=` - jne _pathfinder - mov byte al, [r8+5] - cmp al, `"` - jne _pathfinder - test al, al ;checks for 0 - je _quit + + ;Check that we've found the PATH variable + mov r10, `\0\0PATH="` + mov rax, [r8] + shl rax, 16 + cmp rax,r10 + jne _pathfinder ;if we haven't, let's move right one and see if it's there + shr rax, 56 + test al, al ;checks for a null terminator after 'PATH=' + jz _quit + add r8, 0x6 mov r10, 0x1 push 0x0 diff --git a/sh.o b/sh.o index d628f83..1487269 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From c2b6ae2d55e3eba64e10eaeb39c126ab569e85ae Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sat, 23 Jul 2016 19:34:05 +0100 Subject: Oops, was moving the string into r10 every loop --- sh | Bin 4664 -> 4664 bytes sh.asm | 3 ++- sh.o | Bin 4880 -> 4880 bytes 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/sh b/sh index c41dcb8..c4ab5f8 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 0c5d707..81f616f 100644 --- a/sh.asm +++ b/sh.asm @@ -151,12 +151,13 @@ _parse_path2: jl _quit mov r8, r9 dec r8 + ; Reads the PATH variable + mov r10, `\0\0PATH="` _pathfinder: inc r8 ;Check that we've found the PATH variable - mov r10, `\0\0PATH="` mov rax, [r8] shl rax, 16 cmp rax,r10 diff --git a/sh.o b/sh.o index 1487269..a108f8e 100644 Binary files a/sh.o and b/sh.o differ -- cgit v1.2.3 From 5532c77d3fbc6417fd1a54eb4a5a3dbef0f5497a Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sat, 23 Jul 2016 19:41:37 +0100 Subject: Better makefile, cleans up after itself, makes smaller executables and hassle free debug --- makefile | 7 +++++++ sh | Bin 4664 -> 1704 bytes sh.o | Bin 4880 -> 0 bytes 3 files changed, 7 insertions(+) delete mode 100644 sh.o diff --git a/makefile b/makefile index 3725a30..e86f784 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,12 @@ all: nasm sh.asm -f elf64 -o sh.o ld sh.o -o sh + strip sh + rm sh.o +dbg: + nasm sh.asm -f elf64 -o sh.o + ld sh.o -o sh + rm sh.o + gdb --eval-command="layout asm" -tui sh clean: rm -f sh.o sh diff --git a/sh b/sh index c4ab5f8..72cb49c 100755 Binary files a/sh and b/sh differ diff --git a/sh.o b/sh.o deleted file mode 100644 index a108f8e..0000000 Binary files a/sh.o and /dev/null differ -- cgit v1.2.3 From 5de12284461b784dac2e5c074909265183d4ef75 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sat, 23 Jul 2016 19:58:05 +0100 Subject: Made gdb quiet --- makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefile b/makefile index e86f784..dece0b1 100644 --- a/makefile +++ b/makefile @@ -7,6 +7,6 @@ dbg: nasm sh.asm -f elf64 -o sh.o ld sh.o -o sh rm sh.o - gdb --eval-command="layout asm" -tui sh + gdb -q --eval-command="layout asm" -tui sh clean: rm -f sh.o sh -- cgit v1.2.3 From 0b8b4c47a55e647932edc89d994483a56eb22291 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sun, 24 Jul 2016 11:51:04 +0100 Subject: We can zero the buffer faster now --- sh | Bin 1704 -> 1688 bytes sh.asm | 15 +++++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/sh b/sh index 72cb49c..b603f81 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 81f616f..de56eaf 100644 --- a/sh.asm +++ b/sh.asm @@ -71,7 +71,6 @@ _read_loop: mov rdx, prompt_str_len syscall - mov r8, -0x1 call _bzero _read_loopr: @@ -138,7 +137,6 @@ _parse_path: test rax, rax ;check if rax is 0 jl _quit - mov r8, -0x1 call _bzero _parse_path2: @@ -191,13 +189,15 @@ _pathender1: jmp _pathender ;Writes 0s in buffer r15 until r8 is 255 -;input: r8 (iterator), r15 (buffer), r12 (return address) +;input: r15 (buffer) ;output: none _bzero: - inc r8 - mov byte [r8+r15], 0x0 - cmp r8, 0xff - jle _bzero + xor r8, r8 + _bzero_main: + movups [r15+r8], xmm0 + add r8, 16 + cmp r8, 0xff + jl _bzero_main ret ;input: r15 (buffer) @@ -360,7 +360,6 @@ _treg: cmp r8, 0xff jg _boe xchg r15, rbx - mov r8, -0x1 call _bzero _jrsinc: -- cgit v1.2.3 From b170a6bcb443f6e7ac045de38aef3da78be179a6 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sun, 24 Jul 2016 14:03:55 +0100 Subject: Bugfix for directory parsing not working properly --- sh | Bin 1688 -> 1688 bytes sh.asm | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sh b/sh index b603f81..4257040 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index de56eaf..063bae1 100644 --- a/sh.asm +++ b/sh.asm @@ -231,9 +231,9 @@ _parse1r: jmp _parse1 _sabsf: - cmp r8, 0x1 - jg _parse1r - mov r13, 0x1 + test r8, r8 + je _parse1r + mov r13, 0x2 jmp _parse1r _subz: -- cgit v1.2.3 From 2601b8ee61315a8faac65ded8950cebf168f83c2 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Mon, 25 Jul 2016 10:43:50 +0100 Subject: Faster strlen --- sh | Bin 1688 -> 1688 bytes sh.asm | 18 +++++++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sh b/sh index 4257040..55db507 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 063bae1..ef147ac 100644 --- a/sh.asm +++ b/sh.asm @@ -318,15 +318,15 @@ _parse7: ; absolute path ;input: r12 (return address), r15 (buffer) ;output: r8 (length) _strlen: - xor r8, r8 - -_strlen1: - cmp byte [r8+r15], 0x0 - je _parse1 - cmp byte [r8+r15], 0xa - je _parse1 - inc r8 - jmp _strlen1 + mov r8, -17 ;We're always going to have to take 1 away because of the newline + xchg rsi, rcx ;bacup rcx, let rsi get clobbered + _strlen1: + add r8, 16 + PcmpIstrI xmm0, [r15+r8],0 + jnz _strlen1 + add r8, rcx + xchg rsi,rcx + jmp _parse1 ; input: rcx (a), r13 (b) ; output: rbx (string) -- cgit v1.2.3 From e6e385e3a5b8a875d728706181078f01749e8882 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Mon, 25 Jul 2016 13:07:30 +0100 Subject: Another bugfix for directory parsing being completely broken --- sh | Bin 1688 -> 1728 bytes sh.asm | 14 +++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/sh b/sh index 55db507..1efff95 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index ef147ac..9aafb7d 100644 --- a/sh.asm +++ b/sh.asm @@ -2,6 +2,7 @@ section .data sigs dd 0x2 cpid dd 0x0 eol db `\n` + eol_mask db `\n`,`\n`,`\n`,`\n`,`\n`,`\n`,`\n`,`\n`,`\n`,`\n`,`\n`,`\n`,`\n`,`\n`,`\n`,`\n` env_str db "/etc/environment", 0x0 prompt_str db "[0xdeadbeef] " @@ -231,9 +232,9 @@ _parse1r: jmp _parse1 _sabsf: - test r8, r8 - je _parse1r - mov r13, 0x2 + cmp r8, 1 + jg _parse1r + mov r13, 1 jmp _parse1r _subz: @@ -304,7 +305,7 @@ _parse6: _parse7: ; absolute path mov rax, sys_access - mov rdi, rbx + mov rdi, r15 mov rsi, X_OK syscall @@ -318,12 +319,15 @@ _parse7: ; absolute path ;input: r12 (return address), r15 (buffer) ;output: r8 (length) _strlen: - mov r8, -17 ;We're always going to have to take 1 away because of the newline + mov r8, -16 xchg rsi, rcx ;bacup rcx, let rsi get clobbered + MovDqU xmm1, [eol_mask] _strlen1: add r8, 16 PcmpIstrI xmm0, [r15+r8],0 jnz _strlen1 + PcmpIstrI xmm1, [r15+r8],0 + jnz _strlen1 add r8, rcx xchg rsi,rcx jmp _parse1 -- cgit v1.2.3 From 441b0c35789af6c0f104b06f95d464b19031b47b Mon Sep 17 00:00:00 2001 From: faissaloo Date: Mon, 25 Jul 2016 18:38:32 +0100 Subject: strlen can now be used in other areas --- sh | Bin 1728 -> 1728 bytes sh.asm | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sh b/sh index 1efff95..de08c70 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 9aafb7d..90333eb 100644 --- a/sh.asm +++ b/sh.asm @@ -210,7 +210,7 @@ _parse: ;also needs pipes (|, >, <) xor r13, r13 ;for _strlen push r13 ;push 0 - jmp _strlen + call _strlen ;r8 holds strlen so we start at the end and parse backwards ; go through and sub/push @@ -316,7 +316,7 @@ _parse7: ; absolute path jmp _exec -;input: r12 (return address), r15 (buffer) +;input: r15 (buffer) ;output: r8 (length) _strlen: mov r8, -16 @@ -330,7 +330,7 @@ _strlen: jnz _strlen1 add r8, rcx xchg rsi,rcx - jmp _parse1 + ret ; input: rcx (a), r13 (b) ; output: rbx (string) -- cgit v1.2.3 From ba03889949cede5e1a857beac4ea3cc1b8b2bd69 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 26 Jul 2016 17:39:35 +0100 Subject: Faster concatenation of directory + file name --- sh | Bin 1728 -> 1656 bytes sh.asm | 116 ++++++++++++++++++++++++++++------------------------------------- 2 files changed, 49 insertions(+), 67 deletions(-) diff --git a/sh b/sh index de08c70..76d3769 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 90333eb..ce94392 100644 --- a/sh.asm +++ b/sh.asm @@ -273,10 +273,10 @@ _parse3: cmp r13, rcx je _builtin_cd ;END OF BUILTINS - mov r13, r15 - mov rcx, [r9+r8*8] + ;mov r13, r15 + mov rdi, [r9+r8*8] push r8 - jmp _concat + jmp _concat_dir _parse4: ; Function not found mov rax, sys_write @@ -317,83 +317,65 @@ _parse7: ; absolute path ;input: r15 (buffer) +;Clobbered: rsi ;output: r8 (length) _strlen: - mov r8, -16 + xor r8, r8 xchg rsi, rcx ;bacup rcx, let rsi get clobbered MovDqU xmm1, [eol_mask] _strlen1: - add r8, 16 - PcmpIstrI xmm0, [r15+r8],0 - jnz _strlen1 - PcmpIstrI xmm1, [r15+r8],0 - jnz _strlen1 + PcmpIstrI xmm1, [r15+r8], 122 + jz _strlen_end + PcmpIstrI xmm0, [r15+r8], 122 + jz _strlen_end + add r8, 15 + jmp _strlen1 + _strlen_end: + inc rcx add r8, rcx xchg rsi,rcx ret -; input: rcx (a), r13 (b) +; input: rdi (a), r15 (b) ; output: rbx (string) ; clobbered: r8, r11 -_concat: - mov r8, -0x1 - jmp _jlen1 - -_jlen1: +_concat_dir: + xor r8,r8 ;new string's length (and for _dirty_strcpy_1, also the + ;initial string length) + ;Copies the contents of rdi to rbx in blocks of + ;16, doesn't bother to clean up stuff past the null + ;so moves that too, but it doesn't matter, just make + ;sure the buffer is big enough + _dirty_strcpy_1: + movDqU xmm2, [rdi+r8] + movDqU [rbx+r8],xmm2 + PcmpIstrI xmm1, xmm2, 122 ;if there was a newline in the + ;copied string, exit loop + jz _dirty_strcpy_1_exit + PcmpIstrI xmm0, xmm2, 122 ;if there was a null in the + ;copied string exit loop + jz _dirty_strcpy_1_exit + add r8,15 + jmp _dirty_strcpy_1 + _dirty_strcpy_1_exit: + add r8,rcx inc r8 - cmp byte [r8+rcx], 0x0 - je _jlen02 - cmp byte [r8+rcx], 0xa - je _jlen02 - jmp _jlen1 - -_jlen02: - dec r8 - mov r11, -0x1 - -_jlen2: + mov byte [rbx+r8], '/' inc r8 - inc r11 - cmp byte [r11+r13], 0x0 - je _treg - cmp byte [r11+r13], 0xa - je _treg - jmp _jlen2 - -_treg: - cmp r8, 0xff - jg _boe - xchg r15, rbx - call _bzero - -_jrsinc: - xchg r15, rbx - mov r8, -0x1 - mov r11, -0x1 - -_join1: - inc r8 - cmp byte [r8+rcx], 0x0 - je _join02 - cmp byte [r8+rcx], 0xa - je _join02 - mov al, [r8+rcx] - mov [r8+rbx], al - jmp _join1 - -_join02: - mov byte [r8+rbx], `/` -_join2: - inc r8 - inc r11 - cmp byte [r11+r13], 0x0 - je _parse5 - cmp byte [r11+r13], `\n` - je _parse5 - mov al, [r11+r13] - mov [r8+rbx], al - jmp _join2 - + xor r11, r11 + _dirty_strcpy_2: + movdqu xmm2, [r15+r11] + movdqu [rbx+r8], xmm2 + + pcmpistri xmm1, xmm2, 122 + jz _parse5 + + pcmpistri xmm0, xmm2, 122 + jz _parse5 + + add r8, 15 + add r11, 15 + jmp _dirty_strcpy_2 _boe: mov rax, sys_write mov rdi, 0x1 -- cgit v1.2.3 From cbf3b52eff5a10bacc40ca54eeb8c50ad991a461 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 26 Jul 2016 17:44:42 +0100 Subject: Oh good, apparently I can do away with this utter tripe here --- q.log | 17 +++++++++++++++++ sh | Bin 1656 -> 1648 bytes sh.asm | 6 ------ 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 q.log diff --git a/q.log b/q.log new file mode 100644 index 0000000..0d9508e --- /dev/null +++ b/q.log @@ -0,0 +1,17 @@ +This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian) (preloaded format=amstex 2016.6.16) 26 JUL 2016 17:44 +entering extended mode + restricted \write18 enabled. + %&-line parsing enabled. +**q +(/usr/share/texlive/texmf-dist/tex/latex/tools/q.tex + +AmS-TeX- Version 2.2 + +File ignored +) +! Emergency stop. +<*> q + +*** (job aborted, no legal \end found) + +No pages of output. diff --git a/sh b/sh index 76d3769..36dbb89 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index ce94392..8189393 100644 --- a/sh.asm +++ b/sh.asm @@ -349,9 +349,6 @@ _concat_dir: _dirty_strcpy_1: movDqU xmm2, [rdi+r8] movDqU [rbx+r8],xmm2 - PcmpIstrI xmm1, xmm2, 122 ;if there was a newline in the - ;copied string, exit loop - jz _dirty_strcpy_1_exit PcmpIstrI xmm0, xmm2, 122 ;if there was a null in the ;copied string exit loop jz _dirty_strcpy_1_exit @@ -367,9 +364,6 @@ _concat_dir: movdqu xmm2, [r15+r11] movdqu [rbx+r8], xmm2 - pcmpistri xmm1, xmm2, 122 - jz _parse5 - pcmpistri xmm0, xmm2, 122 jz _parse5 -- cgit v1.2.3 From 69bcd9da88a418642444ebc6de66cbe7b1b3d8d3 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 26 Jul 2016 17:45:18 +0100 Subject: Shakes fist in air furiously --- q.log | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 q.log diff --git a/q.log b/q.log deleted file mode 100644 index 0d9508e..0000000 --- a/q.log +++ /dev/null @@ -1,17 +0,0 @@ -This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian) (preloaded format=amstex 2016.6.16) 26 JUL 2016 17:44 -entering extended mode - restricted \write18 enabled. - %&-line parsing enabled. -**q -(/usr/share/texlive/texmf-dist/tex/latex/tools/q.tex - -AmS-TeX- Version 2.2 - -File ignored -) -! Emergency stop. -<*> q - -*** (job aborted, no legal \end found) - -No pages of output. -- cgit v1.2.3 From 03e957f08f8072d32fdb75e36a937562c3f25495 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 26 Jul 2016 18:40:12 +0100 Subject: I need to remember to make before testing next time lol, yeah it doesn't work --- sh | Bin 1648 -> 1656 bytes sh.asm | 7 +++++++ 2 files changed, 7 insertions(+) diff --git a/sh b/sh index 36dbb89..76d3769 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 8189393..5257997 100644 --- a/sh.asm +++ b/sh.asm @@ -349,6 +349,10 @@ _concat_dir: _dirty_strcpy_1: movDqU xmm2, [rdi+r8] movDqU [rbx+r8],xmm2 + + pcmpistri xmm1, xmm2, 122 + jz _dirty_strcpy_1_exit + PcmpIstrI xmm0, xmm2, 122 ;if there was a null in the ;copied string exit loop jz _dirty_strcpy_1_exit @@ -364,6 +368,9 @@ _concat_dir: movdqu xmm2, [r15+r11] movdqu [rbx+r8], xmm2 + pcmpistri xmm1, xmm2, 122 + jz _parse5 + pcmpistri xmm0, xmm2, 122 jz _parse5 -- cgit v1.2.3 From 3f58585ff37ffca0f757b41a77b9e23a90393ab2 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Tue, 26 Jul 2016 18:43:03 +0100 Subject: Reduced jumps --- sh | Bin 1656 -> 1648 bytes sh.asm | 138 ++++++++++++++++++++++++++++++++--------------------------------- 2 files changed, 68 insertions(+), 70 deletions(-) diff --git a/sh b/sh index 76d3769..da90772 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 5257997..d638653 100644 --- a/sh.asm +++ b/sh.asm @@ -63,7 +63,68 @@ _start: sub rsp, 0x110 mov rbx, rsp - jmp _parse_path + ; input: none + ; output: r9 (path elements), r10 (length) + ; clobbered: r8, r12 + ; other: r15 (buffer) + _parse_path: + mov rax, sys_open + mov rdi, env_str + xor rsi, rsi + syscall + test rax, rax ;check if rax is 0 + jl _quit + + call _bzero + + _parse_path2: + mov rdi, rax + mov rax, sys_read + mov rsi, r9 + mov rdx, 0xff + syscall + test rax, rax + jl _quit + mov r8, r9 + dec r8 + + ; Reads the PATH variable + mov r10, `\0\0PATH="` + _pathfinder: + inc r8 + + ;Check that we've found the PATH variable + mov rax, [r8] + shl rax, 16 + cmp rax,r10 + jne _pathfinder ;if we haven't, let's move right one and see if it's there + shr rax, 56 + test al, al ;checks for a null terminator after 'PATH=' + jz _quit + + add r8, 0x6 + mov r10, 0x1 + push 0x0 + push r8 + dec r8 + + _pathender: + inc r8 + mov al, [r8] + cmp al, `:` + je _pathender1 + cmp al, `"` + jne _pathender + mov byte [r8], 0x0 + mov r9, rsp + jmp _read_loop + + _pathender1: + inc r10 + mov byte [r8], 0x0 + lea rcx, [r8+0x1] + push rcx + jmp _pathender _read_loop: mov rax, sys_write @@ -126,69 +187,6 @@ _wait_for_proc: mov dword [cpid], -0x1 jmp _read_loop -; input: none -; output: r9 (path elements), r10 (length) -; clobbered: r8, r12 -; other: r15 (buffer) -_parse_path: - mov rax, sys_open - mov rdi, env_str - xor rsi, rsi - syscall - test rax, rax ;check if rax is 0 - jl _quit - - call _bzero - -_parse_path2: - mov rdi, rax - mov rax, sys_read - mov rsi, r9 - mov rdx, 0xff - syscall - test rax, rax - jl _quit - mov r8, r9 - dec r8 - -; Reads the PATH variable - mov r10, `\0\0PATH="` -_pathfinder: - inc r8 - - ;Check that we've found the PATH variable - mov rax, [r8] - shl rax, 16 - cmp rax,r10 - jne _pathfinder ;if we haven't, let's move right one and see if it's there - shr rax, 56 - test al, al ;checks for a null terminator after 'PATH=' - jz _quit - - add r8, 0x6 - mov r10, 0x1 - push 0x0 - push r8 - dec r8 - -_pathender: - inc r8 - mov al, [r8] - cmp al, `:` - je _pathender1 - cmp al, `"` - jne _pathender - mov byte [r8], 0x0 - mov r9, rsp - jmp _read_loop - -_pathender1: - inc r10 - mov byte [r8], 0x0 - lea rcx, [r8+0x1] - push rcx - jmp _pathender - ;Writes 0s in buffer r15 until r8 is 255 ;input: r15 (buffer) ;output: none @@ -340,7 +338,7 @@ _strlen: ; output: rbx (string) ; clobbered: r8, r11 _concat_dir: - xor r8,r8 ;new string's length (and for _dirty_strcpy_1, also the + xor r8,r8 ;new string's length (and for _dirty_strcpy_1, also the ;initial string length) ;Copies the contents of rdi to rbx in blocks of ;16, doesn't bother to clean up stuff past the null @@ -349,10 +347,10 @@ _concat_dir: _dirty_strcpy_1: movDqU xmm2, [rdi+r8] movDqU [rbx+r8],xmm2 - + pcmpistri xmm1, xmm2, 122 jz _dirty_strcpy_1_exit - + PcmpIstrI xmm0, xmm2, 122 ;if there was a null in the ;copied string exit loop jz _dirty_strcpy_1_exit @@ -367,13 +365,13 @@ _concat_dir: _dirty_strcpy_2: movdqu xmm2, [r15+r11] movdqu [rbx+r8], xmm2 - + pcmpistri xmm1, xmm2, 122 jz _parse5 - + pcmpistri xmm0, xmm2, 122 jz _parse5 - + add r8, 15 add r11, 15 jmp _dirty_strcpy_2 -- cgit v1.2.3 From 39182a45f3161a8f4862b1a3086ae0f37be963c2 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Wed, 27 Jul 2016 12:37:01 +0100 Subject: All buffers are now allocated by .bss and use macros for size --- sh | Bin 1648 -> 1656 bytes sh.asm | 36 ++++++++++++++++-------------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/sh b/sh index da90772..abae0ca 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index d638653..acae19b 100644 --- a/sh.asm +++ b/sh.asm @@ -11,9 +11,6 @@ section .data not_func_str db "Error: program not found.", `\n` not_func_str_len equ $-not_func_str - boe_str db "Error: input overflows buffer.", `\n` - boe_str_len equ $-boe_str - no_dir_str db "cd: Unknown directory.",`\n` no_dir_str_len equ $-no_dir_str @@ -47,6 +44,16 @@ section .data sys_chdir equ 0x50 sys_waitid equ 0xf7 +section .bss + ;These must all be divisible by 16! + input_buffer_size equ 4096 + input_buffer resb input_buffer_size + + path_buffer_size equ 4096 + path_buffer resb path_buffer_size + + concat_buffer_size equ 8192 + concat_buffer resb concat_buffer_size ;Double so the path and input can never overflow concat_buffer when combined section .text global _start @@ -54,14 +61,9 @@ section .text _start: ; TODO: handle interrupt signal - push rbp - mov rbp, rsp - sub rsp, 0x110 - mov r15, rsp - sub rsp, 0x110 - mov r9, rsp - sub rsp, 0x110 - mov rbx, rsp + mov r15, input_buffer ;stdin input + mov r9, path_buffer ;PATH variable reader + mov rbx, concat_buffer ;Concatenated directory and command name ; input: none ; output: r9 (path elements), r10 (length) @@ -81,7 +83,7 @@ _start: mov rdi, rax mov rax, sys_read mov rsi, r9 - mov rdx, 0xff + mov rdx, path_buffer_size syscall test rax, rax jl _quit @@ -139,7 +141,7 @@ _read_loopr: mov rax, sys_read xor rdi, rdi ;stdin mov rsi, r15 - mov rdx, 0xff + mov rdx, input_buffer_size syscall ; check for _quit @@ -195,7 +197,7 @@ _bzero: _bzero_main: movups [r15+r8], xmm0 add r8, 16 - cmp r8, 0xff + cmp r8, input_buffer_size jl _bzero_main ret @@ -375,12 +377,6 @@ _concat_dir: add r8, 15 add r11, 15 jmp _dirty_strcpy_2 -_boe: - mov rax, sys_write - mov rdi, 0x1 - mov rsi, boe_str - mov rdx, boe_str_len - jmp _quit ; interrupt signal handler _sigint: -- cgit v1.2.3 From 58cc2a376aa1b94e7e2f9bbd6a058f2ff7db992a Mon Sep 17 00:00:00 2001 From: faissaloo Date: Wed, 27 Jul 2016 13:03:00 +0100 Subject: Moved some stuff around to reduce jumps --- sh | Bin 1656 -> 1664 bytes sh.asm | 90 ++++++++++++++++++++++++++++++++--------------------------------- 2 files changed, 44 insertions(+), 46 deletions(-) diff --git a/sh b/sh index abae0ca..9ca3978 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index acae19b..806fd84 100644 --- a/sh.asm +++ b/sh.asm @@ -48,10 +48,10 @@ section .bss ;These must all be divisible by 16! input_buffer_size equ 4096 input_buffer resb input_buffer_size - + path_buffer_size equ 4096 path_buffer resb path_buffer_size - + concat_buffer_size equ 8192 concat_buffer resb concat_buffer_size ;Double so the path and input can never overflow concat_buffer when combined @@ -252,7 +252,7 @@ _subzp: _parse2: push rbx mov r14, rsp - cmp r13, 0x0 + test r13, r13 jg _parse7 mov r8, -0x1 @@ -276,7 +276,47 @@ _parse3: ;mov r13, r15 mov rdi, [r9+r8*8] push r8 - jmp _concat_dir + ; input: rdi (a), r15 (b) + ; output: rbx (string) + ; clobbered: r8, r11 + _concat_dir: + xor r8,r8 ;new string's length (and for _dirty_strcpy_1, also the + ;initial string length) + ;Copies the contents of rdi to rbx in blocks of + ;16, doesn't bother to clean up stuff past the null + ;so moves that too, but it doesn't matter, just make + ;sure the buffer is big enough + _dirty_strcpy_1: + movDqU xmm2, [rdi+r8] + movDqU [rbx+r8],xmm2 + + pcmpistri xmm1, xmm2, 122 + jz _dirty_strcpy_1_exit + + PcmpIstrI xmm0, xmm2, 122 ;if there was a null in the + ;copied string exit loop + jz _dirty_strcpy_1_exit + add r8,15 + jmp _dirty_strcpy_1 + _dirty_strcpy_1_exit: + add r8,rcx + inc r8 + mov byte [rbx+r8], '/' + inc r8 + xor r11, r11 + _dirty_strcpy_2: + movdqu xmm2, [r15+r11] + movdqu [rbx+r8], xmm2 + + pcmpistri xmm1, xmm2, 122 + jz _parse5 + + pcmpistri xmm0, xmm2, 122 + jz _parse5 + + add r8, 15 + add r11, 15 + jmp _dirty_strcpy_2 _parse4: ; Function not found mov rax, sys_write @@ -336,48 +376,6 @@ _strlen: xchg rsi,rcx ret -; input: rdi (a), r15 (b) -; output: rbx (string) -; clobbered: r8, r11 -_concat_dir: - xor r8,r8 ;new string's length (and for _dirty_strcpy_1, also the - ;initial string length) - ;Copies the contents of rdi to rbx in blocks of - ;16, doesn't bother to clean up stuff past the null - ;so moves that too, but it doesn't matter, just make - ;sure the buffer is big enough - _dirty_strcpy_1: - movDqU xmm2, [rdi+r8] - movDqU [rbx+r8],xmm2 - - pcmpistri xmm1, xmm2, 122 - jz _dirty_strcpy_1_exit - - PcmpIstrI xmm0, xmm2, 122 ;if there was a null in the - ;copied string exit loop - jz _dirty_strcpy_1_exit - add r8,15 - jmp _dirty_strcpy_1 - _dirty_strcpy_1_exit: - add r8,rcx - inc r8 - mov byte [rbx+r8], '/' - inc r8 - xor r11, r11 - _dirty_strcpy_2: - movdqu xmm2, [r15+r11] - movdqu [rbx+r8], xmm2 - - pcmpistri xmm1, xmm2, 122 - jz _parse5 - - pcmpistri xmm0, xmm2, 122 - jz _parse5 - - add r8, 15 - add r11, 15 - jmp _dirty_strcpy_2 - ; interrupt signal handler _sigint: cmp dword [cpid], 0x0 -- cgit v1.2.3 From 6cc41e91a21660769a0fba1b591dbf1cfe97b922 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Wed, 27 Jul 2016 13:24:32 +0100 Subject: Some more speed up and formatting --- sh | Bin 1664 -> 1648 bytes sh.asm | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sh b/sh index 9ca3978..fb20e3e 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 806fd84..638614d 100644 --- a/sh.asm +++ b/sh.asm @@ -189,7 +189,7 @@ _wait_for_proc: mov dword [cpid], -0x1 jmp _read_loop -;Writes 0s in buffer r15 until r8 is 255 +;Writes 0s in buffer r15 until r8 reaches input_buffer_size ;input: r15 (buffer) ;output: none _bzero: @@ -431,7 +431,7 @@ _builtin_exit: ;accumulate in rdi since it's also the register used to provide the status ;to sys_exit xor rdx, rdx - cmp r12, 0 + test r12, r12 jz _string_to_int_end _string_to_int_loop: mov dl, [rax] ; Convert this to a number @@ -441,7 +441,7 @@ _builtin_exit: jl _invalid_int cmp dl, 9 ;since we've already subtracted we can just compase with 9 jg _invalid_int - imul rdi,10 + imul rdi, 10 add rdi, rdx inc rax jmp _string_to_int_loop -- cgit v1.2.3 From 7618a44fd16275445b9dc062f4ac50b8d0ff129f Mon Sep 17 00:00:00 2001 From: faissaloo Date: Thu, 28 Jul 2016 12:18:16 +0100 Subject: Added support for /etc/environments that have no speech marks --- environment | 1 + sh | Bin 1648 -> 1664 bytes sh.asm | 24 ++++++++++++++++-------- 3 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 environment diff --git a/environment b/environment new file mode 100644 index 0000000..8232286 --- /dev/null +++ b/environment @@ -0,0 +1 @@ +PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games diff --git a/sh b/sh index fb20e3e..eda9e13 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 638614d..369157f 100644 --- a/sh.asm +++ b/sh.asm @@ -91,20 +91,22 @@ _start: dec r8 ; Reads the PATH variable - mov r10, `\0\0PATH="` + mov r10, `\0\0\0PATH=` _pathfinder: inc r8 ;Check that we've found the PATH variable mov rax, [r8] - shl rax, 16 - cmp rax,r10 + shl rax, 24 + cmp rax, r10 jne _pathfinder ;if we haven't, let's move right one and see if it's there - shr rax, 56 - test al, al ;checks for a null terminator after 'PATH=' + cmp byte [r8], `"` + jne _pathfinder_continue + inc r8 + cmp byte [r8], 0 jz _quit - - add r8, 0x6 + _pathfinder_continue: + add r8, 0x5 mov r10, 0x1 push 0x0 push r8 @@ -116,7 +118,13 @@ _start: cmp al, `:` je _pathender1 cmp al, `"` - jne _pathender + je _pathender_exit + cmp al, `\n` + je _pathender_exit + cmp al, 0 + jz _pathender_exit + jmp _pathender + _pathender_exit: mov byte [r8], 0x0 mov r9, rsp jmp _read_loop -- cgit v1.2.3 From ffe8d881dd5fc91abc125b8d0802198dfbffc69d Mon Sep 17 00:00:00 2001 From: faissaloo Date: Thu, 28 Jul 2016 12:18:46 +0100 Subject: Oops, left this test thing here --- environment | 1 - 1 file changed, 1 deletion(-) delete mode 100644 environment diff --git a/environment b/environment deleted file mode 100644 index 8232286..0000000 --- a/environment +++ /dev/null @@ -1 +0,0 @@ -PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games -- cgit v1.2.3 From d3facea389360f784d4261288df9682fd41b6ac8 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Fri, 5 Aug 2016 16:37:33 +0100 Subject: Speed up for sys_read --- sh | Bin 1664 -> 1656 bytes sh.asm | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sh b/sh index eda9e13..7b767f3 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 369157f..3cc195c 100644 --- a/sh.asm +++ b/sh.asm @@ -81,7 +81,7 @@ _start: _parse_path2: mov rdi, rax - mov rax, sys_read + xor rax, rax ;sys_read mov rsi, r9 mov rdx, path_buffer_size syscall @@ -146,7 +146,7 @@ _read_loop: call _bzero _read_loopr: - mov rax, sys_read + xor rax, rax ;sys_read xor rdi, rdi ;stdin mov rsi, r15 mov rdx, input_buffer_size -- cgit v1.2.3 From eb1a7da82481e51e3c3e46ae2428f31f90d20288 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Fri, 5 Aug 2016 17:25:07 +0100 Subject: Updated README --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 1690ca5..c369464 100644 --- a/README.md +++ b/README.md @@ -1 +1,14 @@ # deadbeef-shell +=== +The deadbeef shell is a fast, minimalist shell written in x86_64 +Assembly for Linux. Currently it contains two builtins: cd and exit. It +can execute any program as a direct path (/path/to/program), from the PATH +variable (progam) or as a relative path (./program). Arguments are +separated by spaces. +To do (lowest to highest priority): + - Implement environment variables, pass them to programs and provide + export. + - Implement variables ($variablename). + - Implement providing a file as an argument instead of stdin so that the + shell can be used as an interpreter. + - Implement speech marks to separate arguments. -- cgit v1.2.3 From 40fe0b2671f5fec3dd00afc51fbed683258c8992 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Fri, 5 Aug 2016 17:38:57 +0100 Subject: Accidentally broke PATHs with speech marks --- sh | Bin 1656 -> 1656 bytes sh.asm | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/sh b/sh index 7b767f3..ae1e2f3 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 3cc195c..d43b1dd 100644 --- a/sh.asm +++ b/sh.asm @@ -101,7 +101,7 @@ _start: cmp rax, r10 jne _pathfinder ;if we haven't, let's move right one and see if it's there cmp byte [r8], `"` - jne _pathfinder_continue + je _pathfinder_continue inc r8 cmp byte [r8], 0 jz _quit -- cgit v1.2.3 From 5c12d1d842da344230a9d8b4b3f0121bef307257 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Fri, 5 Aug 2016 17:40:23 +0100 Subject: Out of date binary --- sh | Bin 1656 -> 1656 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/sh b/sh index ae1e2f3..6564fc2 100755 Binary files a/sh and b/sh differ -- cgit v1.2.3 From 1b716d42c6bb31c48c6b8df0b5b5c5118f723ef2 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Fri, 5 Aug 2016 19:42:28 +0100 Subject: Fixed parsing for PATH variables with or without speech marks --- sh | Bin 1656 -> 1664 bytes sh.asm | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sh b/sh index 6564fc2..2c3bb6d 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index d43b1dd..30f91b7 100644 --- a/sh.asm +++ b/sh.asm @@ -100,10 +100,10 @@ _start: shl rax, 24 cmp rax, r10 jne _pathfinder ;if we haven't, let's move right one and see if it's there - cmp byte [r8], `"` - je _pathfinder_continue + cmp byte [r8+5], `"` + jne _pathfinder_continue inc r8 - cmp byte [r8], 0 + cmp byte [r8+5], 0 jz _quit _pathfinder_continue: add r8, 0x5 -- cgit v1.2.3 From 7c3153078c0491448fffb952ad6df9278dc3a695 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Mon, 10 Oct 2016 15:55:23 +0100 Subject: PATH is now aquired from envp by default --- sh | Bin 1664 -> 4744 bytes sh.asm | 109 +++++++++++++++++++++++++---------------------------------------- 2 files changed, 41 insertions(+), 68 deletions(-) diff --git a/sh b/sh index 2c3bb6d..8481381 100755 Binary files a/sh and b/sh differ diff --git a/sh.asm b/sh.asm index 30f91b7..3d98ee0 100644 --- a/sh.asm +++ b/sh.asm @@ -60,9 +60,15 @@ section .text _start: ; TODO: handle interrupt signal - + _handle_args: + pop r15 ;argc + _handle_args_loop: + pop r9 ;argv[i] + ;Arg value + dec r15 + jnz _handle_args_loop + add rsp, 8 ;Remove the 0 between envp and argv mov r15, input_buffer ;stdin input - mov r9, path_buffer ;PATH variable reader mov rbx, concat_buffer ;Concatenated directory and command name ; input: none @@ -70,72 +76,39 @@ _start: ; clobbered: r8, r12 ; other: r15 (buffer) _parse_path: - mov rax, sys_open - mov rdi, env_str - xor rsi, rsi - syscall - test rax, rax ;check if rax is 0 - jl _quit - - call _bzero - - _parse_path2: - mov rdi, rax - xor rax, rax ;sys_read - mov rsi, r9 - mov rdx, path_buffer_size - syscall - test rax, rax - jl _quit - mov r8, r9 - dec r8 - - ; Reads the PATH variable - mov r10, `\0\0\0PATH=` - _pathfinder: - inc r8 - - ;Check that we've found the PATH variable - mov rax, [r8] - shl rax, 24 - cmp rax, r10 - jne _pathfinder ;if we haven't, let's move right one and see if it's there - cmp byte [r8+5], `"` - jne _pathfinder_continue - inc r8 - cmp byte [r8+5], 0 - jz _quit - _pathfinder_continue: - add r8, 0x5 - mov r10, 0x1 - push 0x0 - push r8 - dec r8 - - _pathender: - inc r8 - mov al, [r8] - cmp al, `:` - je _pathender1 - cmp al, `"` - je _pathender_exit - cmp al, `\n` - je _pathender_exit - cmp al, 0 - jz _pathender_exit - jmp _pathender - _pathender_exit: - mov byte [r8], 0x0 - mov r9, rsp - jmp _read_loop - - _pathender1: - inc r10 - mov byte [r8], 0x0 - lea rcx, [r8+0x1] - push rcx - jmp _pathender - + mov r10, `\0\0\0PATH=` + _parse_path_loop: + pop r8 + ;If we get to the end and find no path exit + cmp r8, 0 + je _quit + mov r12, [r8] + shl r12, 24 + cmp r12, r10 + jne _parse_path_loop + ;The start of the array + add r8, 5 + mov r10, 1 + push 0 + push r8 + dec r8 + + _path_split_loop: + inc r8 + mov al, [r8] + cmp al, 0 + je _path_split_exit + + cmp al, `:` + jne _path_split_loop + ;Add the path to the stack + inc r10 + mov byte [r8], 0 + lea rcx, [r8+1] + push rcx + jmp _path_split_loop + _path_split_exit: + mov r9, rsp _read_loop: mov rax, sys_write mov rdi, fd_stdout -- cgit v1.2.3