Skip to content

Conversation

vuori
Copy link
Contributor

@vuori vuori commented Sep 15, 2025

This adds minimal support to the Lua plugin for cases where the incoming connection is through a Unix socket. Crashing issues are unlikely, but there are two type confusions where an AF_UNIX sockaddr will get treated as AF_INET6. (Technically if the source socket is unnamed, the result getpeername() could be no larger than sizeof(sa_family_t) as unix(7) states. The reality will likely be highly platform-dependent.)

Basically same deal as #12504

@shukitchan shukitchan self-requested a review September 15, 2025 12:52
@shukitchan shukitchan self-assigned this Sep 15, 2025
@shukitchan shukitchan added this to the 10.2.0 milestone Sep 15, 2025
@shukitchan
Copy link
Contributor

I will take a look at it this week

@shukitchan shukitchan requested a review from Copilot September 15, 2025 14:13
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds minimal support for Unix socket connections in the Lua plugin to prevent crashes when handling AF_UNIX socket addresses. The changes address type confusion issues where AF_UNIX sockaddr structures were being incorrectly treated as AF_INET6 addresses.

Key changes:

  • Adds AF_UNIX constant definition for Lua scripts
  • Implements proper handling of Unix socket addresses in client and server request functions
  • Initializes port variables to prevent undefined behavior when dealing with Unix sockets

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
plugins/lua/ts_lua_server_request.cc Adds TS_LUA_AF_UNIX global constant definition
plugins/lua/ts_lua_http.cc Adds AF_UNIX case handling in client address retrieval
plugins/lua/ts_lua_client_request.cc Adds AF_UNIX handling and fixes port initialization for Unix sockets
doc/admin-guide/plugins/lua.en.rst Documents the new TS_LUA_AF_UNIX constant
Comments suppressed due to low confidence (1)

plugins/lua/ts_lua_client_request.cc:807

  • This else clause will incorrectly treat AF_UNIX addresses as AF_INET6, potentially causing undefined behavior when casting sockaddr_un to sockaddr_in6. Add an explicit AF_INET6 check and handle AF_UNIX case separately.
    if (client_ip->sa_family == AF_INET) {
      port = ((struct sockaddr_in *)client_ip)->sin_port;
    } else {
      port = ((struct sockaddr_in6 *)client_ip)->sin6_port;
    }

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Contributor

@shukitchan shukitchan left a comment

Choose a reason for hiding this comment

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

I also think that we need to make changes in ts_lua_vconn.cc
ts_lua_vconn_get_remote_addr() is also not taking care of the situation correctly.

Thanks.


TS_LUA_AF_INET (2)
TS_LUA_AF_INET6 (10)
TS_LUA_AF_UNIX (1)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these variables are really used when we are making changes to the outgoing address. So there is no need to support AF_UNIX here

lua_setglobal(L, "TS_LUA_AF_INET6");

lua_pushinteger(L, AF_UNIX);
lua_setglobal(L, "TS_LUA_AF_UNIX");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these variables are really used when we are making changes to the outgoing address. So there is no need to support AF_UNIX here

@vuori
Copy link
Contributor Author

vuori commented Sep 19, 2025

ts_lua_vconn_get_remote_addr support was added.

(TS_LUA_)AF_UNIX is now returned as the third value from ts_lua_client_request_client_addr_get_addr ts_lua_vconn_get_remote_addr so the new constant is needed for comparisons in Lua code.

@shukitchan
Copy link
Contributor

Thanks.
I am still not seeing your changes on ts_lua_vconn_get_remote_addr
Perhaps there is some error with the branch?

@vuori
Copy link
Contributor Author

vuori commented Sep 19, 2025

Sorry, git error.

shukitchan
shukitchan previously approved these changes Sep 19, 2025
@shukitchan
Copy link
Contributor

On second thought, perhaps we should just take care of the rest consistently.

may be we should fix up ts_lua_server_request.cc as well.

  • There are places where we are retrieving the ip address or port of the next destination. Even though they are not going to be AF_UNIX, for completeness sake, we should just take care of handling only for AF_INET/AF_INET6 and use the default for anything else.
  • There are also places where we can set address in this file. It makes no sense for anything other than INET/INET6. So we can just error out gracefully in those places as well if invalid values are passed.

Thanks so much for your patience and help.

Copy link
Contributor

@shukitchan shukitchan left a comment

Choose a reason for hiding this comment

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

Please also consider my latest notes and perhaps to make further changes to ts_lua_server_request.cc

Thanks.

@vuori
Copy link
Contributor Author

vuori commented Sep 21, 2025

I'll try to look at this during the next week.

@shukitchan
Copy link
Contributor

@vuori are you still working on this? Thanks.

@vuori
Copy link
Contributor Author

vuori commented Oct 14, 2025

Hopefully yes sometime soonish, but the last weeks have been non-stop firefighting on other fronts and there are still a few blazes to put out this week.

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