From patchwork Mon Mar 6 19:09:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 9607367 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 7C339601D2 for ; Mon, 6 Mar 2017 19:10:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 850D926BE9 for ; Mon, 6 Mar 2017 19:10:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7873827B2F; Mon, 6 Mar 2017 19:10:53 +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 9373E26BE9 for ; Mon, 6 Mar 2017 19:10:52 +0000 (UTC) Received: (qmail 9589 invoked by uid 550); 6 Mar 2017 19:10:14 -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 9409 invoked from network); 6 Mar 2017 19:10:09 -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=FHLQQNdY1mZtQEswCK2SqQeAI1kkr43aeJfEeeJJnsE=; b=bbu6Vjp+2uzLDPXkjwdwa6xW5y9DLf8aqGoihXCWjNmNAF215dXao2JivFOuWWvgmW 5Vsq+xUS9rKfeRK7bp13Uzuen+GU3QsNWWXzymL73/oP2byLI1O53FGnwbzR6ZCC9mKZ +ZUMbbkmHYLzmBIr8+tl/b45ZIEp0D26gYhus= 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=FHLQQNdY1mZtQEswCK2SqQeAI1kkr43aeJfEeeJJnsE=; b=HtR4QiJHq85Cg4faoKGYmc+9V4d8PN0yS74VWXICapzaPeNuSVkGqr0qzVFUFoihQF IXGzRw6RgKWT1KpddCmKDnelGP1b5DjEL7S8PxnuMB2pRqk1ykU6nXmcuMfKOlKvx/7U 5BvBWhwtrWXzZs2j+IzTkgUxDCNoyTRw0MA9JXcw2Lyjpd5eq6Dpc/iMoTMBWQpRg7tR lI8o6N8j1LyhKeaJQ/i7zMnljQ/H4mLJpRvmrWMB1KvhtQlSCqkVJu1xIx9w8C9DEo5P S4sTtezWPLjJw5RUm5Y35ERd1zCMi6YMBAaFCElqIQAcbiyfq/JCMGrc6ZNY9zpFi1Yo XIaA== X-Gm-Message-State: AMke39k146vqQRQxtOwg19bhNkSOnp/5D4CPAIJtjGsqqdrvgeXR2pNvSYz/4QbzJStpnHBl X-Received: by 10.84.128.74 with SMTP id 68mr29144352pla.111.1488827397261; Mon, 06 Mar 2017 11:09:57 -0800 (PST) From: Kees Cook To: kernel-hardening@lists.openwall.com Cc: Kees Cook , Andrew Morton , Rik van Riel , "Paul E. McKenney" , Jakub Kicinski , Viresh Kumar , Ingo Molnar , Thomas Gleixner , Dmitry Vyukov , Olof Johansson , Peter Zijlstra , Josh Poimboeuf , linux-kernel@vger.kernel.org Date: Mon, 6 Mar 2017 11:09:46 -0800 Message-Id: <1488827386-87193-7-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488827386-87193-1-git-send-email-keescook@chromium.org> References: <1488827386-87193-1-git-send-email-keescook@chromium.org> Subject: [kernel-hardening] [PATCH 6/6] refcount: Check bad states with CHECK_DATA_CORRUPTION X-Virus-Scanned: ClamAV using ClamSMTP This converts from WARN() to CHECK_DATA_CORRUPTION() (so that system builders can choose between WARN and BUG). Additionally moves refcount_t sanity-check conditionals into regular function flow. Now when built with CONFIG_BUG_ON_DATA_CORRUPTION, the LKDTM REFCOUNT_* tests correctly kill offending processes. Signed-off-by: Kees Cook --- lib/refcount.c | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/refcount.c b/lib/refcount.c index 1d33366189d1..54aff1e0582f 100644 --- a/lib/refcount.c +++ b/lib/refcount.c @@ -37,6 +37,13 @@ #include #include +/* + * CHECK_DATA_CORRUPTION() is defined with __must_check, but we have a + * couple places where we want to report a condition that has already + * been checked, so this lets us cheat __must_check. + */ +#define REFCOUNT_CHECK(cond, str) unlikely(CHECK_DATA_CORRUPTION(cond, str)) + bool refcount_add_not_zero(unsigned int i, refcount_t *r) { unsigned int old, new, val = atomic_read(&r->refs); @@ -58,7 +65,8 @@ bool refcount_add_not_zero(unsigned int i, refcount_t *r) val = old; } - WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n"); + REFCOUNT_CHECK(new == UINT_MAX, + "refcount_t: add saturated; leaking memory.\n"); return true; } @@ -66,7 +74,8 @@ EXPORT_SYMBOL_GPL(refcount_add_not_zero); void refcount_add(unsigned int i, refcount_t *r) { - WARN(!refcount_add_not_zero(i, r), "refcount_t: addition on 0; use-after-free.\n"); + REFCOUNT_CHECK(!refcount_add_not_zero(i, r), + "refcount_t: addition on 0; use-after-free.\n"); } EXPORT_SYMBOL_GPL(refcount_add); @@ -97,7 +106,8 @@ bool refcount_inc_not_zero(refcount_t *r) val = old; } - WARN(new == UINT_MAX, "refcount_t: saturated; leaking memory.\n"); + REFCOUNT_CHECK(new == UINT_MAX, + "refcount_t: inc saturated; leaking memory.\n"); return true; } @@ -111,7 +121,8 @@ EXPORT_SYMBOL_GPL(refcount_inc_not_zero); */ void refcount_inc(refcount_t *r) { - WARN(!refcount_inc_not_zero(r), "refcount_t: increment on 0; use-after-free.\n"); + REFCOUNT_CHECK(!refcount_inc_not_zero(r), + "refcount_t: increment on 0; use-after-free.\n"); } EXPORT_SYMBOL_GPL(refcount_inc); @@ -124,10 +135,9 @@ bool refcount_sub_and_test(unsigned int i, refcount_t *r) return false; new = val - i; - if (new > val) { - 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; - } old = atomic_cmpxchg_release(&r->refs, val, new); if (old == val) @@ -164,7 +174,8 @@ EXPORT_SYMBOL_GPL(refcount_dec_and_test); void refcount_dec(refcount_t *r) { - WARN(refcount_dec_and_test(r), "refcount_t: decrement hit 0; leaking memory.\n"); + REFCOUNT_CHECK(refcount_dec_and_test(r), + "refcount_t: decrement hit 0; leaking memory.\n"); } EXPORT_SYMBOL_GPL(refcount_dec); @@ -203,10 +214,9 @@ bool refcount_dec_not_one(refcount_t *r) return false; new = val - 1; - if (new > val) { - 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; - } old = atomic_cmpxchg_release(&r->refs, val, new); if (old == val) @@ -264,4 +274,3 @@ bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock) return true; } EXPORT_SYMBOL_GPL(refcount_dec_and_lock); -