From patchwork Thu Jan 11 02:02:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10156667 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 6A9B460231 for ; Thu, 11 Jan 2018 02:17:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 582852874C for ; Thu, 11 Jan 2018 02:17:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4C1EB2874F; Thu, 11 Jan 2018 02:17:14 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E02EC2874C for ; Thu, 11 Jan 2018 02:17:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754523AbeAKCRJ (ORCPT ); Wed, 10 Jan 2018 21:17:09 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:38221 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753304AbeAKCDY (ORCPT ); Wed, 10 Jan 2018 21:03:24 -0500 Received: by mail-pg0-f65.google.com with SMTP id t67so1606753pgc.5 for ; Wed, 10 Jan 2018 18:03:24 -0800 (PST) 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=QUqMYYbqg05M3nl/eJD1G7EzT3JsUKrxRzNRqWwwJ+g=; b=AGkrIHBd1Uphj6Skc1zUtehAMkEKf9BhCde+sJRS0FGEaEfQ14oPt/GZZigyUJK7CY DvQQxgzjZiyD2Ly8evzUhIbFUvdcn+wmKnYlj/DW9rfq6VwhCFPfRQuWcjmn8XjW5bT/ JGZju1IR/rP7ilXYHK+fEusc2/b23RHUOpuf4= 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=QUqMYYbqg05M3nl/eJD1G7EzT3JsUKrxRzNRqWwwJ+g=; b=kUZGNAfy1yJa5ZN46zbaq0DhmTNNdfOP3TNQASq/dIVlTZtTwzbsisYv7TLLk256HS 53aK6ht/dZAG706eVec1pcTvMQFP1AIqa+L2Iqd+zcijizwgVflWSs2Qu2j+2z0Pc7Dy NFyWHbXJKRRd3OOsCEO76EJ60nlHUPYZpmOBhazr1r6LgQhVjZkFyB8Y0yPmoeWeiITy ZUWMx8lqMAvNuIKNlajAIjNSwSoN7I49dJ14Q/Im/A7rvtGYl7vhyhpHXsXb/Cd0bbBf 4EqHbmknJoHtCrPxLXr9iU9PXfnsX94LueEXPzDpbuL9AUAQ5rbNzNzZ8aGmEDmNhx+/ tv4g== X-Gm-Message-State: AKGB3mI3z6sGNh1apAsN3ATLYT0Ar4S5ZVtBTgdkIbR8EmXHhPNA4iCD rygQqTa3dWsXBXMI07C++ntr6g== X-Google-Smtp-Source: ACJfBot08zZ10iWWCBkqHGp8Z4bt82Qy8IaQH1doSi7pZQ2P+jyk8U06E1v0rFfjjbU9sn2gtoeF8Q== X-Received: by 10.98.93.157 with SMTP id n29mr9452431pfj.195.1515636203877; Wed, 10 Jan 2018 18:03:23 -0800 (PST) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id h81sm42523915pfh.119.2018.01.10.18.03.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Jan 2018 18:03:18 -0800 (PST) From: Kees Cook To: linux-kernel@vger.kernel.org Cc: Kees Cook , Linus Torvalds , David Windsor , Alexander Viro , Andrew Morton , Andy Lutomirski , Christoph Hellwig , Christoph Lameter , "David S. Miller" , Laura Abbott , Mark Rutland , "Martin K. Petersen" , Paolo Bonzini , Christian Borntraeger , Christoffer Dall , Dave Kleikamp , Jan Kara , Luis de Bethencourt , Marc Zyngier , Rik van Riel , Matthew Garrett , linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, netdev@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Subject: [PATCH 02/38] usercopy: Enhance and rename report_usercopy() Date: Wed, 10 Jan 2018 18:02:34 -0800 Message-Id: <1515636190-24061-3-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1515636190-24061-1-git-send-email-keescook@chromium.org> References: <1515636190-24061-1-git-send-email-keescook@chromium.org> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In preparation for refactoring the usercopy checks to pass offset to the hardened usercopy report, this renames report_usercopy() to the more accurate usercopy_abort(), marks it as noreturn because it is, adds a hopefully helpful comment for anyone investigating such reports, makes the function available to the slab allocators, and adds new "detail" and "offset" arguments. Signed-off-by: Kees Cook --- mm/slab.h | 6 ++++++ mm/usercopy.c | 24 +++++++++++++++++++----- tools/objtool/check.c | 1 + 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/mm/slab.h b/mm/slab.h index ad657ffa44e5..7d29e69ac310 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -526,4 +526,10 @@ static inline int cache_random_seq_create(struct kmem_cache *cachep, static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { } #endif /* CONFIG_SLAB_FREELIST_RANDOM */ +#ifdef CONFIG_HARDENED_USERCOPY +void __noreturn usercopy_abort(const char *name, const char *detail, + bool to_user, unsigned long offset, + unsigned long len); +#endif + #endif /* MM_SLAB_H */ diff --git a/mm/usercopy.c b/mm/usercopy.c index 5df1e68d4585..8006baa4caac 100644 --- a/mm/usercopy.c +++ b/mm/usercopy.c @@ -58,11 +58,25 @@ static noinline int check_stack_object(const void *obj, unsigned long len) return GOOD_STACK; } -static void report_usercopy(unsigned long len, bool to_user, const char *type) +/* + * If this function is reached, then CONFIG_HARDENED_USERCOPY has found an + * unexpected state during a copy_from_user() or copy_to_user() call. + * There are several checks being performed on the buffer by the + * __check_object_size() function. Normal stack buffer usage should never + * trip the checks, and kernel text addressing will always trip the check. + * For cache objects, copies must be within the object size. + */ +void __noreturn usercopy_abort(const char *name, const char *detail, + bool to_user, unsigned long offset, + unsigned long len) { - pr_emerg("kernel memory %s attempt detected %s '%s' (%lu bytes)\n", - to_user ? "exposure" : "overwrite", - to_user ? "from" : "to", type ? : "unknown", len); + pr_emerg("Kernel memory %s attempt detected %s %s%s%s%s (offset %lu, size %lu)!\n", + to_user ? "exposure" : "overwrite", + to_user ? "from" : "to", + name ? : "unknown?!", + detail ? " '" : "", detail ? : "", detail ? "'" : "", + offset, len); + /* * For greater effect, it would be nice to do do_group_exit(), * but BUG() actually hooks all the lock-breaking and per-arch @@ -260,6 +274,6 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user) return; report: - report_usercopy(n, to_user, err); + usercopy_abort(err, NULL, to_user, 0, n); } EXPORT_SYMBOL(__check_object_size); diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 9b341584eb1b..ae39444896d4 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -138,6 +138,7 @@ static int __dead_end_function(struct objtool_file *file, struct symbol *func, "__reiserfs_panic", "lbug_with_loc", "fortify_panic", + "usercopy_abort", }; if (func->bind == STB_WEAK)