From patchwork Wed Mar 29 18:16:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9652233 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 59A936034B for ; Wed, 29 Mar 2017 18:18:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 42FAE2851A for ; Wed, 29 Mar 2017 18:18:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3727F28520; Wed, 29 Mar 2017 18:18:51 +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=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B8C272851A for ; Wed, 29 Mar 2017 18:18:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=haOZQAk929kNR286Kvh8XYY43uabZ39QQNum7xGuWDo=; b=GEyPfWh4LtS8hkksLwNNAbiOwv fKHxBGXN5pQlRP7t6/kNj9h+RQi1SPQ+bNECsOYY7MLHqJVU40yN7xzFa2QoX83Oni0qZtL5EbZHF FJHGE444cxmtwnPNcG1Hojf1Ou7qzP6ZCECd1peNr2QIeE4PHAp7aS1Fq7zbc9sS4z2ahe1PH+Wi1 UuVY9Ql1ncLwtHa8RyWirHGdv4tweqnCxthPdIHl0e04ItHcn+uCQNqTTZ1rwy1Xd/ixF1g1kQ1S9 X/1Vmt+ViRpcNE0HxnTM/HPaHt3NDKge0tdy3JBGQBO9cqifvPAfK9IaEggAz7HDMu2+s9/ZFISW0 kB0CSRfA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1ctIBC-0007M7-Ej; Wed, 29 Mar 2017 18:18:46 +0000 Received: from mail-pg0-x236.google.com ([2607:f8b0:400e:c05::236]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1ctI9G-00058P-6i for linux-arm-kernel@lists.infradead.org; Wed, 29 Mar 2017 18:16:52 +0000 Received: by mail-pg0-x236.google.com with SMTP id x125so14691900pgb.0 for ; Wed, 29 Mar 2017 11:16:25 -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=0CmEdJxoHiUHjW2llx4K/untYV+5fUP/ICusZJ3RoZQ=; b=Y6BP7xcFPGeuw5/JEyNCCa95NiMT+l/IF4ui2CHhCKzgu/ZUhzrJfCsCW+Ua68iA29 yH7d0+K9DFSMeoE8KbN5fJc6ZO6/HRuwCwNuQsLZoHku5X2PiZrqQuxqksy0Z/R9/pJ+ 2Gq7IJIjnjv4FictoD46x8h+VShpjUaLPv14U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0CmEdJxoHiUHjW2llx4K/untYV+5fUP/ICusZJ3RoZQ=; b=qVCL1/svlpbqxgKP8BmNBjJuaAdvV5XnlXjOWAQYG2qPSL75TzdmY0aMwSKsQOh4ue jBWW3MSO0Utd9ygaOZvqgGzjDZrxj2e6vDVqB53sPPw8IjIuanGaMZRoe/2habrrtJZ8 Ckmo3dmeSHppctg6byYBqQ114ZU3w34sO2X72Kfm9SFtR9ePS1vZ8yxEp6j0bwRljGlH k0MwA4Ge43kWhdGafGT8WxNPATSFzLqGYCqdfnMpwFXOOHwFd/APoy5HXQicOIRDQ+nx MONEKvEjvXb1Bmx4EyPZxvaMvypXtb5GJYLXEtyx687eKwvNUFtQYlSTgViR6HwPrOGl gHlw== X-Gm-Message-State: AFeK/H1UwKa5S5dqj1uIuVPeKQU+21Qs5Dq3Hk0i0YPHxRu6eS+z+oqSfoZX/a08UOVirXVy X-Received: by 10.98.160.212 with SMTP id p81mr1841565pfl.204.1490811385418; Wed, 29 Mar 2017 11:16:25 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id p16sm10095592pgc.4.2017.03.29.11.16.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Mar 2017 11:16:24 -0700 (PDT) From: Kees Cook To: kernel-hardening@lists.openwall.com Subject: [RFC v2][PATCH 09/11] list: add rare_write() list helpers Date: Wed, 29 Mar 2017 11:16:01 -0700 Message-Id: <1490811363-93944-10-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1490811363-93944-1-git-send-email-keescook@chromium.org> References: <1490811363-93944-1-git-send-email-keescook@chromium.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170329_111646_410671_66FC5962 X-CRM114-Status: GOOD ( 13.56 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Hoeun Ryu , Kees Cook , x86@kernel.org, Russell King , linux-kernel@vger.kernel.org, Emese Revfy , Andy Lutomirski , PaX Team , linux-arm-kernel@lists.infradead.org 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 Some structures that are intended to be made write-rarely are designed to be linked by lists. As a result, there need to be rare_write()-supported linked list primitives. As found in PaX, this adds list management helpers for doing updates to rarely-changed lists. Signed-off-by: Kees Cook --- include/linux/list.h | 17 +++++++++++++++++ lib/Makefile | 2 +- lib/list_debug.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/include/linux/list.h b/include/linux/list.h index ae537fa46216..50fdd5b737aa 100644 --- a/include/linux/list.h +++ b/include/linux/list.h @@ -126,6 +126,23 @@ static inline void list_del(struct list_head *entry) entry->prev = LIST_POISON2; } +extern void __rare_list_add(struct list_head *new, + struct list_head *prev, + struct list_head *next); + +static inline void +rare_list_add(__wr_rare_type struct list_head *new, struct list_head *head) +{ + __rare_list_add((struct list_head *)new, head, head->next); +} +static inline void +rare_list_add_tail(__wr_rare_type struct list_head *new, struct list_head *head) +{ + __rare_list_add((struct list_head *)new, head->prev, head); +} + +extern void rare_list_del(__wr_rare_type struct list_head *entry); + /** * list_replace - replace old entry by new one * @old : the element to be replaced diff --git a/lib/Makefile b/lib/Makefile index 320ac46a8725..cd64fd8f7a21 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -83,7 +83,7 @@ obj-$(CONFIG_BTREE) += btree.o obj-$(CONFIG_INTERVAL_TREE) += interval_tree.o obj-$(CONFIG_ASSOCIATIVE_ARRAY) += assoc_array.o obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o -obj-$(CONFIG_DEBUG_LIST) += list_debug.o +obj-y += list_debug.o obj-$(CONFIG_DEBUG_OBJECTS) += debugobjects.o ifneq ($(CONFIG_HAVE_DEC_LOCK),y) diff --git a/lib/list_debug.c b/lib/list_debug.c index a34db8d27667..1add73f9479a 100644 --- a/lib/list_debug.c +++ b/lib/list_debug.c @@ -10,7 +10,9 @@ #include #include #include +#include +#ifdef CONFIG_DEBUG_LIST /* * Check that the data structures for the list manipulations are reasonably * valid. Failures here indicate memory corruption (and possibly an exploit @@ -60,3 +62,38 @@ bool __list_del_entry_valid(struct list_head *entry) } EXPORT_SYMBOL(__list_del_entry_valid); + +#endif /* CONFIG_DEBUG_LIST */ + +void __rare_list_add(struct list_head *new, struct list_head *prev, + struct list_head *next) +{ + if (!__list_add_valid(new, prev, next)) + return; + + rare_write_begin(); + __rare_write(next->prev, new); + __rare_write(new->next, next); + __rare_write(new->prev, prev); + __rare_write(prev->next, new); + rare_write_end(); +} +EXPORT_SYMBOL(__rare_list_add); + +void rare_list_del(__wr_rare_type struct list_head *entry_const) +{ + struct list_head *entry = (struct list_head *)entry_const; + struct list_head *prev = entry->prev; + struct list_head *next = entry->next; + + if (!__list_del_entry_valid(entry)) + return; + + rare_write_begin(); + __rare_write(next->prev, prev); + __rare_write(prev->next, next); + __rare_write(entry->next, LIST_POISON1); + __rare_write(entry->prev, LIST_POISON2); + rare_write_end(); +} +EXPORT_SYMBOL(rare_list_del);