Skip to content

Commit bdf0500

Browse files
ChiHuaLkartben
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]>
1 parent 1512ed2 commit bdf0500

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
}
@@ -235,13 +234,13 @@ static uint32_t npcx_itim_evt_elapsed_cyc32(void)
235234
{
236235
uint32_t cnt1 = npcx_itim_get_evt_cyc32();
237236
uint8_t sys_cts = evt_tmr->ITCTS32;
238-
uint16_t cnt2 = npcx_itim_get_evt_cyc32();
237+
uint32_t cnt2 = npcx_itim_get_evt_cyc32();
239238

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

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

262261
LOG_DBG("timeout is %d", ticks);
263262
/* Start a event timer in ticks */
263+
264+
k_spinlock_key_t key = k_spin_lock(&lock);
264265
npcx_itim_start_evt_tmr_by_tick(ticks);
266+
k_spin_unlock(&lock, key);
265267
}
266268

267269
uint32_t sys_clock_elapsed(void)
@@ -273,7 +275,7 @@ uint32_t sys_clock_elapsed(void)
273275

274276
k_spinlock_key_t key = k_spin_lock(&lock);
275277
uint64_t delta_cycle = npcx_itim_get_sys_cyc64() - cyc_sys_announced;
276-
uint32_t delta_ticks = (uint32_t)delta_cycle / SYS_CYCLES_PER_TICK;
278+
uint32_t delta_ticks = (uint32_t)(delta_cycle / SYS_CYCLES_PER_TICK);
277279

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

0 commit comments

Comments
 (0)