Skip to contentSkip to conversation

Syscall changes registers beyond %eax, on linux-i386

DISCUSSION dvorak 3 participants 8 messages Sep 19, 2002

lore
Share & tools
lore

dvorak

author
#0 lore raw

Hi,

recently i came across a situation were on linux-i386 not only %eax was altered after a syscall but also %ebx. I tracked this problem down, to gcc re-using a variable passed to a function.

This was found on a debian system with a 2.4.17 kernel compiled with gcc 2.95.2 and verified on another system, kernel 2.4.18 compiled with 2.95.4 Attached is small program to test for this 'bug'

a syscall gets his data off the stack, the stack looks like:

saved(edx)
saved(ecx)
saved(ebx)
return_addres (somewhere in entry.S)

When the syscall is called.

the register came there through use of 'SAVE_ALL'.

After the syscall returns these registers are restored using RESTORE_ALL and execution is transferred to userland again.

A short snippet of sys_poll, with irrelavant data removed.

sys_poll(struct pollfd *ufds, .. , ..) {
    ...
    ufds++;
    ...
}

It seems that gcc in certain cases optimizes in such a way that it changes the variable ufds as placed on the stack directly. Which results in saved(ebx) being overwritten and thus in a changed %ebx on return from the system call.

I don't know if this is considered a bug, and if it is, from whom. If it's not a bug it means low-level userland programs need to be rewritten to store all registers on a syscall and restore them on return.

It shouldn't be a bug in gcc, since the C-standard doesn't talk about how to pass variables and stuff. So it seems like a kernel(-gcc-interaction) bug.

To solve this issue 2 solutions spring to mind
1) add a flag to gcc to tell it that it shouldn't do this optimization, this won't work with the gcc's already out there.
2) When calling a syscall explicitly push all variable an extra time, since the code in entry.S doesn't know the amount of variables to a syscall it needs to push all theoretical 6 parameters every time, a not so nice overhead.

I hope someone can shed some light on this issue, i am not myself reading the linux-kernel mailing list, and would like to be cc'd if possible (i'll also check the archives so it's not 100% needed).

Thanks in advance,
Dvorak

/*
* usage is easy, though not very friendly:
* gcc reg-bug.c
* ./a.out | od -tx4
* <ENTER>
* if the values outputted by hexdump are different the 'bug' is present
* else the bug is not present
* on a system without the bug:

dvorak$ dmesg | head -1
Linux version 2.2.21 (kernel@debian) (gcc version 2.95.4 20011002 (Debian prerelease)) #6 Sat Sep 7 22:48:42 CEST 2002 dvorak$ gcc reg-bug.c
dvorak$ ./a.out | od -tx4

0000000 bff7de6c bff7de6c

* on a 'buggy' system:
*

(m4xx) dmesg | head -1
(m4xx) Linux version 2.4.18 (maxx@meuuh) (gcc version 2.95.4 20011002 (Debian +prerelease)) #2 Mon Jul 29 17:01:30 CEST 2002
(m4xx) $ gcc reg-bug.c
(m4xx) $ ./a.out | od -tx4
(m4xx) 0000000 bffffdcc bffffdbc

*/
int main(void)

