From patchwork Sun Feb 5 12:14:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: kpark3469@gmail.com X-Patchwork-Id: 9556063 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 36222602B1 for ; Sun, 5 Feb 2017 12:14:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2709A269E2 for ; Sun, 5 Feb 2017 12:14:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1BDF527F85; Sun, 5 Feb 2017 12:14:50 +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_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, 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 19573269E2 for ; Sun, 5 Feb 2017 12:14:48 +0000 (UTC) Received: (qmail 31761 invoked by uid 550); 5 Feb 2017 12:14: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 30658 invoked from network); 5 Feb 2017 12:14:44 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Pvcr2ico4GqMzoMYIs0ncvMbeHX4Pmtjq4kJgpQL8Y8=; b=JlV4ZZKn5NOeObuSsGFpq0F5HOi7gvcKm6jN1zAzdoDDxEGJ5H1vJETMUEkBgrcORF lixhVYAencUitPf5ckcinTACGAfEtdVwLdJ632C0aTgiuVU/yAI22DCBhh3q+PogKAa2 CBEDCvgdgrALy7ceVPvAhbKil+bSkoNfzJIe/7PiOVHam3MZYeA2FritfzvXRlCPOk+J /Fh5hjAZ79htKnEiTSsdXubptGK1o8KNvjQ2jBaW3+VyO+uOM7j+4eUPCaluTqGpqFYg SsBQhB7+VYg5bR/wdfo+8mVAzVePWwQK4WX3nQHKa5hTnzD6Bu8Ksv0kmhVxL4wMDa1C X/OA== 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=Pvcr2ico4GqMzoMYIs0ncvMbeHX4Pmtjq4kJgpQL8Y8=; b=cLfbzxpK1U9elElWvR8ODgV6s8nlkeHiMNUf7VRTPOXk7RZP5ejtoCMj7x+NypddIM emDvT2I3PSQ4jYLqoSJl01BwSD2K2HW3IKNXGSNNIor+Kl6txrxp72VSgpD6R3O5ScoW BwVzBDGI8RXyDNlN/WpVRkrngat+VPUrV8muidfg+rUEAJ0nZrtKnr7VqHwVCU1Q+6az SkL/0S+nKDKdArsjgSiPeDzjJn9lx7nfr9+6QsLJ49AidaIt0I4h/E5usnc+Leck9+bH +qs+ilTcMw2OpGwFcHC7PEyHfyt0CUPG5tb3gUuESAyclmQCeRydhOF/nD5kTg3GsoEd bY+A== X-Gm-Message-State: AIkVDXIi7yRxdJXzrnXr4oqjCmQLs5TocokD9TFG87nl5fQHxUj44XRXEHWvOhR0BPQQ2A== X-Received: by 10.25.167.135 with SMTP id q129mr1996227lfe.115.1486296873000; Sun, 05 Feb 2017 04:14:33 -0800 (PST) From: kpark3469@gmail.com To: kernel-hardening@lists.openwall.com Cc: catalin.marinas@arm.com, keescook@chromium.org, will.deacon@arm.com, mark.rutland@arm.com, james.morse@arm.com, panand@redhat.com, keun-o.park@darkmatter.ae Date: Sun, 5 Feb 2017 16:14:10 +0400 Message-Id: <1486296850-16045-3-git-send-email-kpark3469@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1486296850-16045-2-git-send-email-kpark3469@gmail.com> References: <1486296850-16045-1-git-send-email-kpark3469@gmail.com> <1486296850-16045-2-git-send-email-kpark3469@gmail.com> Subject: [kernel-hardening] [PATCH v3 3/3] lkdtm: add tests for dynamic array in local stack X-Virus-Scanned: ClamAV using ClamSMTP From: Sahara This seems an unusual case in kernel. But, when an array is dynamically created, this variable can be placed ahead of current frame pointer. This patch tests this dynamic array case. Signed-off-by: Sahara Reported-by: AKASHI Takahiro Suggested-by: Kees Cook --- drivers/misc/lkdtm_usercopy.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/drivers/misc/lkdtm_usercopy.c b/drivers/misc/lkdtm_usercopy.c index 1dd6114..898a323 100644 --- a/drivers/misc/lkdtm_usercopy.c +++ b/drivers/misc/lkdtm_usercopy.c @@ -7,6 +7,7 @@ #include #include #include +#include #include /* @@ -33,7 +34,14 @@ static noinline unsigned char *trick_compiler(unsigned char *stack) static noinline unsigned char *do_usercopy_stack_callee(int value) { - unsigned char buf[32]; + /* + * buf[128] is big enough to put it in the position ahead of + * check_stack_object()'s frame pointer. Because dynamically allocated + * array can be placed between check_stack_object()'s frame pointer and + * do_usercopy_stack()'s frame pointer, we need an object which exists + * ahead of check_stack_object()'s frame pointer. + */ + unsigned char buf[128]; int i; /* Exercise stack to avoid everything living in registers. */ @@ -49,6 +57,7 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame) unsigned long user_addr; unsigned char good_stack[32]; unsigned char *bad_stack; + unsigned int array_size; int i; /* Exercise stack to avoid everything living in registers. */ @@ -72,7 +81,9 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame) return; } + array_size = get_random_int() & 0x0f; if (to_user) { + unsigned char array[array_size]; pr_info("attempting good copy_to_user of local stack\n"); if (copy_to_user((void __user *)user_addr, good_stack, unconst + sizeof(good_stack))) { @@ -80,6 +91,13 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame) goto free_user; } + pr_info("attempting good copy_to_user of callee stack\n"); + if (copy_to_user((void __user *)user_addr, array, + unconst + sizeof(array))) { + pr_warn("copy_to_user failed unexpectedly?!\n"); + goto free_user; + } + pr_info("attempting bad copy_to_user of distant stack\n"); if (copy_to_user((void __user *)user_addr, bad_stack, unconst + sizeof(good_stack))) { @@ -87,6 +105,7 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame) goto free_user; } } else { + unsigned char array[array_size]; /* * There isn't a safe way to not be protected by usercopy * if we're going to write to another thread's stack. @@ -101,6 +120,13 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame) goto free_user; } + pr_info("attempting good copy_from_user of callee stack\n"); + if (copy_from_user(array, (void __user *)user_addr, + unconst + sizeof(array))) { + pr_warn("copy_from_user failed unexpectedly?!\n"); + goto free_user; + } + pr_info("attempting bad copy_from_user of distant stack\n"); if (copy_from_user(bad_stack, (void __user *)user_addr, unconst + sizeof(good_stack))) {