Skip to content

Commit 6dc2540

Browse files
edumazetkuba-moo
authored andcommitted
net/sched: sch_taprio: fix undefined behavior in ktime_mono_to_any
1) if q->tk_offset == TK_OFFS_MAX, then get_tcp_tstamp() calls ktime_mono_to_any() with out-of-bound value. 2) if q->tk_offset is changed in taprio_parse_clockid(), taprio_get_time() might also call ktime_mono_to_any() with out-of-bound value as sysbot found: UBSAN: array-index-out-of-bounds in kernel/time/timekeeping.c:908:27 index 3 is out of range for type 'ktime_t *[3]' CPU: 1 PID: 25668 Comm: kworker/u4:0 Not tainted 5.15.0-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Workqueue: bat_events batadv_iv_send_outstanding_bat_ogm_packet Call Trace: <TASK> __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106 ubsan_epilogue+0xb/0x5a lib/ubsan.c:151 __ubsan_handle_out_of_bounds.cold+0x62/0x6c lib/ubsan.c:291 ktime_mono_to_any+0x1d4/0x1e0 kernel/time/timekeeping.c:908 get_tcp_tstamp net/sched/sch_taprio.c:322 [inline] get_packet_txtime net/sched/sch_taprio.c:353 [inline] taprio_enqueue_one+0x5b0/0x1460 net/sched/sch_taprio.c:420 taprio_enqueue+0x3b1/0x730 net/sched/sch_taprio.c:485 dev_qdisc_enqueue+0x40/0x300 net/core/dev.c:3785 __dev_xmit_skb net/core/dev.c:3869 [inline] __dev_queue_xmit+0x1f6e/0x3630 net/core/dev.c:4194 batadv_send_skb_packet+0x4a9/0x5f0 net/batman-adv/send.c:108 batadv_iv_ogm_send_to_if net/batman-adv/bat_iv_ogm.c:393 [inline] batadv_iv_ogm_emit net/batman-adv/bat_iv_ogm.c:421 [inline] batadv_iv_send_outstanding_bat_ogm_packet+0x6d7/0x8e0 net/batman-adv/bat_iv_ogm.c:1701 process_one_work+0x9b2/0x1690 kernel/workqueue.c:2298 worker_thread+0x658/0x11f0 kernel/workqueue.c:2445 kthread+0x405/0x4f0 kernel/kthread.c:327 ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:295 Fixes: 7ede7b0 ("taprio: make clock reference conversions easier") Fixes: 5400206 ("taprio: Adjust timestamps for TCP packets") Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Vedang Patel <vedang.patel@intel.com> Reported-by: syzbot <syzkaller@googlegroups.com> Reviewed-by: Vinicius Costa Gomes <vinicius.gomes@intel.com> Link: https://lore.kernel.org/r/20211108180815.1822479-1-eric.dumazet@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 43aa493 commit 6dc2540

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

net/sched/sch_taprio.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,22 @@ static ktime_t sched_base_time(const struct sched_gate_list *sched)
9595
return ns_to_ktime(sched->base_time);
9696
}
9797

98-
static ktime_t taprio_get_time(struct taprio_sched *q)
98+
static ktime_t taprio_mono_to_any(const struct taprio_sched *q, ktime_t mono)
9999
{
100-
ktime_t mono = ktime_get();
100+
/* This pairs with WRITE_ONCE() in taprio_parse_clockid() */
101+
enum tk_offsets tk_offset = READ_ONCE(q->tk_offset);
101102

102-
switch (q->tk_offset) {
103+
switch (tk_offset) {
103104
case TK_OFFS_MAX:
104105
return mono;
105106
default:
106-
return ktime_mono_to_any(mono, q->tk_offset);
107+
return ktime_mono_to_any(mono, tk_offset);
107108
}
109+
}
108110

109-
return KTIME_MAX;
111+
static ktime_t taprio_get_time(const struct taprio_sched *q)
112+
{
113+
return taprio_mono_to_any(q, ktime_get());
110114
}
111115

112116
static void taprio_free_sched_cb(struct rcu_head *head)
@@ -319,7 +323,7 @@ static ktime_t get_tcp_tstamp(struct taprio_sched *q, struct sk_buff *skb)
319323
return 0;
320324
}
321325

322-
return ktime_mono_to_any(skb->skb_mstamp_ns, q->tk_offset);
326+
return taprio_mono_to_any(q, skb->skb_mstamp_ns);
323327
}
324328

325329
/* There are a few scenarios where we will have to modify the txtime from
@@ -1352,6 +1356,7 @@ static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
13521356
}
13531357
} else if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
13541358
int clockid = nla_get_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
1359+
enum tk_offsets tk_offset;
13551360

13561361
/* We only support static clockids and we don't allow
13571362
* for it to be modified after the first init.
@@ -1366,22 +1371,24 @@ static int taprio_parse_clockid(struct Qdisc *sch, struct nlattr **tb,
13661371

13671372
switch (clockid) {
13681373
case CLOCK_REALTIME:
1369-
q->tk_offset = TK_OFFS_REAL;
1374+
tk_offset = TK_OFFS_REAL;
13701375
break;
13711376
case CLOCK_MONOTONIC:
1372-
q->tk_offset = TK_OFFS_MAX;
1377+
tk_offset = TK_OFFS_MAX;
13731378
break;
13741379
case CLOCK_BOOTTIME:
1375-
q->tk_offset = TK_OFFS_BOOT;
1380+
tk_offset = TK_OFFS_BOOT;
13761381
break;
13771382
case CLOCK_TAI:
1378-
q->tk_offset = TK_OFFS_TAI;
1383+
tk_offset = TK_OFFS_TAI;
13791384
break;
13801385
default:
13811386
NL_SET_ERR_MSG(extack, "Invalid 'clockid'");
13821387
err = -EINVAL;
13831388
goto out;
13841389
}
1390+
/* This pairs with READ_ONCE() in taprio_mono_to_any */
1391+
WRITE_ONCE(q->tk_offset, tk_offset);
13851392

13861393
q->clockid = clockid;
13871394
} else {

0 commit comments

Comments
 (0)