Security_RNRF

0x20. 본문

LiveOverFlow/Binary

0x20.

RNRF 2021. 11. 3. 00:55

20. The Heap: what does malloc() do? - bin.0x14

: Content
-> Heap - memory region
-> mmap()
-> malloc()
-> abstract interpretation of the heap

: What exactly is "heap"?
: What does "malloc" actually do?
-> You will generally know that more memory is needed for programming.
-> Then, allocate memory through "malloc" to create the heap and release it again when finished.

: First, before we talk about "malloc," let's talk about how the process gets memory.
-> Let's check the "mmap".
-> man mmap
-> They are system calls, so they are requested directly to the kernel.
-> "mmap" provides the kernel with a new virtual address space and essentially requests a new virtual address space.
-> Length Memory Segment
-> In addition to "mmap", there is also "brk", which can already be used to change the size of the donkeys.
-> man brk
-> change data segment size.
: This creates an amazing phenomenon.
-> This creates an amazing phenomenon.After all, the process does not care how this memory is implemented. If there's "RAM."
-> There is enough RAM to store it in the correct location or if there is no SWAP file.
-> The kernel and hardware handle it and map its memory to the process.
-> This means that the process can work with transparent access to the corresponding memory address.
-> Transparency means "you don't need to know anything about it."

: You can run "heap1" using "stace" and see that all calls to "mmap" are initially set.
-> strace ./heap1
-> execve("./heap1", ["./heap1"], [/* 16 vars */]) = 0
brk(0)                                  = 0x804a000
fcntl64(0, F_GETFD)                     = 0
fcntl64(1, F_GETFD)                     = 0
fcntl64(2, F_GETFD)                     = 0
access("/etc/suid-debug", F_OK)         = -1 ENOENT (No such file or directory)
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7fe0000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=13796, ...}) = 0
mmap2(NULL, 13796, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7fdc000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/libc.so.6", O_RDONLY)        = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\320m\1\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1319176, ...}) = 0
mmap2(NULL, 1329480, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7e97000
mprotect(0xb7fd5000, 4096, PROT_NONE)   = 0
mmap2(0xb7fd6000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x13e) = 0xb7fd6000
mmap2(0xb7fd9000, 10568, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb7fd9000
close(3)                                = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7e96000
set_thread_area({entry_number:-1 -> 6, base_addr:0xb7e966c0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0
mprotect(0xb7fd6000, 8192, PROT_READ)   = 0
mprotect(0xb7ffe000, 4096, PROT_READ)   = 0
munmap(0xb7fdc000, 13796)               = 0
brk(0)                                  = 0x804a000
brk(0x806b000)                          = 0x806b000
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++
Segmentation fault
-> Finally, set the heap using "brk".

: Why not use "mmap" or "brk" to get more memory in the process?
: And why do "malloc" and "free", hip talk about? (= why use “malloc()”?)
-> In fact, "malloc" is a very colorful and convenient rapper. And do much more.
-> "malloc" gets more memory by invoking "mmap" or "brk" if there is no hip or too small.
-> But on top of that, it will help organize and manage memory.

: Therefore, the heap is considered a large chunk of fixed memory to keep things simple.
-> Mapped memory. We can do what we want from this memory.
-> Therefore, when referring to the heap, it actually refers to this memory area.

: What does "malloc" mean to manage the heap?
-> Let's take an abstract look at this first.
-> I'm going to allocate 8 bytes.
-> Therefore, call malloc to 8.
-> That is, block 8 bytes from the heap.
-> Each time "malloc" is called, the address of the heap whose area is blocked is returned.
-> Where can use the 8 bytes to this address.
-> Now, at that address "AAAABBBB"
-> Now let's allocate two more times. malloc(8) & malloc(8)

-> Questions are raised as to what blocking means.
-> And how does "malloc" know which address to return?
-> The most common method of implementation in a safe manner is the implementation of "DLmalloc"(= Doug Lea malloc).
-> And the "malloc" algorithm blocks the size of large chunks for each large chunk.
-> It is also empty before it is emptied four bytes for free implementation.

: Use this example to see how "malloc" actually works.
: Let's take a look at the codes one by one and imagine how the hip works.

: Heap1 (C Code)(/opt/protostar/bin/heap1)
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>

struct internet {
  int priority;
  char *name;
};

void winner()
{
  printf("and we have a winner @ %d\n", time(NULL));
}

int main(int argc, char **argv)
{
  struct internet *i1, *i2, *i3;

  i1 = malloc(sizeof(struct internet));
  i1->priority = 1;
  i1->name = malloc(8);

  i2 = malloc(sizeof(struct internet));
  i2->priority = 2;
  i2->name = malloc(8);

  strcpy(i1->name, argv[1]);
  strcpy(i2->name, argv[2]);

  printf("and that's a wrap folks!\n");
}
-> The goal here seems to be to call "winner().
-> Therefore, code execution should be redirected to that function.
-> A structure called "internet" is defined in the code, where there are two priority members.
-> The second member is a character pointer named "name." In other words, "name" includes a pointer.
-> And "main()" defines three variables in the structure structure “Internet” pointer, but only two.
-> Firstly there is enough space for object "1 I" to invoke "malloc".
-> Returns the number of bytes needed for this structure is "sizeof".
-> Allocated heap and defined name.
-> Then copy and insert the value through "strcpy".
-> And be aware that "strcpy" is always very suspicious because there is no length indication.
-> In this case, the "internet" name is assigned only 8 bytes, so it can actually ruin things by more than that 8 bytes.
-> i1->name = malloc(8);
-> It will then "printf" and then end the program.

-> There is a correction to be made.
-> The lowest bit of chunk is used to indicate that a PREVIOUS chunk has been used.
-> This is important in the "free()" algorithm.

'LiveOverFlow > Binary' 카테고리의 다른 글

0x22.  (0) 2021.11.03
0x21.  (0) 2021.11.03
0x19.  (0) 2021.11.03
0x18.  (0) 2021.11.03
0x17.  (0) 2021.11.03
Comments