]> git.itanic.dy.fi Git - linux-stable/commitdiff
can: dev: register_candev(): ensure that bittiming const are valid
authorMarc Kleine-Budde <mkl@pengutronix.de>
Mon, 9 Aug 2021 19:07:14 +0000 (21:07 +0200)
committerMarc Kleine-Budde <mkl@pengutronix.de>
Mon, 6 Feb 2023 12:57:26 +0000 (13:57 +0100)
Implement the function can_bittiming_const_valid() to check the
validity of the specified bit timing constant. Call this function from
register_candev() to check the bit timing constants during the
registration of the CAN interface.

Link: https://lore.kernel.org/all/20230202110854.2318594-6-mkl@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
drivers/net/can/dev/dev.c

index c1956b1e9faf7388f60fd6b29b5c0c7ca31e76d6..3b51055be40e6089cda2ced6f2ebe2712ac57c36 100644 (file)
@@ -498,6 +498,18 @@ static int can_get_termination(struct net_device *ndev)
        return 0;
 }
 
+static bool
+can_bittiming_const_valid(const struct can_bittiming_const *btc)
+{
+       if (!btc)
+               return true;
+
+       if (!btc->sjw_max)
+               return false;
+
+       return true;
+}
+
 /* Register the CAN network device */
 int register_candev(struct net_device *dev)
 {
@@ -518,6 +530,10 @@ int register_candev(struct net_device *dev)
        if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
                return -EINVAL;
 
+       if (!can_bittiming_const_valid(priv->bittiming_const) ||
+           !can_bittiming_const_valid(priv->data_bittiming_const))
+               return -EINVAL;
+
        if (!priv->termination_const) {
                err = can_get_termination(dev);
                if (err)