Commit 8ab4b00 ("[libc] Rewrite setjmp() and longjmp()") introduced a
regression in which the saved values of %ebx, %esi, and %edi were all
accidentally restored into %esp. The result is that the second and
subsequent returns from setjmp() would effectively corrupt %ebx, %esi,
%edi, and the stack pointer %esp.
Use of setjmp() and longjmp() is generally discouraged: our only use
occurs as part of the implementation of PXENV_RESTART_TFTP, since the
PXE API effectively mandates its use here. The call to setjmp()
occurs at the start of pxe_start_nbp(), where there are almost
certainly no values held in %ebx, %esi, or %edi. The corruption of
these registers therefore had no visible effect on program execution.
The corruption of %esp would have been visible on return from
pxe_start_nbp(), but there are no known PXE NBPs which first call
PXENV_RESTART_TFTP and subsequently attempt to return to the PXE base
code. The effect on program execution was therefore similar to that
of moving the stack to a pseudo-random location in the 32-bit address
space; this will often allow execution to complete successfully since
there is a high chance that the pseudo-random location will be unused.
The regression therefore went undetected for around one month.
Fix by restoring the correct registers from the saved jmp_buf
structure.
Signed-off-by: Michael Brown <mcb30@ipxe.org>
65 lines
1.3 KiB
ArmAsm
65 lines
1.3 KiB
ArmAsm
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )
|
|
|
|
.text
|
|
.arch i386
|
|
.code32
|
|
|
|
/* Must match jmp_buf structure layout */
|
|
.struct 0
|
|
env_retaddr: .long 0
|
|
env_stack: .long 0
|
|
env_ebx: .long 0
|
|
env_esi: .long 0
|
|
env_edi: .long 0
|
|
env_ebp: .long 0
|
|
.previous
|
|
|
|
/*
|
|
* Save stack context for non-local goto
|
|
*/
|
|
.globl setjmp
|
|
setjmp:
|
|
/* Get jmp_buf pointer in %edx */
|
|
movl 4(%esp),%edx
|
|
/* Save return address */
|
|
movl 0(%esp),%eax
|
|
movl %eax, env_retaddr(%edx)
|
|
/* Save stack pointer */
|
|
movl %esp, env_stack(%edx)
|
|
/* Save other registers */
|
|
movl %ebx, env_ebx(%edx)
|
|
movl %esi, env_esi(%edx)
|
|
movl %edi, env_edi(%edx)
|
|
movl %ebp, env_ebp(%edx)
|
|
/* Return 0 when returning as setjmp() */
|
|
xorl %eax, %eax
|
|
ret
|
|
.size setjmp, . - setjmp
|
|
|
|
/*
|
|
* Non-local jump to a saved stack context
|
|
*/
|
|
.globl longjmp
|
|
longjmp:
|
|
/* Get jmp_buf pointer in %edx */
|
|
movl 4(%esp),%edx
|
|
/* Get result in %eax */
|
|
movl 8(%esp),%eax
|
|
/* Force result to non-zero */
|
|
testl %eax, %eax
|
|
jnz 1f
|
|
incl %eax
|
|
1: /* Restore stack pointer */
|
|
movl env_stack(%edx), %esp
|
|
/* Restore other registers */
|
|
movl env_ebx(%edx), %ebx
|
|
movl env_esi(%edx), %esi
|
|
movl env_edi(%edx), %edi
|
|
movl env_ebp(%edx), %ebp
|
|
/* Replace return address on the new stack */
|
|
popl %ecx /* discard */
|
|
pushl env_retaddr(%edx)
|
|
/* Return to setjmp() caller */
|
|
ret
|
|
.size longjmp, . - longjmp
|