Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 742291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:43:53+00:00 2026-05-14T08:43:53+00:00

i have been reading about a function that can overwrite its return address. void

  • 0

i have been reading about a function that can overwrite its return address.

void foo(const char* input)
{
    char buf[10];

    //What? No extra arguments supplied to printf?
    //It's a cheap trick to view the stack 8-)
    //We'll see this trick again when we look at format strings.
    printf("My stack looks like:\n%p\n%p\n%p\n%p\n%p\n% p\n\n"); //%p ie expect pointers

    //Pass the user input straight to secure code public enemy #1.
    strcpy(buf, input);
    printf("%s\n", buf);

    printf("Now the stack looks like:\n%p\n%p\n%p\n%p\n%p\n%p\n\n");
}  

It was sugggested that this is how the stack would look like

Address of foo = 00401000

My stack looks like:
00000000
00000000
7FFDF000
0012FF80
0040108A <– We want to overwrite the return address for foo.
00410EDE

Question:
-. Why did the author arbitrarily choose the second last value as the return address of foo()?

-. Are values added to the stack from the bottom or from the top?

  • apart from the function return address, what are the other values i apparently see on the stack? ie why isn’t it filled with zeros

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:43:54+00:00Added an answer on May 14, 2026 at 8:43 am

    The one above it is the previous EBP (0012FF80). The value above the prev-EBP is always the return address.

    (This obviously assumes a non-FPO binary and 32bit Windows)1.

    If you recall, the prologue looks like:

    push ebp      ; back up the previous ebp on the stack
    mov ebp, esp  ; set up the new frame pointer
    

    and when a function is called, e.g.

    call 0x00401000
    

    The current EIP is pushed on the stack (used as the return address), so the stack after the prologue looks like:

    [ebp+0xc]  ; contains parameter 1, etc
    [ebp+0x8]  ; contains parameter 0
    [ebp+0x4]  ; contains return address
    [ebp]      ; contains prev-EBP
    

    So for each %p, printf uses the next 4 bytes starting from [ebp+0xc] (the first %p parameter). Eventually you hit the previous EBP value stored on the stack, which is (0012FF80) and then the next one is the Return Address.

    Note these addresses must ‘make sense’, which they clearly do here (though it may not be ‘clear’ for all).

    Re Q2) The stack grows down. So when you push eax, 4 is subtracted from esp, then the value of eax is moved to [esp], equivalently in code:

    push eax
    ;  <=>
    sub esp, 4
    mov [esp], eax
    

    1. The book is Writing Secure Code, yes?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 384k
  • Answers 384k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, don't require to use dotted dict, you can easily… May 14, 2026 at 11:04 pm
  • Editorial Team
    Editorial Team added an answer Regarding the last part of the error, which you are… May 14, 2026 at 11:04 pm
  • Editorial Team
    Editorial Team added an answer ..posting myself the solution i have found elsewhere problem was… May 14, 2026 at 11:04 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.