All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Roger Pau Monné" <roger.pau@citrix.com>
To: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: xen-devel@lists.xenproject.org,
	Ross Lagerwall <ross.lagerwall@citrix.com>
Subject: Re: [PATCH v2 2/6] xen/livepatch: zero pointer to temporary load buffer
Date: Thu, 26 Sep 2024 10:36:23 +0200	[thread overview]
Message-ID: <ZvUdBzMr9iqy8Fct@macbook.local> (raw)
In-Reply-To: <7ce3e68e-8e55-4cb1-8568-b1ce3b207866@citrix.com>

On Thu, Sep 26, 2024 at 09:24:56AM +0100, Andrew Cooper wrote:
> On 26/09/2024 9:22 am, Roger Pau Monné wrote:
> > On Wed, Sep 25, 2024 at 07:28:42PM +0100, Andrew Cooper wrote:
> >> On 25/09/2024 11:00 am, Roger Pau Monné wrote:
> >>> On Wed, Sep 25, 2024 at 10:33:39AM +0100, Andrew Cooper wrote:
> >>>> On 25/09/2024 9:42 am, Roger Pau Monne wrote:
> >>>>> The livepatch_elf_sec data field points to the temporary load buffer, it's the
> >>>>> load_addr field that points to the stable loaded section data.  Zero the data
> >>>>> field once load_addr is set, as it would otherwise become a dangling pointer
> >>>>> once the load buffer is freed.
> >>>>>
> >>>>> No functional change intended.
> >>>>>
> >>>>> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> >>>>> ---
> >>>>> Changes since v1:
> >>>>>  - New in this version.
> >>>>> ---
> >>>>>  xen/common/livepatch.c | 3 +++
> >>>>>  1 file changed, 3 insertions(+)
> >>>>>
> >>>>> diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c
> >>>>> index df41dcce970a..87b3db03e26d 100644
> >>>>> --- a/xen/common/livepatch.c
> >>>>> +++ b/xen/common/livepatch.c
> >>>>> @@ -383,6 +383,9 @@ static int move_payload(struct payload *payload, struct livepatch_elf *elf)
> >>>>>              }
> >>>>>              else
> >>>>>                  memset(elf->sec[i].load_addr, 0, elf->sec[i].sec->sh_size);
> >>>>> +
> >>>>> +            /* Avoid leaking pointers to temporary load buffers. */
> >>>>> +            elf->sec[i].data = NULL;
> >>>>>          }
> >>>>>      }
> >>>>>  
> >>>> Where is the data allocated and freed?
> >>>>
> >>>> I don't see it being freed in this loop, so how is freed subsequently?
> >>> It's allocated and freed by livepatch_upload(), it's the raw_data
> >>> buffer that's allocated in the context of that function.
> >> Well, this is a mess isn't it.
> >>
> >> Ok, so livepatch_upload() makes a temporary buffer to copy everything into.
> >>
> >> In elf_resolve_sections(), we set up sec[i].data pointing into this
> >> temporary buffer.
> >>
> >> And here, we copy the data from the temporary buffer, into the final
> >> destination in the Xen .text/data/rodata region.
> >>
> >> So yes, this does end up being a dangling pointer, and clobbering it is
> >> good.
> >>
> >>
> >> But, seeing the `n = sec->load_addr ?: sec->data` in patch 4, wouldn't
> >> it be better to drop this second pointer and just have move_payload()
> >> update it here?
> >>
> >> I can't see anything good which can come from having two addresses, nor
> >> a reason why we'd need both.
> >>
> >> Then again, if this is too hard to arrange, it probably can be left as
> >> an exercise to a future developer.
> > So this is how it ends up looking,  there's a bit of churn due to
> > having to drop const on some function parameters.
> 
> Looks fine to me.
> 
> I'd be tempted to name the final field 'addr' because for most of its
> life it is the load address.

I've changed to `addr`.  I however feel it's kind of redundant to name
a pointer field `addr`, as by the type itself (being a pointer) it's
clear it's an address.

> For the comment on the field, I'd say "this is first a temporary buffer,
> then later the load address".

Adjusted.

Thanks, Roger.


  reply	other threads:[~2024-09-26  8:36 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-25  8:42 [PATCH v2 0/6] xen/livepatch: improvements to loading Roger Pau Monne
2024-09-25  8:42 ` [PATCH v2 1/6] xen/livepatch: remove useless check for duplicated sections Roger Pau Monne
2024-09-25  8:52   ` Jan Beulich
2024-09-25  9:21     ` Roger Pau Monné
2024-09-25  9:29   ` Andrew Cooper
2024-09-25  8:42 ` [PATCH v2 2/6] xen/livepatch: zero pointer to temporary load buffer Roger Pau Monne
2024-09-25  9:33   ` Andrew Cooper
2024-09-25 10:00     ` Roger Pau Monné
2024-09-25 18:28       ` Andrew Cooper
2024-09-26  7:06         ` Roger Pau Monné
2024-09-26  8:22         ` Roger Pau Monné
2024-09-26  8:24           ` Andrew Cooper
2024-09-26  8:36             ` Roger Pau Monné [this message]
2024-09-25  8:42 ` [PATCH v2 3/6] xen/livepatch: simplify and unify logic in prepare_payload() Roger Pau Monne
2024-09-25  9:37   ` Andrew Cooper
2024-09-25 10:02     ` Roger Pau Monné
2024-09-25  8:42 ` [PATCH v2 4/6] xen/livepatch: do Xen build-id check earlier Roger Pau Monne
2024-09-25 10:21   ` Andrew Cooper
2024-09-25 13:39     ` Roger Pau Monné
2024-09-25  8:42 ` [PATCH v2 5/6] x86/alternatives: do not BUG during apply Roger Pau Monne
2024-09-25  9:01   ` Jan Beulich
2024-09-25  9:28     ` Roger Pau Monné
2024-09-25 10:02       ` Jan Beulich
2024-09-25 10:25         ` Andrew Cooper
2024-09-25 10:53   ` Andrew Cooper
2024-09-25 13:55     ` Roger Pau Monné
2024-09-25  8:42 ` [PATCH v2 6/6] x86/alternative: build time check feature is in range Roger Pau Monne
2024-09-25  8:51   ` Andrew Cooper
2024-09-25  9:24     ` Roger Pau Monné
2024-09-25  9:04   ` Jan Beulich
2024-09-25  9:23     ` Roger Pau Monné

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZvUdBzMr9iqy8Fct@macbook.local \
    --to=roger.pau@citrix.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=ross.lagerwall@citrix.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.