{
    __asm__("
        pushl   $0x00010001 
        pushl   $0x0
        pushl   $0x00010001 
        pushl   $0x1
        movl    %esp, %ebx
        pushl   %ebx
        movl    $0x2, %ecx
        movl    $0xa8, %eax
        movl    $(-1), %edx
        int     $0x80
        pushl   %ebx
        movl    %esp, %ecx
        movl    $0x08, %edx
        movl    $0x04, %eax
        movl    $0x01, %ebx
        int     $0x80
        movl    $0x01, %eax
        xorl    %ebx, %ebx
        int     $0x80

");
}

#1 lore raw
Quoted 32 lines · dvorak

Hi,

recently i came across a situation were on linux-i386 not only %eax was altered after a syscall but also %ebx. I tracked this problem down, to gcc re-using a variable passed to a function.

This was found on a debian system with a 2.4.17 kernel compiled with gcc 2.95.2 and verified on another system, kernel 2.4.18 compiled with 2.95.4 Attached is small program to test for this 'bug'

a syscall gets his data off the stack, the stack looks like:

saved(edx)
saved(ecx)
saved(ebx)
return_addres (somewhere in entry.S)

When the syscall is called.

the register came there through use of 'SAVE_ALL'.

After the syscall returns these registers are restored using RESTORE_ALL and execution is transferred to userland again.

A short snippet of sys_poll, with irrelavant data removed.

sys_poll(struct pollfd *ufds, .. , ..) {
    ...
    ufds++;
    ...
}
Quoted 4 lines

It seems that gcc in certain cases optimizes in such a way that it changes the variable ufds as placed on the stack directly. Which results in saved(ebx) being overwritten and thus in a changed %ebx on return from the system call.

The 'C' compiler must make room on the stack for any local variables except register types. If it was doing as you state, you couldn't even execute a "hello world" program. Further, the local variables are after the return address. It would screw up the return address and you'd go off into hyper-space upon return.

Quoted 4 lines

I don't know if this is considered a bug, and if it is, from whom. If it's not a bug it means low-level userland programs need to be rewritten to store all registers on a syscall and restore them on return.

No. Various 'C' implementers have standardized calling methods even though it's not part of the 'C' standard. gcc and others assume that a called procedure is not going to change any segments or index registers. There are various optimization things, like "-fcaller-saves" where the called procedure can destroy anything. You may be using something that was wrongly compiled using that switch.

Quoted 12 lines

It shouldn't be a bug in gcc, since the C-standard doesn't talk about how to pass variables and stuff. So it seems like a kernel(-gcc-interaction) bug.

To solve this issue 2 solutions spring to mind
1) add a flag to gcc to tell it that it shouldn't do this optimization, this won't work with the gcc's already out there.
2) When calling a syscall explicitly push all variable an extra time, since the code in entry.S doesn't know the amount of variables to a syscall it needs to push all theoretical 6 parameters every time, a not so nice overhead.

There is a bug in some other code. Try this. It will show that ebx is not being killed in a syscall. You can prove that this code works by changing ebx to eax, which will get destroyed and print "Broken" before exit.

#include <stdio.h>
#include <unistd.h>

void barf(void);
void barf()

{
    puts("Broken\n");
    exit(0);
}

int main()

{
    __asm__ __volatile__("movl	$0xdeadface, %ebx\n");
    (void)getpid();
    __asm__ __volatile__("cmpl	$0xdeadface, %ebx\n"
                         "jnz   barf\n");

    return 0;
}

Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips). The US military has given us many words, FUBAR, SNAFU, now ENRON. Yes, top management were graduates of West Point and Annapolis.

#2 lore raw
Quoted 76 lines · Richard B. Johnson
Quoted 31 lines · dvorak

Hi,

recently i came across a situation were on linux-i386 not only %eax was altered after a syscall but also %ebx. I tracked this problem down, to gcc re-using a variable passed to a function.

This was found on a debian system with a 2.4.17 kernel compiled with gcc
2.95.2 and verified on another system, kernel 2.4.18 compiled with 2.95.4 Attached is small program to test for this 'bug'

a syscall gets his data off the stack, the stack looks like:

saved(edx)
saved(ecx)
saved(ebx)
return_addres (somewhere in entry.S)

When the syscall is called.

the register came there through use of 'SAVE_ALL'.

After the syscall returns these registers are restored using RESTORE_ALL and execution is transferred to userland again.

A short snippet of sys_poll, with irrelavant data removed.

sys_poll(struct pollfd *ufds, .. , ..) {
   ...
   ufds++;
   ...
}
Quoted 4 lines

It seems that gcc in certain cases optimizes in such a way that it changes the variable ufds as placed on the stack directly. Which results in saved(ebx) being overwritten and thus in a changed %ebx on return from the system call.

The 'C' compiler must make room on the stack for any local variables except register types. If it was doing as you state, you couldn't even execute a "hello world" program. Further, the local variables are after the return address. It would screw up the return address and you'd go off into hyper-space upon return.

Quoted 4 lines

I don't know if this is considered a bug, and if it is, from whom. If it's not a bug it means low-level userland programs need to be rewritten to store all registers on a syscall and restore them on return.

No. Various 'C' implementers have standardized calling methods even though it's not part of the 'C' standard. gcc and others assume that a called procedure is not going to change any segments or index registers. There are various optimization things, like "-fcaller-saves" where the called procedure can destroy anything. You may be using something that was wrongly compiled using that switch.

Quoted 11 lines

