Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-128881: Do not ignore address and flags parameters in socket.{send,recv}_fds #128882

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

GalaxySnail
Copy link
Contributor

@GalaxySnail GalaxySnail commented Jan 15, 2025

@GalaxySnail
Copy link
Contributor Author

I have no idea what's happening on macOS. Should we just skip it on macOS?

@Zheaoli
Copy link
Contributor

Zheaoli commented Jan 16, 2025

MSG_PEEK may got different action on macOS/FreeBSD, let's figure it out.

BTW, we can skip the macOS test first

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also have a test that combine both address and flags as well for send_fds please? and see if we can have test coverage for Windows (just using a 0 flag for instance would be sufficient).

According to https://docs.python.org/3/library/socket.html#socket.send_fds, send_fds and recv_fds are is available on Windows and Unix, so we should be able to have tests for them as well.

@picnixz picnixz changed the title gh-128881: Fix address and flags for send_fds/recv_fds gh-128881: Do not ignore address and flags parameters in socket.{send,recv}_fds Jan 17, 2025
@GalaxySnail
Copy link
Contributor Author

GalaxySnail commented Jun 2, 2025

I found that FreeBSD doesn't seem to support receiving only 1 file descriptor with recvmsg. Using CMSG_LEN(sizeof(int)) leads to MSG_CTRUNC, while CMSG_LEN(2 * sizeof(int)) is required to successfully receive a single file descriptor.

Here is a POC: https://fars.ee/Ih-8/c

$ curl https://fars.ee/Ih-8 -o foo.c
$ cc -Wall -Wextra -Wpedantic -std=c17 foo.c && truss -o out.txt ./a.out && ggrep -E 'sendmsg|recvmsg' out.txt
sendmsg(3,{NULL,0,[{"Hello world!",12}],1,{{level=SOL_SOCKET,type=SCM_RIGHTS,data={0x01,0x00,0x00,0x00}}},20,0},0) = 12 (0xc)
recvmsg(4,{NULL,0,[{"Hello world!",12}],1,{},0,MSG_CTRUNC},0) = 12 (0xc)
$ gsed -i 's|fds\[\] = {1}|fds[] = {1, 2}|' foo.c
$ cc -Wall -Wextra -Wpedantic -std=c17 foo.c && truss -o out.txt ./a.out && ggrep -E 'sendmsg|recvmsg' out.txt
sendmsg(3,{NULL,0,[{"Hello world!",12}],1,{{level=SOL_SOCKET,type=SCM_RIGHTS,data={0x01,0x00,0x00,0x00,0x02,0x00,0x00,0x00}}},24,0},0) = 12 (0xc)
recvmsg(4,{NULL,0,[{"Hello world!",12}],1,{{level=SOL_SOCKET,type=SCM_RIGHTS,data={0x05,0x00,0x00,0x00,0x06,0x00,0x00,0x00}}},24,0},0) = 12 (0xc)

@GalaxySnail GalaxySnail requested a review from picnixz June 2, 2025 08:18
@GalaxySnail GalaxySnail requested a review from picnixz June 2, 2025 13:41
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are close to the end!

Comment on lines +7344 to +7349
if sys.platform.startswith("freebsd"):
# FreeBSD requires at least CMSG_LEN(2*sizeof(int)),
# otherwise the access control message is truncated.
max_fds = 2
else:
max_fds = 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if sys.platform.startswith("freebsd"):
# FreeBSD requires at least CMSG_LEN(2*sizeof(int)),
# otherwise the access control message is truncated.
max_fds = 2
else:
max_fds = 1
# FreeBSD requires at least CMSG_LEN(2*sizeof(int)),
# otherwise the access control message is truncated.
max_fds = 2 if sys.platform.startswith("freebsd") else 1

@@ -579,7 +580,7 @@ def recv_fds(sock, bufsize, maxfds, flags=0):
# Array of ints
fds = array.array("i")
msg, ancdata, flags, addr = sock.recvmsg(bufsize,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename the result flags as msg_flags to be consistent with the signature (don't forget to update return ...)

Comment on lines +7337 to +7340
assert len(msg) < 512
os.write(wfd, msg)
data = os.read(rfd, 512)
self.assertEqual(data, msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert len(msg) < 512
os.write(wfd, msg)
data = os.read(rfd, 512)
self.assertEqual(data, msg)
os.write(wfd, msg)
data = os.read(rfd, len(msg))
self.assertEqual(data, msg)

I think we can do it like this right?

for index, fds in enumerate(pipes):
rfd, wfd = fds
os.write(wfd, str(index).encode())
for index, ((_, wfd), rfd) in enumerate(zip(pipes, fds2)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for index, ((_, wfd), rfd) in enumerate(zip(pipes, fds2)):
for index, ((_, wfd), rfd) in enumerate(zip(pipes, fds2, strict=True)):

Comment on lines +7414 to +7415
peek_len = len(MSG) // 2
msg, fds, flags, addr = socket.recv_fds(sock2, peek_len, 1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
peek_len = len(MSG) // 2
msg, fds, flags, addr = socket.recv_fds(sock2, peek_len, 1,
peek_len = len(MSG) // 2
self.assertGreater(peek_len, 0)
msg, fds, flags, addr = socket.recv_fds(sock2, peek_len, 1,

for _ in range(64 * 1024):
socket.send_fds(sock1, [MSG], [rfd], socket.MSG_DONTWAIT)

msg, fds, flags, addr = self._recv_one_fd(sock2, len(MSG))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also check flags below

self._test_pipe(fds[0], wfd, MSG)

@requireAttrs(socket, "MSG_DONTWAIT")
@unittest.skipIf(sys.platform in ("darwin",),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above, the test for sendmsg only enabled it for @skipWithClientIf(sys.platform not in {"linux", "android", "freebsd"}. I think it's better if we check this here as well because there might be more platforms than {"linux", "android", "freebsd", "darwin"} in the future.

Or: we change the test above with @skipWithClientIf(sys.platform not in ("darwin",) instead and add @requireAttrs(socket, "MSG_DONTWAIT")


def testSendAndRecvFds(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def testSendAndRecvFds(self):
def test_send_and_recv_fds(self):

self._test_pipe(fds[0], wfd, MSG)

@requireAttrs(socket, "MSG_PEEK")
@unittest.skipUnless(sys.platform in ("linux", "android"), "works on Linux")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm back here, but does it work on freebsd?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants