From patchwork Tue Jul 25 09:42:12 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Popov X-Patchwork-Id: 9861561 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 13F5F6038C for ; Tue, 25 Jul 2017 09:42:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0051B2860A for ; Tue, 25 Jul 2017 09:42:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E8CD028615; Tue, 25 Jul 2017 09:42:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id EABC12860A for ; Tue, 25 Jul 2017 09:42:31 +0000 (UTC) Received: (qmail 3554 invoked by uid 550); 25 Jul 2017 09:42:29 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 3529 invoked from network); 25 Jul 2017 09:42:28 -0000 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:subject:from:to:cc:references :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=Bex93xdxOSV7urv0ZD748sM2C/sJDY2LNA9qsjqbuYU=; b=a9Z30rFR4ILdQq5Ifqoz+Im4KAO/C6AVcwF1HaNjfDUHnHvV/9xwyYI+7vekELNYnf 8+xMVyKUcI6xx4ihPUPFar3c283bQBy2ZqY0hufnYGqOh7M590JGNfb/ibcmq+zDp49w 0Qg0ZHI7OBSaTMH7CVSHINtfT3WXCugYO29rrOAEshNCoinROEcEY++d8IPmgp3ahn4E 7sSD2FPsRlJ8vj95umZA/grCpjtPD6KDfHQpWVr7d0bHIaSGBWb8Vph2iKHlAaQUywPj JiduI84Z73utKDH8mkx91SjFUQI7BGVbJhZ7yOT2aJ7AASAZPft+8b/4Pjhu/hL0Z2+l 4s2w== X-Gm-Message-State: AIVw111szI+Xp4/ACx4KutjXZxJ6Zmarx84j8OsakFRNqsU4g9Ez/Vm8 puQjPFIp0QVYxQ== X-Received: by 10.25.206.205 with SMTP id e196mr6114651lfg.126.1500975737119; Tue, 25 Jul 2017 02:42:17 -0700 (PDT) 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-Language: en-US Subject: [kernel-hardening] Re: [v3] mm: Add SLUB free list pointer obfuscation X-Virus-Scanned: ClamAV using ClamSMTP 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); }