Code Monkey home page Code Monkey logo

Comments (11)

Kyle-Kyle avatar Kyle-Kyle commented on August 22, 2024

Because c should have the same range as a. size of a is 512. Size of c is 500. They fall in the same range in libc.

from how2heap.

Kyle-Kyle avatar Kyle-Kyle commented on August 22, 2024

There is nothing wrong with the code.

from how2heap.

0x3c3e avatar 0x3c3e commented on August 22, 2024

@Kyle-Kyle so I got the idea wrong? On the end of execution a != c (pointers) and it's ok?
As I understand from heap-exploitation 512 bytes chunk is splitted in two 500 and probably 12 bytes (it's probably how it work on my local machine). And if c size fall in range from 505 to 520 malloc choose a chunk without splitting.

from how2heap.

Kyle-Kyle avatar Kyle-Kyle commented on August 22, 2024

There is no spliting at all in this case on you local machine. You know how bins works right? When you do a malloc, the size will be rounded up. So in this case, 512 will be rounded up to 520. Everything between 505 to 520 will be rounded up to 520. If the size does not fall in the range, it does not use the size at all, there is no way to return the same chunk.

from how2heap.

0x3c3e avatar 0x3c3e commented on August 22, 2024

@Kyle-Kyle so output as below is expected? thank you for answers

This file doesn't demonstrate an attack, but shows the nature of glibc's allocator.
glibc uses a first-fit algorithm to select a free chunk.
If a chunk is free and large enough, malloc will select this chunk.
This can be exploited in a use-after-free situation.
Allocating 2 buffers. They can be large, don't have to be fastbin.
1st malloc(512): 0x557a17c7c260
2nd malloc(256): 0x557a17c7c470
we could continue mallocing here...
now let's put a string at a that we can read later "this is A!"
first allocation 0x557a17c7c260 points to this is A!
Freeing the first one...
We don't need to free anything again. As long as we allocate less than 512, it will end up at 0x557a17c7c260
So, let's allocate 500 bytes
3rd malloc(500): 0x557a17c7c580
And put a different string here, "this is C!"
3rd allocation 0x557a17c7c580 points to this is C!
first allocation 0x557a17c7c260 points to 
If we reuse the first allocation, it now holds the data from the third allocation.

from how2heap.

m1ghtym0 avatar m1ghtym0 commented on August 22, 2024

@0x3c3e Is right, because of tcache this example does not work as expected right now.
Because 500 and 512 end up in two different tcache bins.
I'll fix it in a sec.

from how2heap.

m1ghtym0 avatar m1ghtym0 commented on August 22, 2024

The TCache index is calculated as:

/* When "x" is from chunksize().  */
# define csize2tidx(x) (((x) - MINSIZE + MALLOC_ALIGNMENT - 1) / MALLOC_ALIGNMENT)
/* When "x" is a user-provided size.  */
# define usize2tidx(x) csize2tidx (request2size (x))

#define request2size(req)                                         \
(((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE)  ?             \
MINSIZE :                                                      \
((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)

/* The smallest possible chunk */
#define MIN_CHUNK_SIZE        (offsetof(struct malloc_chunk, fd_nextsize))

/* The smallest size we can malloc is an aligned minimal chunk */
#define MINSIZE  \
  (unsigned long)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK))

#define MALLOC_ALIGNMENT (2 * SIZE_SZ < __alignof__ (long double) \
			  ? __alignof__ (long double) : 2 * SIZE_SZ)

So if we request 505 we get:

req = 505
size = (505 + 8 + 0xf) & ~0xf  = 528
tidx = (528 - 0x20 +0x10 - 1) / 0x10 = 31

If we request 520 we get:

req = 520
size = (520 + 8 + 0xf) & ~0xf  = 528
tidx = (528 - 0x20 +0x10 - 1) / 0x10 = 31

So those are the extremes around 512 that end up in the same tcache idx
I should add this calculation to the first_fit.c example maybe...

from how2heap.

m1ghtym0 avatar m1ghtym0 commented on August 22, 2024

@Kyle-Kyle Before it was going in the unsorted_bin so it returned the same chunk.
In that regard the behavior changed.
We could also just make it big enough to not fall in tcache range.

from how2heap.

m1ghtym0 avatar m1ghtym0 commented on August 22, 2024

In fact, I'll just do that and add an example for tcache indexing.

from how2heap.

Kyle-Kyle avatar Kyle-Kyle commented on August 22, 2024

yeap. I finally get the problem. My bad. @0x3c3e you are right, this is not expected.

from how2heap.

m1ghtym0 avatar m1ghtym0 commented on August 22, 2024

@0x3c3e Should be fixed now, see also the calc_tcache_idx example

from how2heap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.