Package: policykit-1 / 0.105-15~deb8u2

0.113/CVE-2015-4625-Use-unpredictable-cookie-values-keep-t.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
From: Colin Walters <walters@redhat.com>
Date: Thu, 4 Jun 2015 12:15:18 -0400
Subject: CVE-2015-4625: Use unpredictable cookie values, keep them secret
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

Tavis noted that it'd be possible with a 32 bit counter for someone to
cause the cookie to wrap by creating Authentication requests in a
loop.

Something important to note here is that wrapping of signed integers
is undefined behavior in C, so we definitely want to fix that.  All
counter integers used in this patch are unsigned.

See the comment above `authentication_agent_generate_cookie` for
details, but basically we're now using a cookie of the form:

```
        <agent serial> - <agent random id> - <session serial> - <session
random id>
```

Which has multiple 64 bit counters, plus unpredictable random 128 bit
integer ids (effectively UUIDs, but we're not calling them that
because we don't need to be globally unique.

We further ensure that the cookies are not visible to other processes
by changing the setuid helper to accept them over standard input.  This
means that an attacker would have to guess both ids.

In any case, the security hole here is better fixed with the other
change to bind user id (uid) of the agent with cookie lookups, making
cookie guessing worthless.

Nevertheless, I think it's worth doing this change too, for defense in
depth.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=90832
CVE: CVE-2015-4625
Reported-by: Tavis Ormandy <taviso@google.com>
Reviewed-by: Miloslav Trma─Ź <mitr@redhat.com>
Signed-off-by: Colin Walters <walters@redhat.com>
Origin: upstream, 0.113, commit:ea544ffc18405237ccd95d28d7f45afef49aca17
Bug-Debian: https://bugs.debian.org/796134
---
 configure.ac                                       |  2 +-
 src/polkitagent/polkitagenthelper-pam.c            | 12 ++-
 src/polkitagent/polkitagenthelper-shadow.c         | 12 ++-
 src/polkitagent/polkitagenthelperprivate.c         | 33 ++++++++
 src/polkitagent/polkitagenthelperprivate.h         |  2 +
 src/polkitagent/polkitagentsession.c               | 30 ++++---
 .../polkitbackendinteractiveauthority.c            | 99 +++++++++++++++++-----
 7 files changed, 150 insertions(+), 40 deletions(-)

diff --git a/configure.ac b/configure.ac
index aa2760f..388605d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -123,7 +123,7 @@ if test "x$GCC" = "xyes"; then
   changequote([,])dnl
 fi
 
-PKG_CHECK_MODULES(GLIB, [gio-2.0 >= 2.28.0])
+PKG_CHECK_MODULES(GLIB, [gmodule-2.0 gio-unix-2.0 >= 2.30.0])
 AC_SUBST(GLIB_CFLAGS)
 AC_SUBST(GLIB_LIBS)
 
diff --git a/src/polkitagent/polkitagenthelper-pam.c b/src/polkitagent/polkitagenthelper-pam.c
index 85a2671..e1747c1 100644
--- a/src/polkitagent/polkitagenthelper-pam.c
+++ b/src/polkitagent/polkitagenthelper-pam.c
@@ -65,7 +65,7 @@ main (int argc, char *argv[])
 {
   int rc;
   const char *user_to_auth;
-  const char *cookie;
+  char *cookie = NULL;
   struct pam_conv pam_conversation;
   pam_handle_t *pam_h;
   const void *authed_user;
@@ -97,7 +97,7 @@ main (int argc, char *argv[])
   openlog ("polkit-agent-helper-1", LOG_CONS | LOG_PID, LOG_AUTHPRIV);
 
   /* check for correct invocation */
-  if (argc != 3)
+  if (!(argc == 2 || argc == 3))
     {
       syslog (LOG_NOTICE, "inappropriate use of helper, wrong number of arguments [uid=%d]", getuid ());
       fprintf (stderr, "polkit-agent-helper-1: wrong number of arguments. This incident has been logged.\n");
@@ -105,7 +105,10 @@ main (int argc, char *argv[])
     }
 
   user_to_auth = argv[1];
-  cookie = argv[2];
+
+  cookie = read_cookie (argc, argv);
+  if (!cookie)
+    goto error;
 
   if (getuid () != 0)
     {
@@ -203,6 +206,8 @@ main (int argc, char *argv[])
       goto error;
     }
 
+  free (cookie);
+
 #ifdef PAH_DEBUG
   fprintf (stderr, "polkit-agent-helper-1: successfully sent D-Bus message to PolicyKit daemon\n");
 #endif /* PAH_DEBUG */
@@ -212,6 +217,7 @@ main (int argc, char *argv[])
   return 0;
 
 error:
+  free (cookie);
   if (pam_h != NULL)
     pam_end (pam_h, rc);
 
diff --git a/src/polkitagent/polkitagenthelper-shadow.c b/src/polkitagent/polkitagenthelper-shadow.c
index a4f73ac..e877915 100644
--- a/src/polkitagent/polkitagenthelper-shadow.c
+++ b/src/polkitagent/polkitagenthelper-shadow.c
@@ -46,7 +46,7 @@ main (int argc, char *argv[])
 {
   struct spwd *shadow;
   const char *user_to_auth;
-  const char *cookie;
+  char *cookie = NULL;
   time_t now;
 
   /* clear the entire environment to avoid attacks with
@@ -67,7 +67,7 @@ main (int argc, char *argv[])
   openlog ("polkit-agent-helper-1", LOG_CONS | LOG_PID, LOG_AUTHPRIV);
 
   /* check for correct invocation */
-  if (argc != 3)
+  if (!(argc == 2 || argc == 3))
     {
       syslog (LOG_NOTICE, "inappropriate use of helper, wrong number of arguments [uid=%d]", getuid ());
       fprintf (stderr, "polkit-agent-helper-1: wrong number of arguments. This incident has been logged.\n");
@@ -86,7 +86,10 @@ main (int argc, char *argv[])
     }
 
   user_to_auth = argv[1];
-  cookie = argv[2];
+
+  cookie = read_cookie (argc, argv);
+  if (!cookie)
+    goto error;
 
 #ifdef PAH_DEBUG
   fprintf (stderr, "polkit-agent-helper-1: user to auth is '%s'.\n", user_to_auth);
@@ -153,6 +156,8 @@ main (int argc, char *argv[])
       goto error;
     }
 
+  free (cookie);
+
 #ifdef PAH_DEBUG
   fprintf (stderr, "polkit-agent-helper-1: successfully sent D-Bus message to PolicyKit daemon\n");
 #endif /* PAH_DEBUG */
@@ -162,6 +167,7 @@ main (int argc, char *argv[])
   return 0;
 
 error:
+  free (cookie);
   fprintf (stdout, "FAILURE\n");
   flush_and_wait ();
   return 1;
diff --git a/src/polkitagent/polkitagenthelperprivate.c b/src/polkitagent/polkitagenthelperprivate.c
index 4417e70..a99de7d 100644
--- a/src/polkitagent/polkitagenthelperprivate.c
+++ b/src/polkitagent/polkitagenthelperprivate.c
@@ -23,6 +23,7 @@
 #include "config.h"
 #include "polkitagenthelperprivate.h"
 #include <stdio.h>
+#include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 
@@ -45,6 +46,38 @@ _polkit_clearenv (void)
 #endif
 
 
+char *
+read_cookie (int argc, char **argv)
+{
+  /* As part of CVE-2015-4625, we started passing the cookie
+   * on standard input, to ensure it's not visible to other
+   * processes.  However, to ensure that things continue
+   * to work if the setuid binary is upgraded while old
+   * agents are still running (this will be common with
+   * package managers), we support both modes.
+   */
+  if (argc == 3)
+    return strdup (argv[2]);
+  else
+    {
+      char *ret = NULL;
+      size_t n = 0;
+      ssize_t r = getline (&ret, &n, stdin);
+      if (r == -1)
+        {
+          if (!feof (stdin))
+            perror ("getline");
+          free (ret);
+          return NULL;
+        }
+      else
+        {
+          g_strchomp (ret);
+          return ret;
+        }
+    }
+}
+
 gboolean
 send_dbus_message (const char *cookie, const char *user)
 {
diff --git a/src/polkitagent/polkitagenthelperprivate.h b/src/polkitagent/polkitagenthelperprivate.h
index aeca2c7..547fdcc 100644
--- a/src/polkitagent/polkitagenthelperprivate.h
+++ b/src/polkitagent/polkitagenthelperprivate.h
@@ -38,6 +38,8 @@
 
 int _polkit_clearenv (void);
 
+char *read_cookie (int argc, char **argv);
+
 gboolean send_dbus_message (const char *cookie, const char *user);
 
 void flush_and_wait ();
diff --git a/src/polkitagent/polkitagentsession.c b/src/polkitagent/polkitagentsession.c
index a658a22..6a3d6bc 100644
--- a/src/polkitagent/polkitagentsession.c
+++ b/src/polkitagent/polkitagentsession.c
@@ -55,6 +55,7 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <gio/gunixoutputstream.h>
 #include <pwd.h>
 
 #include "polkitagentmarshal.h"
@@ -88,7 +89,7 @@ struct _PolkitAgentSession
   gchar *cookie;
   PolkitIdentity *identity;
 
-  int child_stdin;
+  GOutputStream *child_stdin;
   int child_stdout;
   GPid child_pid;
 
@@ -129,7 +130,6 @@ G_DEFINE_TYPE (PolkitAgentSession, polkit_agent_session, G_TYPE_OBJECT);
 static void
 polkit_agent_session_init (PolkitAgentSession *session)
 {
-  session->child_stdin = -1;
   session->child_stdout = -1;
 }
 
@@ -395,11 +395,7 @@ kill_helper (PolkitAgentSession *session)
       session->child_stdout = -1;
     }
 
-  if (session->child_stdin != -1)
-    {
-      g_warn_if_fail (close (session->child_stdin) == 0);
-      session->child_stdin = -1;
-    }
+  g_clear_object (&session->child_stdin);
 
   session->helper_is_running = FALSE;
 
@@ -545,9 +541,9 @@ polkit_agent_session_response (PolkitAgentSession *session,
 
   add_newline = (response[response_len] != '\n');
 
-  write (session->child_stdin, response, response_len);
+  (void) g_output_stream_write_all (session->child_stdin, response, response_len, NULL, NULL, NULL);
   if (add_newline)
-    write (session->child_stdin, newline, 1);
+    (void) g_output_stream_write_all (session->child_stdin, newline, 1, NULL, NULL, NULL);
 }
 
 /**
@@ -567,8 +563,9 @@ polkit_agent_session_initiate (PolkitAgentSession *session)
 {
   uid_t uid;
   GError *error;
-  gchar *helper_argv[4];
+  gchar *helper_argv[3];
   struct passwd *passwd;
+  int stdin_fd = -1;
 
   g_return_if_fail (POLKIT_AGENT_IS_SESSION (session));
 
@@ -600,10 +597,8 @@ polkit_agent_session_initiate (PolkitAgentSession *session)
 
   helper_argv[0] = PACKAGE_LIBEXEC_DIR "/polkit-agent-helper-1";
   helper_argv[1] = passwd->pw_name;
-  helper_argv[2] = session->cookie;
-  helper_argv[3] = NULL;
+  helper_argv[2] = NULL;
 
-  session->child_stdin = -1;
   session->child_stdout = -1;
 
   error = NULL;
@@ -615,7 +610,7 @@ polkit_agent_session_initiate (PolkitAgentSession *session)
                                  NULL,
                                  NULL,
                                  &session->child_pid,
-                                 &session->child_stdin,
+                                 &stdin_fd,
                                  &session->child_stdout,
                                  NULL,
                                  &error))
@@ -628,6 +623,13 @@ polkit_agent_session_initiate (PolkitAgentSession *session)
   if (G_UNLIKELY (_show_debug ()))
     g_print ("PolkitAgentSession: spawned helper with pid %d\n", (gint) session->child_pid);
 
+  session->child_stdin = (GOutputStream*)g_unix_output_stream_new (stdin_fd, TRUE);
+
+  /* Write the cookie on stdin so it can't be seen by other processes */
+  (void) g_output_stream_write_all (session->child_stdin, session->cookie, strlen (session->cookie),
+                                    NULL, NULL, NULL);
+  (void) g_output_stream_write_all (session->child_stdin, "\n", 1, NULL, NULL, NULL);
+
   session->child_stdout_channel = g_io_channel_unix_new (session->child_stdout);
   session->child_stdout_watch_source = g_io_create_watch (session->child_stdout_channel,
                                                           G_IO_IN | G_IO_ERR | G_IO_HUP);
diff --git a/src/polkitbackend/polkitbackendinteractiveauthority.c b/src/polkitbackend/polkitbackendinteractiveauthority.c
index 00ee044..10eda2c 100644
--- a/src/polkitbackend/polkitbackendinteractiveauthority.c
+++ b/src/polkitbackend/polkitbackendinteractiveauthority.c
@@ -212,6 +212,8 @@ typedef struct
 
   GDBusConnection *system_bus_connection;
   guint name_owner_changed_signal_id;
+
+  guint64 agent_serial;
 } PolkitBackendInteractiveAuthorityPrivate;
 
 /* ---------------------------------------------------------------------------------------------------- */
@@ -430,11 +432,15 @@ struct AuthenticationAgent
   volatile gint ref_count;
 
   PolkitSubject *scope;
+  guint64 serial;
 
   gchar *locale;
   GVariant *registration_options;
   gchar *object_path;
   gchar *unique_system_bus_name;
+  GRand *cookie_pool;
+  gchar *cookie_prefix;
+  guint64  cookie_serial;
 
   GDBusProxy *proxy;
 
@@ -1430,9 +1436,54 @@ authentication_session_cancelled_cb (GCancellable *cancellable,
   authentication_session_cancel (session);
 }
 
+/* We're not calling this a UUID, but it's basically
+ * the same thing, just not formatted that way because:
+ *
+ *  - I'm too lazy to do it
+ *  - If we did, people might think it was actually
+ *    generated from /dev/random, which we're not doing
+ *    because this value doesn't actually need to be
+ *    globally unique.
+ */
+static void
+append_rand_u128_str (GString *buf,
+                      GRand   *pool)
+{
+  g_string_append_printf (buf, "%08x%08x%08x%08x",
+                          g_rand_int (pool),
+                          g_rand_int (pool),
+                          g_rand_int (pool),
+                          g_rand_int (pool));
+}
+
+/* A value that should be unique to the (AuthenticationAgent, AuthenticationSession)
+ * pair, and not guessable by other agents.
+ *
+ * <agent serial> - <agent uuid> - <session serial> - <session uuid>
+ *
+ * See http://lists.freedesktop.org/archives/polkit-devel/2015-June/000425.html
+ *
+ */
+static gchar *
+authentication_agent_generate_cookie (AuthenticationAgent *agent)
+{
+  GString *buf = g_string_new ("");
+
+  g_string_append (buf, agent->cookie_prefix);
+  
+  g_string_append_c (buf, '-');
+  agent->cookie_serial++;
+  g_string_append_printf (buf, "%" G_GUINT64_FORMAT, 
+                          agent->cookie_serial);
+  g_string_append_c (buf, '-');
+  append_rand_u128_str (buf, agent->cookie_pool);
+
+  return g_string_free (buf, FALSE);
+}
+
+
 static AuthenticationSession *
 authentication_session_new (AuthenticationAgent         *agent,
-                            const gchar                 *cookie,
                             PolkitSubject               *subject,
                             PolkitIdentity              *user_of_subject,
                             PolkitSubject               *caller,
@@ -1449,7 +1500,7 @@ authentication_session_new (AuthenticationAgent         *agent,
 
   session = g_new0 (AuthenticationSession, 1);
   session->agent = authentication_agent_ref (agent);
-  session->cookie = g_strdup (cookie);
+  session->cookie = authentication_agent_generate_cookie (agent);
   session->subject = g_object_ref (subject);
   session->user_of_subject = g_object_ref (user_of_subject);
   session->caller = g_object_ref (caller);
@@ -1496,16 +1547,6 @@ authentication_session_free (AuthenticationSession *session)
   g_free (session);
 }
 
-static gchar *
-authentication_agent_new_cookie (AuthenticationAgent *agent)
-{
-  static gint counter = 0;
-
-  /* TODO: use a more random-looking cookie */
-
-  return g_strdup_printf ("cookie%d", counter++);
-}
-
 static PolkitSubject *
 authentication_agent_get_scope (AuthenticationAgent *agent)
 {
@@ -1553,12 +1594,15 @@ authentication_agent_unref (AuthenticationAgent *agent)
       g_free (agent->unique_system_bus_name);
       if (agent->registration_options != NULL)
         g_variant_unref (agent->registration_options);
+      g_rand_free (agent->cookie_pool);
+      g_free (agent->cookie_prefix);
       g_free (agent);
     }
 }
 
 static AuthenticationAgent *
-authentication_agent_new (PolkitSubject *scope,
+authentication_agent_new (guint64      serial,
+                          PolkitSubject *scope,
                           const gchar *unique_system_bus_name,
                           const gchar *locale,
                           const gchar *object_path,
@@ -1592,6 +1636,7 @@ authentication_agent_new (PolkitSubject *scope,
 
   agent = g_new0 (AuthenticationAgent, 1);
   agent->ref_count = 1;
+  agent->serial = serial;
   agent->scope = g_object_ref (scope);
   agent->object_path = g_strdup (object_path);
   agent->unique_system_bus_name = g_strdup (unique_system_bus_name);
@@ -1599,6 +1644,25 @@ authentication_agent_new (PolkitSubject *scope,
   agent->registration_options = registration_options != NULL ? g_variant_ref (registration_options) : NULL;
   agent->proxy = proxy;
 
+  {
+    GString *cookie_prefix = g_string_new ("");
+    GRand *agent_private_rand = g_rand_new ();
+
+    g_string_append_printf (cookie_prefix, "%" G_GUINT64_FORMAT "-", agent->serial);
+
+    /* Use a uniquely seeded PRNG to get a prefix cookie for this agent,
+     * whose sequence will not correlate with the per-authentication session
+     * cookies.
+     */
+    append_rand_u128_str (cookie_prefix, agent_private_rand);
+    g_rand_free (agent_private_rand);
+
+    agent->cookie_prefix = g_string_free (cookie_prefix, FALSE);
+    
+    /* And a newly seeded pool for per-session cookies */
+    agent->cookie_pool = g_rand_new ();
+  }
+
   return agent;
 }
 
@@ -2083,7 +2147,6 @@ authentication_agent_initiate_challenge (AuthenticationAgent         *agent,
                                          gpointer                     user_data)
 {
   AuthenticationSession *session;
-  gchar *cookie;
   GList *l;
   GList *identities;
   gchar *localized_message;
@@ -2104,8 +2167,6 @@ authentication_agent_initiate_challenge (AuthenticationAgent         *agent,
                                     &localized_icon_name,
                                     &localized_details);
 
-  cookie = authentication_agent_new_cookie (agent);
-
   identities = NULL;
 
   /* select admin user if required by the implicit authorization */
@@ -2125,7 +2186,6 @@ authentication_agent_initiate_challenge (AuthenticationAgent         *agent,
     }
 
   session = authentication_session_new (agent,
-                                        cookie,
                                         subject,
                                         user_of_subject,
                                         caller,
@@ -2179,7 +2239,6 @@ authentication_agent_initiate_challenge (AuthenticationAgent         *agent,
 
   g_list_foreach (identities, (GFunc) g_object_unref, NULL);
   g_list_free (identities);
-  g_free (cookie);
 
   g_free (localized_message);
   g_free (localized_icon_name);
@@ -2326,7 +2385,9 @@ polkit_backend_interactive_authority_register_authentication_agent (PolkitBacken
       goto out;
     }
 
-  agent = authentication_agent_new (subject,
+  priv->agent_serial++;
+  agent = authentication_agent_new (priv->agent_serial,
+                                    subject,
                                     polkit_system_bus_name_get_name (POLKIT_SYSTEM_BUS_NAME (caller)),
                                     locale,
                                     object_path,