mlxsw: Verify the accessed index doesn't exceed the array length
authorDanielle Ratson <danieller@nvidia.com>
Mon, 17 May 2021 17:03:57 +0000 (20:03 +0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 1 Dec 2021 08:19:05 +0000 (09:19 +0100)
[ Upstream commit 837ec05cfea08284c575e8e834777b107da5ff9d ]

There are few cases in which an array index queried from a fw register,
is accessed without any validation that it doesn't exceed the array
length.

Add a proper length validation, so accessing memory past the end of an
array will be forbidden.

Signed-off-by: Danielle Ratson <danieller@nvidia.com>
Signed-off-by: Ido Schimmel <idosch@nvidia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/net/ethernet/mellanox/mlxsw/minimal.c
drivers/net/ethernet/mellanox/mlxsw/spectrum.c
drivers/net/ethernet/mellanox/mlxsw/spectrum_ptp.c
drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c
drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c

index c010db2c9dba99566a2f06dcc70adebc5cbb5c6e..443dc44452ef8b5fcf9df28df480b4c757cb1be9 100644 (file)
@@ -234,6 +234,7 @@ static void mlxsw_m_port_remove(struct mlxsw_m *mlxsw_m, u8 local_port)
 static int mlxsw_m_port_module_map(struct mlxsw_m *mlxsw_m, u8 local_port,
                                   u8 *last_module)
 {
+       unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
        u8 module, width;
        int err;
 
@@ -249,6 +250,9 @@ static int mlxsw_m_port_module_map(struct mlxsw_m *mlxsw_m, u8 local_port,
        if (module == *last_module)
                return 0;
        *last_module = module;
+
+       if (WARN_ON_ONCE(module >= max_ports))
+               return -EINVAL;
        mlxsw_m->module_to_port[module] = ++mlxsw_m->max_ports;
 
        return 0;
index b08853f71b2beae79e1458e2fc2cb77eb6d4dec5..1a9978f501ba9b22e73bbb1d13c24b20f3d43dd2 100644 (file)
@@ -2052,9 +2052,14 @@ static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg,
        struct mlxsw_sp *mlxsw_sp = priv;
        struct mlxsw_sp_port *mlxsw_sp_port;
        enum mlxsw_reg_pude_oper_status status;
+       unsigned int max_ports;
        u8 local_port;
 
+       max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
        local_port = mlxsw_reg_pude_local_port_get(pude_pl);
+
+       if (WARN_ON_ONCE(local_port >= max_ports))
+               return;
        mlxsw_sp_port = mlxsw_sp->ports[local_port];
        if (!mlxsw_sp_port)
                return;
index ca8090a28dec600fd4e82164755c98e74363adf1..50eca2daad8439f95222da1a179729eb1cab02cb 100644 (file)
@@ -568,10 +568,13 @@ void mlxsw_sp1_ptp_got_timestamp(struct mlxsw_sp *mlxsw_sp, bool ingress,
                                 u8 domain_number, u16 sequence_id,
                                 u64 timestamp)
 {
+       unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
        struct mlxsw_sp_port *mlxsw_sp_port;
        struct mlxsw_sp1_ptp_key key;
        u8 types;
 
+       if (WARN_ON_ONCE(local_port >= max_ports))
+               return;
        mlxsw_sp_port = mlxsw_sp->ports[local_port];
        if (!mlxsw_sp_port)
                return;
index 4381f8c6c3fb718698091172d1567e3b8056d2ac..53128382fc2e05e25c655f8fc0cf5f076d98240d 100644 (file)
@@ -2177,6 +2177,7 @@ static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
                                                   char *rauhtd_pl,
                                                   int ent_index)
 {
+       u64 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
        struct net_device *dev;
        struct neighbour *n;
        __be32 dipn;
@@ -2185,6 +2186,8 @@ static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
 
        mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
 
+       if (WARN_ON_ONCE(rif >= max_rifs))
+               return;
        if (!mlxsw_sp->router->rifs[rif]) {
                dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
                return;
index 6501ce94ace586eb6c6742f179f97254aa1d3f05..368fa0e5ad3152e150e983ac632bb76cc78a1b17 100644 (file)
@@ -2410,6 +2410,7 @@ static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
                                            char *sfn_pl, int rec_index,
                                            bool adding)
 {
+       unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
        struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
        struct mlxsw_sp_bridge_device *bridge_device;
        struct mlxsw_sp_bridge_port *bridge_port;
@@ -2422,6 +2423,9 @@ static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
        int err;
 
        mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
+
+       if (WARN_ON_ONCE(local_port >= max_ports))
+               return;
        mlxsw_sp_port = mlxsw_sp->ports[local_port];
        if (!mlxsw_sp_port) {
                dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");