From patchwork Tue Aug 7 01:22:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 10558573 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 AE3701057 for ; Tue, 7 Aug 2018 11:14:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C89520243 for ; Tue, 7 Aug 2018 11:14:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 903412787C; Tue, 7 Aug 2018 11:14:33 +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 B745920243 for ; Tue, 7 Aug 2018 11:14:32 +0000 (UTC) Received: (qmail 11358 invoked by uid 550); 7 Aug 2018 11:14:31 -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 5236 invoked from network); 7 Aug 2018 01:23:26 -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=EeM9pl7nl45D9C+13M38eu9VuNZcRlR9egYZrxuwyYM=; b=u7Y24NcD9NIt0JcxdraGBb4gYa4PIrSBLk5f/pcgg3hc8WdbhAr3zZtAsT6cYl6HXM Q0xtRU+800C2V88IYcQral504EqTMW/jZ6NoY5tnt6uj8MQgiJdOAB3vk4JGsiJV9KRj HJDH1Y7gIbQg/CvEUogC9F2zn5zDm2OMjm5JvAw4mJtTBvgfPy5Dvp5wlfXys1NtH+7w GJjX+MHaa+nclBKhSD0sZZ457WXR7JTDbkwB3XaudINjXLrvrv9j7JobPOLAcalYPpOW eNiaLBQQH1dfBsvl9YZq9MuJiKDxWCGP8AOBxziWnsLhvm6+3zkCOzcxp79TPmB74FC1 +ODQ== 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=EeM9pl7nl45D9C+13M38eu9VuNZcRlR9egYZrxuwyYM=; b=CSOkbk8JIgvFy68QmlGnRm9aeEyAFkgpdypl5fKt8nhRWOtNW7h3ZsSmDNts7m+OT3 1nF0LMm4SAxeNDzTyj1VJRzZ8I4ItJ/DfgGvBrWZfyXmmwK4bTNEJvDasc9Xecdzoj6m pcnJOrEmdJPraxkAwViQA4lpuauUY8LyultWD/Q8IMDO338hPcOXnhLP+mnzjy9j9FRJ 7Ujctv+ucoiPB2sqv7RRJxGUvi6LufpOxo1+sMQ53442gmNG0JPkefu5aaN30udsmmah 8PQBrcOZ70NVP5/rbCx5SkiABxLecGjRanoqS12dj8W0e4M4Mqm465skLnzfup39FY2N vkuQ== X-Gm-Message-State: AOUpUlHeUilOo+O264g1h3jmpA5z0plCxg/Z7qPpCBrhSql/JQTGPn/R B6TAfo7xgemjMhvNW/hgTuNRGc/5lw== X-Google-Smtp-Source: AAOMgpfjq9p0WM8Uf/Iwew7A+yo9m73QyrbmhFOxtwn2BDzJksPRiBDDIHftMNJFhWPoFbVxXBJFZuB9NA== X-Received: by 2002:a1f:a347:: with SMTP id m68-v6mr11036590vke.43.1533604994654; Mon, 06 Aug 2018 18:23:14 -0700 (PDT) Date: Tue, 7 Aug 2018 03:22:57 +0200 In-Reply-To: <20180807012257.20157-1-jannh@google.com> Message-Id: <20180807012257.20157-2-jannh@google.com> Mime-Version: 1.0 References: <20180807012257.20157-1-jannh@google.com> X-Mailer: git-send-email 2.18.0.597.ga71716f1ad-goog Subject: [RFC PATCH 2/2] lkdtm: test copy_to_user() on bad kernel pointer under KERNEL_DS From: Jann Horn To: Kees Cook , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, kernel-hardening@lists.openwall.com, jannh@google.com Cc: linux-kernel@vger.kernel.org, Andy Lutomirski , dvyukov@google.com 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. Test with "DIRECT" mode. 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. */