Segmentation fault when binding to multiple addresses in peers section
Detailed Description of the Problem
HAProxy terminates with an error due to a segmentation fault when specifying multiple addresses in the bind directive in the peers section, for example:
peers example
bind ipv4@:10000,ipv6@:10000 v6only
cfg_parse_peers calls str2listener and str2listener calls create_listeners for every entry in the list. In case of the example, ipv4@:10000 and ipv6@:10000. However, create_listeners does not set accept in struct listener. Therefore, accept is NULL for all listeners created by create_listeners and, therefore, str2listeners. Then, cfg_parse_peers selects the last of the listeners and only sets accept to session_accept_fd for this listener. accept remains NULL for all other listeners. Later, when a connection arrives on the socket of a listener where accept is NULL and listener_accept tries to call accept, a NULL pointer is dereferenced and a segmentation fault occurs, for example, in HAProxy 2.6.9 in line 1201 in listener.c:
ret = l->accept(cli_conn);
Expected Behavior
HAProxy should either be able to listen on multiple addresses or reject a bind directive in the peers section with multiple addresses.
Steps to Reproduce the Behavior
- Create a HAProxy configuration with the following peers section:
peers example
bind ipv4@:10000,ipv6@:10000 v6only
- Start HAProxy.
- Connect to localhost port 10000 over IPv4.
An alternative would be to listen on multiple IPv4 or IPv6 addresses or ports and to connect to any but the last in the address list.
Do you have any idea what may have caused this?
A full description of the control flow that causes the problem is provided in the problem description. If it is not detailed enough, a full control flow trace can be provided for a specific HAProxy version.
Do you have an idea how to solve the issue?
Yes, I have an idea how to solve the problem. I can provide a patch and create a pull request for either possibility that is described in the expected behaviour. I just need a decision about what is the expected behaviour. From my perspective HAProxy should be able to listen on multiple addresses for peer connections. However, I also realize that this might introduce concurrency aspects and perhaps this part of HAProxy is to be kept as simple as possible.
What is your configuration?
The actual configuration is not important. As mentioned, the relevant section is:
peers example
bind ipv4@:10000,ipv6@:10000 v6only
Output of haproxy -vv
HAProxy 2.6.9 and HAProxy 2.4.22
I can provide further details in private correspondence. However, the exact version and build parameters are not important. See the description of control flow.
Last Outputs and Backtraces
I have diagnosed the issue myself but if absolutely necessary can provide a backtrace and coredump in private correspondence. However, I don't think that it would add additional information.
Additional Information
No response
Reproduced in 2.6 and 2.7, but not in master. It seems that ->accept struct listener callback has been moved to bind_conf struct between these versions. There are chances that these modifications have silently fixed some issues. See https://github.com/haproxy/haproxy/commit/3083615410849c8fd109abb61e1c3e26f59c75be
I've checked and, contrary to what the configuration state, it seems it is unsupported to have several listeners for a peers section. If you defined several bind directives, an error is triggered during the configuration parsing. But when several addresses are defined on the same bind line, only the last listener is properly configured because it is unexpected (in the code) to have more than one listener.
I don't know if it could be fixed or if the documentation must be amended.
@EmericBr, @haproxyFred, could you confirm my analysis is correct ?
I confirm the same issue on all stable versions.
You are correct. Not sure this issue may be easily fixed. So, the documentation should be amended.
Copy that ! thanks @haproxyFred
Should be fixed. It may be backported with other fixes. Thanks !
For 2.2 and 2.0, the doc was fixed but the fix about the peers code won't be backported.