It shouldn't be a bug in gcc, since the C-standard doesn't talk about how to pass variables and stuff. So it seems like a kernel(-gcc-interaction) bug.

To solve this issue 2 solutions spring to mind
1) add a flag to gcc to tell it that it shouldn't do this optimization, this won't work with the gcc's already out there.
2) When calling a syscall explicitly push all variable an extra time, since the code in entry.S doesn't know the amount of variables to a syscall it needs to push all theoretical 6 parameters every time, a not so nice overhead.

Quoted 4 lines

There is a bug in some other code. Try this. It will show that ebx is not being killed in a syscall. You can prove that this code works by changing ebx to eax, which will get destroyed and print "Broken" before exit.

The bug is only with _some_ syscalls, and getpid() is not one of them, so your example is flawed. It happens when a syscall modifies one of it's parameter values. The solution is to assign the parameter to a local variable before modifying it.

--
Brian Gerst

#3 lore raw
Quoted 84 lines · Brian Gerst
Quoted 78 lines · Richard B. Johnson
Quoted 32 lines · dvorak

Hi,

recently i came across a situation were on linux-i386 not only %eax was altered after a syscall but also %ebx. I tracked this problem down, to gcc re-using a variable passed to a function.

This was found on a debian system with a 2.4.17 kernel compiled with gcc 2.95.2 and verified on another system, kernel 2.4.18 compiled with 2.95.4 Attached is small program to test for this 'bug'

a syscall gets his data off the stack, the stack looks like:

saved(edx)
saved(ecx)
saved(ebx)
return_addres (somewhere in entry.S)

When the syscall is called.

the register came there through use of 'SAVE_ALL'.

After the syscall returns these registers are restored using RESTORE_ALL and execution is transferred to userland again.

A short snippet of sys_poll, with irrelavant data removed.

sys_poll(struct pollfd *ufds, .. , ..) {
   ...
   ufds++;
   ...
}
Quoted 4 lines

It seems that gcc in certain cases optimizes in such a way that it changes the variable ufds as placed on the stack directly. Which results in saved(ebx) being overwritten and thus in a changed %ebx on return from the system call.

The 'C' compiler must make room on the stack for any local variables except register types. If it was doing as you state, you couldn't even execute a "hello world" program. Further, the local variables are after the return address. It would screw up the return address and you'd go off into hyper-space upon return.

Quoted 4 lines

I don't know if this is considered a bug, and if it is, from whom. If it's not a bug it means low-level userland programs need to be rewritten to store all registers on a syscall and restore them on return.

No. Various 'C' implementers have standardized calling methods even though it's not part of the 'C' standard. gcc and others assume that a called procedure is not going to change any segments or index registers. There are various optimization things, like "-fcaller-saves" where the called procedure can destroy anything. You may be using something that was wrongly compiled using that switch.

Quoted 12 lines

It shouldn't be a bug in gcc, since the C-standard doesn't talk about how to pass variables and stuff. So it seems like a kernel(-gcc-interaction) bug.

To solve this issue 2 solutions spring to mind
1) add a flag to gcc to tell it that it shouldn't do this optimization, this won't work with the gcc's already out there.
2) When calling a syscall explicitly push all variable an extra time, since the code in entry.S doesn't know the amount of variables to a syscall it needs to push all theoretical 6 parameters every time, a not so nice overhead.

Quoted 4 lines

There is a bug in some other code. Try this. It will show that ebx is not being killed in a syscall. You can prove that this code works by changing ebx to eax, which will get destroyed and print "Broken" before exit.

Quoted 5 lines

The bug is only with _some_ syscalls, and getpid() is not one of them, so your example is flawed. It happens when a syscall modifies one of it's parameter values. The solution is to assign the parameter to a local variable before modifying it.

Well which one? Here is an ioctl(). It certainly modifies one of its parameter values.

#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>

void barf(void);
void barf()

{
    puts("Broken\n");
    exit(0);
}
int main()
{
    struct termios t;

    __asm__ __volatile__("movl	$0xdeadface, %ebx\n");
    (void)ioctl(0, TCGETS, &t); 
    (void)getpid();
    __asm__ __volatile__("cmpl	$0xdeadface, %ebx\n"
                         "jnz   barf\n");

    return 0;
}

