Skip to content

Commit

Permalink
Merge pull request clementgallet#577 from CasualPokePlayer/xconnect_w…
Browse files Browse the repository at this point in the history
…hitelist

Allow inet socket connections for XOpenDisplay/xcb_connect and native calls
  • Loading branch information
clementgallet authored Oct 22, 2023
2 parents 86686a3 + 28e4eee commit 1f4f3d7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/library/socketwrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "socketwrappers.h"
#include "logging.h"
#include "global.h"
#include "GlobalState.h"

#include <sys/socket.h>
#include <errno.h>
Expand All @@ -31,16 +32,33 @@ DEFINE_ORIG_POINTER(socket)
/* Override */ int socket (int domain, int type, int protocol) __THROW
{
DEBUGLOGCALL(LCF_SOCKET);
LINK_NAMESPACE_GLOBAL(socket);

/* Passthrough socket call if this is a native call (e.g. ALSA init) or our own code (e.g. X connections) */
if (GlobalState::isNative() || GlobalState::isOwnCode()) {
return orig::socket(domain, type, protocol);
}

/* Deny internet access */
if (domain == AF_INET || domain == AF_INET6) {
/* HACK: ALSA might use PulseAudio for host audio playback, for e.g. WSL.
* PulseAudio might then proceed to call socket with AF_INET on a new thread
* We need to allow this connection, otherwise ALSA init will fail.
* We also can't mark PulseAudio's thread with pthread_setname_np,
* as PulseAudio will bypass that with prctl (a variadic function!) */
char thread_name[16];
if (pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name)) == 0) {
if (strcmp(thread_name, "threaded-ml") == 0) {
return orig::socket(domain, type, protocol);
}
}

if (!(Global::shared_config.debug_state & SharedConfig::DEBUG_NATIVE_INET)) {
errno = EACCES;
return -1;
}
}

LINK_NAMESPACE_GLOBAL(socket);
return orig::socket(domain, type, protocol);
}

Expand Down
9 changes: 8 additions & 1 deletion src/library/xcb/xcbconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "../hook.h"
#include "../logging.h"
#include "XcbEventQueueList.h"
#include "../GlobalState.h"

namespace libtas {

Expand All @@ -34,7 +35,13 @@ xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
DEBUGLOGCALL(LCF_WINDOW);
LINK_NAMESPACE_GLOBAL(xcb_connect);

xcb_connection_t* c = orig::xcb_connect(displayname, screenp);
xcb_connection_t* c;
OWNCALL(c = orig::xcb_connect(displayname, screenp));

if (!c) {
debuglogstdio(LCF_WINDOW | LCF_ERROR, "Could not open xcb connection to %s (%d)", displayname ? displayname : "$DISPLAY", screenp ? *screenp : 0);
return nullptr;
}

int i;
for (i=0; i<GAMECONNECTIONNUM; i++) {
Expand Down
9 changes: 8 additions & 1 deletion src/library/xlib/xdisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "xatom.h"
#include "XlibEventQueueList.h"
#include "../global.h"
#include "../GlobalState.h"

namespace libtas {

Expand All @@ -38,7 +39,13 @@ Display *XOpenDisplay(const char *display_name)
DEBUGLOGCALL(LCF_WINDOW);
LINK_NAMESPACE_GLOBAL(XOpenDisplay);

Display* display = orig::XOpenDisplay(display_name);
Display* display;
OWNCALL(display = orig::XOpenDisplay(display_name));

if (!display) {
debuglogstdio(LCF_WINDOW | LCF_ERROR, "Could not open X connection to %s", display_name ? display_name : "$DISPLAY");
return nullptr;
}

int i;
for (i=0; i<GAMEDISPLAYNUM; i++) {
Expand Down

0 comments on commit 1f4f3d7

Please sign in to comment.