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: 10156471 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 77B9D605BA for ; Thu, 11 Jan 2018 02:04:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 658D9286DB for ; Thu, 11 Jan 2018 02:04:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 59463286DE; Thu, 11 Jan 2018 02:04:48 +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 72554286DC for ; Thu, 11 Jan 2018 02:04:47 +0000 (UTC) Received: (qmail 30142 invoked by uid 550); 11 Jan 2018 02:03:45 -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 30020 invoked from network); 11 Jan 2018 02:03:40 -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=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=jlbHsjkyZ59+tPGwaSr66VUcPGAn6Zst+UifsmTX7M/PsujwiKcPRB0zVSuBSO85O1 PbSpcfxcCL68I+BjbtOZGX/5ajpCnExWi9lC9JSxXmdR2U3rg0efOZC8B49ginmqVA8g UpR/wsSuhQ2RaNb/ZlLjRA9u86v91EkaOKLczFrplA9a69Cb5zwmVie56M1xzu0ChNac UZuuZ2LLO7xFluAQlstN2GSxXGpIljp0Ygt9EchP0CjLiFgnVF9ItpG7f6clP4sqlBWN ZD/+L7Ol8wn3+LCjfn/ZdposlMUsL2NxgznwzEjURi/xZYxQLAhX+lQ5xKW3gS09ftqL Po1w== X-Gm-Message-State: AKGB3mInmztZM4bTAwvCBIo3au9YL9nL+2Cn+UsDHg9PZMU0gt6sP3IM Ppu1Sutc3nQxPRt4cnOldJI6Kw== 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) 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 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> Subject: [kernel-hardening] [PATCH 02/38] usercopy: Enhance and rename report_usercopy() 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)