Issues with open() (asm)
I wrote a small program that is supposed to (keyword: supposed) create a file and put the word BORING! inside. The problem I have is that it wont take O_RDONLY (or anything else for that matter)
I copied a program using this exact method from Programming from the Ground up and it worked. So... here is the code. Code:
.section .data |
PS: if you download the pdf of Programming from the ground up, check page 89.
|
1) i dont see where u have defined the constant O_RDONLY, 2) if you are writing to afile, why would u use O_RDONLY, and 3) the args to open are supposed to be:
int open(const char *pathname, int flags, mode_t mode); so ebx should have the name of the file, 'crack' seems to be what you want to write to the file?? and if u want to create the file for writing, here is constant i used from a project i did: OUTFILE_FLAGS = 0x241 ;//O_WRONLY | O_CREAT | O_TRUNC |
just look in the header files from man open to see the other constant values.
|
Ok, so I fixed some of the errors. Code still doesn't work of course.
Code:
.section .data |
when I strace it, I get the following, which hints that there is something wrong with my messages :
Code:
execve("./harhar", ["./harhar"], [/* 46 vars */]) = 0 |
I've done some work on the code. In the past say, 20 minutes. (probably less). Here is the strace once more:
Code:
execve("./harhar", ["./harhar"], [/* 47 vars */]) = 0 Code:
.section .data |
Code:
(gdb) r Code:
.section .data pushl %ebp movl %esp, %ebp subl STACK_SZ, %esp and leave is: movl %ebp, %esp popl %ebp also notice i added the sys_exit function. when u enter a program via _start, there is not stack frame setup by libc init code, so u can't just "return" from main. if u ran your code rfurther, u would find it segfaulting after close_file. |
Do you have to use a ret after?
|
you CANNOT ret when u call _start directly, u have to call exit like above. else u need to define main as a global function and use gcc to compile so that libc init code can be inserted, rather than 'ld && as' like i imagine ur using now.
|
Ok, I understand that now.
One last thing, why "enter $STACK_FRAME_SZ, $0"? I understand that enter is equivalent to building the stack, but why the operands? what do those serve? |
the first arg is the size of the frame, which is the amount to subtract from esp to create it, usually it looks like:
movl %esp, %ebp pushl %ebp subl $STACK_FRAME_SZ, %esp that is just a shortcut for those 3 ins. and the 2nd arg to enter is some crap about nested frames that i dont recall, its not important for simple use. |
All times are GMT -5. The time now is 11:21 PM. |