Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp3056704rwe; Mon, 29 Aug 2022 05:14:59 -0700 (PDT) X-Google-Smtp-Source: AA6agR48zpLNC8wdSBhaBBNMg+9gJ3GB9ozxWUGYuJwwKzQzpjC7/lWyM1dwVvid10m87qpkElYk X-Received: by 2002:a17:902:710e:b0:170:8d34:9447 with SMTP id a14-20020a170902710e00b001708d349447mr16252255pll.126.1661775298927; Mon, 29 Aug 2022 05:14:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661775298; cv=none; d=google.com; s=arc-20160816; b=Ixfc8IAlGAchXn/6DKLZhy2OEus7PXZwIDlpV3gHUxy0T+mf2jk6TXdWWaIxYE1YUH Kk/VFXRBT4jEyeAEeEumVl2L5gp4h4T5y+twK81xawhKwKxl0g+t8yXoTaPgl4xjIIpz BCm7ubW9nFSk1aBjFz3slE9TqOUT+v6YcytYWpyuC6J+saDU8IVsG0FlZnjnPPVi5k98 Eu8kctIwxtLD3dztra/I770au9w7Gle8jEq7fAPGxNF0vmvhLpBpF/d/dEUIwCZ6IghX jUzlpMfJL/Mh0Hb9P4kvjbVEOORXH2W235g0DfYSs66/JB8srpbPRi3AuGGfCH6jHvIm y7mw== 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=lASqaE/+azZF5nWl9JmBL6KZtyeCjFsr5GzSbeDOm+M=; b=UvSGbEQF3b3eI28X6CjxDevdQl6m0PYU/8oS52Km0+La4c88fj2CDveOEzawdxw3js mFvGyOwUTfEzqC3fcNN6PyFhvNg+78oxTpN8ShEHISnAuh3qyogBCuXeR9UbkNLzVPeR aXYw9bSub40r514y/ExEegoXjdGuHTTtkrROgAOTvN0VZOauIjQuei6XwTCW2CNmSIaa r75nXwONhDQKY1IbVSPIms30e6n0q1fTRuxNHktdPw0Erdh+EpYd+MyqLrpM0XMsl13A o0qvs3LAYnjDvA/BCML9SYj+CNwCV3yOZ3GRJTz6hVs0GCZ5vEApMOQ6vlNoRu2EUzg1 huag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="PKIGE/3F"; 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 q20-20020a656854000000b00429e7ce9d04si8572849pgt.764.2022.08.29.05.14.46; Mon, 29 Aug 2022 05:14:58 -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="PKIGE/3F"; 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 S232978AbiH2Lib (ORCPT + 99 others); Mon, 29 Aug 2022 07:38:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232743AbiH2LhR (ORCPT ); Mon, 29 Aug 2022 07:37:17 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B20F97F24F; Mon, 29 Aug 2022 04:21:38 -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 ams.source.kernel.org (Postfix) with ESMTPS id 3D290B80F1A; Mon, 29 Aug 2022 11:19:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9D5D8C433C1; Mon, 29 Aug 2022 11:19:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661771958; bh=wwLdWrksQLW5dniCLV2oT7GECpfGk/Axa4/6mAegmok=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PKIGE/3FLLosENx6bTIQHTxxW755fa38FM4bYOAhTjeVmZlOZUk3B34tBw0OMXO3I OK/Vqtt7ii2FnXnGPUA6P+tau5CQSbdQ0zALk0ygCIH1kQ5xJa7nB9ooeCTDuSzIMu ZRHq5SC367vkADi/g/CqPJ/Ejrp0jKQ7hVr1Q9Fc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Brian Foster , Gerald Schaefer , Heiko Carstens , Vasily Gorbik Subject: [PATCH 5.19 120/158] s390: fix double free of GS and RI CBs on fork() failure Date: Mon, 29 Aug 2022 12:59:30 +0200 Message-Id: <20220829105814.140338292@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220829105808.828227973@linuxfoundation.org> References: <20220829105808.828227973@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 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: Brian Foster commit 13cccafe0edcd03bf1c841de8ab8a1c8e34f77d9 upstream. The pointers for guarded storage and runtime instrumentation control blocks are stored in the thread_struct of the associated task. These pointers are initially copied on fork() via arch_dup_task_struct() and then cleared via copy_thread() before fork() returns. If fork() happens to fail after the initial task dup and before copy_thread(), the newly allocated task and associated thread_struct memory are freed via free_task() -> arch_release_task_struct(). This results in a double free of the guarded storage and runtime info structs because the fields in the failed task still refer to memory associated with the source task. This problem can manifest as a BUG_ON() in set_freepointer() (with CONFIG_SLAB_FREELIST_HARDENED enabled) or KASAN splat (if enabled) when running trinity syscall fuzz tests on s390x. To avoid this problem, clear the associated pointer fields in arch_dup_task_struct() immediately after the new task is copied. Note that the RI flag is still cleared in copy_thread() because it resides in thread stack memory and that is where stack info is copied. Signed-off-by: Brian Foster Fixes: 8d9047f8b967c ("s390/runtime instrumentation: simplify task exit handling") Fixes: 7b83c6297d2fc ("s390/guarded storage: simplify task exit handling") Cc: # 4.15 Reviewed-by: Gerald Schaefer Reviewed-by: Heiko Carstens Link: https://lore.kernel.org/r/20220816155407.537372-1-bfoster@redhat.com Signed-off-by: Vasily Gorbik Signed-off-by: Greg Kroah-Hartman --- arch/s390/kernel/process.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) --- a/arch/s390/kernel/process.c +++ b/arch/s390/kernel/process.c @@ -91,6 +91,18 @@ int arch_dup_task_struct(struct task_str memcpy(dst, src, arch_task_struct_size); dst->thread.fpu.regs = dst->thread.fpu.fprs; + + /* + * Don't transfer over the runtime instrumentation or the guarded + * storage control block pointers. These fields are cleared here instead + * of in copy_thread() to avoid premature freeing of associated memory + * on fork() failure. Wait to clear the RI flag because ->stack still + * refers to the source thread. + */ + dst->thread.ri_cb = NULL; + dst->thread.gs_cb = NULL; + dst->thread.gs_bc_cb = NULL; + return 0; } @@ -150,13 +162,11 @@ int copy_thread(struct task_struct *p, c frame->childregs.flags = 0; if (new_stackp) frame->childregs.gprs[15] = new_stackp; - - /* Don't copy runtime instrumentation info */ - p->thread.ri_cb = NULL; + /* + * Clear the runtime instrumentation flag after the above childregs + * copy. The CB pointer was already cleared in arch_dup_task_struct(). + */ frame->childregs.psw.mask &= ~PSW_MASK_RI; - /* Don't copy guarded storage control block */ - p->thread.gs_cb = NULL; - p->thread.gs_bc_cb = NULL; /* Set a new TLS ? */ if (clone_flags & CLONE_SETTLS) {