Skip to content
Snippets Groups Projects
Commit 9307dcd3 authored by Simon McVittie's avatar Simon McVittie
Browse files

utils: Add pv_get_random_uuid()


This will be used to generate securely-unique socket names that are not
an opportunity for denial of service.

Signed-off-by: default avatarSimon McVittie <smcv@collabora.com>
parent 797a2de2
No related branches found
No related tags found
No related merge requests found
......@@ -544,3 +544,34 @@ pv_async_signal_safe_error (const char *message,
_exit (exit_status);
}
#define PROC_SYS_KERNEL_RANDOM_UUID "/proc/sys/kernel/random/uuid"
/**
* pv_get_random_uuid:
* @error: Used to raise an error on failure
*
* Return a random UUID (RFC 4122 version 4) as a string.
* It is a 128-bit quantity, with 122 bits of entropy, and 6 fixed bits
* indicating the "variant" (type, 0b10) and "version" (subtype, 0b0100).
*
* Returns: (transfer full): A random UUID, or %NULL on error
*/
gchar *
pv_get_random_uuid (GError **error)
{
g_autofree gchar *contents = NULL;
if (!g_file_get_contents (PROC_SYS_KERNEL_RANDOM_UUID,
&contents, NULL, error))
return NULL;
g_strchomp (contents); /* delete trailing newline */
/* Format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx */
if (strlen (contents) != 36)
return glnx_null_throw (error, "%s not in expected format",
PROC_SYS_KERNEL_RANDOM_UUID);
return g_steal_pointer (&contents);
}
......@@ -58,3 +58,5 @@ FILE *pv_divert_stdout_to_stderr (GError **error);
void pv_async_signal_safe_error (const char *message,
int exit_status) G_GNUC_NORETURN;
gchar *pv_get_random_uuid (GError **error);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment