Commit fc689534 authored by Paolo Abeni's avatar Paolo Abeni
Browse files

Merge branch 'net_sched-hfsc-address-reentrant-enqueue-adding-class-to-eltree-twice'

Pedro Tammela says:

====================
net_sched: hfsc: Address reentrant enqueue adding class to eltree twice

Savino says:
    "We are writing to report that this recent patch
    (141d3439)
    can be bypassed, and a UAF can still occur when HFSC is utilized with
    NETEM.

    The patch only checks the cl->cl_nactive field to determine whether
    it is the first insertion or not, but this field is only
    incremented by init_vf.

    By using HFSC_RSC (which uses init_ed), it is possible to bypass the
    check and insert the class twice in the eltree.
    Under normal conditions, this would lead to an infinite loop in
    hfsc_dequeue for the reasons we already explained in this report.

    However, if TBF is added as root qdisc and it is configured with a
    very low rate,
    it can be utilized to prevent packets from being dequeued.
    This behavior can be exploited to perform subsequent insertions in the
    HFSC eltree and cause a UAF."

To fix both the UAF and the infinite loop, with netem as an hfsc child,
check explicitly in hfsc_enqueue whether the class is already in the eltree
whenever the HFSC_RSC flag is set.

Also add a TDC test to reproduce the UAF scenario.
====================

Link: https://patch.msgid.link/20250522181448.1439717-1-pctammela@mojatatu.com


Signed-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
parents 67af4ec9 2945ff73
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -175,6 +175,11 @@ struct hfsc_sched {

#define	HT_INFINITY	0xffffffffffffffffULL	/* infinite time value */

static bool cl_in_el_or_vttree(struct hfsc_class *cl)
{
	return ((cl->cl_flags & HFSC_FSC) && cl->cl_nactive) ||
		((cl->cl_flags & HFSC_RSC) && !RB_EMPTY_NODE(&cl->el_node));
}

/*
 * eligible tree holds backlogged classes being sorted by their eligible times.
@@ -1040,6 +1045,8 @@ hfsc_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
	if (cl == NULL)
		return -ENOBUFS;

	RB_CLEAR_NODE(&cl->el_node);

	err = tcf_block_get(&cl->block, &cl->filter_list, sch, extack);
	if (err) {
		kfree(cl);
@@ -1572,7 +1579,7 @@ hfsc_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
	sch->qstats.backlog += len;
	sch->q.qlen++;

	if (first && !cl->cl_nactive) {
	if (first && !cl_in_el_or_vttree(cl)) {
		if (cl->cl_flags & HFSC_RSC)
			init_ed(cl, len);
		if (cl->cl_flags & HFSC_FSC)
+35 −0
Original line number Diff line number Diff line
@@ -600,5 +600,40 @@
        "matchPattern": "qdisc hfsc",
        "matchCount": "1",
        "teardown": ["$TC qdisc del dev $DEV1 root handle 1: drr"]
    },
    {
        "id": "309e",
        "name": "Test HFSC eltree double add with reentrant enqueue behaviour on netem",
        "category": [
            "qdisc",
            "hfsc"
        ],
        "plugins": {
            "requires": "nsPlugin"
        },
        "setup": [
            "$IP link set dev $DUMMY up || true",
            "$IP addr add 10.10.11.10/24 dev $DUMMY || true",
            "$TC qdisc add dev $DUMMY root handle 1: tbf rate 8bit burst 100b latency 1s",
            "$TC qdisc add dev $DUMMY parent 1:0 handle 2:0 hfsc",
            "ping -I $DUMMY -f -c10 -s48 -W0.001 10.10.11.1 || true",
            "$TC class add dev $DUMMY parent 2:0 classid 2:1 hfsc rt m2 20Kbit",
            "$TC qdisc add dev $DUMMY parent 2:1 handle 3:0 netem duplicate 100%",
            "$TC class add dev $DUMMY parent 2:0 classid 2:2 hfsc rt m2 20Kbit",
            "$TC filter add dev $DUMMY parent 2:0 protocol ip prio 1 u32 match ip dst 10.10.11.2/32 flowid 2:1",
            "$TC filter add dev $DUMMY parent 2:0 protocol ip prio 2 u32 match ip dst 10.10.11.3/32 flowid 2:2",
            "ping -c 1 10.10.11.2 -I$DUMMY > /dev/null || true",
            "$TC filter del dev $DUMMY parent 2:0 protocol ip prio 1",
            "$TC class del dev $DUMMY classid 2:1",
            "ping -c 1 10.10.11.3 -I$DUMMY > /dev/null || true"
        ],
        "cmdUnderTest": "$TC class change dev $DUMMY parent 2:0 classid 2:2 hfsc sc m2 20Kbit",
        "expExitCode": "0",
        "verifyCmd": "$TC -j class ls dev $DUMMY classid 2:1",
        "matchJSON": [],
        "teardown": [
            "$TC qdisc del dev $DUMMY handle 1:0 root",
            "$IP addr del 10.10.10.10/24 dev $DUMMY || true"
        ]
    }
]