Skip to contentSkip to conversation

x86/vPIT: check/bound values loaded from state save record

PATCH In review Jan Beulich 3 participants 5 messages May 11, 2023 – Oct 25, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/vPIT: check/bound values loaded from state save record

In particular pit_latch_status() and speaker_ioport_read() perform calculations which assume in-bounds values. Several of the state save record fields can hold wider ranges, though.

Note that ->gate should only be possible to be zero for channel 2; enforce that as well.

Adjust pit_reset()'s writing of ->mode as well, to not unduly affect the value pit_latch_status() may calculate. The chosen mode of 7 is still one which cannot be established by writing the control word.

---
Of course an alternative would be to simply reject state save records with out of bounds values.

xen/arch/x86/emul-i8254.c +29 -1
@@ -47,6 +47,7 @@
 #define RW_STATE_MSB 2
 #define RW_STATE_WORD0 3
 #define RW_STATE_WORD1 4
+#define RW_STATE_NUM 5
 
 static int cf_check handle_pit_io(
     int dir, unsigned int port, unsigned int bytes, uint32_t *val);
@@ -426,6 +427,33 @@ static int cf_check pit_load(struct doma
     }
     
     /*
+     * Convert loaded values to be within valid range, for them to represent
+     * actually reachable state.  Uses of some of the values elsewhere assume
+     * this is the case.
+     */
+    for ( i = 0; i < ARRAY_SIZE(pit->hw.channels); ++i )
+    {
+        struct hvm_hw_pit_channel *ch = &pit->hw.channels[i];
+
+        /* pit_load_count() will convert 0 suitably back to 0x10000. */
+        ch->count &= 0xffff;
+        if ( ch->count_latched >= RW_STATE_NUM )
+            ch->count_latched = 0;
+        if ( ch->read_state >= RW_STATE_NUM )
+            ch->read_state = 0;
+        if ( ch->read_state >= RW_STATE_NUM )
+            ch->write_state = 0;
+        if ( ch->rw_mode > RW_STATE_WORD0 )
+            ch->rw_mode = 0;
+        if ( (ch->mode &= 7) > 5 )
+            ch->mode -= 4;
+        ch->bcd = !!ch->bcd;
+        ch->gate = i != 2 || ch->gate;
+    }
+
+    pit->hw.speaker_data_on = !!pit->hw.speaker_data_on;
+
+    /*
      * Recreate platform timers from hardware state.  There will be some 
      * time jitter here, but the wall-clock will have jumped massively, so 
      * we hope the guest can handle it.
@@ -464,7 +492,7 @@ void pit_reset(struct domain *d)
     for ( i = 0; i < 3; i++ )
     {
         s = &pit->hw.channels[i];
-        s->mode = 0xff; /* the init mode */
+        s->mode = 7; /* the init mode */
         s->gate = (i != 2);
         pit_load_count(pit, i, 0);
     }
#1 lore raw
Quoted 37 lines · Jan Beulich

In particular pit_latch_status() and speaker_ioport_read() perform calculations which assume in-bounds values. Several of the state save record fields can hold wider ranges, though.

Note that ->gate should only be possible to be zero for channel 2; enforce that as well.

Adjust pit_reset()'s writing of ->mode as well, to not unduly affect the value pit_latch_status() may calculate. The chosen mode of 7 is still one which cannot be established by writing the control word.

---
Of course an alternative would be to simply reject state save records with out of bounds values.

xen/arch/x86/emul-i8254.c +7 -0
@@ -47,6 +47,7 @@
 #define RW_STATE_MSB 2
 #define RW_STATE_WORD0 3
 #define RW_STATE_WORD1 4
+#define RW_STATE_NUM 5
 
 static int cf_check handle_pit_io(
     int dir, unsigned int port, unsigned int bytes, uint32_t *val);
