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

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