security-context: avoid UB in C macro (#8229)

to safely use wl_container_of with a class the class has to be no
virtual functions, no inheritance, and uniform access control (e.g all
public)

work around this by putting this into a destroywrapper struct.
This commit is contained in:
Tom Englund 2024-10-25 12:26:48 +02:00 committed by GitHub
parent 3cec45d821
commit f0e023bff2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 7 deletions

View file

@ -22,7 +22,8 @@ SP<CSecurityContextSandboxedClient> CSecurityContextSandboxedClient::create(int
}
static void onSecurityContextClientDestroy(wl_listener* l, void* d) {
CSecurityContextSandboxedClient* client = wl_container_of(l, client, destroyListener);
CSecurityContextSandboxedClientDestroyWrapper* wrap = wl_container_of(l, wrap, listener);
CSecurityContextSandboxedClient* client = wrap->parent;
client->onDestroy();
}
@ -31,12 +32,15 @@ CSecurityContextSandboxedClient::CSecurityContextSandboxedClient(int clientFD_)
if (!client)
return;
destroyListener.notify = ::onSecurityContextClientDestroy;
wl_client_add_destroy_late_listener(client, &destroyListener);
wl_list_init(&destroyListener.listener.link);
destroyListener.listener.notify = ::onSecurityContextClientDestroy;
destroyListener.parent = this;
wl_client_add_destroy_late_listener(client, &destroyListener.listener);
}
CSecurityContextSandboxedClient::~CSecurityContextSandboxedClient() {
wl_list_remove(&destroyListener.link);
wl_list_remove(&destroyListener.listener.link);
wl_list_init(&destroyListener.listener.link);
close(clientFD);
}

View file

@ -37,6 +37,12 @@ class CSecurityContextManagerResource {
SP<CWpSecurityContextManagerV1> resource;
};
class CSecurityContextSandboxedClient;
struct CSecurityContextSandboxedClientDestroyWrapper {
wl_listener listener;
CSecurityContextSandboxedClient* parent = nullptr;
};
class CSecurityContextSandboxedClient {
public:
static SP<CSecurityContextSandboxedClient> create(int clientFD);
@ -44,7 +50,7 @@ class CSecurityContextSandboxedClient {
void onDestroy();
wl_listener destroyListener;
CSecurityContextSandboxedClientDestroyWrapper destroyListener;
private:
CSecurityContextSandboxedClient(int clientFD_);