Subject: [PATCH mellanox-tree] net/mlx5: prevent an integer underflow in mlx5_perout_configure()

From: Dan Carpenter <[email-masked]>
Date: Fri, 19 Feb 2021 12:57:52 +0300

The value of "sec" comes from the user.  Negative values will lead to
shift wrapping inside the perout_conf_real_time() function and triggger
a UBSan warning.

Add a check and return -EINVAL to prevent that from happening.

Fixes: 432119de33d9 ("net/mlx5: Add cyc2time HW translation mode support")
Signed-off-by: Dan Carpenter <[email-masked]>
---
Saeed, I think this goes through your git tree and you will send a pull
request to the networking?

>From static analysis.  Not tested.

 drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c b/drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c
index b0e129d0f6d8..286824ca62b5 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/lib/clock.c
@@ -516,7 +516,7 @@ static int mlx5_perout_configure(struct ptp_clock_info *ptp,
 		nsec = rq->perout.start.nsec;
 		sec = rq->perout.start.sec;
 
-		if (rt_mode && sec > U32_MAX)
+		if (rt_mode && (sec < 0 || sec > U32_MAX))
 			return -EINVAL;
 
 		time_stamp = rt_mode ? perout_conf_real_time(sec, nsec) :
-- 
2.30.0