> For the complete documentation index, see [llms.txt](https://sansong.gitbook.io/cyber/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sansong.gitbook.io/cyber/pwn/protections/stack-canary/ssp-memory-leak.md).

# SSP memory leak

Empêcher un buffer overflow au prix d'une lecture mémoire

QUE POUR 32bits ?

### \_\_libc\_argv\[0]

Si le canari est écrasé la fonction `__stack_chk_fail_local` est appelée. Elle fait elle-même appel à `__stack_chk_fail`.

```c
__stack_chk_fail_local (void)
{
  __stack_chk_fail ();
}
```

A son tour, celle-ci fait appel à `__fortify_fail_abort`.

```c
__stack_chk_fail (void)
{
  __fortify_fail_abort (false, "stack smashing detected");
}
```

C'est elle qui nous intéresse.

```c
__fortify_fail_abort (_Bool need_backtrace, const char *msg)
{
  /* The loop is added only to keep gcc happy.  Don't pass down
     __libc_argv[0] if we aren't doing backtrace since __libc_argv[0]
     may point to the corrupted stack.  */
  while (1)
    __libc_message (need_backtrace ? (do_abort | do_backtrace) : do_abort,
		    "*** %s ***: %s terminated\n",
		    msg,
		    (need_backtrace && __libc_argv[0] != NULL
		     ? __libc_argv[0] : "<unknown>"));
}
```

Elle affiche un message avec un format string `%s`. Parmi les informations affichées on voit `__libc_argv[0]`. C'est une variable qui contient le nom du programme **mais qui est situé sur la pile**. On peut donc l'écraser avec le buffer overflow et **mettre un pointeur vers une adresse dont on souhaite lire le contenu**.

### Références

{% embed url="<https://seclists.org/bugtraq/2010/Apr/243>" %}

{% embed url="<https://github.com/lattera/glibc/tree/master/debug>" %}

{% embed url="<https://github.com/ctfs/write-ups-2015/blob/master/32c3-ctf-2015/pwn/readme-200/README.md>" %}

{% embed url="<https://0xswitch.fr/posts/leak-via-stack-smashing-protection>" %}
