dc3ed5
diff -up ./src/polkitbackend/polkitbackendinteractiveauthority.c.ori ./src/polkitbackend/polkitbackendinteractiveauthority.c
dc3ed5
--- ./src/polkitbackend/polkitbackendinteractiveauthority.c.ori	2019-01-21 17:30:43.468115782 +0100
dc3ed5
+++ ./src/polkitbackend/polkitbackendinteractiveauthority.c	2019-01-21 17:31:03.220029178 +0100
dc3ed5
@@ -2904,6 +2904,43 @@ temporary_authorization_store_free (Temp
dc3ed5
   g_free (store);
dc3ed5
 }
dc3ed5
 
dc3ed5
+/* See the comment at the top of polkitunixprocess.c */
dc3ed5
+static gboolean
dc3ed5
+subject_equal_for_authz (PolkitSubject *a,
dc3ed5
+                         PolkitSubject *b)
dc3ed5
+{
dc3ed5
+  if (!polkit_subject_equal (a, b))
dc3ed5
+    return FALSE;
dc3ed5
+
dc3ed5
+  /* Now special case unix processes, as we want to protect against
dc3ed5
+   * pid reuse by including the UID.
dc3ed5
+   */
dc3ed5
+  if (POLKIT_IS_UNIX_PROCESS (a) && POLKIT_IS_UNIX_PROCESS (b)) {
dc3ed5
+    PolkitUnixProcess *ap = (PolkitUnixProcess*)a;
dc3ed5
+    int uid_a = polkit_unix_process_get_uid ((PolkitUnixProcess*)a);
dc3ed5
+    PolkitUnixProcess *bp = (PolkitUnixProcess*)b;
dc3ed5
+    int uid_b = polkit_unix_process_get_uid ((PolkitUnixProcess*)b);
dc3ed5
+
dc3ed5
+    if (uid_a != -1 && uid_b != -1)
dc3ed5
+      {
dc3ed5
+        if (uid_a == uid_b)
dc3ed5
+          {
dc3ed5
+            return TRUE;
dc3ed5
+          }
dc3ed5
+        else
dc3ed5
+          {
dc3ed5
+            g_printerr ("denying slowfork; pid %d uid %d != %d!\n",
dc3ed5
+                        polkit_unix_process_get_pid (ap),
dc3ed5
+                        uid_a, uid_b);
dc3ed5
+            return FALSE;
dc3ed5
+          }
dc3ed5
+      }
dc3ed5
+    /* Fall through; one of the uids is unset so we can't reliably compare */
dc3ed5
+  }
dc3ed5
+
dc3ed5
+  return TRUE;
dc3ed5
+}
dc3ed5
+
dc3ed5
 static gboolean
