Skip to content

Commit 4286a3c

Browse files
ChiHuaLnashif
authored andcommitted
driver: timer: npcx: fix possible vulnerabilities
This commit fixes some potential leakage in the timer driver. Signed-off-by: Jun Lin <[email protected]> (cherry picked from commit bdf0500)
1 parent 8b7305d commit 4286a3c

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

drivers/timer/npcx_itim_timer.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ static inline void npcx_itim_evt_disable(void)
140140
/* ITIM local functions */
141141
static int npcx_itim_start_evt_tmr_by_tick(int32_t ticks)
142142
{
143-
k_spinlock_key_t key = k_spin_lock(&lock);
144-
145143
/*
146144
* Get desired cycles of event timer from the requested ticks which
147145
* round up to next tick boundary.
@@ -152,7 +150,7 @@ static int npcx_itim_start_evt_tmr_by_tick(int32_t ticks)
152150
} else {
153151
uint64_t next_cycs;
154152
uint64_t curr = npcx_itim_get_sys_cyc64();
155-
uint32_t dcycles;
153+
uint64_t dcycles;
156154

157155
if (ticks <= 0) {
158156
ticks = 1;
@@ -162,11 +160,13 @@ static int npcx_itim_start_evt_tmr_by_tick(int32_t ticks)
162160
if (unlikely(next_cycs <= curr)) {
163161
cyc_evt_timeout = 1;
164162
} else {
163+
uint32_t dticks;
164+
165165
dcycles = next_cycs - curr;
166-
cyc_evt_timeout =
167-
CLAMP((dcycles / SYS_CYC_PER_EVT_CYC), 1, NPCX_ITIM32_MAX_CNT);
166+
dticks = DIV_ROUND_UP(dcycles * EVT_CYCLES_PER_SEC,
167+
sys_clock_hw_cycles_per_sec());
168+
cyc_evt_timeout = CLAMP(dticks, 1, NPCX_ITIM32_MAX_CNT);
168169
}
169-
170170
}
171171
LOG_DBG("ticks %x, cyc_evt_timeout %x", ticks, cyc_evt_timeout);
172172

@@ -178,7 +178,6 @@ static int npcx_itim_start_evt_tmr_by_tick(int32_t ticks)
178178
/* Upload counter of event timer */
179179
evt_tmr->ITCNT32 = MAX(cyc_evt_timeout - 1, 1);
180180

181-
k_spin_unlock(&lock, key);
182181
/* Enable event timer and start ticking */
183182
return npcx_itim_evt_enable();
184183
}
@@ -234,13 +233,13 @@ static uint32_t npcx_itim_evt_elapsed_cyc32(void)
234233
{
235234
uint32_t cnt1 = npcx_itim_get_evt_cyc32();
236235
uint8_t sys_cts = evt_tmr->ITCTS32;
237-
uint16_t cnt2 = npcx_itim_get_evt_cyc32();
236+
uint32_t cnt2 = npcx_itim_get_evt_cyc32();
238237

239238
/* Event has been triggered but timer ISR doesn't handle it */
240239
if (IS_BIT_SET(sys_cts, NPCX_ITCTSXX_TO_STS) || (cnt2 > cnt1)) {
241240
cnt2 = cyc_evt_timeout;
242241
} else {
243-
cnt2 = cyc_evt_timeout - cnt2;
242+
cnt2 = cyc_evt_timeout - cnt2 - 1;
244243
}
245244

246245
/* Return elapsed cycles of 32-bit counter of event timer */
@@ -260,7 +259,10 @@ void sys_clock_set_timeout(int32_t ticks, bool idle)
260259

261260
LOG_DBG("timeout is %d", ticks);
262261
/* Start a event timer in ticks */
262+
263+
k_spinlock_key_t key = k_spin_lock(&lock);
263264
npcx_itim_start_evt_tmr_by_tick(ticks);
265+
k_spin_unlock(&lock, key);
264266
}
265267

266268
uint32_t sys_clock_elapsed(void)
@@ -272,7 +274,7 @@ uint32_t sys_clock_elapsed(void)
272274

273275
k_spinlock_key_t key = k_spin_lock(&lock);
274276
uint64_t delta_cycle = npcx_itim_get_sys_cyc64() - cyc_sys_announced;
275-
uint32_t delta_ticks = (uint32_t)delta_cycle / SYS_CYCLES_PER_TICK;
277+
uint32_t delta_ticks = (uint32_t)(delta_cycle / SYS_CYCLES_PER_TICK);
276278

277279
last_elapsed = delta_ticks;
278280
k_spin_unlock(&lock, key);

0 commit comments

Comments
 (0)