Until you can show the syscall that doesn't follow the correct rules, then my example is not flawed. In fact a modified example can be used to find any broken calls.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips). The US military has given us many words, FUBAR, SNAFU, now ENRON. Yes, top management were graduates of West Point and Annapolis.

#4 lore raw
Quoted 39 lines · Richard B. Johnson
Quoted 8 lines · Brian Gerst
Quoted 4 lines · Richard B. Johnson

There is a bug in some other code. Try this. It will show that ebx is not being killed in a syscall. You can prove that this code works by changing ebx to eax, which will get destroyed and print "Broken" before exit.

The bug is only with _some_ syscalls, and getpid() is not one of them, so your example is flawed. It happens when a syscall modifies one of it's parameter values. The solution is to assign the parameter to a local variable before modifying it.

Well which one? Here is an ioctl(). It certainly modifies one of its parameter values.

#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>

void barf(void);
void barf()

{
    puts("Broken\n");
    exit(0);
}
int main()
{
    struct termios t;

    __asm__ __volatile__("movl	$0xdeadface, %ebx\n");
    (void)ioctl(0, TCGETS, &t); 
    (void)getpid();
    __asm__ __volatile__("cmpl	$0xdeadface, %ebx\n"
                         "jnz   barf\n");

    return 0;
}
Quoted 3 lines

Until you can show the syscall that doesn't follow the correct rules, then my example is not flawed. In fact a modified example can be used to find any broken calls.

Well the original poster gave one valid example: sys_poll(). We're not talking about it modifying userspace though a pointer. We're talking about it taking it's parameter on the kernel stack (which is really the pt_regs structure saved from user space) and modifying it. Which then gets restored to the user registers upon syscall exit.

This is how the kernel stack looks like inside a syscall (x86):
OLDSS
OLDESP
EFLAGS
CS
EIP
ORIG_EAX
ES
DS
EAX <- syscall number
EBP <- syscall arg6
EDI <- syscall arg5
ESI <- syscall arg4
EDX <- syscall arg3
ECX <- syscall arg2
EBX <- syscall arg1
(return address)
(local variables)

Everything above the return address is the pt_regs struct that gets restored to user space. If the syscall modifies any of its args (*not memory pointed to by the args*), they get written back to the stack in the pt_regs area, and then get restored to userspace modified. Understand now?

--
Brian Gerst

#5 lore raw
Quoted 72 lines · Brian Gerst
Quoted 42 lines · Richard B. Johnson
Quoted 11 lines · Brian Gerst
Quoted 4 lines · Richard B. Johnson

There is a bug in some other code. Try this. It will show that ebx is not being killed in a syscall. You can prove that this code works by changing ebx to eax, which will get destroyed and print "Broken" before exit.

The bug is only with _some_ syscalls, and getpid() is not one of them, so your example is flawed. It happens when a syscall modifies one of it's parameter values. The solution is to assign the parameter to a local variable before modifying it.

Well which one? Here is an ioctl(). It certainly modifies one of its parameter values.

#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>

void barf(void);
void barf()

{
    puts("Broken\n");
    exit(0);
}
int main()
{
    struct termios t;

    __asm__ __volatile__("movl	$0xdeadface, %ebx\n");
    (void)ioctl(0, TCGETS, &t); 
    (void)getpid();
    __asm__ __volatile__("cmpl	$0xdeadface, %ebx\n"
                         "jnz   barf\n");

    return 0;
}
Quoted 3 lines

Until you can show the syscall that doesn't follow the correct rules, then my example is not flawed. In fact a modified example can be used to find any broken calls.

Well the original poster gave one valid example: sys_poll(). We're not talking about it modifying userspace though a pointer. We're talking about it taking it's parameter on the kernel stack (which is really the pt_regs structure saved from user space) and modifying it. Which then gets restored to the user registers upon syscall exit.

This is how the kernel stack looks like inside a syscall (x86):
OLDSS
OLDESP
EFLAGS
CS
EIP
ORIG_EAX
ES
DS
EAX <- syscall number
EBP <- syscall arg6
EDI <- syscall arg5
ESI <- syscall arg4
EDX <- syscall arg3
ECX <- syscall arg2
EBX <- syscall arg1
(return address)
(local variables)

Quoted 6 lines

Everything above the return address is the pt_regs struct that gets restored to user space. If the syscall modifies any of its args (*not memory pointed to by the args*), they get written back to the stack in the pt_regs area, and then get restored to userspace modified. Understand now?

