Blame SOURCES/0001-credentials-Invalid-Linux-struct-ucred-means-no-info.patch

475f90
From 1485a97d8051b0aa047987f7b0c0bfe4ba4ce55b Mon Sep 17 00:00:00 2001
475f90
From: Simon McVittie <smcv@collabora.com>
475f90
Date: Fri, 18 Oct 2019 10:55:09 +0100
475f90
Subject: [PATCH] credentials: Invalid Linux struct ucred means "no
475f90
 information"
475f90
475f90
On Linux, if getsockopt SO_PEERCRED is used on a TCP socket, one
475f90
might expect it to fail with an appropriate error like ENOTSUP or
475f90
EPROTONOSUPPORT. However, it appears that in fact it succeeds, but
475f90
yields a credentials structure with pid 0, uid -1 and gid -1. These
475f90
are not real process, user and group IDs that can be allocated to a
475f90
real process (pid 0 needs to be reserved to give kill(0) its documented
475f90
special semantics, and similarly uid and gid -1 need to be reserved for
475f90
setresuid() and setresgid()) so it is not meaningful to signal them to
475f90
high-level API users.
475f90
475f90
An API user with Linux-specific knowledge can still inspect these fields
475f90
via g_credentials_get_native() if desired.
475f90
475f90
Similarly, if SO_PASSCRED is used to receive a SCM_CREDENTIALS message
475f90
on a receiving Unix socket, but the sending socket had not enabled
475f90
SO_PASSCRED at the time that the message was sent, it is possible
475f90
for it to succeed but yield a credentials structure with pid 0, uid
475f90
/proc/sys/kernel/overflowuid and gid /proc/sys/kernel/overflowgid. Even
475f90
if we were to read those pseudo-files, we cannot distinguish between
475f90
the overflow IDs and a real process that legitimately has the same IDs
475f90
(typically they are set to 'nobody' and 'nogroup', which can be used
475f90
by a real process), so we detect this situation by noticing that
475f90
pid == 0, and to save syscalls we do not read the overflow IDs from
475f90
/proc at all.
475f90
475f90
This results in a small API change: g_credentials_is_same_user() now
475f90
returns FALSE if we compare two credentials structures that are both
475f90
invalid. This seems like reasonable, conservative behaviour: if we cannot
475f90
prove that they are the same user, we should assume they are not.
475f90
475f90
Signed-off-by: Simon McVittie <smcv@collabora.com>
475f90
---
475f90
 gio/gcredentials.c | 42 +++++++++++++++++++++++++++++++++++++++---
475f90
 1 file changed, 39 insertions(+), 3 deletions(-)
475f90
475f90
diff --git a/gio/gcredentials.c b/gio/gcredentials.c
475f90
index c350e3c88..c4794ded7 100644
475f90
--- a/gio/gcredentials.c
475f90
+++ b/gio/gcredentials.c
475f90
@@ -265,6 +265,35 @@ g_credentials_to_string (GCredentials *credentials)
475f90
 
475f90
 /* ---------------------------------------------------------------------------------------------------- */
475f90
 
475f90
+#if G_CREDENTIALS_USE_LINUX_UCRED
475f90
+/*
475f90
+ * Check whether @native contains invalid data. If getsockopt SO_PEERCRED
475f90
+ * is used on a TCP socket, it succeeds but yields a credentials structure
475f90
+ * with pid 0, uid -1 and gid -1. Similarly, if SO_PASSCRED is used on a
475f90
+ * receiving Unix socket when the sending socket did not also enable
475f90
+ * SO_PASSCRED, it can succeed but yield a credentials structure with
475f90
+ * pid 0, uid /proc/sys/kernel/overflowuid and gid
475f90
+ * /proc/sys/kernel/overflowgid.
475f90
+ */
475f90
+static gboolean
475f90
+linux_ucred_check_valid (struct ucred  *native,
475f90
+                         GError       **error)
475f90
+{
475f90
+  if (native->pid == 0
475f90
+      || native->uid == -1
475f90
+      || native->gid == -1)
475f90
+    {
475f90
+      g_set_error_literal (error,
475f90
+                           G_IO_ERROR,
475f90
+                           G_IO_ERROR_INVALID_DATA,
475f90
+                           _("GCredentials contains invalid data"));
475f90
+      return FALSE;
475f90
+    }
475f90
+
475f90
+  return TRUE;
475f90
+}
475f90
+#endif
475f90
+
475f90
 /**
475f90
  * g_credentials_is_same_user:
475f90
  * @credentials: A #GCredentials.
475f90
@@ -294,7 +323,8 @@ g_credentials_is_same_user (GCredentials  *credentials,
475f90
 
475f90
   ret = FALSE;
475f90
 #if G_CREDENTIALS_USE_LINUX_UCRED
475f90
-  if (credentials->native.uid == other_credentials->native.uid)
475f90
+  if (linux_ucred_check_valid (&credentials->native, NULL)
475f90
+      && credentials->native.uid == other_credentials->native.uid)
475f90
     ret = TRUE;
475f90
 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
475f90
   if (credentials->native.cmcred_euid == other_credentials->native.cmcred_euid)
475f90
@@ -453,7 +483,10 @@ g_credentials_get_unix_user (GCredentials    *credentials,
475f90
   g_return_val_if_fail (error == NULL || *error == NULL, -1);
475f90
 
475f90
 #if G_CREDENTIALS_USE_LINUX_UCRED
475f90
-  ret = credentials->native.uid;
475f90
+  if (linux_ucred_check_valid (&credentials->native, error))
475f90
+    ret = credentials->native.uid;
475f90
+  else
475f90
+    ret = -1;
475f90
 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
475f90
   ret = credentials->native.cmcred_euid;
475f90
 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
475f90
@@ -499,7 +532,10 @@ g_credentials_get_unix_pid (GCredentials    *credentials,
475f90
   g_return_val_if_fail (error == NULL || *error == NULL, -1);
475f90
 
475f90
 #if G_CREDENTIALS_USE_LINUX_UCRED
475f90
-  ret = credentials->native.pid;
475f90
+  if (linux_ucred_check_valid (&credentials->native, error))
475f90
+    ret = credentials->native.pid;
475f90
+  else
475f90
+    ret = -1;
475f90
 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
475f90
   ret = credentials->native.cmcred_pid;
475f90
 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
475f90
-- 
475f90
2.23.0
475f90