Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp586222iog; Mon, 13 Jun 2022 08:37:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy6yQKTdfuMKrhHNdSMIma6lOiY5Y5HZpLsrQiYFud7fh9GAK5Qp0k5lnYtqqlvCmWKOWOj X-Received: by 2002:a05:6a00:2390:b0:51c:21e1:782 with SMTP id f16-20020a056a00239000b0051c21e10782mr87037pfc.21.1655134673757; Mon, 13 Jun 2022 08:37:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655134673; cv=none; d=google.com; s=arc-20160816; b=VPj8vY4ifCEOQ3DNOeOv1USfDOZeDF+oGSlAmf48BCyX5tKv/iGkF9OUIBLn1eo5KW HD4iKfttVI8Vhf1KXBRok6wbm7fHobKFHkY2Q3As68WdppxTqYurSdVtoBY3OQ9YTHia Dg0HXCZ1AMK74c8gSNiE1JJ6a+/W8Dajux6km6O5YzlVYtelvJP5zKkPMHUUGYBMXJhM gQ1CoBCCQsLJk4e8tbGS4LPXps3NpO9B1FPhIVNLZbrlqdmZC+sPWSsnEN/q7f7JiXtE zfhiL7URjl+MdrePzhAkMwVsEm1kjgh1QSWjlXcogxXu8w8bspzRfC0xe2U/kHeJiBIO NP6g== 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=lvB2v/k1OIo4WQC9Aav8y3z1IQlsULUk4DkkbHJXmGA=; b=GfjWi31Xca4zCJbWaKYBoNlPHI5mxD2o6bJw1OxjN3wPNofBNC5UTuh+8E+0SCyVZc pTrrWRM09gsuYsSQ5Ju4njgnBQXPk7E31mymsQJtyV7tZ/PTh2rY/9ds/8BoYtfzWGRp cKLF3A9WwYJNhliJlFPYcHEB5vpUnOfhW2RfVk8bfeKFXSJxWb2HRhRbIm7s9289xFUs CjMHPsz3LLH9zaiIoGFOpcKOmHc0kQekFTbuMkhRRUHxc3E6IYMCzzl4cID0rVGdb+N4 psisWyKn95eOTEct+Qk9nnWSBDIHDMuruuRrexNzF9gzFKZbHLKSCUVoXsQQ+jg+xuep o36Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=rTortXwu; 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 r4-20020a63e504000000b003fe26a09fe1si9929414pgh.477.2022.06.13.08.37.41; Mon, 13 Jun 2022 08:37:53 -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=rTortXwu; 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 S1356151AbiFMLt5 (ORCPT + 99 others); Mon, 13 Jun 2022 07:49:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356199AbiFMLn4 (ORCPT ); Mon, 13 Jun 2022 07:43:56 -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 C4CD246B3C; Mon, 13 Jun 2022 03:50:34 -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 79D6A61343; Mon, 13 Jun 2022 10:50:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 862D1C34114; Mon, 13 Jun 2022 10:50:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655117432; bh=9fscbB/2/WC67uRuZ4ZUNRPLnqkEBlREFuhjDFoHd/s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rTortXwutidUatrl05405/BArWE9gmi+fyGvsR4QtrDzzIX8grxJLDFX2BMksCqkA xw2+bxwwwGigUF5XIOooozDc15tLSCcHQX/nP49WNdoZZhgdazyNwmHMCt2ZhbdXUR b5bQf7spWh51dwy8X7YrG1xPUgr+J7Vgzsfjn8Rk= 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 5.4 367/411] lkdtm/usercopy: Expand size of "out of frame" object Date: Mon, 13 Jun 2022 12:10:40 +0200 Message-Id: <20220613094939.690561949@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094928.482772422@linuxfoundation.org> References: <20220613094928.482772422@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 e172719dd86d..4617c63b1026 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