Maybe. So, if the 'C' runtime library puts 0xdeadfeed into the ebx register and executes a syscall, upon return from the syscall, this value is no longer 0xdeadfeed? If this is true, then is the kernel supposed to save the values of registers modified by user-code, before calling the function? I expect that the 'C' runtime library expects the index registers to be preserved and EBX is an index register.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips). The US military has given us many words, FUBAR, SNAFU, now ENRON. Yes, top management were graduates of West Point and Annapolis.

author
#6 lore raw
In reply to Richard B. Johnson
Quoted 8 lines · Richard B. Johnson
Quoted 6 lines · Brian Gerst
Quoted 5 lines · Richard B. Johnson
Quoted 2 lines · dvorak

Hi,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

recently i came across a situation were on linux-i386 not only %eax was altered after a syscall but also %ebx. I tracked this problem down, to gcc re-using a variable passed to a function.

This was found on a debian system with a 2.4.17 kernel compiled with gcc 2.95.2 and verified on another system, kernel 2.4.18 compiled with 2.95.4 Attached is small program to test for this 'bug'

<SNIP part of the explanation>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 4 lines

It seems that gcc in certain cases optimizes in such a way that it changes the variable ufds as placed on the stack directly. Which results in saved(ebx) being overwritten and thus in a changed %ebx on return from the system call.

The 'C' compiler must make room on the stack for any local variables except register types. If it was doing as you state, you couldn't even execute a "hello world" program. Further, the local variables are after the return address. It would screw up the return address and you'd go off into hyper-space upon return.

The problem is it uses one of the _arguments_ passed to the function, that argument gets modified, normally this happens on a copy, but there is no 'garantue' that is doesn't modify the original argument as putted on the stack by the calling function.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

No. Various 'C' implementers have standardized calling methods even though it's not part of the 'C' standard. gcc and others assume that a called procedure is not going to change any segments or index registers. There are various optimization things, like "-fcaller-saves" where the called procedure can destroy anything. You may be using something that was wrongly compiled using that switch.

This is not what happens here, what happens is that one of the _arguments_ placed on the stack is being modified, normally a calling function discards these values after use (addl $0x10, %esp or similar) but in this case they are reused. (in the RESTORE_ALL call)

Quoted 6 lines
Quoted 6 lines

The bug is only with _some_ syscalls, and getpid() is not one of them, so your example is flawed. It happens when a syscall modifies one of it's parameter values. The solution is to assign the parameter to a local variable before modifying it.

and only with _some_ compiler + kernel combinations.

Quoted 13 lines

int main()

{
    struct termios t;

    __asm__ __volatile__("movl	$0xdeadface, %ebx\n");
    (void)ioctl(0, TCGETS, &t); 
    (void)getpid();
    __asm__ __volatile__("cmpl	$0xdeadface, %ebx\n"
                         "jnz   barf\n");

    return 0;
}
Quoted 3 lines

Until you can show the syscall that doesn't follow the correct rules, then my example is not flawed. In fact a modified example can be used to find any broken calls.

