From patchwork Tue Aug 28 20:14:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 10579187 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C3067920 for ; Tue, 28 Aug 2018 21:09:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BA78928DC3 for ; Tue, 28 Aug 2018 21:09:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ADE882A9F1; Tue, 28 Aug 2018 21:09:04 +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=-12.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, USER_IN_DEF_DKIM_WL 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 C700128DC3 for ; Tue, 28 Aug 2018 21:09:03 +0000 (UTC) Received: (qmail 28376 invoked by uid 550); 28 Aug 2018 21:08: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 Delivered-To: moderator for kernel-hardening@lists.openwall.com Received: (qmail 1839 invoked from network); 28 Aug 2018 20:15:13 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=orkRpEOZI7lmcFqEOmIoRKnoNlpBSRaZPpEarFrxgn4=; b=dJtCuA9+jn3JAd7mRLBblEhQcu5ltG2x2ErJEb4AoFY6cAWBnnE9WrZUPagGV6lPS/ Isk7kxKGsTe7qKU5K8pvQBCTBKDgZ5XoD3YU07b3hcWTCY5X5sEHOgV55KbG9jvGeTR8 eNHoOcCNYhBrWY/AzOpz4S2FKOl5BFe2si0NqeWAuKLgg2t1/uK6xcvm6cfM6qBRP/1f B4mD/dy3Wxaa+EQA1v7Aan4ChgCArgOPGqmYLQWLUGrAkmZzY5klGbh6hqRbqutOWXQC ASj6uLgwRB23No3sHnOnXZRBTtHtxRjU4kG0N149tX+0ldcEBocZhrYIDAaMne6Nup0M 8phw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=orkRpEOZI7lmcFqEOmIoRKnoNlpBSRaZPpEarFrxgn4=; b=Mm2Y2QgcjhBqp2tXblYHMrSHGr8/CtWGQlwYDEx303YSEpPQ7NCteYAkYkkrDezZBq AvIXkOSfknzSXatVvqUXmZk5FyEsdaBFo1h4LwnpTS1vIEuwnIksG4p55A6xd4jsaznZ C3T1ylnrFcS5RBw7RBYDIYD75aV2kXcCSdHMmM3uzcsjzyEgqvQGzX19LMPMH0ooc0OB dCLjsF2t0C0aTJl2jVxw9ex99RtMF3D4FX84iyE2MA+C9DyWH8mpI8KqJ/x8jjGrmG38 qQ7350moICn0ahRLOxvnC5N9VjwtuLz+sabCLwEJcJsvU9CZh5Q+2HXV0LM8Ej00bHwD FpBA== X-Gm-Message-State: APzg51ATm/ZArIva9R2Db3vharsHYtiE19evcanvgk4PtqE9TRQSAXb8 oP0CcBrRzvmIuMJ2XIEazuuKH0vElg== X-Google-Smtp-Source: ANB0VdZBbCL7UFF1k+aW0ff9wf579S1iabnTvek8YhkrSkZNOC1Y6bJfoziFPHprHIkWIEL+QMDgVZmdfg== X-Received: by 2002:a0c:c3cd:: with SMTP id p13-v6mr1692849qvi.22.1535487301486; Tue, 28 Aug 2018 13:15:01 -0700 (PDT) Date: Tue, 28 Aug 2018 22:14:21 +0200 In-Reply-To: <20180828201421.157735-1-jannh@google.com> Message-Id: <20180828201421.157735-8-jannh@google.com> Mime-Version: 1.0 References: <20180828201421.157735-1-jannh@google.com> X-Mailer: git-send-email 2.19.0.rc0.228.g281dcd1b4d0-goog Subject: [PATCH v3 7/7] lkdtm: test copy_to_user() on bad kernel pointer under KERNEL_DS From: Jann Horn To: Kees Cook , Thomas Gleixner , Ingo Molnar , x86@kernel.org, Andy Lutomirski , kernel-hardening@lists.openwall.com, jannh@google.com Cc: linux-kernel@vger.kernel.org, dvyukov@google.com, Masami Hiramatsu , "Naveen N. Rao" , Anil S Keshavamurthy , "David S. Miller" , Alexander Viro , linux-fsdevel@vger.kernel.org, Borislav Petkov X-Virus-Scanned: ClamAV using ClamSMTP Test whether the kernel WARN()s when, under KERNEL_DS, a bad kernel pointer is used as "userspace" pointer. Should normally be used in "DIRECT" mode. Acked-by: Kees Cook Signed-off-by: Jann Horn --- drivers/misc/lkdtm/core.c | 1 + drivers/misc/lkdtm/lkdtm.h | 1 + drivers/misc/lkdtm/usercopy.c | 13 +++++++++++++ 3 files changed, 15 insertions(+) diff --git a/drivers/misc/lkdtm/core.c b/drivers/misc/lkdtm/core.c index 2154d1bfd18b..5a755590d3dc 100644 --- a/drivers/misc/lkdtm/core.c +++ b/drivers/misc/lkdtm/core.c @@ -183,6 +183,7 @@ static const struct crashtype crashtypes[] = { CRASHTYPE(USERCOPY_STACK_FRAME_FROM), CRASHTYPE(USERCOPY_STACK_BEYOND), CRASHTYPE(USERCOPY_KERNEL), + CRASHTYPE(USERCOPY_KERNEL_DS), }; diff --git a/drivers/misc/lkdtm/lkdtm.h b/drivers/misc/lkdtm/lkdtm.h index 9e513dcfd809..07db641d71d0 100644 --- a/drivers/misc/lkdtm/lkdtm.h +++ b/drivers/misc/lkdtm/lkdtm.h @@ -82,5 +82,6 @@ void lkdtm_USERCOPY_STACK_FRAME_TO(void); void lkdtm_USERCOPY_STACK_FRAME_FROM(void); void lkdtm_USERCOPY_STACK_BEYOND(void); void lkdtm_USERCOPY_KERNEL(void); +void lkdtm_USERCOPY_KERNEL_DS(void); #endif diff --git a/drivers/misc/lkdtm/usercopy.c b/drivers/misc/lkdtm/usercopy.c index 9725aed305bb..389475b25bb7 100644 --- a/drivers/misc/lkdtm/usercopy.c +++ b/drivers/misc/lkdtm/usercopy.c @@ -322,6 +322,19 @@ void lkdtm_USERCOPY_KERNEL(void) vm_munmap(user_addr, PAGE_SIZE); } +void lkdtm_USERCOPY_KERNEL_DS(void) +{ + char __user *user_ptr = (char __user *)ERR_PTR(-EINVAL); + mm_segment_t old_fs = get_fs(); + char buf[10] = {0}; + + pr_info("attempting copy_to_user on unmapped kernel address\n"); + set_fs(KERNEL_DS); + if (copy_to_user(user_ptr, buf, sizeof(buf))) + pr_info("copy_to_user un unmapped kernel address failed\n"); + set_fs(old_fs); +} + void __init lkdtm_usercopy_init(void) { /* Prepare cache that lacks SLAB_USERCOPY flag. */