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 5/7] x86: detect PIT aliasing on ports other than 0x4[0-3]
Date: Thu, 26 Oct 2023 17:13:49 +0200 [thread overview]
Message-ID: <ZTqCLbFJkAyKCPuc@macbook> (raw)
In-Reply-To: <bbc0d98b-8908-3fdb-ebb9-6cff5caf940c@suse.com>
On Thu, Oct 26, 2023 at 05:10:41PM +0200, Jan Beulich wrote:
> On 26.10.2023 15:57, Roger Pau Monné wrote:
> > On Thu, Oct 26, 2023 at 02:31:27PM +0200, Jan Beulich wrote:
> >> On 26.10.2023 12:25, Roger Pau Monné wrote:
> >>> On Thu, May 11, 2023 at 02:07:12PM +0200, Jan Beulich wrote:
> >>>> ... in order to also deny Dom0 access through the alias ports. Without
> >>>> this it is only giving the impression of denying access to PIT. Unlike
> >>>> for CMOS/RTC, do detection pretty early, to avoid disturbing normal
> >>>> operation later on (even if typically we won't use much of the PIT).
> >>>>
> >>>> Like for CMOS/RTC a fundamental assumption of the probing is that reads
> >>>> from the probed alias port won't have side effects (beyond such that PIT
> >>>> reads have anyway) in case it does not alias the PIT's.
> >>>>
> >>>> At to the port 0x61 accesses: Unlike other accesses we do, this masks
> >>>> off the top four bits (in addition to the bottom two ones), following
> >>>> Intel chipset documentation saying that these (read-only) bits should
> >>>> only be written with zero.
> >>>
> >>> As said in previous patches, I think this is likely too much risk for
> >>> little benefit. I understand the desire to uniformly deny access to
> >>> any ports that allow interaction with devices in use by Xen (or not
> >>> allowed to be used by dom0), but there's certainly a risk in
> >>> configuring such devices in the way that we do by finding a register
> >>> that can be read and written to.
> >>>
> >>> I think if anything this alias detection should have a command line
> >>> option in order to disable it.
> >>
> >> Well, we could have command line options (for each of the RTC/CMOS,
> >> PIC, and PIT probing allowing the alias masks to be specified (so we
> >> don't need to probe). A value of 1 would uniformly mean "no probing,
> >> no aliases" (as all three decode the low bit, so aliasing can happen
> >> there). We could further make the default of these variables (yes/no,
> >> no actual mask values of course) controllable by a Kconfig setting.
> >
> > If you want to make this more fine grained, or even allow the user to
> > provide custom masks that's all fine, but there's already
> > dom0_ioports_disable that allows disabling a list of IO port ranges.
> >
> > What I would require is a way to avoid all the probing, so that we
> > could return to the previous behavior.
> >
> >>>> --- a/xen/arch/x86/time.c
> >>>> +++ b/xen/arch/x86/time.c
> >>>> @@ -425,6 +425,69 @@ static struct platform_timesource __init
> >>>> .resume = resume_pit,
> >>>> };
> >>>>
> >>>> +unsigned int __initdata pit_alias_mask;
> >>>> +
> >>>> +static void __init probe_pit_alias(void)
> >>>> +{
> >>>> + unsigned int mask = 0x1c;
> >>>> + uint8_t val = 0;
> >>>> +
> >>>> + /*
> >>>> + * Use channel 2 in mode 0 for probing. In this mode even a non-initial
> >>>> + * count is loaded independent of counting being / becoming enabled. Thus
> >>>> + * we have a 16-bit value fully under our control, to write and then check
> >>>> + * whether we can also read it back unaltered.
> >>>> + */
> >>>> +
> >>>> + /* Turn off speaker output and disable channel 2 counting. */
> >>>> + outb(inb(0x61) & 0x0c, 0x61);
> >>>> +
> >>>> + outb((2 << 6) | (3 << 4) | (0 << 1), PIT_MODE); /* Mode 0, LSB/MSB. */
> >>>> +
> >>>> + do {
> >>>> + uint8_t val2;
> >>>> + unsigned int offs;
> >>>> +
> >>>> + outb(val, PIT_CH2);
> >>>> + outb(val ^ 0xff, PIT_CH2);
> >>>> +
> >>>> + /* Wait for the Null Count bit to clear. */
> >>>> + do {
> >>>> + /* Latch status. */
> >>>> + outb((3 << 6) | (1 << 5) | (1 << 3), PIT_MODE);
> >>>> +
> >>>> + /* Try to make sure we're actually having a PIT here. */
> >>>> + val2 = inb(PIT_CH2);
> >>>> + if ( (val2 & ~(3 << 6)) != ((3 << 4) | (0 << 1)) )
> >>>> + return;
> >>>> + } while ( val2 & (1 << 6) );
> >>>
> >>> We should have some kind of timeout here, just in case...
> >>
> >> Hmm, I indeed did consider the need for a timeout here. With what
> >> we've done up to here we already assume a functioning PIT, verifying
> >> simply as we go. The issue with truly using some form of timeout is
> >> the determination of how long to wait at most.
> >
> > I would likely make it based on iterations, could you get some figures
> > on how many iterations it takes for the bit to be clear?
> >
> > I would think something like 1000 should be enough, but really have no
> > idea.
>
> Except that how long a given number of iterations takes is unknown. 1000
> may be enough today or on the systems we test, but may not be tomorrow
> or on other peoples' systems. Hence why I'm hesitant ...
Hm, but getting stuck in a loop here can't be good either. Let's do
it time wise if you prefer, 1s should be more than enough I would
think.
In any case the worse that could if the timeout is hit is that aliases
are not properly detected, but that's way better than the possibility
of getting stuck in an infinite loop.
Thanks, Roger.
next prev parent reply other threads:[~2023-10-26 15:14 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-11 12:03 [PATCH 0/7] x86: Dom0 I/O port access permissions Jan Beulich
2023-05-11 12:05 ` [PATCH 1/7] x86: don't allow Dom0 access to port CF9 Jan Beulich
2023-10-25 12:36 ` Roger Pau Monné
2023-10-25 13:59 ` Jan Beulich
2023-05-11 12:05 ` [PATCH 2/7] x86: don't allow Dom0 access to port 92 Jan Beulich
2023-10-25 12:49 ` Roger Pau Monné
2023-10-25 14:11 ` Jan Beulich
2023-05-11 12:06 ` [PATCH 3/7] x86/PVH: deny Dom0 access to the ISA DMA controller Jan Beulich
2023-10-25 13:22 ` Roger Pau Monné
2023-05-11 12:06 ` [PATCH 4/7] x86: detect PIC aliasing on ports other than 0x[2A][01] Jan Beulich
2023-10-26 8:34 ` Roger Pau Monné
2023-10-26 9:03 ` Jan Beulich
2023-10-26 13:24 ` Roger Pau Monné
2023-10-26 15:07 ` Jan Beulich
2023-10-26 15:19 ` Roger Pau Monné
2023-10-30 12:24 ` Jan Beulich
2023-10-30 15:14 ` Roger Pau Monné
2023-10-30 15:19 ` Jan Beulich
2023-10-30 15:23 ` Roger Pau Monné
2023-10-30 15:35 ` Jan Beulich
2023-10-30 16:25 ` Roger Pau Monné
2023-05-11 12:07 ` [PATCH 5/7] x86: detect PIT aliasing on ports other than 0x4[0-3] Jan Beulich
2023-10-26 10:25 ` Roger Pau Monné
2023-10-26 12:31 ` Jan Beulich
2023-10-26 13:57 ` Roger Pau Monné
2023-10-26 15:10 ` Jan Beulich
2023-10-26 15:13 ` Roger Pau Monné [this message]
2023-10-30 12:50 ` Jan Beulich
2023-05-11 12:07 ` [PATCH 6/7] x86: don't allow Dom0 (direct) access to port F0 Jan Beulich
2023-10-26 10:48 ` Roger Pau Monné
2023-05-11 12:08 ` [PATCH 7/7] x86: don't allow Dom0 access to ELCR ports Jan Beulich
2023-10-26 11:02 ` Roger Pau Monné
2023-10-26 12:51 ` Jan Beulich
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=ZTqCLbFJkAyKCPuc@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.