Skip to content

Commit 744a4cf

Browse files
jpirkodavem330
authored andcommitted
net: sched: fix use after free when tcf_chain_destroy is called multiple times
The goto_chain termination action takes a reference of a chain. In that case, there is an issue when block_put is called tcf_chain_destroy directly. The follo-up call of tcf_chain_put by goto_chain action free works with memory that is already freed. This was caught by kasan: [ 220.337908] BUG: KASAN: use-after-free in tcf_chain_put+0x1b/0x50 [ 220.344103] Read of size 4 at addr ffff88036d1f2cec by task systemd-journal/261 [ 220.353047] CPU: 0 PID: 261 Comm: systemd-journal Not tainted 4.13.0-rc5jiri+ #54 [ 220.360661] Hardware name: Mellanox Technologies Ltd. Mellanox switch/Mellanox x86 mezzanine board, BIOS 4.6.5 08/02/2016 [ 220.371784] Call Trace: [ 220.374290] <IRQ> [ 220.376355] dump_stack+0xd5/0x150 [ 220.391485] print_address_description+0x86/0x410 [ 220.396308] kasan_report+0x181/0x4c0 [ 220.415211] tcf_chain_put+0x1b/0x50 [ 220.418949] free_tcf+0x95/0xc0 So allow tcf_chain_destroy to be called multiple times, free only in case the reference count drops to 0. Fixes: 5bc1701 ("net: sched: introduce multichain support for filters") Signed-off-by: Jiri Pirko <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent fd6055a commit 744a4cf

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

net/sched/cls_api.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,17 @@ static void tcf_chain_flush(struct tcf_chain *chain)
215215

216216
static void tcf_chain_destroy(struct tcf_chain *chain)
217217
{
218-
list_del(&chain->list);
218+
/* May be already removed from the list by the previous call. */
219+
if (!list_empty(&chain->list))
220+
list_del_init(&chain->list);
221+
219222
tcf_chain_flush(chain);
220-
kfree(chain);
223+
224+
/* There might still be a reference held when we got here from
225+
* tcf_block_put. Wait for the user to drop reference before free.
226+
*/
227+
if (!chain->refcnt)
228+
kfree(chain);
221229
}
222230

223231
struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,

0 commit comments

Comments
 (0)