From: "Roger Pau Monné" <roger.pau@citrix.com>
To: Jan Beulich <jbeulich@suse.com>
Cc: "xen-devel@lists.xenproject.org" <xen-devel@lists.xenproject.org>,
Andrew Cooper <andrew.cooper3@citrix.com>, Wei Liu <wl@xen.org>
Subject: Re: [PATCH v3 2/6] x86/HVM: split restore state checking from state loading
Date: Tue, 5 Dec 2023 16:55:02 +0100 [thread overview]
Message-ID: <ZW9H1uE_6k3d-uWn@macbook> (raw)
In-Reply-To: <2ded19f7-2ba6-4b1c-8752-a73894dcdae0@suse.com>
On Tue, Dec 05, 2023 at 03:59:13PM +0100, Jan Beulich wrote:
> On 05.12.2023 15:29, Roger Pau Monné wrote:
> > On Tue, Dec 05, 2023 at 09:52:31AM +0100, Jan Beulich wrote:
> >> On 04.12.2023 18:27, Roger Pau Monné wrote:
> >>> On Tue, Nov 28, 2023 at 11:34:04AM +0100, Jan Beulich wrote:
> >>>> ..., at least as reasonably feasible without making a check hook
> >>>> mandatory (in particular strict vs relaxed/zero-extend length checking
> >>>> can't be done early this way).
> >>>>
> >>>> Note that only one of the two uses of hvm_load() is accompanied with
> >>>> hvm_check(). The other directly consumes hvm_save() output, which ought
> >>>> to be well-formed. This means that while input data related checks don't
> >>>> need repeating in the "load" function when already done by the "check"
> >>>> one (albeit assertions to this effect may be desirable), domain state
> >>>> related checks (e.g. has_xyz(d)) will be required in both places.
> >>>>
> >>>> Suggested-by: Roger Pau Monné <roger.pau@citrix.com>
> >>>> Signed-off-by: Jan Beulich <jbeulich@suse.com>
> >>>> ---
> >>>> Do we really need all the copying involved in use of _hvm_read_entry()
> >>>> (backing hvm_load_entry()? Zero-extending loads are likely easier to
> >>>> handle that way, but for strict loads all we gain is a reduced risk of
> >>>> unaligned accesses (compared to simply pointing into h->data[]).
> >>>
> >>> See below, but I wonder whether the checks could be performed as part
> >>> of hvm_load() without having to introduce a separate handler and loop
> >>> over the context entries.
> >>
> >> Specifically not. State loading (in the longer run) would better not fail
> >> once started. (Imo it should have been this way from the beginning.) Only
> >> then will the vCPU still be in a predictable state even after a possible
> >> error.
> >
> > Looking at the callers, does such predictable state after failure
> > matter?
> >
> > One caller is an hypercall used by the toolstack at domain create,
> > failing can just lead to the domain being destroyed. The other caller
> > is vm fork, which will also lead to the fork being destroyed if
> > context loading fails.
> >
> > Maybe I'm overlooking something.
>
> You don't (I think), but existing callers necessarily have to behave the
> way you describe. From an abstract perspective, though, failed state
> loading would better allow a retry. And really I thought that when you
> suggested to split checking from loading, you had exactly that in mind.
Not really TBH, because I didn't think that much on a possible
implementation when proposing it.
Maybe a suitable compromise would be to reset the state to the initial
(at domain build) one on failure?
I do dislike the duplicated loops, as it seems like a lot of duplicate
boilerplate code, and I have fears of it going out of sync.
> >>>> Would the hvm_sr_handlers[] better use array_access_nospec()?
> >>>
> >>> Maybe? Given this is a domctl I do wonder whether a domain already
> >>> having access to such interface won't have easier ways to leak data
> >>> from Xen. Maybe for a disaggregated setup.
> >>
> >> Hmm, now we're in the middle - Andrew effectively said "no need to".
> >
> > I'm certainly not an expert on whether array_access_nospec() should be
> > used, so if Andrew says no need, that's likely better advice.
> >
> > Maybe the xsm check used in such desegregated setups would already
> > stop speculation?
>
> There's no XSM check anywhere near, and even if there was I don't see
> how it would stop mis-speculation on those array accesses.
This being a slow path anyway, I don't think the extra
array_access_nospec() would make much of an impact, but again I have
to admit it's unclear to me when those are actually required, so I
might suggest adding them out of precaution.
> >>>> @@ -275,6 +281,78 @@ int hvm_save(struct domain *d, hvm_domai
> >>>> return 0;
> >>>> }
> >>>>
> >>>> +int hvm_check(const struct domain *d, hvm_domain_context_t *h)
> >>>> +{
> >>>> + const struct hvm_save_header *hdr;
> >>>> + int rc;
> >>>> +
> >>>> + if ( d->is_dying )
> >>>> + return -EINVAL;
> >>>> +
> >>>> + /* Get at the save header, which must be first. */
> >>>> + hdr = hvm_get_entry(HEADER, h);
> >>>> + if ( !hdr )
> >>>> + return -ENODATA;
> >>>> +
> >>>> + rc = arch_hvm_check(d, hdr);
> >>>> + if ( rc )
> >>>> + return rc;
> >>>> +
> >>>> + for ( ; ; )
> >>>> + {
> >>>> + const struct hvm_save_descriptor *desc;
> >>>> + hvm_check_handler handler;
> >>>> +
> >>>> + if ( h->size - h->cur < sizeof(*desc) )
> >>>> + {
> >>>> + /* Run out of data */
> >>>> + printk(XENLOG_G_ERR
> >>>> + "HVM restore %pd: save did not end with a null entry\n",
> >>>> + d);
> >>>> + return -ENODATA;
> >>>> + }
> >>>> +
> >>>> + /* Read the typecode of the next entry and check for the end-marker. */
> >>>> + desc = (const void *)&h->data[h->cur];
> >>>> + if ( desc->typecode == HVM_SAVE_CODE(END) )
> >>>> + {
> >>>> + /* Reset cursor for hvm_load(). */
> >>>> + h->cur = 0;
> >>>> + return 0;
> >>>> + }
> >>>> +
> >>>> + /* Find the handler for this entry. */
> >>>> + if ( desc->typecode >= ARRAY_SIZE(hvm_sr_handlers) ||
> >>>> + !hvm_sr_handlers[desc->typecode].name ||
> >>>> + !hvm_sr_handlers[desc->typecode].load )
> >>>> + {
> >>>> + printk(XENLOG_G_ERR "HVM restore %pd: unknown entry typecode %u\n",
> >>>> + d, desc->typecode);
> >>>> + return -EINVAL;
> >>>> + }
> >>>> +
> >>>> + /* Check the entry. */
> >>>> + handler = hvm_sr_handlers[desc->typecode].check;
> >>>> + if ( !handler )
> >>>> + {
> >>>> + if ( desc->length > h->size - h->cur - sizeof(*desc) )
> >>>> + return -ENODATA;
> >>>> + h->cur += sizeof(*desc) + desc->length;
> >>>> + }
> >>>> + else if ( (rc = handler(d, h)) )
> >>>> + {
> >>>> + printk(XENLOG_G_ERR
> >>>> + "HVM restore %pd: failed to check %s:%u rc %d\n",
> >>>> + d, hvm_sr_handlers[desc->typecode].name, desc->instance, rc);
> >>>> + return rc;
> >>>> + }
> >>>> +
> >>>> + process_pending_softirqs();
> >>>
> >>> Looking at this, won't it be better to call the check() hooks inside
> >>> the hvm_load() function instead of duplicating the loop?
> >>>
> >>> I realize that you only perform the checks when the state is loaded
> >>> from a domctl, but still seems quite a lot of code duplication for
> >>> little benefit.
> >>>
> >>> hvm_load() could gain an extra parameter to select whether the input
> >>> must be checked or not, and that would avoid having to iterate twice
> >>> over the context.
> >>
> >> Well, see above.
> >>
> >>>> + }
> >>>> +
> >>>> + /* Not reached */
> >>>
> >>> ASSERT_UNREACHABLE() maybe?
> >>
> >> Hmm, I'd find it kind of odd to have such here. While hvm_load() doesn't
> >> have such either, perhaps that's not a meaningful reference. Adding this
> >> would make me fear introducing a Misra violation (adding dead code).
> >
> > But isn't this the purpose of ASSERT_UNREACHABLE() exactly? IOW:
> > Misra will need an exception for all usage of ASSERT_UNREACHABLE()
> > already.
> >
> > I think ASSERT_UNREACHABLE() is much better than a Not reached
> > comment: conveys the same information to readers of the code and has
> > a run-time consequence on debug builds.
>
> I see a difference between uses on paths were we assert that a certain
> state cannot be reached (if all our logic is right) vs a case like the
> one here where the compiler (or another tool) can actually prove that
> the loop can't be exited the "normal" way.
Can't be exited with the current code, but the purpose of
ASSERT_UNREACHABLE() is also to guarantee that further changes might
not break this condition.
Thanks, Roger.
next prev parent reply other threads:[~2023-12-05 15:55 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-28 10:32 [PATCH v3 0/6] x86/HVM: load state checking Jan Beulich
2023-11-28 10:33 ` [PATCH v3 1/6] x86/HVM: introduce hvm_get_entry() Jan Beulich
2023-11-28 10:34 ` [PATCH v3 2/6] x86/HVM: split restore state checking from state loading Jan Beulich
2023-12-04 17:27 ` Roger Pau Monné
2023-12-05 8:52 ` Jan Beulich
2023-12-05 14:29 ` Roger Pau Monné
2023-12-05 14:59 ` Jan Beulich
2023-12-05 15:55 ` Roger Pau Monné [this message]
2023-12-06 7:27 ` Jan Beulich
2023-12-11 10:46 ` Roger Pau Monné
2023-12-11 11:31 ` Jan Beulich
2023-12-11 12:43 ` Roger Pau Monné
2023-12-11 13:15 ` Jan Beulich
2023-11-28 10:34 ` [PATCH v3 3/6] x86/HVM: adjust save/restore hook registration for optional check handler Jan Beulich
2023-11-28 10:35 ` [PATCH v3 4/6] x86/vPIT: check values loaded from state save record Jan Beulich
2023-12-04 17:46 ` Roger Pau Monné
2023-11-28 10:35 ` [PATCH v3 5/6] x86/vPIC: vpic_elcr_mask() master bit 2 control Jan Beulich
2023-12-05 17:29 ` Roger Pau Monné
2023-12-06 7:22 ` Jan Beulich
2023-11-28 10:36 ` [PATCH v3 6/6] x86/vPIC: check values loaded from state save record Jan Beulich
2023-12-05 17:41 ` 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=ZW9H1uE_6k3d-uWn@macbook \
--to=roger.pau@citrix.com \
--cc=andrew.cooper3@citrix.com \
--cc=jbeulich@suse.com \
--cc=wl@xen.org \
--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.