dc3ed5
 temporary_authorization_store_has_authorization (TemporaryAuthorizationStore *store,
dc3ed5
                                                  PolkitSubject               *subject,
dc3ed5
@@ -2946,7 +2983,7 @@ temporary_authorization_store_has_author
dc3ed5
     TemporaryAuthorization *authorization = l->data;
dc3ed5
 
dc3ed5
     if (strcmp (action_id, authorization->action_id) == 0 &&
dc3ed5
-        polkit_subject_equal (subject_to_use, authorization->subject))
dc3ed5
+        subject_equal_for_authz (subject_to_use, authorization->subject))
dc3ed5
       {
dc3ed5
         ret = TRUE;
dc3ed5
         if (out_tmp_authz_id != NULL)
dc3ed5
diff -up ./src/polkit/polkitsubject.c.ori ./src/polkit/polkitsubject.c
dc3ed5
--- ./src/polkit/polkitsubject.c.ori	2013-05-29 16:51:37.000000000 +0200
dc3ed5
+++ ./src/polkit/polkitsubject.c	2019-01-21 17:31:03.218029187 +0100
dc3ed5
@@ -99,6 +99,8 @@ polkit_subject_hash (PolkitSubject *subj
dc3ed5
  * @b: A #PolkitSubject.
dc3ed5
  *
dc3ed5
  * Checks if @a and @b are equal, ie. represent the same subject.
dc3ed5
+ * However, avoid calling polkit_subject_equal() to compare two processes;
dc3ed5
+ * for more information see the `PolkitUnixProcess` documentation.
dc3ed5
  *
dc3ed5
  * This function can be used in e.g. g_hash_table_new().
dc3ed5
  *
dc3ed5
diff -up ./src/polkit/polkitunixprocess.c.ori ./src/polkit/polkitunixprocess.c
dc3ed5
--- ./src/polkit/polkitunixprocess.c.ori	2019-01-21 17:30:43.477115743 +0100
dc3ed5
+++ ./src/polkit/polkitunixprocess.c	2019-01-21 17:31:03.219029182 +0100
dc3ed5
@@ -44,13 +44,82 @@
dc3ed5
  * @title: PolkitUnixProcess
dc3ed5
  * @short_description: Unix processs
dc3ed5
  *
dc3ed5
- * An object for representing a UNIX process.
dc3ed5
+ * An object for representing a UNIX process.  NOTE: This object as
dc3ed5
+ * designed is now known broken; a mechanism to exploit a delay in
dc3ed5
+ * start time in the Linux kernel was identified.  Avoid
dc3ed5
+ * calling polkit_subject_equal() to compare two processes.
dc3ed5
  *
dc3ed5
  * To uniquely identify processes, both the process id and the start
dc3ed5
  * time of the process (a monotonic increasing value representing the
dc3ed5
  * time since the kernel was started) is used.
dc3ed5
  */
dc3ed5
 
dc3ed5
+/* See https://gitlab.freedesktop.org/polkit/polkit/issues/75
dc3ed5
+
dc3ed5
+  But quoting the original email in full here to ensure it's preserved:
dc3ed5
+
dc3ed5
+  From: Jann Horn <jannh@google.com>
dc3ed5
+  Subject: [SECURITY] polkit: temporary auth hijacking via PID reuse and non-atomic fork
dc3ed5
+  Date: Wednesday, October 10, 2018 5:34 PM
dc3ed5
+
dc3ed5
+When a (non-root) user attempts to e.g. control systemd units in the system
dc3ed5
+instance from an active session over DBus, the access is gated by a polkit
dc3ed5
+policy that requires "auth_admin_keep" auth. This results in an auth prompt
dc3ed5
+being shown to the user, asking the user to confirm the action by entering the
dc3ed5
+password of an administrator account.
dc3ed5
+
dc3ed5
+After the action has been confirmed, the auth decision for "auth_admin_keep" is
dc3ed5
+cached for up to five minutes. Subject to some restrictions, similar actions can
dc3ed5
+then be performed in this timespan without requiring re-auth:
dc3ed5
+
dc3ed5
+ - The PID of the DBus client requesting the new action must match the PID of
dc3ed5
+   the DBus client requesting the old action (based on SO_PEERCRED information
dc3ed5
+   forwarded by the DBus daemon).
dc3ed5
+ - The "start time" of the client's PID (as seen in /proc/$pid/stat, field 22)
dc3ed5
+   must not have changed. The granularity of this timestamp is in the
dc3ed5
+   millisecond range.
dc3ed5
+ - polkit polls every two seconds whether a process with the expected start time
dc3ed5
+   still exists. If not, the temporary auth entry is purged.
dc3ed5
+
dc3ed5
+Without the start time check, this would obviously be buggy because an attacker
dc3ed5
+could simply wait for the legitimate client to disappear, then create a new
dc3ed5
+client with the same PID.
dc3ed5
+
dc3ed5
+Unfortunately, the start time check is bypassable because fork() is not atomic.
dc3ed5
+Looking at the source code of copy_process() in the kernel:
dc3ed5
+
dc3ed5
+        p->start_time = ktime_get_ns();
dc3ed5
+        p->real_start_time = ktime_get_boot_ns();
dc3ed5
+        [...]
dc3ed5
+        retval = copy_thread_tls(clone_flags, stack_start, stack_size, p, tls);
dc3ed5
+        if (retval)
dc3ed5
+                goto bad_fork_cleanup_io;
dc3ed5
+
dc3ed5
+        if (pid != &init_struct_pid) {
dc3ed5
+                pid = alloc_pid(p->nsproxy->pid_ns_for_children);
dc3ed5
+                if (IS_ERR(pid)) {
dc3ed5
+                        retval = PTR_ERR(pid);
dc3ed5
+                        goto bad_fork_cleanup_thread;
dc3ed5
+                }
dc3ed5
+        }
dc3ed5
+
dc3ed5
+The ktime_get_boot_ns() call is where the "start time" of the process is
dc3ed5
+recorded. The alloc_pid() call is where a free PID is allocated. In between
dc3ed5
+these, some time passes; and because the copy_thread_tls() call between them can
dc3ed5
+access userspace memory when sys_clone() is invoked through the 32-bit syscall
dc3ed5
+entry point, an attacker can even stall the kernel arbitrarily long at this
dc3ed5
+point (by supplying a pointer into userspace memory that is associated with a
dc3ed5
+userfaultfd or is backed by a custom FUSE filesystem).
dc3ed5
+
dc3ed5
+This means that an attacker can immediately call sys_clone() when the victim
dc3ed5
+process is created, often resulting in a process that has the exact same start
dc3ed5
+time reported in procfs; and then the attacker can delay the alloc_pid() call
dc3ed5
+until after the victim process has died and the PID assignment has cycled
dc3ed5
+around. This results in an attacker process that polkit can't distinguish from
dc3ed5
+the victim process.
dc3ed5
+*/
dc3ed5
+
dc3ed5
+
dc3ed5
 /**
dc3ed5
  * PolkitUnixProcess:
dc3ed5
  *