linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] slub: Relocate freelist pointer to middle of object
@ 2020-03-06  0:26 Kees Cook
  2020-03-08 19:21 ` Christopher Lameter
  0 siblings, 1 reply; 6+ messages in thread
From: Kees Cook @ 2020-03-06  0:26 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Christoph Lameter, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Daniel Micay, Vitaly Nikolenko, Silvio Cesare, linux-mm,
	linux-kernel

In a recent discussion[1] with Vitaly Nikolenko and Silvio Cesare,
it became clear that moving the freelist pointer away from the edge of
allocations would likely improve the overall defensive posture of the
inline freelist pointer. My benchmarks show no meaningful change to
performance (they seem to show it being faster), so this looks like a
reasonable change to make.

Instead of having the freelist pointer at the very beginning of an
allocation (offset 0) or at the very end of an allocation (effectively
offset -sizeof(void *) from the next allocation), move it away from
the edges of the allocation and into the middle. This provides some
protection against small-sized neighboring overflows (or underflows),
for which the freelist pointer is commonly the target. (Large or well
controlled overwrites are much more likely to attack live object contents,
instead of attempting freelist corruption.)

The vaunted kernel build benchmark, across 5 runs. Before:

	Mean: 250.05
	Std Dev: 1.85

and after, which appears mysteriously faster:

	Mean: 247.13
	Std Dev: 0.76

Attempts at running "sysbench --test=memory" show the change to be well
in the noise (sysbench seems to be pretty unstable here -- it's not
really measuring allocation).

Hackbench is more allocation-heavy, and while the std dev is above the
difference, it looks like may manifest as an improvement as well:

20 runs of "hackbench -g 20 -l 1000", before:

	Mean: 36.322
	Std Dev: 0.577

and after:

	Mean: 36.056
	Std Dev: 0.598

[1] https://twitter.com/vnik5287/status/1235113523098685440

Cc: Vitaly Nikolenko <vnik@duasynt.com>
Cc: Silvio Cesare <silvio.cesare@gmail.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 mm/slub.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/mm/slub.c b/mm/slub.c
index 107d9d89cf96..45926cb4514f 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -3562,6 +3562,13 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order)
 		 */
 		s->offset = size;
 		size += sizeof(void *);
+	} else if (size > sizeof(void *)) {
+		/*
+		 * Store freelist pointer near middle of object to keep
+		 * it away from the edges of the object to avoid small
+		 * sized over/underflows from neighboring allocations.
+		 */
+		s->offset = ALIGN(size / 2, sizeof(void *));
 	}
 
 #ifdef CONFIG_SLUB_DEBUG
-- 
2.20.1


-- 
Kees Cook

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] slub: Relocate freelist pointer to middle of object
  2020-03-06  0:26 [PATCH] slub: Relocate freelist pointer to middle of object Kees Cook
@ 2020-03-08 19:21 ` Christopher Lameter
  2020-03-11 14:48   ` David Laight
  0 siblings, 1 reply; 6+ messages in thread
From: Christopher Lameter @ 2020-03-08 19:21 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Daniel Micay, Vitaly Nikolenko, Silvio Cesare, linux-mm,
	linux-kernel

On Thu, 5 Mar 2020, Kees Cook wrote:

> Instead of having the freelist pointer at the very beginning of an
> allocation (offset 0) or at the very end of an allocation (effectively
> offset -sizeof(void *) from the next allocation), move it away from
> the edges of the allocation and into the middle. This provides some
> protection against small-sized neighboring overflows (or underflows),
> for which the freelist pointer is commonly the target. (Large or well
> controlled overwrites are much more likely to attack live object contents,
> instead of attempting freelist corruption.)

Sounds good. You could even randomize the position to avoid attacks on via
the freelist pointer.

Acked-by: Christoph Lameter <cl@linux.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH] slub: Relocate freelist pointer to middle of object
  2020-03-08 19:21 ` Christopher Lameter
@ 2020-03-11 14:48   ` David Laight
  2020-03-11 17:37     ` Kees Cook
  2020-03-11 17:44     ` Kees Cook
  0 siblings, 2 replies; 6+ messages in thread
From: David Laight @ 2020-03-11 14:48 UTC (permalink / raw)
  To: 'Christopher Lameter', Kees Cook
  Cc: Andrew Morton, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Daniel Micay, Vitaly Nikolenko, Silvio Cesare, linux-mm,
	linux-kernel

From: Christopher Lameter
> Sent: 08 March 2020 19:21

> 
> On Thu, 5 Mar 2020, Kees Cook wrote:
> 
> > Instead of having the freelist pointer at the very beginning of an
> > allocation (offset 0) or at the very end of an allocation (effectively
> > offset -sizeof(void *) from the next allocation), move it away from
> > the edges of the allocation and into the middle. This provides some
> > protection against small-sized neighboring overflows (or underflows),
> > for which the freelist pointer is commonly the target. (Large or well
> > controlled overwrites are much more likely to attack live object contents,
> > instead of attempting freelist corruption.)
> 
> Sounds good. You could even randomize the position to avoid attacks on via
> the freelist pointer.

Random overwrites could be detected (fairly cheaply) by putting two
copies of the pointer into the same cacheline in the buffer.
Or better make the second one 'pointer xor constant'.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] slub: Relocate freelist pointer to middle of object
  2020-03-11 14:48   ` David Laight
