From patchwork Wed Jul 13 21:56:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9228531 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 3B91960868 for ; Wed, 13 Jul 2016 21:57:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 271F928308 for ; Wed, 13 Jul 2016 21:57:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1B7B428319; Wed, 13 Jul 2016 21:57:33 +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.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID 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 4B40528308 for ; Wed, 13 Jul 2016 21:57:32 +0000 (UTC) Received: (qmail 17780 invoked by uid 550); 13 Jul 2016 21:56:33 -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: Reply-To: kernel-hardening@lists.openwall.com Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 17652 invoked from network); 13 Jul 2016 21:56:30 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2QxqiVZOBFkV4vIvpe3kiUFybcER7ygIQn97/7/1pRE=; b=Dc7W4d/aZrVj7zkkAT9ahONlJnmCMtOD3tHakU3ggy/XcskMR/pwNzzqyKkIPEU5o8 AjWGV2/hsE2IZqPgWg/MlNEUhRPn6is03/Bdr9MTKsXfU4dfTDfMc3VhPJ0p1TdFdzxM qobkd9D+p3a8m0zyeB6zJQLRW7Ympb/0ZpJQM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=2QxqiVZOBFkV4vIvpe3kiUFybcER7ygIQn97/7/1pRE=; b=KaTQH/g48kIccVj1cJ9Pdibi3X6uGugSBPWIiPDpNehg2LAFeyJq3N9QMk7fneuL+e WCJ6YRzcaRIcT0sTHfmqBtTRMZvbZ/Igw8lNLLAdJeL+LBvQSZ2AoHFjn7eRj4z4xHY2 e5Quz1q/aPXoX6gSUAMLN7fpAusJSBcvBSvstmt43iyIXIhp0JeccCpXMbIey6dx437t nO5aD7DcTdf+fQRlJaUSE/F/Ky/A/wHpH95zi1Q47RBR5ExEOTGEeeHp8K10xMUDB6r0 XBgD8gi0bBR2G0Ry3sMd7pMWx+OttJtrFRbn7DDJq00MzAgEiKihACzQr2jyzH3+kcxt g51A== X-Gm-Message-State: ALyK8tJGuPQH8gFPkn64wfC71UyMUrJqmO1+FuOFc8RLYeUke8FNwavwqGJR5XSwyhjx5CZ9 X-Received: by 10.66.8.37 with SMTP id o5mr17452529paa.100.1468446978940; Wed, 13 Jul 2016 14:56:18 -0700 (PDT) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , Rik van Riel , Casey Schaufler , PaX Team , Brad Spengler , Russell King , Catalin Marinas , Will Deacon , Ard Biesheuvel , Benjamin Herrenschmidt , Michael Ellerman , Tony Luck , Fenghua Yu , "David S. Miller" , x86@kernel.org, Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Andy Lutomirski , Borislav Petkov , Mathias Krause , Jan Kara , Vitaly Wool , Andrea Arcangeli , Dmitry Vyukov , Laura Abbott , linux-arm-kernel@lists.infradead.org, linux-ia64@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, sparclinux@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Date: Wed, 13 Jul 2016 14:56:03 -0700 Message-Id: <1468446964-22213-11-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1468446964-22213-1-git-send-email-keescook@chromium.org> References: <1468446964-22213-1-git-send-email-keescook@chromium.org> Subject: [kernel-hardening] [PATCH v2 10/11] mm: SLAB hardened usercopy support X-Virus-Scanned: ClamAV using ClamSMTP Under CONFIG_HARDENED_USERCOPY, this adds object size checking to the SLAB allocator to catch any copies that may span objects. Based on code from PaX and grsecurity. Signed-off-by: Kees Cook --- init/Kconfig | 1 + mm/slab.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/init/Kconfig b/init/Kconfig index f755a602d4a1..798c2020ee7c 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1757,6 +1757,7 @@ choice config SLAB bool "SLAB" + select HAVE_HARDENED_USERCOPY_ALLOCATOR help The regular slab allocator that is established and known to work well in all environments. It organizes cache hot objects in diff --git a/mm/slab.c b/mm/slab.c index cc8bbc1e6bc9..5e2d5f349aca 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -4477,6 +4477,36 @@ static int __init slab_proc_init(void) module_init(slab_proc_init); #endif +#ifdef CONFIG_HARDENED_USERCOPY +/* + * Rejects objects that are incorrectly sized. + * + * Returns NULL if check passes, otherwise const char * to name of cache + * to indicate an error. + */ +const char *__check_heap_object(const void *ptr, unsigned long n, + struct page *page) +{ + struct kmem_cache *cachep; + unsigned int objnr; + unsigned long offset; + + /* Find and validate object. */ + cachep = page->slab_cache; + objnr = obj_to_index(cachep, page, (void *)ptr); + BUG_ON(objnr >= cachep->num); + + /* Find offset within object. */ + offset = ptr - index_to_obj(cachep, page, objnr) - obj_offset(cachep); + + /* Allow address range falling entirely within object size. */ + if (offset <= cachep->object_size && n <= cachep->object_size - offset) + return NULL; + + return cachep->name; +} +#endif /* CONFIG_HARDENED_USERCOPY */ + /** * ksize - get the actual amount of memory allocated for a given object * @objp: Pointer to the object