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

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