From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f194.google.com (mail-pf1-f194.google.com [209.85.210.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21198279DC8 for ; Thu, 4 Jun 2026 01:49:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.194 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780537743; cv=none; b=DkIxx5vO5F68IdjRo/GYC4rF59J4fGZu3NxS5CgJaHF9qsYS04NUpmCosRnqt61CaR4yale39aXX9kEM0TEdOCHk7gIlbiB0DqHe3c7igbl5S2Jtbmcxk0M9ztfbh4cm4/hpsCFqkxJgDZivehx64Uoe7YOA+dx5PYd2M5Un5Xg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780537743; c=relaxed/simple; bh=gamBR4h2rf1Imsoj8RobEGb1Bt2afo71z/VsoMkVocw=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Je5QVlD9ibRonAfuoF019qc21YtbqYDdohv+B9QUUM/fWn0lZkCOerHhKpSgysw5rw0fkMwaiWL5wHz/dlnzaWth+s9qcmOTMEJrwlWGyIFPrlX0wQleL7hbHP1NuPZ3XzFBINCzpHliShCaDCvCdUNLkKDMbQpAgnFHnHkf0yA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=GzKYh+Q6; arc=none smtp.client-ip=209.85.210.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GzKYh+Q6" Received: by mail-pf1-f194.google.com with SMTP id d2e1a72fcca58-842307473b5so113573b3a.2 for ; Wed, 03 Jun 2026 18:49:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780537741; x=1781142541; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=R9vTs/JHnHpc6BmzGsq91wlnVhg5cqc1lx/XeBVEZpQ=; b=GzKYh+Q6LF1yOU5QGHQSc2uflOqpcVZX/+CQme/uY/0e7iAFJ0i9kL8LvVcxKD5K+r C4NY+HlBKuAAoiSGYxTE4/fRxlqdDy+/UQkotTde0m3dA/TbsytmwrwdmM67Hekklmoh IPA3d/SDcTceDD0C5VxlLDm7gNfLc3yffsqm3u8hy/6yFSUsUtBdS/OHfasoyAaPng/r XIC32YBozORzD/n9Ot5euDw+fNUFU8jeXtKwDm23AIIibeis1tndN8ueBzOI/17X8FCY sePvNYZfBkzuFW3HxWrUqHy5/QH3MXq0py61r/pxG4mALYbRy4akoqE531B0tcxTr5ZI /1tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780537741; x=1781142541; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=R9vTs/JHnHpc6BmzGsq91wlnVhg5cqc1lx/XeBVEZpQ=; b=Wm7eGsWCzeCIDaR55X9uHMsr7No6bENW4CNuBniBFbdq3fZ2VmDsgdqkOTCDtZTFJe clJkJ3StkZSEgGRoQMFZm7fDRCXdw0YYkyXH90jGfnQlOi8aF3Q4dwKzrWsH0bjn4DXC /s+SyP6HUpbg707VnJgoS7ZxUzRuzDWY7TSmzAVvELo3Otv3DZmtStv5+j3/2MIlUKzX DxjDtJ/Kw27bea+AzqV1kbOPcXsOkeYscRVnAGnYpMiUmEwJJGam7s88LA4jILfE/A5K uGUwUft7HGu6Tq4itg0RJ7CN2r1/3Jkjmf0NAl7Zj6JFOUZg0vnNMB+LpumSoZh45aoR Ed8A== X-Forwarded-Encrypted: i=1; AFNElJ/w5A/zZ8RkZv7oP+RWoMB2/pdZH4jFolez3ia4bhOQcMACTsFJm46Ut95NdhXc/pwelpFvKoCirhWzJ6k=@vger.kernel.org X-Gm-Message-State: AOJu0YyImSgFIf8ftme0YWi5+EItSvl+GKV9gx9fqu7Ip251vzIJ2o46 oXtHl2KrtVSGlKDkh/KOy/tQfceLfBJ7vo3rAxbfdR+tLfl2OcC69uf1QHkFj5DNS06VIQ== X-Gm-Gg: Acq92OG3mWJq5y+OrQvh580ed9f4LUCd6qVpCbid9/82kSFaxOZLxlTutxHmEYNcOqR z8dMIT3AymXxNkTSTROi/jaFJ5FlzInRg9xSMSMQReM9q1oRyfR0+Z/es+TyPvV8yOVTI/4kr1N DBzQl99pt6nyapbt5bpB1DGGxtHj9y2RD7bS6BSRiz/99IWPR6XmRndeQlsXRZzheGUBBlhXTbp +Z22KKSJLdBNeqdcdiF8vsKXdhKwq07lMpL2lq79u+8npN6f+McsZaVam4vjW/V0GRL62EYbkHH FfDWkT5MjoH5HV/ObVPeMDGGNeHhdGZQLEmNktzcCaHs4OFlKikf7pvBnNOil0VwLv8A5daJ6jc LF7VpO7huwTr0GDr2v6io8VCxlXQCPccHm/1LoWrUBkT28G9FmGYxZbdIgC/t82hvzAAZKfg0fZ rECH5o4XGvmpPzHJoS6NJJyJi2nF6HPIX/WAn782mo1g== X-Received: by 2002:aa7:8882:0:b0:842:38d2:a35f with SMTP id d2e1a72fcca58-84284e8dc75mr6032800b3a.31.1780537741272; Wed, 03 Jun 2026 18:49:01 -0700 (PDT) Received: from [10.125.112.20] ([210.184.73.204]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84282350f8csm4439444b3a.14.2026.06.03.18.48.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Jun 2026 18:49:00 -0700 (PDT) Message-ID: <8e088395-c22d-4bc8-9e58-84235af1e56b@gmail.com> Date: Thu, 4 Jun 2026 09:48:44 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 03/11] of: reserved_mem: avoid post-init UAF when alloc_reserved_mem_array() fails To: Rob Herring Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, kexec@lists.infradead.org, iommu@lists.linux.dev, zhaomeijing@lixiang.com, catalin.marinas@arm.com, will@kernel.org, chenhuacai@kernel.org, kernel@xen0n.name, pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, saravanak@kernel.org, akpm@linux-foundation.org, bhe@redhat.com, rppt@kernel.org, pasha.tatashin@soleen.com, pratyush@kernel.org, ruirui.yang@linux.dev, m.szyprowski@samsung.com, robin.murphy@arm.com, quic_obabatun@quicinc.com References: <20260527032917.3385849-1-chenwandun1@gmail.com> <20260527032917.3385849-4-chenwandun1@gmail.com> <20260602162450.GA442759-robh@kernel.org> <79932afc-2e91-4a54-aff9-f550be784c36@gmail.com> Content-Language: en-US From: Wandun In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 6/4/26 01:44, Rob Herring wrote: > On Wed, Jun 3, 2026 at 1:44 AM Wandun wrote: >> >> >> On 6/3/26 00:24, Rob Herring wrote: >>> On Wed, May 27, 2026 at 11:29:09AM +0800, Wandun Chen wrote: >>>> From: Wandun Chen >>>> >>>> The global pointer 'reserved_mem' continues to reference the >>>> reserved_mem_array which lives in __initdata if >>>> alloc_reserved_mem_array() fails. of_reserved_mem_lookup() is >>>> exported for post-init use, that would dereference freed memory >>>> and trigger a use-after-free. >>>> >>>> So reset reserved_mem_count to 0 when alloc_reserved_mem_array() >>>> fails. >>>> >>>> Fixes: 00c9a452a235 ("of: reserved_mem: Add code to dynamically allocate reserved_mem array") >>> Fixes should come first in a series. >> Understood, will do in future submissions. >>>> Signed-off-by: Wandun Chen >>>> --- >>>> drivers/of/of_reserved_mem.c | 20 ++++++++++++++------ >>>> 1 file changed, 14 insertions(+), 6 deletions(-) >>>> >>>> diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c >>>> index 313cbc57aa45..6d479381ff1f 100644 >>>> --- a/drivers/of/of_reserved_mem.c >>>> +++ b/drivers/of/of_reserved_mem.c >>>> @@ -69,29 +69,31 @@ static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, >>>> * the initial static array is copied over to this new array and >>>> * the new array is used from this point on. >>>> */ >>>> -static void __init alloc_reserved_mem_array(void) >>>> +static bool __init alloc_reserved_mem_array(void) >>>> { >>>> struct reserved_mem *new_array; >>>> size_t alloc_size, copy_size, memset_size; >>>> >>>> + if (!total_reserved_mem_cnt) >>>> + return true; >>>> + >>>> alloc_size = array_size(total_reserved_mem_cnt, sizeof(*new_array)); >>>> if (alloc_size == SIZE_MAX) { >>>> pr_err("Failed to allocate memory for reserved_mem array with err: %d", -EOVERFLOW); >>>> - return; >>>> + goto fail; >>>> } >>>> >>>> new_array = memblock_alloc(alloc_size, SMP_CACHE_BYTES); >>>> if (!new_array) { >>>> pr_err("Failed to allocate memory for reserved_mem array with err: %d", -ENOMEM); >>>> - return; >>>> + goto fail; >>>> } >>>> >>>> copy_size = array_size(reserved_mem_count, sizeof(*new_array)); >>>> if (copy_size == SIZE_MAX) { >>>> memblock_free(new_array, alloc_size); >>>> - total_reserved_mem_cnt = MAX_RESERVED_REGIONS; >>>> pr_err("Failed to allocate memory for reserved_mem array with err: %d", -EOVERFLOW); >>> These prints could be moved to 'fail'. Perhaps instead of just printing >>> an error value, you can return the error value instead of boolean. >> Will do, consolidating pr_err() under 'fail' and changing the return type >> to int. >>> If you respin just this patch, I can pick it up for 7.2. >> Before I respin, I'd like to flag a dependency: >> patch 05/07 in this series build on the signature change introduced by this >> patch ("the void -> bool return type change of alloc_reserved_mem_array()") >> >> Could you let me know which of the following you'd prefer: >> a) Take patch 03 alone via your tree as you suggested, after it lands, I'll >> respin the remaining patches of this series. > I would go with this option. AIUI, this series isn't going to land in > 7.2, so ultimately you will rebase on v7.2-rc1 which will have the > fix. OK, will send v4. Best regards, Wandun > >> b) Keep patch 03 in the v4 respin of the full series, reordered to the front >> per your earlier comment. > > Rob