From patchwork Fri Feb 3 23:26:52 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9555301 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 B3651602B7 for ; Fri, 3 Feb 2017 23:27:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9F77627D29 for ; Fri, 3 Feb 2017 23:27:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 93FEE28425; Fri, 3 Feb 2017 23:27:27 +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 B3A7C27D29 for ; Fri, 3 Feb 2017 23:27:26 +0000 (UTC) Received: (qmail 5131 invoked by uid 550); 3 Feb 2017 23:27:17 -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 4063 invoked from network); 3 Feb 2017 23:27:16 -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=VEpJ1JsfKxfKa2JAQMVVxgkp1cV6yrm8YH/vFMGpQ2A=; b=jOaWIAW2aSoLh+He7zwp68Jzx3Vu6NYFlLo2Wz/vrBiGbK9Xc2eyoHTofqkjx+cqED WjaGO9rz62aqzc+HdK1KcdnuUKmznEIcI9HL/6Zng6cwArV688pERx7CoVpLNTwvHcFq uUsqry3JNWHh+QOS5gus78rgO/T/aIfdimcTo= 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=VEpJ1JsfKxfKa2JAQMVVxgkp1cV6yrm8YH/vFMGpQ2A=; b=KNIT1P40YrW726fFyrrxhAM+1gCsCTZCKJ/20mF6FBTYHCcmQVhikjYpE6ip34XHvT CnwNuLHV+H2PEBKDe71Tjvg4i7xNT7O6lYVG4B4bP/2GnSGcPfo0EeRohn5y9XhmL0Kp +jecPTd+kz8tuZvK7klTOa8SpChp6HPhOo8lG/zhF7n+uPCCy5UYbp2YZOKwP+Pg7lb1 X0w0zRxaBAC+Rb3QsroEIZIgOIAvz3Pdx314qwUUMCIffsdem+avV1S33/LHC1/m4K7l bO/5qG5QZAnzH/OESf2zbAhnulsjNWK5NQvfZNvzK0bzAJElFl2633qVz/UU+Or2p8Zx dSZQ== X-Gm-Message-State: AIkVDXLLXZeXssCyXri8kk6QUs28ZW6vuaBs39l0kIWIYqoBUCyfI3QS5m8ZHsQ15P1n5teF X-Received: by 10.84.210.35 with SMTP id z32mr24824333plh.112.1486164424278; Fri, 03 Feb 2017 15:27:04 -0800 (PST) From: Kees Cook To: Peter Zijlstra Cc: Kees Cook , elena.reshetova@intel.com, gregkh@linuxfoundation.org, arnd@arndb.de, tglx@linutronix.de, mingo@kernel.org, h.peter.anvin@intel.com, will.deacon@arm.com, dwindsor@gmail.com, Hans Liljestrand , dhowells@redhat.com, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Date: Fri, 3 Feb 2017 15:26:52 -0800 Message-Id: <1486164412-7338-5-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1486164412-7338-1-git-send-email-keescook@chromium.org> References: <1486164412-7338-1-git-send-email-keescook@chromium.org> Subject: [kernel-hardening] [PATCH 4/4] refcount: Report failures through CHECK_DATA_CORRUPTION X-Virus-Scanned: ClamAV using ClamSMTP This converts from WARN_ON() to CHECK_DATA_CORRUPTION() in the CONFIG_DEBUG_REFCOUNT case. Additionally moves refcount_t sanity check conditionals into regular function flow. Since CHECK_DATA_CORRUPTION() is marked __much_check, we override few cases where the failure has already been handled but we want to explicitly report it. Signed-off-by: Kees Cook --- include/linux/refcount.h | 35 ++++++++++++++++++++++------------- lib/Kconfig.debug | 2 ++ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/include/linux/refcount.h b/include/linux/refcount.h index 5b89cad62237..ef32910c7dd8 100644 --- a/include/linux/refcount.h +++ b/include/linux/refcount.h @@ -43,10 +43,10 @@ #include #if CONFIG_DEBUG_REFCOUNT -#define REFCOUNT_WARN(cond, str) WARN_ON(cond) +#define REFCOUNT_CHECK(cond, str) CHECK_DATA_CORRUPTION(cond, str) #define __refcount_check __must_check #else -#define REFCOUNT_WARN(cond, str) (void)(cond) +#define REFCOUNT_CHECK(cond, str) (!!(cond)) #define __refcount_check #endif @@ -86,14 +86,18 @@ bool refcount_add_not_zero(unsigned int i, refcount_t *r) break; } - REFCOUNT_WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n"); + val = REFCOUNT_CHECK(new == UINT_MAX, + "refcount_t: add saturated; leaking memory.\n"); return true; } static inline void refcount_add(unsigned int i, refcount_t *r) { - REFCOUNT_WARN(!refcount_add_not_zero(i, r), "refcount_t: addition on 0; use-after-free.\n"); + bool __always_unused b; + + b = REFCOUNT_CHECK(!refcount_add_not_zero(i, r), + "refcount_t: addition on 0; use-after-free.\n"); } /* @@ -121,7 +125,8 @@ bool refcount_inc_not_zero(refcount_t *r) break; } - REFCOUNT_WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n"); + val = REFCOUNT_CHECK(new == UINT_MAX, + "refcount_t: inc saturated; leaking memory.\n"); return true; } @@ -134,7 +139,10 @@ bool refcount_inc_not_zero(refcount_t *r) */ static inline void refcount_inc(refcount_t *r) { - REFCOUNT_WARN(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n"); + bool __always_unused b; + + b = REFCOUNT_CHECK(!refcount_inc_not_zero(r), + "refcount_t: increment on 0; use-after-free.\n"); } /* @@ -155,10 +163,9 @@ bool refcount_sub_and_test(unsigned int i, refcount_t *r) return false; new = val - i; - if (new > val) { - REFCOUNT_WARN(new > val, "refcount_t: underflow; use-after-free.\n"); + if (REFCOUNT_CHECK(new > val, + "refcount_t: sub underflow; use-after-free.\n")) return false; - } if (atomic_try_cmpxchg_release(&r->refs, &val, new)) break; @@ -183,7 +190,10 @@ bool refcount_dec_and_test(refcount_t *r) static inline void refcount_dec(refcount_t *r) { - REFCOUNT_WARN(refcount_dec_and_test(r), "refcount_t: decrement hit 0; leaking memory.\n"); + bool __always_unused b; + + b = REFCOUNT_CHECK(refcount_dec_and_test(r), + "refcount_t: decrement hit 0; leaking memory.\n"); } /* @@ -224,10 +234,9 @@ bool refcount_dec_not_one(refcount_t *r) return false; new = val - 1; - if (new > val) { - REFCOUNT_WARN(new > val, "refcount_t: underflow; use-after-free.\n"); + if (REFCOUNT_CHECK(new > val, + "refcount_t: dec underflow; use-after-free.\n")) return true; - } if (atomic_try_cmpxchg_release(&r->refs, &val, new)) break; diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 20fde8d4523a..01e7aa578456 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -731,6 +731,7 @@ source "lib/Kconfig.kasan" config DEBUG_REFCOUNT bool "Verbose refcount checks" + depends on DEBUG_KERNEL || BUG_ON_DATA_CORRUPTION help Say Y here if you want reference counters (refcount_t and kref) to generate WARNs on dubious usage. Without this refcount_t will still @@ -2011,6 +2012,7 @@ config TEST_STATIC_KEYS config BUG_ON_DATA_CORRUPTION bool "Trigger a BUG when data corruption is detected" select DEBUG_LIST + select DEBUG_REFCOUNT help Select this option if the kernel should BUG when it encounters data corruption in kernel memory structures when they get checked