Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

can: netlink: can_validate(): validate sample point for CAN and CAN-FD

The sample point is a value in tenths of a percent. Meaningful values
are between 0 and 1000. Invalid values are rejected and an error
message is returned to user space via netlink.

Link: https://lore.kernel.org/all/20230202110854.2318594-8-mkl@pengutronix.de
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

+32 -1
+32 -1
drivers/net/can/dev/netlink.c
··· 36 36 [IFLA_CAN_TDC_TDCF] = { .type = NLA_U32 }, 37 37 }; 38 38 39 + static int can_validate_bittiming(const struct can_bittiming *bt, 40 + struct netlink_ext_ack *extack) 41 + { 42 + /* sample point is in one-tenth of a percent */ 43 + if (bt->sample_point >= 1000) { 44 + NL_SET_ERR_MSG(extack, "sample point must be between 0 and 100%"); 45 + 46 + return -EINVAL; 47 + } 48 + 49 + return 0; 50 + } 51 + 39 52 static int can_validate(struct nlattr *tb[], struct nlattr *data[], 40 53 struct netlink_ext_ack *extack) 41 54 { 42 55 bool is_can_fd = false; 56 + int err; 43 57 44 58 /* Make sure that valid CAN FD configurations always consist of 45 59 * - nominal/arbitration bittiming ··· 64 50 65 51 if (!data) 66 52 return 0; 53 + 54 + if (data[IFLA_CAN_BITTIMING]) { 55 + struct can_bittiming bt; 56 + 57 + memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt)); 58 + err = can_validate_bittiming(&bt, extack); 59 + if (err) 60 + return err; 61 + } 67 62 68 63 if (data[IFLA_CAN_CTRLMODE]) { 69 64 struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]); ··· 94 71 */ 95 72 if (data[IFLA_CAN_TDC]) { 96 73 struct nlattr *tb_tdc[IFLA_CAN_TDC_MAX + 1]; 97 - int err; 98 74 99 75 err = nla_parse_nested(tb_tdc, IFLA_CAN_TDC_MAX, 100 76 data[IFLA_CAN_TDC], ··· 122 100 if (data[IFLA_CAN_DATA_BITTIMING] || data[IFLA_CAN_TDC]) { 123 101 if (!is_can_fd) 124 102 return -EOPNOTSUPP; 103 + } 104 + 105 + if (data[IFLA_CAN_DATA_BITTIMING]) { 106 + struct can_bittiming bt; 107 + 108 + memcpy(&bt, nla_data(data[IFLA_CAN_DATA_BITTIMING]), sizeof(bt)); 109 + err = can_validate_bittiming(&bt, extack); 110 + if (err) 111 + return err; 125 112 } 126 113 127 114 return 0;