Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: socket: recvmsg() doesn't update msg_controllen accordingly #77303

Closed
axelnxp opened this issue Aug 20, 2024 · 5 comments · Fixed by #77345
Closed

net: socket: recvmsg() doesn't update msg_controllen accordingly #77303

axelnxp opened this issue Aug 20, 2024 · 5 comments · Fixed by #77345
Assignees
Labels
area: Networking bug The issue is a bug, or the PR is fixing a bug priority: medium Medium impact/importance bug Regression Something, which was working, does not anymore

Comments

@axelnxp
Copy link
Contributor

axelnxp commented Aug 20, 2024

Describe the bug

The bug is exactly the same than it was with #68352.
This was fixed at the time, but I think the changes from #73713 introduced the regression.
msg_controllen is still set to the initial length, while csmg_len is set to 0.

If I bypass this added code, I don't reproduce the issue.

Maybe the problem comes from add_timestamping ? Does it update the csmg_len accordingly (if applicable) ?

To Reproduce
One can reuse the matter function where the issue is observed:
https://github.com/project-chip/connectedhomeip/blob/master/src/inet/UDPEndPointImplSockets.cpp#L611
And make sure no control data is received.

Expected behavior
msg_controllen field shall be updated by recvmsg according to the control data received.

Impact
Impact is high, in the current state, any Matter application over Zephyr would be impacted, likely unable to even perform a commissioning.

@axelnxp axelnxp added the bug The issue is a bug, or the PR is fixing a bug label Aug 20, 2024
@axelnxp
Copy link
Contributor Author

axelnxp commented Aug 20, 2024

cc @jukkar

@dleach02 dleach02 added area: Networking Regression Something, which was working, does not anymore priority: medium Medium impact/importance bug labels Aug 20, 2024
@jukkar
Copy link
Member

jukkar commented Aug 20, 2024

Cc: @awojasinski please take a look, seems to be related to the ptp pr

@axelnxp
Copy link
Contributor Author

axelnxp commented Aug 21, 2024

I've been able to get more info on the root cause.
In my case, add_timestamping returns -ENOTSUP because net_context_get_option sets timestamping to 0.
In this case, csmg_len is not updated, so msg_controllen should be cleared, as no pkt info has been added.

So the problem comes from the clear_controllen logic.

I came up with the following patch, the idea is to not clear msg_controllen only when adding pkt info was a success.
I can submit the PR if this patch looks fine to you.

