From: Kurt Van Dijck dev.kurt@vandijck-laurijssen.be
mainline inclusion from mainline-v5.4-rc1 commit 9868b5d44f3df9dd75247acd23dddff0a42f79be category: feature bugzilla: 38684 CVE: NA
---------------------------
The size of this structure will be increased with J1939 support. To stay binary compatible, the CAN_REQUIRED_SIZE macro is introduced for existing CAN protocols.
Signed-off-by: Kurt Van Dijck dev.kurt@vandijck-laurijssen.be Signed-off-by: Oleksij Rempel o.rempel@pengutronix.de Acked-by: Oliver Hartkopp socketcan@hartkopp.net Signed-off-by: Marc Kleine-Budde mkl@pengutronix.de Signed-off-by: Zhang Changzhong zhangchangzhong@huawei.com Reviewed-by: YueHaibing yuehaibing@huawei.com Signed-off-by: Yang Yingliang yangyingliang@huawei.com --- include/linux/can/core.h | 8 ++++++++ net/can/bcm.c | 4 ++-- net/can/raw.c | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/include/linux/can/core.h b/include/linux/can/core.h index 6099bc18bd0c..85bdc7d25067 100644 --- a/include/linux/can/core.h +++ b/include/linux/can/core.h @@ -41,6 +41,14 @@ struct can_proto { struct proto *prot; };
+/* required_size + * macro to find the minimum size of a struct + * that includes a requested member + */ +#define CAN_REQUIRED_SIZE(struct_type, member) \ + (offsetof(typeof(struct_type), member) + \ + sizeof(((typeof(struct_type) *)(NULL))->member)) + /* function prototypes for the CAN networklayer core (af_can.c) */
extern int can_proto_register(const struct can_proto *cp); diff --git a/net/can/bcm.c b/net/can/bcm.c index 79bb8afa9c0c..f5f97d9c17d2 100644 --- a/net/can/bcm.c +++ b/net/can/bcm.c @@ -1345,7 +1345,7 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) /* no bound device as default => check msg_name */ DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
- if (msg->msg_namelen < sizeof(*addr)) + if (msg->msg_namelen < CAN_REQUIRED_SIZE(*addr, can_ifindex)) return -EINVAL;
if (addr->can_family != AF_CAN) @@ -1587,7 +1587,7 @@ static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len, struct net *net = sock_net(sk); int ret = 0;
- if (len < sizeof(*addr)) + if (len < CAN_REQUIRED_SIZE(*addr, can_ifindex)) return -EINVAL;
lock_sock(sk); diff --git a/net/can/raw.c b/net/can/raw.c index c70207537488..ae552a611b9a 100644 --- a/net/can/raw.c +++ b/net/can/raw.c @@ -399,7 +399,7 @@ static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len) int err = 0; int notify_enetdown = 0;
- if (len < sizeof(*addr)) + if (len < CAN_REQUIRED_SIZE(*addr, can_ifindex)) return -EINVAL; if (addr->can_family != AF_CAN) return -EINVAL; @@ -735,7 +735,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) if (msg->msg_name) { DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
- if (msg->msg_namelen < sizeof(*addr)) + if (msg->msg_namelen < CAN_REQUIRED_SIZE(*addr, can_ifindex)) return -EINVAL;
if (addr->can_family != AF_CAN)