Skip to content

Commit 4346a66

Browse files
edumazetgregkh
authored andcommitted
sch_netem: fix issues in netem_change() vs get_dist_table()
commit 11b7331 upstream. In blamed commit, I missed that get_dist_table() was allocating memory using GFP_KERNEL, and acquiring qdisc lock to perform the swap of newly allocated table with current one. In this patch, get_dist_table() is allocating memory and copy user data before we acquire the qdisc lock. Then we perform swap operations while being protected by the lock. Note that after this patch netem_change() no longer can do partial changes. If an error is returned, qdisc conf is left unchanged. Fixes: 2174a08 ("sch_netem: acquire qdisc lock in netem_change()") Reported-by: syzbot <syzkaller@googlegroups.com> Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Stephen Hemminger <stephen@networkplumber.org> Acked-by: Jamal Hadi Salim <jhs@mojatatu.com> Reviewed-by: Simon Horman <simon.horman@corigine.com> Link: https://lore.kernel.org/r/20230622181503.2327695-1-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 3ae919c commit 4346a66

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

net/sched/sch_netem.c

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -773,12 +773,10 @@ static void dist_free(struct disttable *d)
773773
* signed 16 bit values.
774774
*/
775775

776-
static int get_dist_table(struct Qdisc *sch, struct disttable **tbl,
777-
const struct nlattr *attr)
776+
static int get_dist_table(struct disttable **tbl, const struct nlattr *attr)
778777
{
779778
size_t n = nla_len(attr)/sizeof(__s16);
780779
const __s16 *data = nla_data(attr);
781-
spinlock_t *root_lock;
782780
struct disttable *d;
783781
int i;
784782

@@ -793,13 +791,7 @@ static int get_dist_table(struct Qdisc *sch, struct disttable **tbl,
793791
for (i = 0; i < n; i++)
794792
d->table[i] = data[i];
795793

796-
root_lock = qdisc_root_sleeping_lock(sch);
797-
798-
spin_lock_bh(root_lock);
799-
swap(*tbl, d);
800-
spin_unlock_bh(root_lock);
801-
802-
dist_free(d);
794+
*tbl = d;
803795
return 0;
804796
}
805797

@@ -956,6 +948,8 @@ static int netem_change(struct Qdisc *sch, struct nlattr *opt,
956948
{
957949
struct netem_sched_data *q = qdisc_priv(sch);
958950
struct nlattr *tb[TCA_NETEM_MAX + 1];
951+
struct disttable *delay_dist = NULL;
952+
struct disttable *slot_dist = NULL;
959953
struct tc_netem_qopt *qopt;
960954
struct clgstate old_clg;
961955
int old_loss_model = CLG_RANDOM;
@@ -966,6 +960,18 @@ static int netem_change(struct Qdisc *sch, struct nlattr *opt,
966960
if (ret < 0)
967961
return ret;
968962

963+
if (tb[TCA_NETEM_DELAY_DIST]) {
964+
ret = get_dist_table(&delay_dist, tb[TCA_NETEM_DELAY_DIST]);
965+
if (ret)
966+
goto table_free;
967+
}
968+
969+
if (tb[TCA_NETEM_SLOT_DIST]) {
970+
ret = get_dist_table(&slot_dist, tb[TCA_NETEM_SLOT_DIST]);
971+
if (ret)
972+
goto table_free;
973+
}
974+
969975
sch_tree_lock(sch);
970976
/* backup q->clg and q->loss_model */
971977
old_clg = q->clg;
@@ -975,26 +981,17 @@ static int netem_change(struct Qdisc *sch, struct nlattr *opt,
975981
ret = get_loss_clg(q, tb[TCA_NETEM_LOSS]);
976982
if (ret) {
977983
q->loss_model = old_loss_model;
984+
q->clg = old_clg;
978985
goto unlock;
979986
}
980987
} else {
981988
q->loss_model = CLG_RANDOM;
982989
}
983990

984-
if (tb[TCA_NETEM_DELAY_DIST]) {
985-
ret = get_dist_table(sch, &q->delay_dist,
986-
tb[TCA_NETEM_DELAY_DIST]);
987-
if (ret)
988-
goto get_table_failure;
989-
}
990-
991-
if (tb[TCA_NETEM_SLOT_DIST]) {
992-
ret = get_dist_table(sch, &q->slot_dist,
993-
tb[TCA_NETEM_SLOT_DIST]);
994-
if (ret)
995-
goto get_table_failure;
996-
}
997-
991+
if (delay_dist)
992+
swap(q->delay_dist, delay_dist);
993+
if (slot_dist)
994+
swap(q->slot_dist, slot_dist);
998995
sch->limit = qopt->limit;
999996

1000997
q->latency = PSCHED_TICKS2NS(qopt->latency);
@@ -1044,17 +1041,11 @@ static int netem_change(struct Qdisc *sch, struct nlattr *opt,
10441041

10451042
unlock:
10461043
sch_tree_unlock(sch);
1047-
return ret;
10481044

1049-
get_table_failure:
1050-
/* recover clg and loss_model, in case of
1051-
* q->clg and q->loss_model were modified
1052-
* in get_loss_clg()
1053-
*/
1054-
q->clg = old_clg;
1055-
q->loss_model = old_loss_model;
1056-
1057-
goto unlock;
1045+
table_free:
1046+
dist_free(delay_dist);
1047+
dist_free(slot_dist);
1048+
return ret;
10581049
}
10591050

10601051
static int netem_init(struct Qdisc *sch, struct nlattr *opt,

0 commit comments

Comments
 (0)