@@ -426,6 +427,33 @@ static int cf_check pit_load(struct doma
     }
 
     /*
+     * Convert loaded values to be within valid range, for them to represent
+     * actually reachable state.  Uses of some of the values elsewhere assume
+     * this is the case.
+     */
+    for ( i = 0; i < ARRAY_SIZE(pit->hw.channels); ++i )
+    {

xen/arch/x86/emul-i8254.c static int cf_check pit_load(struct doma · line 445

Quoted 10 lines
xen/arch/x86/emul-i8254.c +10 -0
+        struct hvm_hw_pit_channel *ch = &pit->hw.channels[i];
+
+        /* pit_load_count() will convert 0 suitably back to 0x10000. */
+        ch->count &= 0xffff;
+        if ( ch->count_latched >= RW_STATE_NUM )
+            ch->count_latched = 0;
+        if ( ch->read_state >= RW_STATE_NUM )
+            ch->read_state = 0;
+        if ( ch->read_state >= RW_STATE_NUM )
+            ch->write_state = 0;

Should these both be write_state?

Regards,
Jason

#2 lore raw
Quoted 15 lines · Jason Andryuk
Quoted 14 lines · Jan Beulich
xen/arch/x86/emul-i8254.c +10 -0
@@ -426,6 +427,33 @@ static int cf_check pit_load(struct doma
     }
 
     /*
+     * Convert loaded values to be within valid range, for them to represent
+     * actually reachable state.  Uses of some of the values elsewhere assume
+     * this is the case.
+     */
+    for ( i = 0; i < ARRAY_SIZE(pit->hw.channels); ++i )
+    {
+        struct hvm_hw_pit_channel *ch = &pit->hw.channels[i];
+
+        /* pit_load_count() will convert 0 suitably back to 0x10000. */
+        ch->count &= 0xffff;
Quoted 8 lines

xen/arch/x86/emul-i8254.c static int cf_check pit_load(struct doma · line 445

Quoted 6 lines
xen/arch/x86/emul-i8254.c +6 -0
+        if ( ch->count_latched >= RW_STATE_NUM )
+            ch->count_latched = 0;
+        if ( ch->read_state >= RW_STATE_NUM )
+            ch->read_state = 0;
+        if ( ch->read_state >= RW_STATE_NUM )
+            ch->write_state = 0;

Should these both be write_state?

Definitely. Thanks for noticing!

Jan

#3 lore raw
In reply to Jan Beulich
Quoted 7 lines · Jan Beulich

In particular pit_latch_status() and speaker_ioport_read() perform calculations which assume in-bounds values. Several of the state save record fields can hold wider ranges, though.

Note that ->gate should only be possible to be zero for channel 2; enforce that as well.

Quoted 8 lines

Adjust pit_reset()'s writing of ->mode as well, to not unduly affect the value pit_latch_status() may calculate. The chosen mode of 7 is still one which cannot be established by writing the control word.

---
Of course an alternative would be to simply reject state save records with out of bounds values.

I've been taking a look at how we load other records, and lapic_load_hidden() for example will return error when invalid values are found.

IMO that seems safer, I think there's a risk in the adjustments done below to also lead to not safe combinations of fields.

So we either reject the state and return an error, or we silently reject and leave the PIT in the reset state.

Unless there's a reason we need to handle such bogus state.

Quoted 14 lines
xen/arch/x86/emul-i8254.c +1 -0
@@ -47,6 +47,7 @@
 #define RW_STATE_MSB 2
 #define RW_STATE_WORD0 3
 #define RW_STATE_WORD1 4
+#define RW_STATE_NUM 5
 
 static int cf_check handle_pit_io(
     int dir, unsigned int port, unsigned int bytes, uint32_t *val);
@@ -426,6 +427,33 @@ static int cf_check pit_load(struct doma
     }
 
     /*

xen/arch/x86/emul-i8254.c static int cf_check pit_load(struct doma · line 439

Quoted 10 lines
xen/arch/x86/emul-i8254.c +10 -0
+     * Convert loaded values to be within valid range, for them to represent
+     * actually reachable state.  Uses of some of the values elsewhere assume
+     * this is the case.
+     */
+    for ( i = 0; i < ARRAY_SIZE(pit->hw.channels); ++i )
+    {
+        struct hvm_hw_pit_channel *ch = &pit->hw.channels[i];
+
+        /* pit_load_count() will convert 0 suitably back to 0x10000. */
+        ch->count &= 0xffff;

Might be helpful to have this in a define, as it's also used by pit_get_count().

Thanks, Roger.

#4 lore raw
Quoted 21 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich

In particular pit_latch_status() and speaker_ioport_read() perform calculations which assume in-bounds values. Several of the state save record fields can hold wider ranges, though.

Note that ->gate should only be possible to be zero for channel 2; enforce that as well.

Quoted 8 lines

Adjust pit_reset()'s writing of ->mode as well, to not unduly affect the value pit_latch_status() may calculate. The chosen mode of 7 is still one which cannot be established by writing the control word.

---
Of course an alternative would be to simply reject state save records with out of bounds values.

I've been taking a look at how we load other records, and lapic_load_hidden() for example will return error when invalid values are found.

Quoted 7 lines

IMO that seems safer, I think there's a risk in the adjustments done below to also lead to not safe combinations of fields.

So we either reject the state and return an error, or we silently reject and leave the PIT in the reset state.

Unless there's a reason we need to handle such bogus state.

Well, I've tried to be conservative (similarly in the vPIC equivalent change) as to affecting guests with potentially bogus incoming migration streams. Such guests may have been migrated multiple times, from pretty old Xen, and I don't consider it reasonable to check each and every Xen version as to possibly permitting out-of-range values to be reached and then saved when migrating the guest away. Imo if we were to reject bad input, we'd need to have a way to override that. Requiring people to "fix" the data in the migration stream seems to me like demanding too much in such a situation.

What we could also consider is have the tool stack do the adjustment (perhaps optionally, e.g. driven by a command line option), while unconditionally refusing out of range input in the hypervisor. But of course that's more involved, overall (and could still end in perceived regressions, even if those are then easier to overcome).

Quoted 17 lines
Quoted 4 lines
xen/arch/x86/emul-i8254.c +0 -0
@@ -426,6 +427,33 @@ static int cf_check pit_load(struct doma
     }
 
     /*

xen/arch/x86/emul-i8254.c static int cf_check pit_load(struct doma · line 439

Quoted 10 lines
xen/arch/x86/emul-i8254.c +10 -0
+     * Convert loaded values to be within valid range, for them to represent
+     * actually reachable state.  Uses of some of the values elsewhere assume
+     * this is the case.
+     */
+    for ( i = 0; i < ARRAY_SIZE(pit->hw.channels); ++i )
+    {
+        struct hvm_hw_pit_channel *ch = &pit->hw.channels[i];
+
+        /* pit_load_count() will convert 0 suitably back to 0x10000. */
+        ch->count &= 0xffff;

Might be helpful to have this in a define, as it's also used by pit_get_count().

Hmm, the two uses of 0xffff aren't exactly for the same purpose. Here we're bounding the input range, whereas there we simply need to deal with overflow that unavoidably can happen when the counter has wrapped at least once. Really the comment is here to avoid putting in place the more precise but also more involved

        if ( ch->count > 0x10000 )
            ch->count &= 0xffff;

If we used this, divergence from pit_get_count() would be yet larger.

Jan