Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp440426iog; Mon, 13 Jun 2022 06:04:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxR87qh0viK2e4Tq7gOx37rQwhUxvqFo8h27MbVN1tn6q56dFXNT1pXg0LT20zVzJ7BuDit X-Received: by 2002:a17:907:1ca8:b0:706:9ee2:dbc with SMTP id nb40-20020a1709071ca800b007069ee20dbcmr53422676ejc.398.1655125451102; Mon, 13 Jun 2022 06:04:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655125451; cv=none; d=google.com; s=arc-20160816; b=l+N+lB89TzkVJ7MPR/P9CCf9fblqPnDqXBoOYFmQW4AGVnVbBjuGiR2yFrbQXADBvL +sysrRNmUP6Aq3aGTza0hep70kkRyF1Nr60ARqP5PXIH6g0NhyUoJV3py+M0EHiKPiF7 bQwafOXmUWo90s8SZe5fvYbUG3lUQB5xs6WSq+iLv9cCbZ/dipfvM/CLAcO84GWfBoxM Uzv9cgQbyiQGEh4S2BekjiXaouG+cr+wyIyj7Ger5DpE++6XBl8vElvHV+sb/voo6tHj vK3Nz+Aem3/G7KpZTz4c/oOJ2Fl6floHN3iQffYd6D9NT46ynlScdVVyMbDHpXqfJ2c/ iJmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=nPgCUsxJxNIrQgQC2rf4iB+qsEQgH2ENZLOSGIkBNrA=; b=vo/XS2eKOX8lGSk/3R7c76JgPFaLGIWJcUVcXVBzx7GiIwBvXq9Ahe/pS8VVPNUcjp oYNKV+0NjArkDCLJzlNp3Hc/RwLqOROk89mPuSWMHgL2L8uY1dC7e6yss9cvFotPNiE2 EItIUDquzyRG7SBqSA7ashYU8pP2plR2VNTNXdeDcf5jyBHLq46oWDk9HsN+1K6MV0As 2zVOUY1/Yv1r2fxhxf9PDsHQ2pZsL4vlTabgp89BBC8AM8NXv3tfWbfzl/ptJSAkzZjA GTDj6cUbaC7spg4gLkRep14uRNJ+obPwZkBRyqG9kXj4AGoNhCQ5DoDoXeDBAdE0xVaR 4mXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=prSb9HJ2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y16-20020a170906519000b006fedcc0e599si7257912ejk.118.2022.06.13.06.03.45; Mon, 13 Jun 2022 06:04:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=prSb9HJ2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351510AbiFMLIC (ORCPT + 99 others); Mon, 13 Jun 2022 07:08:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349881AbiFMLAx (ORCPT ); Mon, 13 Jun 2022 07:00:53 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAA783120E; Mon, 13 Jun 2022 03:33:10 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1D6D560FDD; Mon, 13 Jun 2022 10:33:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 28C03C34114; Mon, 13 Jun 2022 10:33:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655116389; bh=LtWRdp+pRimpsjuHTcv0lC8/M5fAbpiffwBSYWWitDM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=prSb9HJ2t2oW3bDtBSHXwHM7zHyGXBoANJRToRqYBEBachDy/92aH9wPFGnNJoH3K /qpTUEn+ejODUQAiwMsHfT8bwt54hohFBg3nKcQ2Q4jSv2mj68/pnmKjiL8Hyml0lS qlxYU7Mk37WSgtPnL3VF80Y++4km1BCIZi6Cqufg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Muhammad Usama Anjum , Arnd Bergmann , Kees Cook , Sasha Levin Subject: [PATCH 4.14 182/218] lkdtm/usercopy: Expand size of "out of frame" object Date: Mon, 13 Jun 2022 12:10:40 +0200 Message-Id: <20220613094926.130121619@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094908.257446132@linuxfoundation.org> References: <20220613094908.257446132@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kees Cook [ Upstream commit f387e86d3a74407bdd9c5815820ac9d060962840 ] To be sufficiently out of range for the usercopy test to see the lifetime mismatch, expand the size of the "bad" buffer, which will let it be beyond current_stack_pointer regardless of stack growth direction. Paired with the recent addition of stack depth checking under CONFIG_HARDENED_USERCOPY=y, this will correctly start tripping again. Reported-by: Muhammad Usama Anjum Cc: Arnd Bergmann Cc: Greg Kroah-Hartman Reviewed-by: Muhammad Usama Anjum Link: https://lore.kernel.org/lkml/762faf1b-0443-5ddf-4430-44a20cf2ec4d@collabora.com/ Signed-off-by: Kees Cook Signed-off-by: Sasha Levin --- drivers/misc/lkdtm_usercopy.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/drivers/misc/lkdtm_usercopy.c b/drivers/misc/lkdtm_usercopy.c index a64372cc148d..178b55141772 100644 --- a/drivers/misc/lkdtm_usercopy.c +++ b/drivers/misc/lkdtm_usercopy.c @@ -30,12 +30,12 @@ static const unsigned char test_text[] = "This is a test.\n"; */ static noinline unsigned char *trick_compiler(unsigned char *stack) { - return stack + 0; + return stack + unconst; } static noinline unsigned char *do_usercopy_stack_callee(int value) { - unsigned char buf[32]; + unsigned char buf[128]; int i; /* Exercise stack to avoid everything living in registers. */ @@ -43,7 +43,12 @@ static noinline unsigned char *do_usercopy_stack_callee(int value) buf[i] = value & 0xff; } - return trick_compiler(buf); + /* + * Put the target buffer in the middle of stack allocation + * so that we don't step on future stack users regardless + * of stack growth direction. + */ + return trick_compiler(&buf[(128/2)-32]); } static noinline void do_usercopy_stack(bool to_user, bool bad_frame) @@ -66,6 +71,12 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame) bad_stack -= sizeof(unsigned long); } +#ifdef ARCH_HAS_CURRENT_STACK_POINTER + pr_info("stack : %px\n", (void *)current_stack_pointer); +#endif + pr_info("good_stack: %px-%px\n", good_stack, good_stack + sizeof(good_stack)); + pr_info("bad_stack : %px-%px\n", bad_stack, bad_stack + sizeof(good_stack)); + user_addr = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, 0); -- 2.35.1