Question

I am working in a buffer overflow attack program for a class assignment. I have provided the C code, as well as the disassembled code, and one of my jobs is to annotate the disassembly code. I don't need anyone to annotate the whole thing, but am I on the right track with my comments? If not, maybe annotate a couple lines to get me on the right track. Thanks!

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

/* Like gets, except that characters are typed as pairs of hex digits.
   Nondigit characters are ignored.  Stops when encounters newline */
char *getxs(char *dest)
{
  int   c;
  int   even   = 1;     /* Have read even number of digits */
  int   otherd = 0;     /* Other hex digit of pair */
  char* sp     = dest;
  while ((c = getchar()) != EOF && c != '\n') 
  {
    if (isxdigit(c))
    {
      int val;
      if ('0' <= c && c <= '9')
        val = c - '0';
      else if ('A' <= c && c <= 'F')
          val = c - 'A' + 10;
      else
          val = c - 'a' + 10;

      if (even)
      {
          otherd = val;
          even = 0;
      }
      else
      {
          *sp++ = otherd * 16 + val;
          even = 1;
      }
    }
  }

  *sp++ = '\0';
  return dest;
}

int getbuf()
{
  char buf[12];
  getxs(buf);
  return 1;
}

void test()
{
  int val;
  printf("Type hex string:  ");
  val = getbuf();
  printf("getbuf returned 0x%x\n", val);
}

int main()
{
  int buf[16];
  /* This little hack is an attempt to get the stack to be in a
     stable position
  */
  int  offset = (((int)buf) & 0xFFF);
  int* space  = (int*) alloca(offset);
  *space = 0; /* So that we don't get complaint of unused variable */
  test();
  return 0;
}

The annotated disassembly is:

buffer.o:     file format elf32-i386


disassembly of section .text:

0000000 <getxs>:
  0:    55                      push   %ebp                 // pushes stack pointer to top
  1:    89 e5                   mov    %esp,%ebp            // stack pointer = c
  3:    83 ec 28                sub    $0x28,%esp           // allocates space for c
  6:    c7 45 e8 01 00 00 00    movl   $0x1,-0x18(%ebp)     // even = 1 
  d:    c7 45 ec 00 00 00 00    movl   $0x0,-0x14(%ebp)     // otherd = 0 
 14:    8b 45 08                mov    0x8(%ebp),%eax       // sp = dest 
 17:    89 45 f0                mov    %eax,-0x10(%ebp)     // conditional setup
 1a:    e9 89 00 00 00          jmp    a8 <getxs+0xa8>      
 1f:    e8 fc ff ff ff          call   20 <getxs+0x20>  
 24:    8b 00                   mov    (%eax),%eax          
 26:    8b 55 e4                mov    -0x1c(%ebp),%edx     
 29:    01 d2                   add    %edx,%edx
 2b:    01 d0                   movzwl (%eax),%eax
 30:    0f b7 c0                add    %edx,%eax
 2d:    0f b7 00                movzwl %ax,%eax
 33:    25 00 10 00 00          and    $0x1000,%eax
 38:    85 c0                   test   %eax,%eax
 3a:    74 6c                   je     a8 <getxs+0xa8>
 3c:    83 7d e4 2f             cmpl   $0x2f,-0x1c(%ebp)
 40:    7e 11                   jle    53 <getxs+0x53>
 42:    83 7d e4 39             cmpl   $0x39,-0x1c(%ebp)
 46:    7f 0b                   jg     53 <getxs+0x53>
 48:    8b 45 e4                mov    -0x1c(%ebp),%eax
 4b:    83 e8 30                sub    $0x30,%eax
 4e:    89 45 f4                mov    %eax,-0xc(%ebp)
 51:    eb 20                   jmp    73 <getxs+0x73>
 53:    83 7d e4 40             cmpl   $0x40,-0x1c(%ebp)
 57:    7e 11                   jle    6a <getxs+0x6a>
 59:    83 7d e4 46             cmpl   $0x46,-0x1c(%ebp)
 5d:    7f 0b                   jg     6a <getxs+0x6a>
 5f:    8b 45 e4                mov    -0x1c(%ebp),%eax
 62:    83 e8 37                sub    $0x37,%eax
 65:    89 45 f4                mov    %eax,-0xc(%ebp)
 68:    eb 09                   jmp    73 <getxs+0x73>
 6a:    8b 45 e4                mov    -0x1c(%ebp),%eax
 6d:    83 e8 57                sub    $0x57,%eax
 70:    89 45 f4                mov    %eax,-0xc(%ebp)
 73:    83 7d e8 00             cmpl   $0x0,-0x18(%ebp)
 77:    74 0f                   je     88 <getxs+0x88>
 79:    8b 45 f4                mov    -0xc(%ebp),%eax
 7c:    89 45 ec                mov    %eax,-0x14(%ebp)
 7f:    c7 45 e8 00 00 00 00    movl   $0x0,-0x18(%ebp)
 86:    eb 20                   jmp    a8 <getxs+0xa8>
 88:    8b 45 ec                mov    -0x14(%ebp),%eax
 8b:    89 c2                   mov    %eax,%edx
 8d:    c1 e2 04                shl    $0x4,%edx
 90:    8b 45 f4                mov    -0xc(%ebp),%eax
 93:    8d 04 02                lea    (%edx,%eax,1),%eax
 96:    89 c2                   mov    %eax,%edx
 98:    8b 45 f0                mov    -0x10(%ebp),%eax
 9b:    88 10                   mov    %dl,(%eax)
 9d:    83 45 f0 01             addl   $0x1,-0x10(%ebp)
 a1:    c7 45 e8 01 00 00 00    movl   $0x1,-0x18(%ebp)
 a8:    e8 fc ff ff ff          call   a9 <getxs+0xa9>
 ad:    89 45 e4                mov    %eax,-0x1c(%ebp)
 b0:    83 7d e4 ff             cmpl   $0xffffffff,-0x1c(%ebp)
 b4:    74 0a                   je     c0 <getxs+0xc0>
 b6:    83 7d e4 0a             cmpl   $0xa,-0x1c(%ebp)
 ba:    0f 85 5f ff ff ff       jne    1f <getxs+0x1f>
 c0:    8b 45 f0                mov    -0x10(%ebp),%eax
 c3:    c6 00 00                movb   $0x0,(%eax)
 c6:    83 45 f0 01             addl   $0x1,-0x10(%ebp)
 ca:    8b 45 08                mov    0x8(%ebp),%eax
 cd:    c9                      leave  
 ce:    c3                      ret    

