Skip to content

Commit 18912c5

Browse files
Stanislav Fomichevkuba-moo
authored andcommitted
tcp: devmem: don't write truncated dmabuf CMSGs to userspace
Currently, we report -ETOOSMALL (err) only on the first iteration (!sent). When we get put_cmsg error after a bunch of successful put_cmsg calls, we don't signal the error at all. This might be confusing on the userspace side which will see truncated CMSGs but no MSG_CTRUNC signal. Consider the following case: - sizeof(struct cmsghdr) = 16 - sizeof(struct dmabuf_cmsg) = 24 - total cmsg size (CMSG_LEN) = 40 (16+24) When calling recvmsg with msg_controllen=60, the userspace will receive two(!) dmabuf_cmsg(s), the first one will be a valid one and the second one will be silently truncated. There is no easy way to discover the truncation besides doing something like "cm->cmsg_len != CMSG_LEN(sizeof(dmabuf_cmsg))". Introduce new put_devmem_cmsg wrapper that reports an error instead of doing the truncation. Mina suggests that it's the intended way this API should work. Note that we might now report MSG_CTRUNC when the users (incorrectly) call us with msg_control == NULL. Fixes: 8f0b3cc ("tcp: RX path for devmem TCP") Reviewed-by: Mina Almasry <[email protected]> Signed-off-by: Stanislav Fomichev <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent bab3a6e commit 18912c5

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

include/linux/socket.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ struct ucred {
392392

393393
extern int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr);
394394
extern int put_cmsg(struct msghdr*, int level, int type, int len, void *data);
395+
extern int put_cmsg_notrunc(struct msghdr *msg, int level, int type, int len,
396+
void *data);
395397

396398
struct timespec64;
397399
struct __kernel_timespec;

net/core/scm.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,16 @@ int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
282282
}
283283
EXPORT_SYMBOL(put_cmsg);
284284

285+
int put_cmsg_notrunc(struct msghdr *msg, int level, int type, int len,
286+
void *data)
287+
{
288+
/* Don't produce truncated CMSGs */
289+
if (!msg->msg_control || msg->msg_controllen < CMSG_LEN(len))
290+
return -ETOOSMALL;
291+
292+
return put_cmsg(msg, level, type, len, data);
293+
}
294+
285295
void put_cmsg_scm_timestamping64(struct msghdr *msg, struct scm_timestamping_internal *tss_internal)
286296
{
287297
struct scm_timestamping64 tss;

net/ipv4/tcp.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,14 +2438,12 @@ static int tcp_recvmsg_dmabuf(struct sock *sk, const struct sk_buff *skb,
24382438
*/
24392439
memset(&dmabuf_cmsg, 0, sizeof(dmabuf_cmsg));
24402440
dmabuf_cmsg.frag_size = copy;
2441-
err = put_cmsg(msg, SOL_SOCKET, SO_DEVMEM_LINEAR,
2442-
sizeof(dmabuf_cmsg), &dmabuf_cmsg);
2443-
if (err || msg->msg_flags & MSG_CTRUNC) {
2444-
msg->msg_flags &= ~MSG_CTRUNC;
2445-
if (!err)
2446-
err = -ETOOSMALL;
2441+
err = put_cmsg_notrunc(msg, SOL_SOCKET,
2442+
SO_DEVMEM_LINEAR,
2443+
sizeof(dmabuf_cmsg),
2444+
&dmabuf_cmsg);
2445+
if (err)
24472446
goto out;
2448-
}
24492447

24502448
sent += copy;
24512449

@@ -2499,16 +2497,12 @@ static int tcp_recvmsg_dmabuf(struct sock *sk, const struct sk_buff *skb,
24992497
offset += copy;
25002498
remaining_len -= copy;
25012499

2502-
err = put_cmsg(msg, SOL_SOCKET,
2503-
SO_DEVMEM_DMABUF,
2504-
sizeof(dmabuf_cmsg),
2505-
&dmabuf_cmsg);
2506-
if (err || msg->msg_flags & MSG_CTRUNC) {
2507-
msg->msg_flags &= ~MSG_CTRUNC;
2508-
if (!err)
2509-
err = -ETOOSMALL;
2500+
err = put_cmsg_notrunc(msg, SOL_SOCKET,
2501+
SO_DEVMEM_DMABUF,
2502+
sizeof(dmabuf_cmsg),
2503+
&dmabuf_cmsg);
2504+
if (err)
25102505
goto out;
2511-
}
25122506

25132507
atomic_long_inc(&niov->pp_ref_count);
25142508
tcp_xa_pool.netmems[tcp_xa_pool.idx++] = skb_frag_netmem(frag);

0 commit comments

Comments
 (0)