From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750864AbdGYJmT (ORCPT ); Tue, 25 Jul 2017 05:42:19 -0400 Received: from mail-lf0-f68.google.com ([209.85.215.68]:38679 "EHLO mail-lf0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750781AbdGYJmS (ORCPT ); Tue, 25 Jul 2017 05:42:18 -0400 Reply-To: alex.popov@linux.com Subject: Re: [v3] mm: Add SLUB free list pointer obfuscation From: Alexander Popov To: Kees Cook Cc: Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , "Paul E. McKenney" , Ingo Molnar , Josh Triplett , Andy Lutomirski , Nicolas Pitre , Tejun Heo , Daniel Mack , Sebastian Andrzej Siewior , Sergey Senozhatsky , Helge Deller , Rik van Riel , linux-mm@kvack.org, Tycho Andersen , linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, alex.popov@linux.com References: <20170706002718.GA102852@beast> Message-ID: <2a30f7bf-601b-f442-9664-7de5a1501206@linux.com> Date: Tue, 25 Jul 2017 12:42:12 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org >>From 86f4f1f6deb76849e00c761fa30eeb479f789c35 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Mon, 24 Jul 2017 23:16:28 +0300 Subject: [PATCH 2/2] mm/slub.c: add a naive detection of double free or corruption On 25.07.2017 00:17, Alexander Popov wrote: > On 06.07.2017 03:27, Kees Cook wrote: >> This SLUB free list pointer obfuscation code is modified from Brad >> Spengler/PaX Team's code in the last public patch of grsecurity/PaX based >> on my understanding of the code. Changes or omissions from the original >> code are mine and don't reflect the original grsecurity/PaX code. >> >> This adds a per-cache random value to SLUB caches that is XORed with >> their freelist pointer address and value. This adds nearly zero overhead >> and frustrates the very common heap overflow exploitation method of >> overwriting freelist pointers. A recent example of the attack is written >> up here: http://cyseclabs.com/blog/cve-2016-6187-heap-off-by-one-exploit >> >> This is based on patches by Daniel Micay, and refactored to minimize the >> use of #ifdef. > > Hello! > > This is an addition to the SLAB_FREELIST_HARDENED feature. I'm sending it > according the discussion here: > http://www.openwall.com/lists/kernel-hardening/2017/07/17/9 In my previous message my email client wrapped one line and corrupted the patch. Excuse me for that. See the fixed patch below. -- >8 -- Add an assertion similar to "fasttop" check in GNU C Library allocator as a part of SLAB_FREELIST_HARDENED feature. An object added to a singly linked freelist should not point to itself. That helps to detect some double free errors (e.g. CVE-2017-2636) without slub_debug and KASAN. Signed-off-by: Alexander Popov --- mm/slub.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mm/slub.c b/mm/slub.c index c92d636..f39d06e 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -290,6 +290,10 @@ static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp) { unsigned long freeptr_addr = (unsigned long)object + s->offset; +#ifdef CONFIG_SLAB_FREELIST_HARDENED + BUG_ON(object == fp); /* naive detection of double free or corruption */ +#endif + *(void **)freeptr_addr = freelist_ptr(s, fp, freeptr_addr); } -- 2.7.4 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf0-f71.google.com (mail-lf0-f71.google.com [209.85.215.71]) by kanga.kvack.org (Postfix) with ESMTP id D0F2B6B0292 for ; Tue, 25 Jul 2017 05:42:18 -0400 (EDT) Received: by mail-lf0-f71.google.com with SMTP id g25so2302437lfh.13 for ; Tue, 25 Jul 2017 02:42:18 -0700 (PDT) Received: from mail-lf0-f66.google.com (mail-lf0-f66.google.com. [209.85.215.66]) by mx.google.com with ESMTPS id r76si5326181lfi.366.2017.07.25.02.42.17 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Jul 2017 02:42:17 -0700 (PDT) Received: by mail-lf0-f66.google.com with SMTP id 65so552103lfa.0 for ; Tue, 25 Jul 2017 02:42:17 -0700 (PDT) Reply-To: alex.popov@linux.com Subject: Re: [v3] mm: Add SLUB free list pointer obfuscation From: Alexander Popov References: <20170706002718.GA102852@beast> Message-ID: <2a30f7bf-601b-f442-9664-7de5a1501206@linux.com> Date: Tue, 25 Jul 2017 12:42:12 +0300 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: owner-linux-mm@kvack.org List-ID: To: Kees Cook Cc: Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , "Paul E. McKenney" , Ingo Molnar , Josh Triplett , Andy Lutomirski , Nicolas Pitre , Tejun Heo , Daniel Mack , Sebastian Andrzej Siewior , Sergey Senozhatsky , Helge Deller , Rik van Riel , linux-mm@kvack.org, Tycho Andersen , linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, alex.popov@linux.com From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alexander Popov References: <20170706002718.GA102852@beast> Message-ID: <2a30f7bf-601b-f442-9664-7de5a1501206@linux.com> Date: Tue, 25 Jul 2017 12:42:12 +0300 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Subject: [kernel-hardening] Re: [v3] mm: Add SLUB free list pointer obfuscation To: Kees Cook Cc: Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , "Paul E. McKenney" , Ingo Molnar , Josh Triplett , Andy Lutomirski , Nicolas Pitre , Tejun Heo , Daniel Mack , Sebastian Andrzej Siewior , Sergey Senozhatsky , Helge Deller , Rik van Riel , linux-mm@kvack.org, Tycho Andersen , linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com, alex.popov@linux.com List-ID: >>From 86f4f1f6deb76849e00c761fa30eeb479f789c35 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Mon, 24 Jul 2017 23:16:28 +0300 Subject: [PATCH 2/2] mm/slub.c: add a naive detection of double free or corruption On 25.07.2017 00:17, Alexander Popov wrote: > On 06.07.2017 03:27, Kees Cook wrote: >> This SLUB free list pointer obfuscation code is modified from Brad >> Spengler/PaX Team's code in the last public patch of grsecurity/PaX based >> on my understanding of the code. Changes or omissions from the original >> code are mine and don't reflect the original grsecurity/PaX code. >> >> This adds a per-cache random value to SLUB caches that is XORed with >> their freelist pointer address and value. This adds nearly zero overhead >> and frustrates the very common heap overflow exploitation method of >> overwriting freelist pointers. A recent example of the attack is written >> up here: http://cyseclabs.com/blog/cve-2016-6187-heap-off-by-one-exploit >> >> This is based on patches by Daniel Micay, and refactored to minimize the >> use of #ifdef. > > Hello! > > This is an addition to the SLAB_FREELIST_HARDENED feature. I'm sending it > according the discussion here: > http://www.openwall.com/lists/kernel-hardening/2017/07/17/9 In my previous message my email client wrapped one line and corrupted the patch. Excuse me for that. See the fixed patch below. -- >8 -- Add an assertion similar to "fasttop" check in GNU C Library allocator as a part of SLAB_FREELIST_HARDENED feature. An object added to a singly linked freelist should not point to itself. That helps to detect some double free errors (e.g. CVE-2017-2636) without slub_debug and KASAN. Signed-off-by: Alexander Popov --- mm/slub.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mm/slub.c b/mm/slub.c index c92d636..f39d06e 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -290,6 +290,10 @@ static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp) { unsigned long freeptr_addr = (unsigned long)object + s->offset; +#ifdef CONFIG_SLAB_FREELIST_HARDENED + BUG_ON(object == fp); /* naive detection of double free or corruption */ +#endif + *(void **)freeptr_addr = freelist_ptr(s, fp, freeptr_addr); } -- 2.7.4