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: 10579053 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 B493714E1 for ; Tue, 28 Aug 2018 20:15:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A64932ACD9 for ; Tue, 28 Aug 2018 20:15:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 97A642A9B4; Tue, 28 Aug 2018 20:15:10 +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=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham 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 7F8982A9B4 for ; Tue, 28 Aug 2018 20:15:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727488AbeH2AIS (ORCPT ); Tue, 28 Aug 2018 20:08:18 -0400 Received: from mail-qk0-f202.google.com ([209.85.220.202]:42381 "EHLO mail-qk0-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727453AbeH2AIR (ORCPT ); Tue, 28 Aug 2018 20:08:17 -0400 Received: by mail-qk0-f202.google.com with SMTP id z17-v6so2367896qka.9 for ; Tue, 28 Aug 2018 13:15:02 -0700 (PDT) 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=e0reH90h4dFjlO8f2q5t2nVLEy50jOttoogQOvRyzyiKjFaiWrfvHFvKzsyICICp4d Pl4vyIxlftuM7Ag24+JSFCm+bV9cs7NP8fY6+KY9oqwxLzBf8vWB/XlmQl3bsARw/GGK poZcJ133qz/DpWoKl4mBBOHLEXKX6VCeCW5jSctUXqhGngRmlo2vTJHKnArvUtFogYGp T3dnMJqzr1yzwPZ8D5E0wM/AmlVHEbJsbwDxddRi/RQ02W7dyFcuV/tWMh79FwKtsFcw WobPm1rdhtKfsxrQhP/XpoqV08gIywH+jBLZZFC+sSNK2BVq2auwVMv9Jcj2xd2oIqPJ sz1Q== X-Gm-Message-State: APzg51BwrBtD9c/K8GqPpZoP694Tq5Qrat1Nj4C+aQI2o0Kkn4MxBH0T vp7ewILkVXysINOX9tKwm8bq7yr6gQ== 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 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 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. */