I putted in some assembler code in my original post that uses the sys_poll syscall of which i _know_ it modifies one of it's arguments, to be more specific, it's first argument, which is %ebx on passing in:
asmlinkage int sys_poll(struct pollfd * ufds, unsigned int nfds, long timeout)
....

      for(i=0; i < (int)nfds; i++, ufds++, fds1++) {

....

and in fact we saw that the change in %ebx is proportional to the nfds as passed to sys_poll.

now however for sys_ioctl:
it's first argument, fd (%ebx on passing) is never modifed in the code nowhere is there an fd++ or similar, so again this 'example' of yours is flawed.

gtx.
dvorak

P.S. i think my original was quite clear and INCLUDED example code that can easily be checked by someone who reads asm, i attach an extra copy which explains all the asm in there for easier reference.

/*
* usage is easy, though not very friendly:
* gcc reg-bug.c
* ./a.out | od -tx4
* <ENTER>
* if the values outputted by hexdump are different the 'bug' is present
* else the bug is not present
* on a system without the bug:

dvorak$ dmesg | head -1
Linux version 2.2.21 (kernel@debian) (gcc version 2.95.4 20011002 (Debian prerelease)) #6 Sat Sep 7 22:48:42 CEST 2002 dvorak$ gcc reg-bug.c
dvorak$ ./a.out | od -tx4

0000000 bff7de6c bff7de6c

* on a 'buggy' system:
*

(m4xx) dmesg | head -1
(m4xx) Linux version 2.4.18 (maxx@meuuh) (gcc version 2.95.4 20011002 (Debian +prerelease)) #2 Mon Jul 29 17:01:30 CEST 2002
(m4xx) $ gcc reg-bug.c
(m4xx) $ ./a.out | od -tx4
(m4xx) 0000000 bffffdcc bffffdbc

*/
int main(void)

{
    __asm__("
        pushl   $0x00010001     # this is events and revents
        pushl   $0x0            # fd 0
        pushl   $0x00010001     # again events and revents
        pushl   $0x1            # fd 1
        movl    %esp, %ebx      # %ebx now contains a pointer to the
                                # pollfd structure i setted up,
                                # note that only pushes occur
                                # below so this structures stays on the stack
        pushl   %ebx            # we pushl %ebx to compare it later
        movl    $0x2, %ecx      # 2 = number of fd's
        movl    $0xa8, %eax     # __NR_sys_poll
        movl    $(-1), %edx     # no timeout
        int     $0x80           # call kernel
                                # sys_poll(%ebx, %ecx, %edx)
                                #  %ebx == ufds (address of the pollfd structs)o                                #  %ecx == num fd's (2)
                                #  %edx == timeout (-1)
        pushl   %ebx            # we push the returned %ebx on the stack
                                # as well
        movl    %esp, %ecx      # %ecx is now pointer to the 2 saved %ebx's
        movl    $0x08, %edx     # %edx = 8
        movl    $0x04, %eax     # %eax = 4 == __NR_sys_write
        movl    $0x01, %ebx     # %ebx = 1 == fd
        int     $0x80           # call kernel
                                # sys_write(%ebx, %ecx, %edx)
                                #  %ebx = fd (1)
                                #  %ecx = buf (pointer to the 2 saved %ebx's)   
                                #  %edx = len (8)
        movl    $0x01, %eax     # and an sys_exit(0);
        xorl    %ebx, %ebx
        int     $0x80

");
}

#7 lore raw
Quoted 48 lines · dvorak
Quoted 8 lines · Richard B. Johnson
Quoted 6 lines · Brian Gerst
Quoted 5 lines · Richard B. Johnson
Quoted 2 lines · dvorak

Hi,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

recently i came across a situation were on linux-i386 not only %eax was altered after a syscall but also %ebx. I tracked this problem down, to gcc re-using a variable passed to a function.

This was found on a debian system with a 2.4.17 kernel compiled with gcc 2.95.2 and verified on another system, kernel 2.4.18 compiled with 2.95.4 Attached is small program to test for this 'bug'

<SNIP part of the explanation>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 4 lines

It seems that gcc in certain cases optimizes in such a way that it changes the variable ufds as placed on the stack directly. Which results in saved(ebx) being overwritten and thus in a changed %ebx on return from the system call.

The 'C' compiler must make room on the stack for any local variables except register types. If it was doing as you state, you couldn't even execute a "hello world" program. Further, the local variables are after the return address. It would screw up the return address and you'd go off into hyper-space upon return.

The problem is it uses one of the _arguments_ passed to the function, that argument gets modified, normally this happens on a copy, but there is no 'garantue' that is doesn't modify the original argument as putted on the stack by the calling function.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

No. Various 'C' implementers have standardized calling methods even though it's not part of the 'C' standard. gcc and others assume that a called procedure is not going to change any segments or index registers. There are various optimization things, like "-fcaller-saves" where the called procedure can destroy anything. You may be using something that was wrongly compiled using that switch.

This is not what happens here, what happens is that one of the _arguments_ placed on the stack is being modified, normally a calling function discards these values after use (addl $0x10, %esp or similar) but in this case they are reused. (in the RESTORE_ALL call)

Quoted 1 line
Quoted 1 line
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

The bug is only with _some_ syscalls, and getpid() is not one of them, so your example is flawed. It happens when a syscall modifies one of it's parameter values. The solution is to assign the parameter to a local variable before modifying it.

and only with _some_ compiler + kernel combinations.

[SNIPPED...]

Okay. Thanks for the explaination.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips). The US military has given us many words, FUBAR, SNAFU, now ENRON. Yes, top management were graduates of West Point and Annapolis.