Skip to content

Commit

Permalink
drivers: can: be consistent in filter_id checks when removing rx filters
Browse files Browse the repository at this point in the history
Change the CAN controller driver implementations for the
can_remove_rx_filter() API call to be consistent in their validation of the
supplied filter_id.

Fixes: zephyrproject-rtos#64398

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
  • Loading branch information
henrikbrixandersen committed Oct 26, 2023
1 parent 13072b4 commit dfac3e6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions drivers/can/can_loopback.c
Expand Up @@ -171,6 +171,11 @@ void can_loopback_detach(const struct device *dev, int filter_id)
{
struct can_loopback_data *data = DEV_DATA(dev);

if (filter_id < 0 || filter_id >= ARRAY_SIZE(data->filters)) {
LOG_ERR("filter ID %d out of bounds", filter_id);
return;
}

LOG_DBG("Detach filter ID: %d", filter_id);
k_mutex_lock(&data->mtx, K_FOREVER);
data->filters[filter_id].rx_cb = NULL;
Expand Down
7 changes: 6 additions & 1 deletion drivers/can/can_mcan.c
Expand Up @@ -894,11 +894,16 @@ int can_mcan_attach_isr(struct can_mcan_data *data,
void can_mcan_detach(struct can_mcan_data *data,
struct can_mcan_msg_sram *msg_ram, int filter_nr)
{
if (filter_nr < 0) {
LOG_ERR("filter ID %d out of bounds", filter_nr);
return;
}

k_mutex_lock(&data->inst_mutex, K_FOREVER);
if (filter_nr >= NUM_STD_FILTER_DATA) {
filter_nr -= NUM_STD_FILTER_DATA;
if (filter_nr >= NUM_STD_FILTER_DATA) {
LOG_ERR("Wrong filter id");
LOG_ERR("filter ID %d out of bounds", filter_nr);
return;
}

Expand Down
5 changes: 5 additions & 0 deletions drivers/can/can_mcp2515.c
Expand Up @@ -551,6 +551,11 @@ static void mcp2515_detach(const struct device *dev, int filter_nr)
{
struct mcp2515_data *dev_data = DEV_DATA(dev);

if (filter_nr < 0 || filter_nr >= CONFIG_CAN_MAX_FILTER) {
LOG_ERR("filter ID %d out of bounds", filter_nr);
return;
}

k_mutex_lock(&dev_data->mutex, K_FOREVER);
dev_data->filter_usage &= ~BIT(filter_nr);
k_mutex_unlock(&dev_data->mutex);
Expand Down
5 changes: 2 additions & 3 deletions drivers/can/can_mcux_flexcan.c
Expand Up @@ -480,9 +480,8 @@ static void mcux_flexcan_detach(const struct device *dev, int filter_id)
const struct mcux_flexcan_config *config = dev->config;
struct mcux_flexcan_data *data = dev->data;

if (filter_id >= MCUX_FLEXCAN_MAX_RX) {
LOG_ERR("Detach: Filter id >= MAX_RX (%d >= %d)", filter_id,
MCUX_FLEXCAN_MAX_RX);
if (filter_id < 0 || filter_id >= MCUX_FLEXCAN_MAX_RX) {
LOG_ERR("filter ID %d out of bounds", filter_id);
return;
}

Expand Down
3 changes: 2 additions & 1 deletion drivers/can/can_rcar.c
Expand Up @@ -829,7 +829,8 @@ void can_rcar_detach(const struct device *dev, int filter_nr)
{
struct can_rcar_data *data = DEV_CAN_DATA(dev);

if (filter_nr >= CONFIG_CAN_RCAR_MAX_FILTER) {
if (filter_nr < 0 || filter_nr >= CONFIG_CAN_RCAR_MAX_FILTER) {
LOG_ERR("filter ID %d out of bounds", filter_nr);
return;
}

Expand Down

0 comments on commit dfac3e6

Please sign in to comment.