From e2c9fdd5c0f84eb234e122fe8fce9c4d949882cd Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 2 Jun 2025 18:47:49 -0400 Subject: [PATCH] network: fix a potential divide-by-zero (#37705) In function `tc_init`, hz is parsed from the content of file `"/proc/net/psched"` and can be 0. In function `hierarchy_token_bucket_class_verify`, hz is directly used as a divisor in `htb->buffer = htb->rate / hz + htb->mtu;` without any check. This adds a check on hz before using it as a divisor. Co-authored-by: jinyaoguo (cherry picked from commit 1a596054a0f937bfc244580f07510759a0e45657) --- src/network/tc/htb.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/network/tc/htb.c b/src/network/tc/htb.c index 39f436a804d..e1f6050cbc5 100644 --- a/src/network/tc/htb.c +++ b/src/network/tc/htb.c @@ -470,6 +470,8 @@ static int hierarchy_token_bucket_class_verify(TClass *tclass) { if (r < 0) return log_error_errno(r, "Failed to read /proc/net/psched: %m"); + /* Kernel would never hand us 0 Hz. */ + assert(hz > 0); if (htb->buffer == 0) htb->buffer = htb->rate / hz + htb->mtu; if (htb->ceil_buffer == 0) -- 2.47.3