glibc源码分析(三)

今天自己直接进入带debug symbol的程序进行分析所以会有截图emem

1.编译一个带有debug symbol的执行档

在gcc命令中代入-g即可,然后使用gdb调试既可以看到,记得还要在载入symbol文件就可以了主要是为了学习一些在堆块上的分配限制规则

2.进入libc

这里先给出我写的测试源码很简单师傅们可以忽略

#include<stdio.h>
int main()
{
	void *chunk1,*chunk2,*chunk3;
	chunk1=malloc(20);
	chunk2=malloc(30);
	free(chunk1);
	return 0;
}

首先我们到malloc的地方显示进入__libc_malloc(byte)
在这里插入图片描述
将__malloc_hook放入hook然后判断__malloc_hook是否为空不是空则调用返回是空的话就会到主分配区main_arena去寻找大小符合内存
在这里插入图片描述
初始时一般都为空然后就是昨天所说的关于分配哦内存与锁的操作的最后分配成功会返回指针指向memery
在给上结构源码

 _int_malloc (mstate av, size_t bytes)                                                                                              {                                                                                                                                 │
      INTERNAL_SIZE_T nb;               /* normalized request size */                                                                 
    unsigned int idx;                 /* associated bin index */                                                                    
    mbinptr bin;                      /* associated bin */                                                                                                                                                                                            	     mchunkptr victim;                 /* inspected/selected chunk */                                                                    INTERNAL_SIZE_T size;             /* its size */                                                                                    int victim_index;                 /* its bin index */                                                                                mchunkptr remainder;              /* remainder from a split */                                                                       unsigned long remainder_size;     /* its size */                                                                                
  unsigned int block;               /* bit map traverser */                                                                            unsigned int bit;                 /* bit map traverser */                                                                       
 unsigned int map;                 /* current word of binmap */                                                                  
 mchunkptr fwd;                    /* misc temp for linking */                                                                  
 mchunkptr bck;                    /* misc temp for linking */

checked_request2size (bytes, nb);这个函数用来计算分配实际的大小通常会比使用者所分配的要大分配时会对齐
在这里插入图片描述

  if ((unsigned long)(nb) <= (unsigned long)(get_max_fast ()))
   {     idx = fastbin_index(nb);     
   mfastbinptr* fb = &fastbin (av, idx); 
   #ifdef ATOMIC_FASTBINS    
    mchunkptr pp = *fb; 
        do      
    {       
      victim = pp;        
       if (victim == NULL)         
         break;       } 
85 
 
    while ((pp = catomic_compare_and_exchange_val_acq (fb, victim->fd, victim))            != victim);
     #else     victim = *fb; 
     #endif     if (victim != 0) 
     {       if (__builtin_expect (fastbin_index (chunksize (victim)) != idx, 0))         
     {           
    errstr = "malloc(): memory corruption (fast)";        
     errout:           
     malloc_printerr (check_action, errstr, chunk2mem (victim));        
        return NULL;        
         } #ifndef ATOMIC_FASTBINS     
           *fb = victim->fd; #endif      
            check_remalloced_chunk(av, victim, nb);       
            void *p = chunk2mem(victim);    
               if (__builtin_expect (perturb_byte, 0))         
               alloc_perturb (p, bytes);       return p;     
               }   }

这里是判断size符合哪一种bin的大小好像全部检查了一遍
首先是fastbin的检查如果没有开启优化的话就很简单首先就会根据大小获得所属fastbin的index根据index获得所需空闲链表的头指针然后将头指针main_arena中的一块作为洗衣歌chunk的头部fastbin大小的chunk的insues位为一也就是不会发生unlink操作不会与chunk合并(正常情况下)

在这里我们发现fastbin似乎没有对trunk的size对齐做处理只是检查了是否存在size与是否属于fastbin index我们可以通过伪造size字段造成
got hackjk和覆写malloc_hook写到栈上完成栈溢出等一些列地址写的操作

师傅们可能写的有点不好见谅-------

发布了74 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_37433000/article/details/103842728