From: Alexander Chesnokov <[email protected]>

rte_sched_port_config() computes the port byte clock as

    cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
        / params->rate;

and passes the result to rte_reciprocal_value(). The parameter
check rejects rate == 0 only. For rate above
rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT the division yields 0
and rte_reciprocal_value() divides by zero in turn: with d == 0,
rte_fls_u32(d - 1) returns 32 and the following m /= d faults,
crashing the application with SIGFPE. For rate below
rte_get_tsc_hz() >> (32 - RTE_SCHED_TIME_SHIFT) the quotient
exceeds UINT32_MAX and is silently truncated when stored in the
uint32_t cycles_per_byte, corrupting the byte clock.

The byte clock cannot represent rates outside that range, so
configurations within it behave exactly as before.

Move the computation to a helper shared by the parameter check
and the port configuration, and reject configurations whose byte
clock value is zero or does not fit in 32 bits, so that
rte_sched_port_config() and rte_sched_port_get_memory_footprint()
fail cleanly instead of crashing or mis-scheduling.

Fixes: 03d00293ca76 ("sched: eliminate floating point in calculating byte 
clock")
Cc: [email protected]

Signed-off-by: Alexander Chesnokov <[email protected]>
---

Cc: [email protected]

 lib/sched/rte_sched.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/lib/sched/rte_sched.c b/lib/sched/rte_sched.c
index daa31e8b99..ef88df124e 100644
--- a/lib/sched/rte_sched.c
+++ b/lib/sched/rte_sched.c
@@ -436,10 +436,17 @@ subport_profile_check(struct 
rte_sched_subport_profile_params *params,
        return 0;
 }
 
+static uint64_t
+rte_sched_port_cycles_per_byte(uint64_t rate)
+{
+       return (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT) / rate;
+}
+
 static int
 rte_sched_port_check_params(struct rte_sched_port_params *params)
 {
        uint32_t i;
+       uint64_t cycles_per_byte;
 
        if (params == NULL) {
                SCHED_LOG(ERR,
@@ -461,6 +468,15 @@ rte_sched_port_check_params(struct rte_sched_port_params 
*params)
                return -EINVAL;
        }
 
+       cycles_per_byte = rte_sched_port_cycles_per_byte(params->rate);
+       /* rate: the byte clock value must be non-zero and fit in 32 bits */
+       if (cycles_per_byte == 0 ||
+           cycles_per_byte > UINT32_MAX) {
+               SCHED_LOG(ERR,
+                       "%s: Incorrect value for rate", __func__);
+               return -EINVAL;
+       }
+
        /* mtu */
        if (params->mtu == 0) {
                SCHED_LOG(ERR,
@@ -1006,8 +1022,7 @@ rte_sched_port_config(struct rte_sched_port_params 
*params)
        /* Subport profile table */
        rte_sched_port_config_subport_profile_table(port, params, port->rate);
 
-       cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
-               / params->rate;
+       cycles_per_byte = rte_sched_port_cycles_per_byte(params->rate);
        port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
        port->cycles_per_byte = cycles_per_byte;
 
-- 
2.43.0

Reply via email to