summaryrefslogtreecommitdiff
path: root/telepathy-glib/util.c
diff options
context:
space:
mode:
authorSimon McVittie <simon.mcvittie@collabora.co.uk>2013-08-20 20:42:10 +0100
committerSimon McVittie <simon.mcvittie@collabora.co.uk>2013-09-06 15:49:02 +0100
commitf4076dffc10600006dd6415c3584f97bd36907a6 (patch)
tree7657b2f4113f4db0a5adfbc2939a082adbc8f5f1 /telepathy-glib/util.c
parent21b395c356549d821b30253f6ec5be8dbd15e333 (diff)
downloadtelepathy-glib-f4076dffc10600006dd6415c3584f97bd36907a6.tar.gz
_tp_create_temp_unix_socket: avoid using tmpnam()
On current Debian unstable, gcc/ld issues a warning about tmpnam(), because it's usually used in an unsafe way. "gcc -Wl,--fatal-warnings" (which I'm using in my development environment) upgrades that to fatal. Our usage was in fact safe (trying to listen on a socket always behaves like O_EXCL|O_CREAT, which can DoS'd but is not subject to symlink attacks), but we're swimming against the current by trying to use tmpnam(). Instead, create a secure private temporary directory with g_dir_make_tmp(), and put our socket in there. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68350 Reviewed-by: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
Diffstat (limited to 'telepathy-glib/util.c')
-rw-r--r--telepathy-glib/util.c51
1 files changed, 34 insertions, 17 deletions
diff --git a/telepathy-glib/util.c b/telepathy-glib/util.c
index 91a42d5a0..c89bc66d7 100644
--- a/telepathy-glib/util.c
+++ b/telepathy-glib/util.c
@@ -31,6 +31,7 @@
#include "config.h"
+#include <glib/gstdio.h>
#include <gobject/gvaluecollector.h>
#ifdef HAVE_GIO_UNIX
@@ -43,6 +44,7 @@
#include <telepathy-glib/util-internal.h>
#include <telepathy-glib/util.h>
+#include <errno.h>
#include <stdio.h>
#include <string.h>
@@ -1563,32 +1565,47 @@ _tp_quark_array_merge_valist (GArray *array,
#ifdef HAVE_GIO_UNIX
GSocketAddress *
_tp_create_temp_unix_socket (GSocketService *service,
+ gchar **tmpdir,
GError **error)
{
- guint i;
GSocketAddress *address;
+ gchar *dir = g_dir_make_tmp ("tp-glib-socket.XXXXXX", error);
+ gchar *name;
+
+ if (dir == NULL)
+ return NULL;
- /* why doesn't GIO provide a method to create a socket we don't
- * care about? Iterate until we find a valid temporary name.
- *
- * Try a maximum of 10 times to get a socket */
- for (i = 0; i < 10; i++)
+ if (g_chmod (dir, 0700) != 0)
{
- address = g_unix_socket_address_new (tmpnam (NULL));
+ int e = errno;
- g_clear_error (error);
+ g_set_error (error, G_IO_ERROR, g_io_error_from_errno (e),
+ "unable to set permissions of %s to 0700: %s", dir,
+ g_strerror (e));
+ g_free (dir);
+ return NULL;
+ }
- if (g_socket_listener_add_address (
- G_SOCKET_LISTENER (service),
- address, G_SOCKET_TYPE_STREAM,
- G_SOCKET_PROTOCOL_DEFAULT,
- NULL, NULL, error))
- return address;
- else
- g_object_unref (address);
+ name = g_build_filename (dir, "s", NULL);
+ address = g_unix_socket_address_new (name);
+ g_free (name);
+
+ if (!g_socket_listener_add_address (G_SOCKET_LISTENER (service),
+ address, G_SOCKET_TYPE_STREAM,
+ G_SOCKET_PROTOCOL_DEFAULT,
+ NULL, NULL, error))
+ {
+ g_object_unref (address);
+ g_free (dir);
+ return NULL;
}
- return NULL;
+ if (tmpdir != NULL)
+ *tmpdir = dir;
+ else
+ g_free (dir);
+
+ return address;
}
#endif /* HAVE_GIO_UNIX */