00000cf <getbuf>:
 cf:    55                      push   %ebp             // pushes stack pointer to the top
 d0:    89 e5                   mov    %esp,%ebp        // stack pointer = buf[12]
 d2:    83 ec 28                sub    $0x28,%esp       // allocates space (40 bits)
 d5:    8d 45 ec                lea    -0x14(%ebp),%eax // rv = stack pointer - 20
 d8:    89 04 24                mov    %eax,(%esp)
 db:    e8 fc ff ff ff          call   dc <getbuf+0xd>
 e0:    b8 01 00 00 00          mov    $0x1,%eax        // return 1 -- want to return ef be ad de
 e5:    c9                      leave  
 e6:    c3                      ret    

00000e7 <test>:
 e7:    55                      push   %ebp
 e8:    89 e5                   mov    %esp,%ebp
 ea:    83 ec 28                sub    $0x28,%esp
 ed:    b8 00 00 00 00          mov    $0x0,%eax
 f2:    89 04 24                mov    %eax,(%esp)
 f5:    e8 fc ff ff ff          call   f6 <test+0xf>
 fa:    e8 fc ff ff ff          call   fb <test+0x14>
 ff:    89 45 f4                mov    %eax,-0xc(%ebp)
102:    b8 13 00 00 00          mov    $0x13,%eax
107:    8b 55 f4                mov    -0xc(%ebp),%edx
10a:    89 54 24 04             mov    %edx,0x4(%esp)
10e:    89 04 24                mov    %eax,(%esp)
111:    e8 fc ff ff ff          call   112 <test+0x2b>
116:    c9                      leave  
117:    c3                      ret    

0000118 <main>:
118:    8d 4c 24 04             lea    0x4(%esp),%ecx
11c:    83 e4 f0                and    $0xfffffff0,%esp
11f:    ff 71 fc                pushl  -0x4(%ecx)
122:    55                      push   %ebp
123:    89 e5                   mov    %esp,%ebp
125:    51                      push   %ecx
126:    83 ec 54                sub    $0x54,%esp
129:    8d 45 b0                lea    -0x50(%ebp),%eax
12c:    25 ff 0f 00 00          and    $0xfff,%eax
131:    89 45 f0                mov    %eax,-0x10(%ebp)
134:    8b 45 f0                mov    -0x10(%ebp),%eax
137:    83 c0 0f                add    $0xf,%eax
13a:    83 c0 0f                add    $0xf,%eax
13d:    c1 e8 04                shr    $0x4,%eax
140:    c1 e0 04                shl    $0x4,%eax
143:    29 c4                   sub    %eax,%esp
145:    89 e0                   mov    %esp,%eax
147:    83 c0 0f                add    $0xf,%eax
14a:    c1 e8 04                shr    $0x4,%eax
14d:    c1 e0 04                shl    $0x4,%eax
150:    89 45 f4                mov    %eax,-0xc(%ebp)
153:    8b 45 f4                mov    -0xc(%ebp),%eax
156:    c7 00 00 00 00 00       movl   $0x0,(%eax)
15c:    e8 fc ff ff ff          call   15d <main+0x45>
161:    b8 00 00 00 00          mov    $0x0,%eax
166:    8b 4d fc                mov    -0x4(%ebp),%ecx
169:    c9                      leave  
16a:    8d 61 fc                lea    -0x4(%ecx),%esp
16d:    c3                      ret   
Was it helpful?

Solution

The annotations should describe the intent of the instruction or block of instructions. It shouldn't just parrot what the instruction does (incorrectly).

In the first line:

  0:    55                      push   %ebp                 // pushes stack pointer to top

We can see that the instruction pushes the base pointer onto the stack, but the annotation incorrectly states that we're pushing the stack pointer on the stack.

Rather, the sequence of instructions:

  0:    55                      push   %ebp                 // pushes stack pointer to top
  1:    89 e5                   mov    %esp,%ebp            // stack pointer = c
  3:    83 ec 28                sub    $0x28,%esp           // allocates space for c

Is a standard function entry preeamble that establishes the stack frame and allocates 0x28 bytes of local storage. It is useful to document the layout of the stack frame, including the location of the function arguments:

 0x08(%ebp): dest
 0x04(%ebp): return-address
 0x00(%ebp): prev %ebp
-0x04(%ebp): ?
-0x08(%ebp): ?
-0x0c(%ebp): ?
-0x10(%ebp): sp
-0x14(%ebp): otherd
-0x18(%ebp): even
-0x1c(%ebp): ?
-0x20(%ebp): ?
-0x24(%ebp): ?
-0x28(%ebp): ?

In the following:

 14:    8b 45 08                mov    0x8(%ebp),%eax       // sp = dest 
 17:    89 45 f0                mov    %eax,-0x10(%ebp)     // conditional setup

%eax is not really sp, it holds dest temporarily while it is moved from the function argument at 0x8(%ebp) to the local variable sp at -0x10(%ebp). There is no "conditional setup".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top