[dundee] Grabbing the stack pointer...

Nicholas Walker tel0seh at googlemail.com
Mon Mar 23 09:25:11 UTC 2009


gcc on linux :)

it seems that it prefers just "asm" over __asm__

so, compiling:


#include<stdio.h>
unsigned long sp(void)
{
    asm("movl %esp, %eax");
}

void main(void)
{
    printf("0x%x\n", sp());
}



returns:



tel0s at bufferoverflow:~$ cd Desktop/
tel0s at bufferoverflow:~/Desktop$ gcc -o sp sp.c
sp.c: In function ‘main’:
sp.c:9: warning: format ‘%x’ expects type ‘unsigned int’, but argument 2 has
type ‘long unsigned int’
sp.c:8: warning: return type of ‘main’ is not ‘int’
tel0s at bufferoverflow:~/Desktop$


changing main to int main() kills the last error it seems:

tel0s at bufferoverflow:~/Desktop$ gcc -o sp sp.c
sp.c: In function ‘main’:
sp.c:9: warning: format ‘%x’ expects type ‘unsigned int’, but argument 2 has
type ‘long unsigned int’
tel0s at bufferoverflow:~/Desktop$



any ideas?






On Mon, Mar 23, 2009 at 7:50 AM, Robert Ladyman <it at file-away.co.uk> wrote:

> Which O/S, which compiler...and what's the compilation error?
>
> The call might be asm or __asm or __asm__ with curly brackets or
> whatever...the error will help.
>
>
>
> On Sunday 22 March 2009 20:44:36 Nicholas Walker wrote:
> > Hi guys im doing some work with buffer overflows, and I've been
> scratching
> > my head for a while with this one. I have little experience in C, I can
> > read some source adn get a general idea of what each bit does, however
> i'm
> > required to grab the current address that the stack pointer is looking
> at.
> >
> > I've been given this bit of source:
> >
> >
> >
> >
> >
> > 3.  The Stack Pointer
> >     =================
> >
> >
> > Every program has a stack pointer which is the address of the
> > beginning of the stack.  We can
> > find the stack pointer for a system using the following code:
> >
> >
> > unsigned long sp(void)
> > {
> >       __asm__("movl %esp, %eax");
> > }
> >
> > void main(void)
> > {
> >       printf("0x%x\n", sp());
> > }
> >
> >
> > The function sp() contains the assembly instruction movl which copies
> > the value of the stack
> > pointer to the return buffer for the function, so it is returned to
> > the main function and
> > displayed.
> >
> > $ ./sp
> >
> > 0xbfbffbc8
> >
> >
> > however after adding the correct headers, it still wont compile....
> >
> >
> > I understand the Assembly language instruction, ESP holds the address of
> > where the stack pointer is currently pointing, movl esp eax moves it to
> the
> > eax register, for returning. If anyone could help it would be great. its
> > the last bit of work i need to do to finish my project.
>
> --
>
> Robert Ladyman
> File-Away Limited, 32 Church Street, Newtyle
> Perthshire, PH12 8TZ SCOTLAND
> Registered in Scotland, Company Number SC222086
> Tel: +44 (0) 1828 898 158
> Mobile: +44 (0) 7732 771 649
> http://www.file-away.co.uk
>
>
> _______________________________________________
> dundee GNU/Linux Users Group mailing list
> dundee at lists.lug.org.uk  http://dundee.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/dundee
> Chat on IRC, #tlug on dundee.lug.org.uk
>



-- 


Nick Walker
Vice President : The Linux Society
UAD Ethical Hacker
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.lug.org.uk/pipermail/dundee/attachments/20090323/64196dc9/attachment.htm 


More information about the dundee mailing list