高版本glibc的tcache和fastbin指针加密机制

先贴一下源码

tcache:

    static __always_inline void 
    tcache_put (mchunkptr chunk, size_t tc_idx)
    {
      tcache_entry *e = (tcache_entry *) chunk2mem (chunk);
      /* Mark this chunk as "in the tcache" so the test in _int_free will
         detect a double free.  */
      e->key = tcache;
      e->next = PROTECT_PTR (&e->next, tcache->entries[tc_idx]);
      tcache->entries[tc_idx] = e;
      ++(tcache->counts[tc_idx]);
    }
    static __always_inline void *
    tcache_get (size_t tc_idx)
    {
      tcache_entry *e = tcache->entries[tc_idx];
      if (__glibc_unlikely (!aligned_OK (e)))
        malloc_printerr ("malloc(): unaligned tcache chunk detected");
      tcache->entries[tc_idx] = REVEAL_PTR (e->next);
      --(tcache->counts[tc_idx]);
      e->key = NULL;
      return (void *) e;
    }

fsatbin:

    if (SINGLE_THREAD_P)  
    {
        /* Check that the top of the bin is not the record we are going to
            add (i.e., double free).  */
        if (__builtin_expect(old == p, 0))
            malloc_printerr("double free or corruption (fasttop)");
        p->fd = PROTECT_PTR(&p->fd, old);
        *fb = p;
    }
    else
        do
        {
            /* Check that the top of the bin is not the record we are going to
                add (i.e., double free).  */
            if (__builtin_expect(old == p, 0))
                malloc_printerr("double free or corruption (fasttop)");
            old2 = old;
            p->fd = PROTECT_PTR(&p->fd, old);
        } while ((old = catomic_compare_and_exchange_val_rel(fb, p, old2))
            != old2);

从tcache来看:

PROTECT_PTR:对 pos 右移了 12 位(去除了末尾的 3 位信息),再异或原来的指针(在这之前 next 储存的内容)。

随便找个heap题试试

一次free后如下

二次free后如下

此时bins如下:

的计算方式:

取出时(二次异或不变):

所以2.29以后要泄露的不只是libc了,还要泄露key(就是这个东西,第一个chunk的fd<<12,去掉第三位的heap基地址,因为第一个被free的chunk其实是移位后xor了个0所以会这样。)

 

 

 

 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值