From patchwork Wed Jul 19 19:41:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 122801 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2685435vqt; Wed, 19 Jul 2023 13:19:47 -0700 (PDT) X-Google-Smtp-Source: APBJJlEP3k5a4acmESA3Z8FiMCpb0+rrhPgtJf4PtSftiH4leQB/xUFTBux2Q5c9BjjSI39CMsy2 X-Received: by 2002:a17:902:ecd1:b0:1b8:6cae:4400 with SMTP id a17-20020a170902ecd100b001b86cae4400mr3494475plh.37.1689797987374; Wed, 19 Jul 2023 13:19:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689797987; cv=none; d=google.com; s=arc-20160816; b=KqJPdwRgb8gNfzj8g3DJrMJF94aES42l2zCHEvpHyvWh540T/dUu2FYXk7p49TjYg/ MDNBC1BrizOaeFkKVM48ghB4aFd/ZhC8mj4zswZiIW0G6uh/Y3N+2pLVTyTB4pi5v8pU 8igKdpxqIZq7vdugu4F8TTKiaxe4/9PnAH8+dRBoRntPYKH5YLpp5JT3RU5x5G3HSGhI U0EQ/2bNqMGd3i43EdC4W6v2jxAc3PLpSKb1snypZYzFmTJUCWKUbeti4e7W0O1SiuAy n+l7FGICLS06KIBkc/m7jlOEsDYd5LEumo2F9RS/I6dbiAZ73stfuFwYq3yAUjetbWHT /xRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:subject:cc:to:from:date; bh=TxhwGEmi0DVq2VEH35CQcVlYItPea1jOnoeoemjif5Y=; fh=553OgrKXpjGw02ZYS/BmVvzG0IiJjcKMOrFGimh/phQ=; b=g6YkaslL+7uW1Xe+W9HxVk5XXr2sbqYYixpUK57y9W/7vRFw2vX3I44Ew24zu+r/W0 LVSpJ6KR+1kHehcuI8C/TEtcY0aUib7lPegkKCTBF5kFB9n7rMJ1dtlBs5KdRNYjkQSM iMiqvn2oIPw1hD2FyTsNjGENvunLIiMxCVWohYJaEqoilPU0NLJkrBy8qLK3GKWLgEYz 3ZGCVgFIcwwcb10+XgxRaXmftgNOBa2CA/6FstYVMO7lXxJVhysSs28SShB5SaHCgAuU yYgBVkdQEJ0RfHb6iBeqgxClVSWBN8RghZgeyOQ1gajl1Zp6bBVQEGFVBotrZxEXDaGu +GSA== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f3-20020a170902ce8300b001bb3499d8besi4156776plg.478.2023.07.19.13.19.33; Wed, 19 Jul 2023 13:19:47 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231295AbjGSTws (ORCPT + 99 others); Wed, 19 Jul 2023 15:52:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229853AbjGSTwq (ORCPT ); Wed, 19 Jul 2023 15:52:46 -0400 X-Greylist: delayed 654 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Wed, 19 Jul 2023 12:52:44 PDT Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDD3CA7 for ; Wed, 19 Jul 2023 12:52:44 -0700 (PDT) Received: from [2601:18c:8180:ac39:6e0b:84ff:fee2:98bb] (helo=imladris.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1qMD3N-0007gS-36; Wed, 19 Jul 2023 15:41:41 -0400 Date: Wed, 19 Jul 2023 15:41:37 -0400 From: Rik van Riel To: Mike Rapoport Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH] mm,memblock: reset memblock.reserved to system init state to prevent UAF Message-ID: <20230719154137.732d8525@imladris.surriel.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Sender: riel@surriel.com X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771881614510857682 X-GMAIL-MSGID: 1771881614510857682 The memblock_discard function frees the memblock.reserved.regions array, which is good. However, if a subsequent memblock_free (or memblock_phys_free) comes in later, from for example ima_free_kexec_buffer, that will result in a use after free bug in memblock_isolate_range. When running a kernel with CONFIG_KASAN enabled, this will cause a kernel panic very early in boot. Without CONFIG_KASAN, there is a chance that memblock_isolate_range might scribble on memory that is now in use by somebody else. Avoid those issues by making sure that memblock_discard points memblock.reserved.regions back at the static buffer. If memblock_discard is called while there is still memory in the memblock.reserved type, that will print a warning in memblock_remove_region. Signed-off-by: Rik van Riel --- mm/memblock.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mm/memblock.c b/mm/memblock.c index 3feafea06ab2..068289a46903 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -374,6 +374,10 @@ void __init memblock_discard(void) kfree(memblock.reserved.regions); else memblock_free_late(addr, size); + /* Reset to prevent UAF from stray frees. */ + memblock.reserved.regions = memblock_reserved_init_regions; + memblock.reserved.cnt = 1; + memblock_remove_region(&memblock.reserved, 0); } if (memblock.memory.regions != memblock_memory_init_regions) {