Skip to content

ext/sockets: follow-up on AF_PACKET support. #17657

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

Closed
wants to merge 13 commits into from

Conversation

devnexen
Copy link
Member

support from socket_recvfrom and exposing basic PHP userland type.

@devnexen devnexen force-pushed the socket_ether_linux2 branch 6 times, most recently from ab4ee3d to d4a18c7 Compare February 9, 2025 23:16
@devnexen devnexen force-pushed the socket_ether_linux2 branch 3 times, most recently from 08f7cff to fb25000 Compare February 11, 2025 21:31
@devnexen devnexen marked this pull request as ready for review February 11, 2025 22:14
@devnexen devnexen requested a review from kocsismate as a code owner February 11, 2025 22:14
@devnexen devnexen force-pushed the socket_ether_linux2 branch from fb25000 to d6ac80e Compare February 20, 2025 22:37
@devnexen
Copy link
Member Author

cc @arnaud-lb as you reviewed part 1

Copy link
Member

@arnaud-lb arnaud-lb left a comment

Choose a reason for hiding this comment

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

Looks good to me, but I think that unsupported socket types should error, until we support them.

My suggestion comments are only nice to haves and can be separate PRs if you agree with them.

Comment on lines +2166 to +2169
final class SocketEthernetInfo
{
/** @readonly **/
public Socket $socket;
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: Add a constructor taking a raw packet as argument, so that people can create their own instances of this class. This would be useful at least in tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I planned this at some point.


if (UNEXPECTED(!if_indextoname(sll.sll_ifindex, ifrname))) {
PHP_SOCKET_ERROR(php_sock, "unable to get the interface name", errno);
zend_string_efree(recv_buf);
RETURN_FALSE;
}

ZEND_TRY_ASSIGN_REF_NEW_STR(arg2, recv_buf);
struct ethhdr *e = (struct ethhdr *)ZSTR_VAL(recv_buf);
Copy link
Member

Choose a reason for hiding this comment

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

My understanding is that this is only valid for SOCK_RAW packets (not SOCK_DGRAM). As only SOCK_RAW is supported yet, we should error when an other socket type is passed, until we add support for them.

Copy link
Member Author

Choose a reason for hiding this comment

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

agreed

@@ -2156,3 +2161,19 @@ function socket_wsaprotocol_info_import(string $info_id): Socket|false {}

function socket_wsaprotocol_info_release(string $info_id): bool {}
#endif

#ifdef AF_PACKET
final class SocketEthernetInfo
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: In order to reduce the overhead of converting the raw packet to an object, we could make all the props virtual and store the raw packet directly in zend_object (in place of properties_table). Additionally we could expose the raw packet as a property. We could even reuse SocketEthernetInfo instances if we let users pass an instance as argument.

Copy link
Member Author

Choose a reason for hiding this comment

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

I ll see what I can do in a following PR

Comment on lines +2176 to +2177
/** @readonly **/
public array $payload;
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: Expose the payload as a raw string, and expose the parsed payload separately as an object (null when we don't support the protocol). The object would be instantiated on the fly, too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok as object, not sure of the value of raw payload but I can always add it sure.

Copy link
Member

Choose a reason for hiding this comment

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

The raw payload would be useful when the application wants to log packets, or forward them without changing. It's also useful when the object is null due to not being a supported protocol. Also, having the raw payload (always) and the object (when supported) is forward compatible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I see. I can get behind it indeed then.

@devnexen devnexen force-pushed the socket_ether_linux2 branch from d6ac80e to d59fdaa Compare February 24, 2025 19:08
@devnexen devnexen force-pushed the socket_ether_linux2 branch from d59fdaa to da14f3e Compare February 24, 2025 19:14
@devnexen devnexen closed this in de018aa Feb 24, 2025
@nielsdos
Copy link
Member

There's a nightly failure on master+alpine caused by this, pls take a look.

#ifdef AF_PACKET
final class SocketEthernetInfo
{
/** @readonly **/
Copy link
Member

Choose a reason for hiding this comment

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

Why did you use the @readonly annotation instead of real readonly properties?

Copy link
Member Author

Choose a reason for hiding this comment

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

This object will be rewritten in a following PR anyway.

@devnexen
Copy link
Member Author

ok looking

@devnexen
Copy link
Member Author

cannot reproduce locally the UAF, will revert to unblock the situation.

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