From 05cb22908236da54ccf1fa8d04c5ec7860f4d08c Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sun, 15 Mar 2020 18:10:53 +0000 Subject: [PATCH] GCredentials: Add the concept of credentials that lack the process ID struct xucred on macOS doesn't have the process ID, only the user ID and groups. Signed-off-by: Simon McVittie --- gio/gcredentialsprivate.h | 12 ++++++++++++ gio/tests/credentials.c | 9 ++++++++- gio/tests/gdbus-peer.c | 25 +++++++++++++++++++------ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/gio/gcredentialsprivate.h b/gio/gcredentialsprivate.h index e9ec09b9f..6d7284bc7 100644 --- a/gio/gcredentialsprivate.h +++ b/gio/gcredentialsprivate.h @@ -93,6 +93,13 @@ */ #undef G_CREDENTIALS_PREFER_MESSAGE_PASSING +/* + * G_CREDENTIALS_HAS_PID: + * + * Defined to 1 if the %G_CREDENTIALS_NATIVE_TYPE contains the process ID. + */ +#undef G_CREDENTIALS_HAS_PID + #ifdef __linux__ #define G_CREDENTIALS_SUPPORTED 1 #define G_CREDENTIALS_USE_LINUX_UCRED 1 @@ -101,6 +108,7 @@ #define G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1 #define G_CREDENTIALS_SOCKET_GET_CREDENTIALS_SUPPORTED 1 #define G_CREDENTIALS_SPOOFING_SUPPORTED 1 +#define G_CREDENTIALS_HAS_PID 1 #elif defined(__FreeBSD__) || \ defined(__FreeBSD_kernel__) /* Debian GNU/kFreeBSD */ || \ @@ -118,6 +126,7 @@ * SCM_CREDS, and if we implement getpeereid() in future, we should * do the same. */ #define G_CREDENTIALS_PREFER_MESSAGE_PASSING 1 +#define G_CREDENTIALS_HAS_PID 1 #elif defined(__NetBSD__) #define G_CREDENTIALS_SUPPORTED 1 @@ -126,6 +135,7 @@ #define G_CREDENTIALS_NATIVE_SIZE (sizeof (struct unpcbid)) /* #undef G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED */ #define G_CREDENTIALS_SPOOFING_SUPPORTED 1 +#define G_CREDENTIALS_HAS_PID 1 #elif defined(__OpenBSD__) #define G_CREDENTIALS_SUPPORTED 1 @@ -134,6 +144,7 @@ #define G_CREDENTIALS_NATIVE_SIZE (sizeof (struct sockpeercred)) #define G_CREDENTIALS_SOCKET_GET_CREDENTIALS_SUPPORTED 1 #define G_CREDENTIALS_SPOOFING_SUPPORTED 1 +#define G_CREDENTIALS_HAS_PID 1 #elif defined(__sun__) || defined(__illumos__) || defined (__OpenSolaris_kernel__) #include @@ -143,6 +154,7 @@ #define G_CREDENTIALS_NATIVE_SIZE (ucred_size ()) #define G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1 #define G_CREDENTIALS_SOCKET_GET_CREDENTIALS_SUPPORTED 1 +#define G_CREDENTIALS_HAS_PID 1 #endif diff --git a/gio/tests/credentials.c b/gio/tests/credentials.c index 6d7609964..98acb5602 100644 --- a/gio/tests/credentials.c +++ b/gio/tests/credentials.c @@ -54,9 +54,16 @@ test_basic (void) g_assert_cmpuint (g_credentials_get_unix_user (creds, &error), ==, geteuid ()); g_assert_no_error (error); - g_assert_cmpuint (g_credentials_get_unix_pid (creds, &error), ==, + +#if G_CREDENTIALS_HAS_PID + g_assert_cmpint (g_credentials_get_unix_pid (creds, &error), ==, getpid ()); g_assert_no_error (error); +#else + g_assert_cmpint (g_credentials_get_unix_pid (creds, &error), ==, -1); + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED); + g_clear_error (&error); +#endif set = g_credentials_set_unix_user (other, not_me, &error); #if G_CREDENTIALS_SPOOFING_SUPPORTED diff --git a/gio/tests/gdbus-peer.c b/gio/tests/gdbus-peer.c index d01bc9a7a..7ddfdfc7a 100644 --- a/gio/tests/gdbus-peer.c +++ b/gio/tests/gdbus-peer.c @@ -344,7 +344,7 @@ on_new_connection (GDBusServer *server, gpointer user_data) { PeerData *data = user_data; - GError *error; + GError *error = NULL; guint reg_id; //g_printerr ("Client connected.\n" @@ -362,13 +362,19 @@ on_new_connection (GDBusServer *server, g_assert (credentials != NULL); g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==, getuid ()); - g_assert_cmpuint (g_credentials_get_unix_pid (credentials, NULL), ==, - getpid ()); +#if G_CREDENTIALS_HAS_PID + g_assert_cmpint (g_credentials_get_unix_pid (credentials, &error), ==, + getpid ()); + g_assert_no_error (error); +#else + g_assert_cmpint (g_credentials_get_unix_pid (credentials, &error), ==, -1); + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED); + g_clear_error (&error); +#endif } #endif /* export object on the newly established connection */ - error = NULL; reg_id = g_dbus_connection_register_object (connection, "/org/gtk/GDBus/PeerTestObject", test_interface_introspection_data, @@ -922,8 +928,15 @@ do_test_peer (void) g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==, getuid ()); - g_assert_cmpuint (g_credentials_get_unix_pid (credentials, NULL), ==, - getpid ()); +#if G_CREDENTIALS_HAS_PID + g_assert_cmpint (g_credentials_get_unix_pid (credentials, &error), ==, + getpid ()); + g_assert_no_error (error); +#else + g_assert_cmpint (g_credentials_get_unix_pid (credentials, &error), ==, -1); + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED); + g_clear_error (&error); +#endif g_object_unref (credentials); #else g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);