mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-29 13:04:27 +01:00
userland: add assembly support
Move arm assembly cheat here, and start some work on x86 cheat as well.
This commit is contained in:
16
userland/arch/x86_64/c/add.c
Normal file
16
userland/arch/x86_64/c/add.c
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main(void) {
|
||||
uint64_t in1 = 0xFFFFFFFF;
|
||||
uint64_t in2 = 0x1;
|
||||
uint64_t out;
|
||||
__asm__ (
|
||||
"lea (%[in1], %[in2]), %[out];"
|
||||
: [out] "=r" (out)
|
||||
: [in1] "r" (in1),
|
||||
[in2] "r" (in2)
|
||||
:
|
||||
);
|
||||
assert(out == 0x100000000);
|
||||
}
|
||||
20
userland/arch/x86_64/c/binutils_hack.c
Normal file
20
userland/arch/x86_64/c/binutils_hack.c
Normal file
@@ -0,0 +1,20 @@
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#your-first-binutils-hack */
|
||||
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main(void) {
|
||||
#if 0
|
||||
uint64_t in = 0xFFFFFFFF;
|
||||
uint64_t out = 0;
|
||||
__asm__ (
|
||||
"mov %[in], %%rax;"
|
||||
"myinc %%rax;"
|
||||
"movq %%rax, %[out]"
|
||||
: [out] "=g" (out)
|
||||
: [in] "g" (in)
|
||||
: "rax"
|
||||
);
|
||||
assert(out == in + 1);
|
||||
#endif
|
||||
}
|
||||
18
userland/arch/x86_64/c/binutils_nohack.c
Normal file
18
userland/arch/x86_64/c/binutils_nohack.c
Normal file
@@ -0,0 +1,18 @@
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#your-first-binutils-hack */
|
||||
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main(void) {
|
||||
uint64_t in = 0xFFFFFFFF;
|
||||
uint64_t out = 0;
|
||||
__asm__ (
|
||||
"mov %[in], %%rax;"
|
||||
"inc %%rax;"
|
||||
"movq %%rax, %[out]"
|
||||
: [out] "=g" (out)
|
||||
: [in] "g" (in)
|
||||
: "rax"
|
||||
);
|
||||
assert(out == in + 1);
|
||||
}
|
||||
1
userland/arch/x86_64/c/build
Symbolic link
1
userland/arch/x86_64/c/build
Symbolic link
@@ -0,0 +1 @@
|
||||
../build
|
||||
1
userland/arch/x86_64/c/freestanding/build
Symbolic link
1
userland/arch/x86_64/c/freestanding/build
Symbolic link
@@ -0,0 +1 @@
|
||||
../build
|
||||
31
userland/arch/x86_64/c/freestanding/hello.c
Normal file
31
userland/arch/x86_64/c/freestanding/hello.c
Normal file
@@ -0,0 +1,31 @@
|
||||
/* Linux freestanding hello world with inline assembly..*/
|
||||
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include <inttypes.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
ssize_t my_write(int fd, const void *buf, size_t size) {
|
||||
ssize_t ret;
|
||||
__asm__ __volatile__ (
|
||||
"syscall"
|
||||
: "=a" (ret)
|
||||
: "0" (1), "D" (fd), "S" (buf), "d" (size)
|
||||
: "cc", "rcx", "r11", "memory"
|
||||
);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void my_exit(int exit_status) {
|
||||
ssize_t ret;
|
||||
__asm__ __volatile__ (
|
||||
"syscall"
|
||||
: "=a" (ret)
|
||||
: "0" (60), "D" (exit_status)
|
||||
: "cc", "rcx", "r11", "memory"
|
||||
);
|
||||
}
|
||||
|
||||
void _start(void) {
|
||||
char msg[] = "hello\n";
|
||||
my_exit(my_write(1, msg, sizeof(msg)) != sizeof(msg));
|
||||
}
|
||||
37
userland/arch/x86_64/c/freestanding/hello_regvar.c
Normal file
37
userland/arch/x86_64/c/freestanding/hello_regvar.c
Normal file
@@ -0,0 +1,37 @@
|
||||
/* Same as hello.c, but with explicit register variables, see:
|
||||
* https://stackoverflow.com/questions/9506353/how-to-invoke-a-system-call-via-sysenter-in-inline-assembly/54956854#54956854
|
||||
*/
|
||||
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include <inttypes.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
ssize_t my_write(int fd, const void *buf, size_t size) {
|
||||
register int64_t rax __asm__ ("rax") = 1;
|
||||
register int rdi __asm__ ("rdi") = fd;
|
||||
register const void *rsi __asm__ ("rsi") = buf;
|
||||
register size_t rdx __asm__ ("rdx") = size;
|
||||
__asm__ __volatile__ (
|
||||
"syscall"
|
||||
: "+r" (rax)
|
||||
: "r" (rdi), "r" (rsi), "r" (rdx)
|
||||
: "cc", "rcx", "r11", "memory"
|
||||
);
|
||||
return rax;
|
||||
}
|
||||
|
||||
void my_exit(int exit_status) {
|
||||
register int64_t rax __asm__ ("rax") = 60;
|
||||
register int rdi __asm__ ("rdi") = exit_status;
|
||||
__asm__ __volatile__ (
|
||||
"syscall"
|
||||
: "+r" (rax)
|
||||
: "r" (rdi)
|
||||
: "cc", "rcx", "r11", "memory"
|
||||
);
|
||||
}
|
||||
|
||||
void _start(void) {
|
||||
char msg[] = "hello\n";
|
||||
my_exit(my_write(1, msg, sizeof(msg)) != sizeof(msg));
|
||||
}
|
||||
13
userland/arch/x86_64/c/inc.c
Normal file
13
userland/arch/x86_64/c/inc.c
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main(void) {
|
||||
uint64_t io = 1;
|
||||
__asm__ (
|
||||
"lea 1(%[io]), %[io];"
|
||||
: [io] "+r" (io)
|
||||
:
|
||||
:
|
||||
);
|
||||
assert(io == 2);
|
||||
}
|
||||
22
userland/arch/x86_64/c/scratch.c
Normal file
22
userland/arch/x86_64/c/scratch.c
Normal file
@@ -0,0 +1,22 @@
|
||||
/* https://stackoverflow.com/questions/6682733/gcc-prohibit-use-of-some-registers/54963829#54963829 */
|
||||
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main(void) {
|
||||
uint64_t in1 = 0xFFFFFFFF;
|
||||
uint64_t in2 = 1;
|
||||
uint64_t out;
|
||||
uint64_t scratch;
|
||||
__asm__ (
|
||||
"mov %[in2], %[scratch];" /* scratch = in2 */
|
||||
"add %[in1], %[scratch];" /* scratch += in1 */
|
||||
"mov %[scratch], %[out];" /* out = scratch */
|
||||
: [scratch] "=&r" (scratch),
|
||||
[out] "=r" (out)
|
||||
: [in1] "r" (in1),
|
||||
[in2] "r" (in2)
|
||||
:
|
||||
);
|
||||
assert(out == 0x100000000);
|
||||
}
|
||||
20
userland/arch/x86_64/c/scratch_hardcode.c
Normal file
20
userland/arch/x86_64/c/scratch_hardcode.c
Normal file
@@ -0,0 +1,20 @@
|
||||
/* This is a worse version of scratch.c with hardcoded scratch. */
|
||||
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int main(void) {
|
||||
uint64_t in1 = 0xFFFFFFFF;
|
||||
uint64_t in2 = 1;
|
||||
uint64_t out;
|
||||
__asm__ (
|
||||
"mov %[in2], %%rax;" /* scratch = in2 */
|
||||
"add %[in1], %%rax;" /* scratch += in1 */
|
||||
"mov %%rax, %[out];" /* out = scratch */
|
||||
: [out] "=r" (out)
|
||||
: [in1] "r" (in1),
|
||||
[in2] "r" (in2)
|
||||
: "rax"
|
||||
);
|
||||
assert(out == 0x100000000);
|
||||
}
|
||||
Reference in New Issue
Block a user