Skip to content

Commit

Permalink
app-info/host: Only use the cgroup app id if we have a matching GAppInfo
Browse files Browse the repository at this point in the history
This should prevent cases where the connection is from something that's
not actually an app but follows the systemd cgroup standard.

This is safe because
* it's a host app anyway
* we always use an empty app id when x-d-p is built without systemd
* it's the default when the systemd cgroup standard is not followed
  • Loading branch information
swick committed Dec 16, 2024
1 parent a3c7ed1 commit f0ae755
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/xdp-app-info-host.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,16 @@ _xdp_app_info_host_parse_app_id_from_unit_name (const char *unit)
}
#endif /* HAVE_LIBSYSTEMD */

static char *
get_appid_from_pid (pid_t pid)
static gboolean
get_app_from_pid (pid_t pid,
char **app_id_out,
GAppInfo **app_info_out)
{
#ifdef HAVE_LIBSYSTEMD
g_autofree char *unit = NULL;
g_autofree char *desktop_id = NULL;
g_autofree char *app_id = NULL;
g_autoptr(GDesktopAppInfo) desktop_app_info = NULL;
int res;

res = sd_pid_get_user_unit (pid, &unit);
Expand All @@ -173,13 +178,22 @@ get_appid_from_pid (pid_t pid)
* desktop environment (e.g. it's a script run from terminal).
*/
if (res == -ENODATA || res < 0 || !unit || !g_str_has_prefix (unit, "app-"))
return g_strdup ("");
return FALSE;

app_id = _xdp_app_info_host_parse_app_id_from_unit_name (unit);

desktop_id = g_strconcat (app_id, ".desktop", NULL);
desktop_app_info = g_desktop_app_info_new (desktop_id);

return _xdp_app_info_host_parse_app_id_from_unit_name (unit);
if (!desktop_app_info)
return FALSE;

*app_id_out = g_steal_pointer (&app_id);
*app_info_out = G_APP_INFO (g_steal_pointer (&desktop_app_info));
return TRUE;

#else
/* FIXME: we should return NULL and handle id==NULL at callers */
return g_strdup ("");
return FALSE;
#endif /* HAVE_LIBSYSTEMD */
}

Expand All @@ -189,13 +203,10 @@ xdp_app_info_host_new (int pid,
{
g_autoptr (XdpAppInfoHost) app_info_host = NULL;
g_autofree char *appid = NULL;
g_autofree char *desktop_id = NULL;
g_autoptr(GAppInfo) gappinfo = NULL;

appid = get_appid_from_pid (pid);

desktop_id = g_strconcat (appid, ".desktop", NULL);
gappinfo = G_APP_INFO (g_desktop_app_info_new (desktop_id));
if (!get_app_from_pid (pid, &appid, &gappinfo))
appid = g_strdup ("");

app_info_host = g_object_new (XDP_TYPE_APP_INFO_HOST, NULL);
xdp_app_info_initialize (XDP_APP_INFO (app_info_host),
Expand Down

0 comments on commit f0ae755

Please sign in to comment.