@ 2020-03-11 17:37     ` Kees Cook
  2020-03-11 17:44     ` Kees Cook
  1 sibling, 0 replies; 6+ messages in thread
From: Kees Cook @ 2020-03-11 17:37 UTC (permalink / raw)
  To: David Laight
  Cc: 'Christopher Lameter',
	Andrew Morton, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Daniel Micay, Vitaly Nikolenko, Silvio Cesare, linux-mm,
	linux-kernel

On Wed, Mar 11, 2020 at 02:48:05PM +0000, David Laight wrote:
> From: Christopher Lameter
> > Sent: 08 March 2020 19:21
> 
> > 
> > On Thu, 5 Mar 2020, Kees Cook wrote:
> > 
> > > Instead of having the freelist pointer at the very beginning of an
> > > allocation (offset 0) or at the very end of an allocation (effectively
> > > offset -sizeof(void *) from the next allocation), move it away from
> > > the edges of the allocation and into the middle. This provides some
> > > protection against small-sized neighboring overflows (or underflows),
> > > for which the freelist pointer is commonly the target. (Large or well
> > > controlled overwrites are much more likely to attack live object contents,
> > > instead of attempting freelist corruption.)
> > 
> > Sounds good. You could even randomize the position to avoid attacks on via
> > the freelist pointer.
> 
> Random overwrites could be detected (fairly cheaply) by putting two
> copies of the pointer into the same cacheline in the buffer.
> Or better make the second one 'pointer xor constant'.

My sense is that this starts to stray closer to "too much overhead" vs
the mitigation benefit against known heap metadata attacks. I'm open to
seeing patches, of course, though! :)

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] slub: Relocate freelist pointer to middle of object
  2020-03-11 14:48   ` David Laight
  2020-03-11 17:37     ` Kees Cook
@ 2020-03-11 17:44     ` Kees Cook
  2020-03-15 14:11       ` Christopher Lameter
  1 sibling, 1 reply; 6+ messages in thread
From: Kees Cook @ 2020-03-11 17:44 UTC (permalink / raw)
  To: 'Christopher Lameter'
  Cc: David Laight, Andrew Morton, Pekka Enberg, David Rientjes,
	Joonsoo Kim, Daniel Micay, Vitaly Nikolenko, Silvio Cesare,
	linux-mm, linux-kernel

From: Christopher Lameter
> Sent: 08 March 2020 19:21
> 
> On Thu, 5 Mar 2020, Kees Cook wrote:
> 
> > Instead of having the freelist pointer at the very beginning of an
> > allocation (offset 0) or at the very end of an allocation (effectively
> > offset -sizeof(void *) from the next allocation), move it away from
> > the edges of the allocation and into the middle. This provides some
> > protection against small-sized neighboring overflows (or underflows),
> > for which the freelist pointer is commonly the target. (Large or well
> > controlled overwrites are much more likely to attack live object contents,
> > instead of attempting freelist corruption.)
> 
> Sounds good. You could even randomize the position to avoid attacks on via
> the freelist pointer.

That's a good point. "offset" is just calculated once, and for many
slabs, the available space is quite large. I wonder what the best
practice might be for how far from the edge to stay. Hmmm. Maybe simply
carve it into thirds, and randomize the offset within the middle third?

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] slub: Relocate freelist pointer to middle of object
  2020-03-11 17:44     ` Kees Cook
@ 2020-03-15 14:11       ` Christopher Lameter
  0 siblings, 0 replies; 6+ messages in thread
From: Christopher Lameter @ 2020-03-15 14:11 UTC (permalink / raw)
  To: Kees Cook
  Cc: David Laight, Andrew Morton, Pekka Enberg, David Rientjes,
	Joonsoo Kim, Daniel Micay, Vitaly Nikolenko, Silvio Cesare,
	linux-mm, linux-kernel

On Wed, 11 Mar 2020, Kees Cook wrote:

> > Sounds good. You could even randomize the position to avoid attacks on via
> > the freelist pointer.
>
> That's a good point. "offset" is just calculated once, and for many
> slabs, the available space is quite large. I wonder what the best

Correct.

> practice might be for how far from the edge to stay. Hmmm. Maybe simply
> carve it into thirds, and randomize the offset within the middle third?

Take off the first and last word and randomize within the space that is
left?

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-03-15 14:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-06  0:26 [PATCH] slub: Relocate freelist pointer to middle of object Kees Cook
2020-03-08 19:21 ` Christopher Lameter
2020-03-11 14:48   ` David Laight
2020-03-11 17:37     ` Kees Cook
2020-03-11 17:44     ` Kees Cook
2020-03-15 14:11       ` Christopher Lameter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).