diff --git a/subsys/net/lib/sockets/sockets.c b/subsys/net/lib/sockets/sockets.c
index 25328939457..118cf0f9f93 100644
--- a/subsys/net/lib/sockets/sockets.c
+++ b/subsys/net/lib/sockets/sockets.c
@@ -1573,17 +1573,19 @@ static inline ssize_t zsock_recv_dgram(struct net_context *ctx,
                                bool clear_controllen = true;
 
                                if (IS_ENABLED(CONFIG_NET_CONTEXT_TIMESTAMPING)) {
-                                       clear_controllen = false;
                                        if (add_timestamping(ctx, pkt, msg) < 0) {
                                                msg->msg_flags |= ZSOCK_MSG_CTRUNC;
+                                       } else {
+                                               clear_controllen = false;
                                        }
                                }
 
                                if (IS_ENABLED(CONFIG_NET_CONTEXT_RECV_PKTINFO) &&
                                    net_context_is_recv_pktinfo_set(ctx)) {
-                                       clear_controllen = false;
                                        if (add_pktinfo(ctx, pkt, msg) < 0) {
                                                msg->msg_flags |= ZSOCK_MSG_CTRUNC;
+                                       } else {
+                                               clear_controllen = false;
                                        }
                                }

@jukkar
Copy link
Member

jukkar commented Aug 21, 2024

@axelnxp looks good, please send a PR for it.

decsny pushed a commit to nxp-upstream/zephyr that referenced this issue Aug 21, 2024
If adding any control data like timestamping or pkt_info fails, the
msg_controllen wasn't properly cleared.
This commit aims to fix this by clearing the msg_controllen only when
adding control data is successful.

Fixes zephyrproject-rtos#77303

Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
@axelnxp
Copy link
Contributor Author

axelnxp commented Aug 21, 2024

@jukkar done, refer to #77345

axelnxp added a commit to nxp-upstream/zephyr that referenced this issue Aug 22, 2024
According to recvmsg man page, msg_controllen should be set to the sum
of the length of all control messages in the buffer.
This is missing from the current recvmsg implementation.
This commit aims to fix this by updating msg_controllen each time control
data are added to the buffer.
This commit also fixes cases where the msg_controllen is not cleared
correctly.

Fixes zephyrproject-rtos#77303

Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
axelnxp added a commit to nxp-upstream/zephyr that referenced this issue Aug 22, 2024
According to recvmsg man page, msg_controllen should be set to the sum
of the length of all control messages in the buffer.
This is missing from the current recvmsg implementation.
This commit aims to fix this by updating msg_controllen each time control
data are added to the buffer.
This commit also fixes cases where the msg_controllen is not cleared
correctly.

Fixes zephyrproject-rtos#77303

Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
axelnxp added a commit to nxp-upstream/zephyr that referenced this issue Aug 22, 2024
According to recvmsg man page, msg_controllen should be set to the sum
of the length of all control messages in the buffer.
This is missing from the current recvmsg implementation.
This commit aims to fix this by updating msg_controllen each time control
data are added to the buffer.
This commit also fixes cases where the msg_controllen is not cleared
correctly.

Fixes zephyrproject-rtos#77303

Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
axelnxp added a commit to nxp-upstream/zephyr that referenced this issue Aug 22, 2024
According to recvmsg man page, msg_controllen should be set to the sum
of the length of all control messages in the buffer.
This is missing from the current recvmsg implementation.
This commit aims to fix this by updating msg_controllen each time control
data are added to the buffer.
This commit also fixes cases where the msg_controllen is cleared incorrectly.

Fixes zephyrproject-rtos#77303

Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
axelnxp added a commit to nxp-upstream/zephyr that referenced this issue Aug 22, 2024
According to recvmsg man page, msg_controllen should be set to the sum
of the length of all control messages in the buffer.
This is missing from the current recvmsg implementation.
This commit aims to fix this by updating msg_controllen each time control
data are added to the buffer.
This commit also fixes cases where the msg_controllen is cleared incorrectly.

Fixes zephyrproject-rtos#77303

Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
axelnxp added a commit to nxp-upstream/zephyr that referenced this issue Aug 22, 2024
According to recvmsg man page, msg_controllen should be set to the sum
of the length of all control messages in the buffer.
This is missing from the current recvmsg implementation.
This commit aims to fix this by updating msg_controllen each time control
data are added to the buffer.
This commit also fixes cases where the msg_controllen is cleared
incorrectly.

Fixes zephyrproject-rtos#77303

Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
zephyrbot pushed a commit that referenced this issue Aug 23, 2024
According to recvmsg man page, msg_controllen should be set to the sum
of the length of all control messages in the buffer.
This is missing from the current recvmsg implementation.
This commit aims to fix this by updating msg_controllen each time control
data are added to the buffer.
This commit also fixes cases where the msg_controllen is cleared
incorrectly.

Fixes #77303

Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
(cherry picked from commit 5d643f4)
nashif pushed a commit that referenced this issue Aug 31, 2024
According to recvmsg man page, msg_controllen should be set to the sum
of the length of all control messages in the buffer.
This is missing from the current recvmsg implementation.
This commit aims to fix this by updating msg_controllen each time control
data are added to the buffer.
This commit also fixes cases where the msg_controllen is cleared
incorrectly.

Fixes #77303

Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
(cherry picked from commit 5d643f4)
Chenhongren pushed a commit to Chenhongren/zephyr that referenced this issue Sep 6, 2024
According to recvmsg man page, msg_controllen should be set to the sum
of the length of all control messages in the buffer.
This is missing from the current recvmsg implementation.
This commit aims to fix this by updating msg_controllen each time control
data are added to the buffer.
This commit also fixes cases where the msg_controllen is cleared
incorrectly.

Fixes zephyrproject-rtos#77303

(cherry picked from commit 5d643f4)

Original-Signed-off-by: Axel Le Bourhis <axel.lebourhis@nxp.com>
GitOrigin-RevId: 5d643f4
Cr-Build-Id: 8738638626547930177
Cr-Build-Url: https://cr-buildbucket.appspot.com/build/8738638626547930177
Copybot-Job-Name: zephyr-main-copybot-downstream
Change-Id: I22809d442ac56b4f293e52efa6a4a24f090ab02a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/5810440
Tested-by: Ting Shen <phoenixshen@chromium.org>
Commit-Queue: Ting Shen <phoenixshen@chromium.org>
Tested-by: ChromeOS Prod (Robot) <chromeos-ci-prod@chromeos-bot.iam.gserviceaccount.com>
Reviewed-by: Ting Shen <phoenixshen@chromium.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Networking bug The issue is a bug, or the PR is fixing a bug priority: medium Medium impact/importance bug Regression Something, which was working, does not anymore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants