Fix memory leak ambiguity with executable names

This commit is contained in:
Marc Di Luzio 2019-05-18 09:34:36 +01:00
parent 9db7442a31
commit 44ab695246

View File

@ -56,7 +56,7 @@ POSSIBILITY OF SUCH DAMAGE.
typedef struct GameModeClient { typedef struct GameModeClient {
pid_t pid; /**< Process ID */ pid_t pid; /**< Process ID */
struct GameModeClient *next; /**<Next client in the list */ struct GameModeClient *next; /**<Next client in the list */
char *executable; /**<Process executable */ char executable[PATH_MAX]; /**<Process executable */
} GameModeClient; } GameModeClient;
struct GameModeContext { struct GameModeContext {
@ -331,28 +331,34 @@ int game_mode_context_register(GameModeContext *self, pid_t client, pid_t reques
/* Construct a new client if we can */ /* Construct a new client if we can */
GameModeClient *cl = NULL; GameModeClient *cl = NULL;
char *executable = NULL; char *executable = NULL;
int err = -1;
/* Check our requester config first */ /* Check our requester config first */
if (requester != client) { if (requester != client) {
/* Lookup the executable first */ /* Lookup the executable first */
executable = game_mode_context_find_exe(requester); executable = game_mode_context_find_exe(requester);
if (!executable) { if (!executable) {
return -1; goto error_cleanup;
} }
/* Check our blacklist and whitelist */ /* Check our blacklist and whitelist */
if (!config_get_supervisor_whitelisted(self->config, executable)) { if (!config_get_supervisor_whitelisted(self->config, executable)) {
LOG_MSG("Supervisor [%s] was rejected (not in whitelist)\n", executable); LOG_MSG("Supervisor [%s] was rejected (not in whitelist)\n", executable);
free(executable); err = -2;
return -2; goto error_cleanup;
} else if (config_get_supervisor_blacklisted(self->config, executable)) { } else if (config_get_supervisor_blacklisted(self->config, executable)) {
LOG_MSG("Supervisor [%s] was rejected (in blacklist)\n", executable); LOG_MSG("Supervisor [%s] was rejected (in blacklist)\n", executable);
free(executable); err = -2;
return -2; goto error_cleanup;
} }
/* We're done with the requestor */
free(executable);
executable = NULL;
} else if (config_get_require_supervisor(self->config)) { } else if (config_get_require_supervisor(self->config)) {
LOG_ERROR("Direct request made but require_supervisor was set, rejecting request!\n"); LOG_ERROR("Direct request made but require_supervisor was set, rejecting request!\n");
return -2; err = -2;
goto error_cleanup;
} }
/* Cap the total number of active clients */ /* Cap the total number of active clients */
@ -392,10 +398,9 @@ int game_mode_context_register(GameModeContext *self, pid_t client, pid_t reques
/* From now on we depend on the client, initialize it */ /* From now on we depend on the client, initialize it */
cl = game_mode_client_new(client, executable); cl = game_mode_client_new(client, executable);
if (cl) if (!cl)
executable = NULL; // ownership has been delegated
else
goto error_cleanup; goto error_cleanup;
free(executable); /* we're now done with memory */
/* Begin a write lock now to insert our new client at list start */ /* Begin a write lock now to insert our new client at list start */
pthread_rwlock_wrlock(&self->rwlock); pthread_rwlock_wrlock(&self->rwlock);
@ -430,7 +435,7 @@ error_cleanup:
LOG_ERROR("Failed to register client [%d]: %s\n", client, strerror(errno)); LOG_ERROR("Failed to register client [%d]: %s\n", client, strerror(errno));
free(executable); free(executable);
game_mode_client_free(cl); game_mode_client_free(cl);
return -1; return err;
} }
int game_mode_context_unregister(GameModeContext *self, pid_t client, pid_t requester) int game_mode_context_unregister(GameModeContext *self, pid_t client, pid_t requester)
@ -580,7 +585,6 @@ int game_mode_context_query_status(GameModeContext *self, pid_t client, pid_t re
static GameModeClient *game_mode_client_new(pid_t pid, char *executable) static GameModeClient *game_mode_client_new(pid_t pid, char *executable)
{ {
GameModeClient c = { GameModeClient c = {
.executable = executable,
.next = NULL, .next = NULL,
.pid = pid, .pid = pid,
}; };
@ -591,6 +595,7 @@ static GameModeClient *game_mode_client_new(pid_t pid, char *executable)
return NULL; return NULL;
} }
*ret = c; *ret = c;
strncpy(ret->executable, executable, PATH_MAX);
return ret; return ret;
} }
@ -605,9 +610,6 @@ static void game_mode_client_free(GameModeClient *client)
if (client->next) { if (client->next) {
game_mode_client_free(client->next); game_mode_client_free(client->next);
} }
if (client->executable) {
free(client->executable);
}
free(client); free(client);
} }