Blame SOURCES/polkit-0.115-CVE-2019-6133.patch

7cc25b
From 6cc6aafee135ba44ea748250d7d29b562ca190e3 Mon Sep 17 00:00:00 2001
7cc25b
From: Colin Walters <walters@verbum.org>
7cc25b
Date: Fri, 4 Jan 2019 14:24:48 -0500
7cc25b
Subject: [PATCH] backend: Compare PolkitUnixProcess uids for temporary
7cc25b
 authorizations
7cc25b
7cc25b
It turns out that the combination of `(pid, start time)` is not
7cc25b
enough to be unique.  For temporary authorizations, we can avoid
7cc25b
separate users racing on pid reuse by simply comparing the uid.
7cc25b
7cc25b
https://bugs.chromium.org/p/project-zero/issues/detail?id=1692
7cc25b
7cc25b
And the above original email report is included in full in a new comment.
7cc25b
7cc25b
Reported-by: Jann Horn <jannh@google.com>
7cc25b
7cc25b
Closes: https://gitlab.freedesktop.org/polkit/polkit/issues/75
7cc25b
---
7cc25b
 src/polkit/polkitsubject.c                    |  2 +
7cc25b
 src/polkit/polkitunixprocess.c                | 71 ++++++++++++++++++-
7cc25b
 .../polkitbackendinteractiveauthority.c       | 39 +++++++++-
7cc25b
 3 files changed, 110 insertions(+), 2 deletions(-)
7cc25b
7cc25b
diff --git a/src/polkit/polkitsubject.c b/src/polkit/polkitsubject.c
7cc25b
index d4c1182..ccabd0a 100644
7cc25b
--- a/src/polkit/polkitsubject.c
7cc25b
+++ b/src/polkit/polkitsubject.c
7cc25b
@@ -99,6 +99,8 @@ polkit_subject_hash (PolkitSubject *subject)
7cc25b
  * @b: A #PolkitSubject.
7cc25b
  *
7cc25b
  * Checks if @a and @b are equal, ie. represent the same subject.
7cc25b
+ * However, avoid calling polkit_subject_equal() to compare two processes;
7cc25b
+ * for more information see the `PolkitUnixProcess` documentation.
7cc25b
  *
7cc25b
  * This function can be used in e.g. g_hash_table_new().
7cc25b
  *
7cc25b
diff --git a/src/polkit/polkitunixprocess.c b/src/polkit/polkitunixprocess.c
7cc25b
index b02b258..78d7251 100644
7cc25b
--- a/src/polkit/polkitunixprocess.c
7cc25b
+++ b/src/polkit/polkitunixprocess.c
7cc25b
@@ -51,7 +51,10 @@
7cc25b
  * @title: PolkitUnixProcess
7cc25b
  * @short_description: Unix processs
7cc25b
  *
7cc25b
- * An object for representing a UNIX process.
7cc25b
+ * An object for representing a UNIX process.  NOTE: This object as
7cc25b
+ * designed is now known broken; a mechanism to exploit a delay in
7cc25b
+ * start time in the Linux kernel was identified.  Avoid
7cc25b
+ * calling polkit_subject_equal() to compare two processes.
7cc25b
  *
7cc25b
  * To uniquely identify processes, both the process id and the start
