DISPLAY starting with / or unix: is always a socket path

If DISPLAY starts with / or unix:, do not check for anything but a full
filesystem socket path.  In particular, abstract AF_UNIX sockets and TCP
sockets will not be used in this case.   Also be stricter about parsing
the screen part of /path.screen displays, and bail out after all stat()
errors other than ENOENT.

Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
This commit is contained in:
Demi Marie Obenour 2022-08-12 22:02:02 -04:00
parent ccdef1a8a5
commit 095255531b

View File

@ -95,22 +95,22 @@ static int _xcb_parse_display_path_to_socket(const char *name, char **host, char
size_t len; size_t len;
int _screen = 0; int _screen = 0;
if (name[0] != '/')
return 0;
len = strlen(name); len = strlen(name);
if (len >= sizeof(path)) if (len >= sizeof(path))
return 0; return 0;
memcpy(path, name, len + 1); memcpy(path, name, len + 1);
if (0 != stat(path, &sbuf)) { if (0 != stat(path, &sbuf)) {
char *dot = strrchr(path, '.'); unsigned long lscreen;
if (!dot) char *dot = strrchr(path, '.'), *endptr;
if (errno != ENOENT || !dot || dot[1] < '0' || dot[1] > '9')
return 0; return 0;
*dot = '\0'; *dot = '\0';
lscreen = strtoul(dot + 1, &endptr, 10);
if (lscreen > INT_MAX || !endptr || *endptr)
return 0;
if (0 != stat(path, &sbuf)) if (0 != stat(path, &sbuf))
return 0; return 0;
_screen = (int)lscreen;
_screen = atoi(dot + 1);
} }
if (host) { if (host) {
@ -149,8 +149,11 @@ static int _xcb_parse_display(const char *name, char **host, char **protocol,
return 0; return 0;
/* First check for <path to socket>[.<screen>] */ /* First check for <path to socket>[.<screen>] */
if (_xcb_parse_display_path_to_socket(name, host, protocol, displayp, screenp)) if (name[0] == '/')
return 1; return _xcb_parse_display_path_to_socket(name, host, protocol, displayp, screenp);
if (strncmp(name, "unix:", 5) == 0)
return _xcb_parse_display_path_to_socket(name + 5, host, protocol, displayp, screenp);
slash = strrchr(name, '/'); slash = strrchr(name, '/');
@ -235,8 +238,18 @@ static int _xcb_open(const char *host, char *protocol, const int display)
size_t filelen; size_t filelen;
char *file = NULL; char *file = NULL;
int actual_filelen; int actual_filelen;
struct stat sbuf;
if (protocol && strcmp("unix", protocol) == 0 && host && host[0] == '/') {
/* Full path to socket provided, ignore everything else */
filelen = strlen(host) + 1;
if (filelen > INT_MAX)
return -1;
file = malloc(filelen);
if (file == NULL)
return -1;
memcpy(file, host, filelen);
actual_filelen = (int)(filelen - 1);
} else {
/* If protocol or host is "unix", fall through to Unix socket code below */ /* If protocol or host is "unix", fall through to Unix socket code below */
if ((!protocol || (strcmp("unix",protocol) != 0)) && if ((!protocol || (strcmp("unix",protocol) != 0)) &&
(*host != '\0') && (strcmp("unix",host) != 0)) (*host != '\0') && (strcmp("unix",host) != 0))
@ -253,20 +266,17 @@ static int _xcb_open(const char *host, char *protocol, const int display)
{ {
const char *tsol_base = "/var/tsol/doors/.X11-unix/X"; const char *tsol_base = "/var/tsol/doors/.X11-unix/X";
char tsol_socket[PATH_MAX]; char tsol_socket[PATH_MAX];
struct stat sbuf;
snprintf(tsol_socket, sizeof(tsol_socket), "%s%d", tsol_base, display); snprintf(tsol_socket, sizeof(tsol_socket), "%s%d", tsol_base, display);
if (stat(tsol_socket, &sbuf) == 0) if (stat(tsol_socket, &sbuf) == 0)
base = tsol_base; base = tsol_base;
else if (errno != ENOENT)
return 0;
} }
#endif #endif
if (0 == stat(host, &sbuf)) {
file = strdup(host);
if(file == NULL)
return -1;
filelen = actual_filelen = strlen(file);
} else {
filelen = strlen(base) + 1 + sizeof(display) * 3 + 1; filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
file = malloc(filelen); file = malloc(filelen);
if(file == NULL) if(file == NULL)
@ -274,7 +284,6 @@ static int _xcb_open(const char *host, char *protocol, const int display)
/* display specifies Unix socket */ /* display specifies Unix socket */
actual_filelen = snprintf(file, filelen, "%s%d", base, display); actual_filelen = snprintf(file, filelen, "%s%d", base, display);
}
if(actual_filelen < 0) if(actual_filelen < 0)
{ {
@ -290,8 +299,8 @@ static int _xcb_open(const char *host, char *protocol, const int display)
free(file); free(file);
return fd; return fd;
} }
#endif #endif
}
fd = _xcb_open_unix(protocol, file); fd = _xcb_open_unix(protocol, file);
free(file); free(file);