Skip to content

Commit 9868b5d

Browse files
kurt-vdmarckleinebudde
authored andcommitted
can: introduce CAN_REQUIRED_SIZE macro
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 <[email protected]> Signed-off-by: Oleksij Rempel <[email protected]> Acked-by: Oliver Hartkopp <[email protected]> Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent 4f746fb commit 9868b5d

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

include/linux/can/core.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ struct can_proto {
4141
struct proto *prot;
4242
};
4343

44+
/* required_size
45+
* macro to find the minimum size of a struct
46+
* that includes a requested member
47+
*/
48+
#define CAN_REQUIRED_SIZE(struct_type, member) \
49+
(offsetof(typeof(struct_type), member) + \
50+
sizeof(((typeof(struct_type) *)(NULL))->member))
51+
4452
/* function prototypes for the CAN networklayer core (af_can.c) */
4553

4654
extern int can_proto_register(const struct can_proto *cp);

net/can/bcm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
12941294
/* no bound device as default => check msg_name */
12951295
DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
12961296

1297-
if (msg->msg_namelen < sizeof(*addr))
1297+
if (msg->msg_namelen < CAN_REQUIRED_SIZE(*addr, can_ifindex))
12981298
return -EINVAL;
12991299

13001300
if (addr->can_family != AF_CAN)
@@ -1536,7 +1536,7 @@ static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
15361536
struct net *net = sock_net(sk);
15371537
int ret = 0;
15381538

1539-
if (len < sizeof(*addr))
1539+
if (len < CAN_REQUIRED_SIZE(*addr, can_ifindex))
15401540
return -EINVAL;
15411541

15421542
lock_sock(sk);

net/can/raw.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ static int raw_bind(struct socket *sock, struct sockaddr *uaddr, int len)
396396
int err = 0;
397397
int notify_enetdown = 0;
398398

399-
if (len < sizeof(*addr))
399+
if (len < CAN_REQUIRED_SIZE(*addr, can_ifindex))
400400
return -EINVAL;
401401
if (addr->can_family != AF_CAN)
402402
return -EINVAL;
@@ -733,7 +733,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
733733
if (msg->msg_name) {
734734
DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
735735

736-
if (msg->msg_namelen < sizeof(*addr))
736+
if (msg->msg_namelen < CAN_REQUIRED_SIZE(*addr, can_ifindex))
737737
return -EINVAL;
738738

739739
if (addr->can_family != AF_CAN)

0 commit comments

Comments
 (0)