7cc25b
  * time of the process (a monotonic increasing value representing the
7cc25b
@@ -66,6 +69,72 @@
7cc25b
  * polkit_unix_process_new_for_owner() with trusted data.
7cc25b
  */
7cc25b
 
7cc25b
+/* See https://gitlab.freedesktop.org/polkit/polkit/issues/75
7cc25b
+
7cc25b
+  But quoting the original email in full here to ensure it's preserved:
7cc25b
+
7cc25b
+  From: Jann Horn <jannh@google.com>
7cc25b
+  Subject: [SECURITY] polkit: temporary auth hijacking via PID reuse and non-atomic fork
7cc25b
+  Date: Wednesday, October 10, 2018 5:34 PM
7cc25b
+
7cc25b
+When a (non-root) user attempts to e.g. control systemd units in the system
7cc25b
+instance from an active session over DBus, the access is gated by a polkit
7cc25b
+policy that requires "auth_admin_keep" auth. This results in an auth prompt
7cc25b
+being shown to the user, asking the user to confirm the action by entering the
7cc25b
+password of an administrator account.
7cc25b
+
7cc25b
+After the action has been confirmed, the auth decision for "auth_admin_keep" is
7cc25b
+cached for up to five minutes. Subject to some restrictions, similar actions can
7cc25b
+then be performed in this timespan without requiring re-auth:
7cc25b
+
7cc25b
+ - The PID of the DBus client requesting the new action must match the PID of
7cc25b
+   the DBus client requesting the old action (based on SO_PEERCRED information
7cc25b
+   forwarded by the DBus daemon).
7cc25b
+ - The "start time" of the client's PID (as seen in /proc/$pid/stat, field 22)
7cc25b
+   must not have changed. The granularity of this timestamp is in the
7cc25b
+   millisecond range.
7cc25b
+ - polkit polls every two seconds whether a process with the expected start time
7cc25b
+   still exists. If not, the temporary auth entry is purged.
7cc25b
+
7cc25b
+Without the start time check, this would obviously be buggy because an attacker
7cc25b
+could simply wait for the legitimate client to disappear, then create a new
7cc25b
+client with the same PID.
7cc25b
+
7cc25b
+Unfortunately, the start time check is bypassable because fork() is not atomic.
7cc25b
+Looking at the source code of copy_process() in the kernel:
7cc25b
+
7cc25b
+        p->start_time = ktime_get_ns();
7cc25b
+        p->real_start_time = ktime_get_boot_ns();
7cc25b
+        [...]
7cc25b
+        retval = copy_thread_tls(clone_flags, stack_start, stack_size, p, tls);
7cc25b
+        if (retval)
7cc25b
+                goto bad_fork_cleanup_io;
7cc25b
+
7cc25b
+        if (pid != &init_struct_pid) {
7cc25b
+                pid = alloc_pid(p->nsproxy->pid_ns_for_children);
7cc25b
+                if (IS_ERR(pid)) {
7cc25b
+                        retval = PTR_ERR(pid);
7cc25b
+                        goto bad_fork_cleanup_thread;
7cc25b
+                }
7cc25b
+        }
7cc25b
+
7cc25b
+The ktime_get_boot_ns() call is where the "start time" of the process is
7cc25b
+recorded. The alloc_pid() call is where a free PID is allocated. In between
7cc25b
+these, some time passes; and because the copy_thread_tls() call between them can
7cc25b
+access userspace memory when sys_clone() is invoked through the 32-bit syscall
7cc25b
+entry point, an attacker can even stall the kernel arbitrarily long at this
7cc25b
+point (by supplying a pointer into userspace memory that is associated with a
7cc25b
+userfaultfd or is backed by a custom FUSE filesystem).
7cc25b
+
7cc25b
+This means that an attacker can immediately call sys_clone() when the victim
7cc25b
+process is created, often resulting in a process that has the exact same start
7cc25b
+time reported in procfs; and then the attacker can delay the alloc_pid() call
7cc25b
+until after the victim process has died and the PID assignment has cycled
7cc25b
+around. This results in an attacker process that polkit can't distinguish from
7cc25b
+the victim process.
7cc25b
+*/
7cc25b
+
7cc25b
+
7cc25b
 /**
7cc25b
  * PolkitUnixProcess:
7cc25b
  *
7cc25b
diff --git a/src/polkitbackend/polkitbackendinteractiveauthority.c b/src/polkitbackend/polkitbackendinteractiveauthority.c
7cc25b
index a1630b9..80e8141 100644
7cc25b
--- a/src/polkitbackend/polkitbackendinteractiveauthority.c
7cc25b
+++ b/src/polkitbackend/polkitbackendinteractiveauthority.c
7cc25b
@@ -3031,6 +3031,43 @@ temporary_authorization_store_free (TemporaryAuthorizationStore *store)
7cc25b
   g_free (store);
7cc25b
 }
7cc25b
 
7cc25b
+/* See the comment at the top of polkitunixprocess.c */
7cc25b
+static gboolean
7cc25b
+subject_equal_for_authz (PolkitSubject *a,
7cc25b
+                         PolkitSubject *b)
7cc25b
+{
7cc25b
+  if (!polkit_subject_equal (a, b))
7cc25b
+    return FALSE;
7cc25b
+
7cc25b
+  /* Now special case unix processes, as we want to protect against
7cc25b
+   * pid reuse by including the UID.
7cc25b
+   */
7cc25b
+  if (POLKIT_IS_UNIX_PROCESS (a) && POLKIT_IS_UNIX_PROCESS (b)) {
7cc25b
+    PolkitUnixProcess *ap = (PolkitUnixProcess*)a;
7cc25b
+    int uid_a = polkit_unix_process_get_uid ((PolkitUnixProcess*)a);
7cc25b
+    PolkitUnixProcess *bp = (PolkitUnixProcess*)b;
7cc25b
+    int uid_b = polkit_unix_process_get_uid ((PolkitUnixProcess*)b);
7cc25b
+
7cc25b
+    if (uid_a != -1 && uid_b != -1)
7cc25b
+      {
7cc25b
+        if (uid_a == uid_b)
7cc25b
+          {
7cc25b
+            return TRUE;
7cc25b
+          }
7cc25b
+        else
7cc25b
+          {
7cc25b
+            g_printerr ("denying slowfork; pid %d uid %d != %d!\n",
7cc25b
+                        polkit_unix_process_get_pid (ap),
7cc25b
+                        uid_a, uid_b);
7cc25b
+            return FALSE;
7cc25b
+          }
7cc25b
+      }
7cc25b
+    /* Fall through; one of the uids is unset so we can't reliably compare */
7cc25b
+  }
7cc25b
+
7cc25b
+  return TRUE;
7cc25b
+}
7cc25b
+
7cc25b
 static gboolean
7cc25b
 temporary_authorization_store_has_authorization (TemporaryAuthorizationStore *store,
7cc25b
                                                  PolkitSubject               *subject,
7cc25b
@@ -3073,7 +3110,7 @@ temporary_authorization_store_has_authorization (TemporaryAuthorizationStore *st
7cc25b
     TemporaryAuthorization *authorization = l->data;
7cc25b
 
7cc25b
     if (strcmp (action_id, authorization->action_id) == 0 &&
7cc25b
-        polkit_subject_equal (subject_to_use, authorization->subject))
7cc25b
+        subject_equal_for_authz (subject_to_use, authorization->subject))
7cc25b
       {
7cc25b
         ret = TRUE;
7cc25b
         if (out_tmp_authz_id != NULL)
7cc25b
-- 
7cc25b
2.19.2
7cc25b