server: Fix undefined behavior in wl_socket_init_for_display_name
authorFergus Dall <sidereal@google.com>
Fri, 9 Jul 2021 08:04:27 +0000 (18:04 +1000)
committerDaniel Stone <daniels@collabora.com>
Wed, 21 Jul 2021 11:42:42 +0000 (11:42 +0000)
commitf6b78b76b2665598d495d3fe08dc34515405caa4
treece4f818323ce1b3ada9f89062d781e18587041ff
parent80164ef3005e8bb5f785082b97a75cab15444f82
server: Fix undefined behavior in wl_socket_init_for_display_name

This function constructs a socket path in sun_path using snprintf, which
returns the amount of space that would have been used if the buffer was
large enough. It then checks if this is larger then the actual buffer size
and, if so, returns ENAMETOOLONG. This is correct.

However, after calling snprintf and before checking that the length isn't too
long, it tries to compute a pointer to the part of the path that matches the
input name. It does this by adding the computed path length to the pointer to
the start of the path buffer, which will take it to one-past the null
terminator, and then walking backwards. If the path fits in the buffer, this
will take it at most one-past-the-end of the allocation, which is allowed, but
if the path is longer then the buffer then the pointer addition is undefined behavior.

Fix this by moving the display name computation past the check that the path
length is not too long.

This is detected by the test socket_path_overflow_server_create under ubsan.

Signed-off-by: Fergus Dall <sidereal@google.com>
src/wayland-server.c