From patchwork Tue Aug 16 21:11:01 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9284731 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 7BAFA607FD for ; Tue, 16 Aug 2016 21:11:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6BC5B28538 for ; Tue, 16 Aug 2016 21:11:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5996328703; Tue, 16 Aug 2016 21:11:47 +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 7796028538 for ; Tue, 16 Aug 2016 21:11:46 +0000 (UTC) Received: (qmail 7933 invoked by uid 550); 16 Aug 2016 21:11:34 -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 7762 invoked from network); 16 Aug 2016 21:11: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=bVleVWbgQY2ozYS/zZAZ453KKIgcxDDecKusnDX0xxw=; b=ib2cKHT1F+evqMyenJbx9MVhdZG1YZS+Gy66XzHoDnNoMAXF8PgKmexV7ayfHFIMgT PwMvYsHY3wvc8Z5XP+jIDEzHi+fRuGF5SLqlsrU8d4QzNaOaZSD1NCXN1dIZO1QMErXb U6YSK12MfnsuTZ4HJzsmR+99Y3flKsTTJT95Q= 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=bVleVWbgQY2ozYS/zZAZ453KKIgcxDDecKusnDX0xxw=; b=mWWUwoQxYbKpeEmWA2hGppn2Ct1DqscGNFwF9UUdOk17GCkfsO+h2WBO5fvLn7qROx w8nzCne+NPJZmZTxi9CuyzgZdEH4MBEr8Pd49thERtX8QWRLzmGAZmDB2QrUUnvs4UPw BMTQILaLsu63slD872TK5McGHi4Yx13rG/Wi8YfqemvEy3Qyb9LIi2C68r6TIagQsIym f3VroECKq6a8zSpcPJmWGeZP+0oYolsoZ6Ri7V06plEbpgXSplDiDaPXJhQ6gQE3aYES jGRgn7734s2pQiltuIBclAfbxYFZvjUwPz3dDVXjGczYFWg9yUYzIeUF1wr1zAkUMpq3 oueQ== X-Gm-Message-State: AEkoousDf1oghdhAYqDzNNgiKjh57SeVAdL/3lEzSwABATDBLwErhdGrDwgEK3FXWv7VJtz0 X-Received: by 10.98.90.133 with SMTP id o127mr67454165pfb.61.1471381878363; Tue, 16 Aug 2016 14:11:18 -0700 (PDT) From: Kees Cook To: "Paul E . McKenney" Cc: Kees Cook , Stephen Boyd , Daniel Micay , Arnd Bergmann , Greg Kroah-Hartman , Josh Triplett , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Peter Zijlstra , Ingo Molnar , Tejun Heo , Michael Ellerman , "Aneesh Kumar K.V" , "Kirill A. Shutemov" , Andrew Morton , Dan Williams , Jan Kara , Josef Bacik , Thomas Gleixner , Andrey Ryabinin , Nikolay Aleksandrov , Dmitry Vyukov , linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Date: Tue, 16 Aug 2016 14:11:01 -0700 Message-Id: <1471381865-25724-2-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1471381865-25724-1-git-send-email-keescook@chromium.org> References: <1471381865-25724-1-git-send-email-keescook@chromium.org> Subject: [kernel-hardening] [PATCH 1/5] list: Split list_add() debug checking into separate function X-Virus-Scanned: ClamAV using ClamSMTP Right now, __list_add() code is repeated either in list.h or in list_debug.c, but only the debug checks are the different part. This extracts the checking into a separate function and consolidates __list_add(). Additionally this __list_add_debug() will stop list manipulations if a corruption is detected, instead of allowing for further corruption that may lead to even worse conditions. This is slight refactoring of the same hardening done in PaX and Grsecurity. Signed-off-by: Kees Cook --- include/linux/list.h | 22 ++++++++++++++++------ lib/list_debug.c | 35 ++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/include/linux/list.h b/include/linux/list.h index 5183138aa932..c38ff652ab59 100644 --- a/include/linux/list.h +++ b/include/linux/list.h @@ -28,27 +28,37 @@ static inline void INIT_LIST_HEAD(struct list_head *list) list->prev = list; } +#ifdef CONFIG_DEBUG_LIST +extern bool __list_add_debug(struct list_head *new, + struct list_head *prev, + struct list_head *next); +#else +static inline bool __list_add_debug(struct list_head *new, + struct list_head *prev, + struct list_head *next) +{ + return true; +} +#endif + /* * Insert a new entry between two known consecutive entries. * * This is only for internal list manipulation where we know * the prev/next entries already! */ -#ifndef CONFIG_DEBUG_LIST static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next) { + if (!__list_add_debug(new, prev, next)) + return; + next->prev = new; new->next = next; new->prev = prev; WRITE_ONCE(prev->next, new); } -#else -extern void __list_add(struct list_head *new, - struct list_head *prev, - struct list_head *next); -#endif /** * list_add - add a new entry diff --git a/lib/list_debug.c b/lib/list_debug.c index 3859bf63561c..5d78982eeb99 100644 --- a/lib/list_debug.c +++ b/lib/list_debug.c @@ -19,27 +19,28 @@ * the prev/next entries already! */ -void __list_add(struct list_head *new, +bool __list_add_debug(struct list_head *new, struct list_head *prev, struct list_head *next) { - WARN(next->prev != prev, - "list_add corruption. next->prev should be " - "prev (%p), but was %p. (next=%p).\n", - prev, next->prev, next); - WARN(prev->next != next, - "list_add corruption. prev->next should be " - "next (%p), but was %p. (prev=%p).\n", - next, prev->next, prev); - WARN(new == prev || new == next, - "list_add double add: new=%p, prev=%p, next=%p.\n", - new, prev, next); - next->prev = new; - new->next = next; - new->prev = prev; - WRITE_ONCE(prev->next, new); + if (unlikely(next->prev != prev)) { + WARN(1, "list_add corruption. next->prev should be prev (%p), but was %p. (next=%p).\n", + prev, next->prev, next); + return false; + } + if (unlikely(prev->next != next)) { + WARN(1, "list_add corruption. prev->next should be next (%p), but was %p. (prev=%p).\n", + next, prev->next, prev); + return false; + } + if (unlikely(new == prev || new == next)) { + WARN(1, "list_add double add: new=%p, prev=%p, next=%p.\n", + new, prev, next); + return false; + } + return true; } -EXPORT_SYMBOL(__list_add); +EXPORT_SYMBOL(__list_add_debug); void __list_del_entry(struct list_head *entry) {