diff options
author | Diana Z <dzigterman@chromium.org> | 2020-10-09 16:29:59 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-10-14 00:51:26 +0000 |
commit | 5e39e6aaefc4e20e0874af2b2f3c1cd3fc28ac13 (patch) | |
tree | c665683e2f1f026e08729c79469505ca643fa42f /common | |
parent | bf8bda5901a634fa1afeda83344a5b1cfdd30d3d (diff) | |
download | chrome-ec-5e39e6aaefc4e20e0874af2b2f3c1cd3fc28ac13.tar.gz |
TCPMv2: Return valid enum value before TC init
Protect ourselves against using junk TC states by defaulting to
returning our state as the state count before our context is set.
Otherwise, on systems where the EC sysjumps during flashing we may
allow PD host commands to attempt indexing the names array with a
bad value.
This also removes the build-time check that every state name has a
corresponding entry in the states array.
BRANCH=None
BUG=b:170326170
TEST=on waddledoo, detach sub-board to ensure C1 init takes a long time
and run "ectool usbpd 1" while sysjumping the EC. Verify the EC doesn't
crash and instead reports the state as an empty string
Signed-off-by: Diana Z <dzigterman@chromium.org>
Change-Id: I478613edd165c2de650fe80813ec05a9a03ba087
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2463752
Reviewed-by: Denis Brockus <dbrockus@chromium.org>
Reviewed-by: Jett Rink <jettrink@chromium.org>
Diffstat (limited to 'common')
-rw-r--r-- | common/usbc/usb_tc_drp_acc_trysrc_sm.c | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/common/usbc/usb_tc_drp_acc_trysrc_sm.c b/common/usbc/usb_tc_drp_acc_trysrc_sm.c index 270c7f5f51..bbfee498e1 100644 --- a/common/usbc/usb_tc_drp_acc_trysrc_sm.c +++ b/common/usbc/usb_tc_drp_acc_trysrc_sm.c @@ -187,6 +187,8 @@ enum usb_tc_state { TC_LOW_POWER_MODE, TC_CT_UNATTACHED_SNK, TC_CT_ATTACHED_SNK, + + TC_STATE_COUNT, }; /* Forward declare the full list of states. This is indexed by usb_tc_state */ static const struct usb_state tc_states[]; @@ -266,6 +268,8 @@ static const char * const tc_state_names[] = { [TC_CC_OPEN] = "SS:CC_OPEN", [TC_CC_RD] = "SS:CC_RD", [TC_CC_RP] = "SS:CC_RP", + + [TC_STATE_COUNT] = "", }; #else /* @@ -1449,7 +1453,11 @@ static void set_state_tc(const int port, const enum usb_tc_state new_state) /* Get the current TypeC state. */ test_export_static enum usb_tc_state get_state_tc(const int port) { - return tc[port].ctx.current - &tc_states[0]; + /* Default to returning TC_STATE_COUNT if no state has been set */ + if (tc[port].ctx.current == NULL) + return TC_STATE_COUNT; + else + return tc[port].ctx.current - &tc_states[0]; } /* Get the previous TypeC state. */ @@ -3651,6 +3659,5 @@ const struct test_sm_data test_tc_sm_data[] = { .names_size = ARRAY_SIZE(tc_state_names), }, }; -BUILD_ASSERT(ARRAY_SIZE(tc_states) == ARRAY_SIZE(tc_state_names)); const int test_tc_sm_data_size = ARRAY_SIZE(test_tc_sm_data); #endif |