From patchwork Wed Jul 20 20:27:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9240523 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 6DBCD6077C for ; Wed, 20 Jul 2016 20:32:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5F1CC27C26 for ; Wed, 20 Jul 2016 20:32:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 52F7427D4D; Wed, 20 Jul 2016 20:32:10 +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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C7D1327C26 for ; Wed, 20 Jul 2016 20:32:09 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1bPy8j-0003CB-N9; Wed, 20 Jul 2016 20:30:45 +0000 Received: from mail-pa0-x22a.google.com ([2607:f8b0:400e:c03::22a]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bPy5n-0007lc-Uy for linux-arm-kernel@lists.infradead.org; Wed, 20 Jul 2016 20:27:47 +0000 Received: by mail-pa0-x22a.google.com with SMTP id ks6so21478055pab.0 for ; Wed, 20 Jul 2016 13:27:23 -0700 (PDT) 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=X7a9HoLTLu04OH3it1uwSkvi8PaIAHbA035KA3g28yI=; b=NImqlZeddQ0AJw4Mty7OH0QUs9VqHCURXBJKRt1uu5oIVut/5mUH7lpvV7CZIYZjf1 HgpaKgd0ZFBey0zUbvnwr/FfKskrDVDbCkVIcThmT7LEF/ljyGMaeEbDVPsPYjZPNNb5 7w2ilFgbNUAhYp4rWfFptCiyCk0398vmbtq7c= 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=X7a9HoLTLu04OH3it1uwSkvi8PaIAHbA035KA3g28yI=; b=WYwK/MtFsLqxBL8NUhwMs32oU9ym3l5MAI8eiGFXnFYPUKlqLZiQmhDV5N4aGZRtPT geQS01Bg0cDGgvsR8zTk0oCJvP6SDAGhuAb2jJYZQ57tkq+IpZYs0LkE5DLr45p9erJo FTXEQ/fKtsG3umSPNAIwAYgYtYuEKiwZYyKt297mZlVfSTzyBGYBAIWdgmKHvsqHuryw F8zGxJdz6Bef6b8U2SFlUcnUxfcOWLQEETkOO5qOO986IBoe46F3TzlBjXX3sjIHS9yb m93XqpSUbaZe0FVzuwg67YXnnftEK6yzSaroZURKtPMCpEnCab0vjcOq7C5Mgo+RtDEk g33A== X-Gm-Message-State: ALyK8tIXsjMM91l6ZJVPDvmTaK2X8WLzoNmnO6ddd26mQhZb9vvGQG3AIR4UEmZc+FGIc+y2 X-Received: by 10.66.9.42 with SMTP id w10mr77505143paa.34.1469046443172; Wed, 20 Jul 2016 13:27:23 -0700 (PDT) Received: from www.outflux.net ([2002:ada4:7085:0:ae16:2dff:fe07:4fb6]) by smtp.gmail.com with ESMTPSA id cp3sm6623348pad.12.2016.07.20.13.27.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Jul 2016 13:27:22 -0700 (PDT) From: Kees Cook To: kernel-hardening@lists.openwall.com Subject: [PATCH v4 11/12] mm: SLAB hardened usercopy support Date: Wed, 20 Jul 2016 13:27:06 -0700 Message-Id: <1469046427-12696-12-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1469046427-12696-1-git-send-email-keescook@chromium.org> References: <1469046427-12696-1-git-send-email-keescook@chromium.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160720_132744_350368_4B5EF9DA X-CRM114-Status: GOOD ( 14.70 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jan Kara , Benjamin Herrenschmidt , Balbir Singh , Will Deacon , linux-mm@kvack.org, sparclinux@vger.kernel.org, linux-ia64@vger.kernel.org, Christoph Lameter , Andrea Arcangeli , linux-arch@vger.kernel.org, Michael Ellerman , x86@kernel.org, Russell King , linux-arm-kernel@lists.infradead.org, Catalin Marinas , PaX Team , Borislav Petkov , Mathias Krause , Fenghua Yu , Rik van Riel , Kees Cook , Vitaly Wool , David Rientjes , Tony Luck , Andy Lutomirski , Josh Poimboeuf , Andrew Morton , Dmitry Vyukov , Laura Abbott , Brad Spengler , Ard Biesheuvel , linux-kernel@vger.kernel.org, Pekka Enberg , Daniel Micay , Casey Schaufler , Joonsoo Kim , linuxppc-dev@lists.ozlabs.org, "David S. Miller" MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org 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 Tested-by: Valdis Kletnieks --- 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