[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <aO-Q6xMDd8Bfeww2@apocalypse>
Date: Wed, 15 Oct 2025 14:17:47 +0200
From: Andrea della Porta <andrea.porta@...e.com>
To: Rob Herring <robh@...nel.org>
Cc: Andrea della Porta <andrea.porta@...e.com>,
Saravana Kannan <saravanak@...gle.com>, devicetree@...r.kernel.org,
linux-kernel@...r.kernel.org, iivanov@...e.de, svarbanov@...e.de,
mbrugger@...e.com, Phil Elwell <phil@...pberrypi.com>
Subject: Re: [PATCH] of: reserved_mem: Add heuristic to validate reserved
memory regions
Hi Rob,
On 11:25 Tue 14 Oct , Rob Herring wrote:
> On Tue, Oct 14, 2025 at 9:24 AM Andrea della Porta
> <andrea.porta@...e.com> wrote:
> >
> > Hi Rob,
> >
> > On 08:12 Tue 14 Oct , Rob Herring wrote:
> > > On Tue, Oct 14, 2025 at 2:32 AM Andrea della Porta
> > > <andrea.porta@...e.com> wrote:
> > > >
> > > > When parsing static reserved-memory DT nodes, any node with a reg property
> > > > length that is not perfectly conformant is discarded.
> > > > Specifically, any reg property whose length is not a multiple of the parent's
> > > > (#address-cells + #size-cells) is dropped.
> > > >
> > > > Relax this condition (while still treating perfect multiples as having higher
> > > > precedence) by allowing regions that are subsets of the parent's addressable
> > > > space to be considered for inclusion.
> > > > For example, in the following scenario:
> > > >
> > > > / {
> > > > #address-cells = <0x02>;
> > > > #size-cells = <0x02>;
> > > > ...
> > > >
> > > > reserved-memory {
> > > > #address-cells = <0x02>;
> > > > #size-cells = <0x02>;
> > > > ...
> > > >
> > > > nvram {
> > > > reg = <0x00 0x3fd16d00 0x37>;
> > > > ...
> > > > };
> > > > };
> > > > };
> > > >
> > > > Even though the reg property of the nvram node is not well-formed from a DT
> > > > syntax perspective, it still references a perfectly valid memory region of
> > > > 0x37 bytes that should be reserved.
> > >
> > > No it isn't. I could just as easily argue that the reserved size
> > > should be 0x37_00000000 because it's BE data. I have little interest
> > > in supporting incorrect DTs especially generically where we have no
> > > clue what platform needs it and whether we still have to carry the
> > > code. There's enough of that crap with ancient PPC and Sparc systems.
> >
> > I understand the pain, but IIUC the example you mentioned (0x37 0x00) deals
> > with an incorrect size value (due to endianness) over a correct size length
> > (#size-cells = 2), while the case this patch tries to address is the opposite,
> > i.e. correct size values (corrected by the fw) over an incorrect size length.
> > For the former issue, the actual kernel code does not have an answer yet. For
> > the latter I propose this patch.
>
> No, my point was who is to say the error is not 'reg' was treated as
> if #size-cells was 1, but rather 'reg' was truncated by 1 cell by
> mistake. You don't know (in general) which one it is.
Ok, general case can have ambiguity, got it. Since this seems to be a dead end,
I will abandon the heuristic path in favor of fixing the specific (Rpi5) case.
>
> > The point is that the potential erroneous regions we could introduce with this
> > patch are just a subset of the regions that can be erroneously introduced in
> > the actual kernel, so no additional harm could be done.
>
> There's little reason for us to handle such an error as there is
> little excuse for getting it wrong. We have multiple tools that check
> this including the kernel evidently.
>
> > > Furthermore, this looks like an abuse of /reserved-memory which should
> > > *only* be holes in what /memory node(s) define. I don't think we
> > > enforce that and I imagine there is lots of abuse.
> >
> > AFAIK the only enforcement in the kernel is being an integer multiple of the
> > root address + size cells. As you already pointed out, this means easy abuse
> > but this is still a fact with the current kernel, not something that would
> > be exploitable more easily with this patch.
> >
> > >
> > > > This has at least one real-world equivalent on the Raspberry Pi 5, for example,
> > > > on which the firmware incorrectly overwrites the nvram node's reg property
> > > > without taking into account the actual value of the parent's #size-cells.
> > >
> > > If we have to support this broken firmware, the kernel should fixup
> > > the entry to be correct.
> >
> > This is what I first thought of, but it has several issues that complicates
> > its implementation:
> >
> > - I guess there's no current infrastructure to execute fw specific code in
> > the reserved-memory node (something that resembles PCI quirks?)
>
> Not there specifically, but PPC does do a number of fixups.
>
> > - Finding out whether a fix is required depends on identifying the fw, which is
> > possible only reading its fingerprint through the reserved-memory region
> > itself. This is kinda of a recursive problem...
>
> If RPi5, then check and fix 'reg' length in /reserved-memory nodes.
> That doesn't seem hard.
Maybe we can workaround this by just checking if the reserved node has
"raspberrypi,bootloader-config" in the compatible list *and* the reg property
is not aligned with the #size-cells advertised by the parent. In this case we
are surely dealing with the misbehaving fw and we can act accordingly.
Thanks for the heads-up.
>
> > - The reserved memory parsing function is invoked very early in the boot process,
> > so we cannot rely on a driver module to amend that
>
> Does it need to be? If the region truly isn't in DRAM, then we don't
> really need to fix it early.
I think we should. The reseved memory regions are outlined during the first-pass
parsing of the fdt and the nvmem-rmem driver will not work if it wouldn't find
a matching region. I think there's no point in rewriting this logic, let's just
fix the entry sooner rather than later. As an added benefit, whoever will need
to dump the fdt at later time can do so and have the reg property matching its
live tree counterpart. Need to do some testing to verify whether its already doable
or if it needs some new helper functions to deal with the fdt.
>
> > I will try to cook up something on this line, but I guess it will not be easy.
>
> I pushed a branch, dt/fixup-infrastruct, to my kernel.org tree. It's a
> prototype that we ended up not using. It won't work for you if we need
> to fixup the fdt rather than the unflattened tree.
Thanks for that. As said above though, I'd rather fix it early in the fdt rather
than in the live tree, although the actions in your proposed branch have direct
equivalent in the fdt world (or at least they should, I need to verify), so the
trace is still good to follow.
I'll bake something and come back with a renovate V2.
Many thanks,
Andrea
>
> Rob
Powered by blists - more mailing lists