chdir on linux does not work
Dear All,
Anyone can tell me why this chdir code doesnot work on linux ?
-> from what i read, chdir is system call number 12
section .text
global _start
_start:
mov eax, 12
mov ebx, dir
int 80h
mov eax, 1
mov ebx, 0
int 80h
section .data
dir: db '/bin'
Thanks,
Pri
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Hi,
Quoted 1 line · Noprianto
-> from what i read, chdir is system call number 12
Yep, but better use something like
%define __NR_chdir 12
to make code more readable. Also, your example works just fine here:
$ nasm a.asm -f elf
$ gcc a.o -o a -nostdlib
$ strace ./a
execve("./a", ["./a"], [/* 30 vars */]) = 0
chdir("/bin") = 0
_exit(0) = ?Remember that chdir affects only the process that calls chdir. You can't e.g. write a replacement for shell's builtin "cd" command that way.
best regards,
Timo Lindfors
Timo Lindfors wrote:
...
Quoted 1 line
Remember that chdir affects only the process that calls chdir.
I thought Noprianto's problem was the lack of a terminating zero after "/bin"... didn't help, but I still think it's a bug waiting to byte. Adding a call to sys_getcwd (__NR_ 183) (why is this man 3, not man 2 ???) proves that, sure enough, it *does* work... but we change back on exit... as you say...
Quoted 3 lines
You
can't e.g. write a replacement for shell's builtin "cd" command that way.
Okay, how *would* we do that? I know! I'll check "asmutils"! Hmmm... no chdir in asmutils... This is apparently harder than it looks!
Good Question!
Best,
Frank
Quoted 2 lines
Adding a call to
sys_getcwd(__NR_183) (why is this man 3, not man 2 ???)
Good question, actually. I don't know.
Quoted 4 lines
Quoted 2 lines
You can't e.g. write a replacement for shell's builtin "cd" command that way.
Okay, how *would* we do that?
It is simply not possible. Like setenv, cd absolutely must be a shell builtin.
b
Quoted 8 lines · Brian Raiter
Quoted 4 lines
Quoted 2 lines
You can't e.g. write a replacement for shell's builtin "cd" command that way.
Okay, how *would* we do that?
It is simply not possible. Like setenv, cd absolutely must be a shell builtin.
Okay... Thanks for the quick response, Brian - probably saved me a lot of frustration!
Best,
Frank
Hi,
Quoted 2 lines · Brian Raiter
It is simply not possible. Like setenv, cd absolutely must be a shell builtin.
Indeed, unless you do some dirty tricks by using ptrace(2) to attach to the parent and force it to do the chdir syscall :)
best regards,
Timo Lindfors
Quoted 2 lines · Frank Kotler
Adding a call to
sys_getcwd(__NR_183) (why is this man 3, not man 2 ???)
Are you talking about sys_getcwd(3) (which I've never seen or known to exist) or getcwd(3)? If the latter, there's a Linux-specific extension that uses malloc() -- perhaps that's the reason why:
As an extension to the POSIX.1 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc() if buf is NULL on call. In this case, the allocated buffer has the length size unless size is zero, when buf is allocated as big as necessary. It is possi- ble (and, indeed, advisable) to free() the buffers if they have been obtained this way.
- Adrian
Quoted 7 lines · Adrian Ho
Quoted 1 line · Frank Kotler
Adding a call to
sys_getcwd(__NR_183) (why is this man 3, not man 2 ???)Are you talking about
sys_getcwd(3)(which I've never seen or known to exist) or getcwd(3)?
getcwd(3). I refer to it as "sys_???" when I mean the int 80h interface, sorry.
Quoted 2 lines
If the latter, there's a Linux-specific extension that uses
malloc()-- perhaps that's the reason why:
I'll bet you're right. If that isn't the reason, it's as good a reason as any. Good point!
Best,
Frank
Quoted 5 lines
Anyone can tell me why this chdir code doesnot work on linux ?
section .data
dir: db '/bin'
You forgot to NUL-terminate your string. A dangerous mistake, as it can become a Heisenbug. You were lucky it failed to work for you. Use this instead:
dir: db '/bin', 0
b
Quoted 4 lines · Noprianto
Dear All,
Anyone can tell me why this chdir code does not work on linux ?
If you run this program in a terminal it will do the chdir, then restore the original directory when the program exits. Possibly this is what you are seeing?
Quoted 35 lines
-> from what i read, chdir is system call number 12
section .text
global_start
_start:
mov eax, 12
mov ebx, dir
int 80hmov eax, 1
mov ebx, 0
int 80hsection .data
dir: db '/bin'Thanks,
Pri__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-assembly" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi Jeff,
Quoted 5 lines
If you run this program in a terminal it will do the chdir,
then restore the original directory when the program exits. Possibly this is what you are seeing?
Yes, thats what i am seeing.
Quoted 25 lines
Quoted 2 lines
-> from what i read, chdir is system call number
12
Quoted 22 lines
section .text
global_start
_start:
mov eax, 12
mov ebx, dir
int 80hmov eax, 1
mov ebx, 0
int 80hsection .data
dir: db '/bin'
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com