Skip to content

Commit

Permalink
can: isotp: support MSG_TRUNC flag when reading from socket
Browse files Browse the repository at this point in the history
BugLink: https://bugs.launchpad.net/bugs/1969110

[ Upstream commit 42bf50a ]

When providing the MSG_TRUNC flag via recvmsg() syscall the return value
provides the real length of the packet or datagram, even when it was longer
than the passed buffer.

Fixes: e057dd3 ("can: add ISO 15765-2:2016 transport protocol")
Link: linux-can/can-utils#347 (comment)
Link: https://lore.kernel.org/all/20220316164258.54155-3-socketcan@hartkopp.net
Suggested-by: Derek Will <derekrobertwill@gmail.com>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
(cherry picked from commit b184a8fa7dd48ae91d7afe7e4a423f29680bc87a)
Signed-off-by: Paolo Pisati <paolo.pisati@canonical.com>
  • Loading branch information
hartkopp authored and smb49 committed May 20, 2022
1 parent 3be169f commit 36104d7
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions net/can/isotp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1006,29 +1006,28 @@ static int isotp_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
struct sock *sk = sock->sk;
struct sk_buff *skb;
struct isotp_sock *so = isotp_sk(sk);
int err = 0;
int noblock;
int noblock = flags & MSG_DONTWAIT;
int ret = 0;

noblock = flags & MSG_DONTWAIT;
flags &= ~MSG_DONTWAIT;
if (flags & ~(MSG_DONTWAIT | MSG_TRUNC))
return -EINVAL;

if (!so->bound)
return -EADDRNOTAVAIL;

skb = skb_recv_datagram(sk, flags, noblock, &err);
flags &= ~MSG_DONTWAIT;
skb = skb_recv_datagram(sk, flags, noblock, &ret);
if (!skb)
return err;
return ret;

if (size < skb->len)
msg->msg_flags |= MSG_TRUNC;
else
size = skb->len;

err = memcpy_to_msg(msg, skb->data, size);
if (err < 0) {
skb_free_datagram(sk, skb);
return err;
}
ret = memcpy_to_msg(msg, skb->data, size);
if (ret < 0)
goto out_err;

sock_recv_timestamp(msg, sk, skb);

Expand All @@ -1038,9 +1037,13 @@ static int isotp_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
}

/* set length of return value */
ret = (flags & MSG_TRUNC) ? skb->len : size;

out_err:
skb_free_datagram(sk, skb);

return size;
return ret;
}

static int isotp_release(struct socket *sock)
Expand Down

0 comments on commit 36104d7

Please sign in to comment.