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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
|
/*
* Copyright © 2010-2012 Nokia Corporation
* Copyright © 2014 Collabora Ltd.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <config.h>
#include <dbus/dbus.h>
#include <dbus/dbus-internals.h>
#include <dbus/dbus-sysdeps.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#ifdef G_OS_UNIX
# include <dbus/dbus-sysdeps-unix.h>
# include <errno.h>
# include <fcntl.h>
# ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
# endif
# include <sys/stat.h>
# include <sys/time.h>
# include <sys/types.h>
# include <unistd.h>
#endif
#include "test-utils-glib.h"
/* Arbitrary; included here to avoid relying on the default */
#define MAX_MESSAGE_UNIX_FDS 20
/* This test won't work on Linux unless this is true. */
_DBUS_STATIC_ASSERT (MAX_MESSAGE_UNIX_FDS <= 253);
/* Arbitrary; included here to avoid relying on the default. */
#define MAX_INCOMING_UNIX_FDS (MAX_MESSAGE_UNIX_FDS * 4)
/* Arbitrary, except that MAX_MESSAGE_UNIX_FDS * SOME_MESSAGES should be
* less than the process's file descriptor limit. */
#define SOME_MESSAGES 20
/* To cover some situations on Linux we want this to be true. */
_DBUS_STATIC_ASSERT (MAX_MESSAGE_UNIX_FDS * SOME_MESSAGES > 256);
/* Linux won't allow more than 253 fds per sendmsg(). */
#define TOO_MANY_FDS 255
_DBUS_STATIC_ASSERT (MAX_MESSAGE_UNIX_FDS < TOO_MANY_FDS);
/* As in test/relay.c, this is a miniature dbus-daemon: we relay messages
* from the client on the left to the client on the right.
*
* left socket left dispatch right socket right
* client ===========> server --------------> server ===========> client
* conn conn conn conn
*/
typedef struct {
TestMainContext *ctx;
DBusError e;
gboolean skip;
DBusServer *server;
DBusConnection *left_client_conn;
DBusConnection *left_server_conn;
DBusConnection *right_server_conn;
DBusConnection *right_client_conn;
/* queue of DBusMessage received by right_client_conn */
GQueue messages;
int fd_before;
} Fixture;
static void oom (const gchar *doing) G_GNUC_NORETURN;
static void
oom (const gchar *doing)
{
g_error ("out of memory (%s)", doing);
abort ();
}
static void
assert_no_error (const DBusError *e)
{
if (G_UNLIKELY (dbus_error_is_set (e)))
g_error ("expected success but got error: %s: %s", e->name, e->message);
}
static DBusHandlerResult
left_server_message_cb (DBusConnection *server_conn,
DBusMessage *message,
void *data)
{
Fixture *f = data;
g_assert (server_conn == f->left_server_conn);
g_assert (f->right_server_conn != NULL);
dbus_connection_send (f->right_server_conn, message, NULL);
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult
right_client_message_cb (DBusConnection *client_conn,
DBusMessage *message,
void *data)
{
Fixture *f = data;
g_assert (client_conn == f->right_client_conn);
g_queue_push_tail (&f->messages, dbus_message_ref (message));
return DBUS_HANDLER_RESULT_HANDLED;
}
static void
new_conn_cb (DBusServer *server,
DBusConnection *server_conn,
void *data)
{
Fixture *f = data;
dbus_connection_set_max_message_unix_fds (server_conn,
MAX_MESSAGE_UNIX_FDS);
dbus_connection_set_max_received_unix_fds (server_conn,
MAX_INCOMING_UNIX_FDS);
if (f->left_server_conn == NULL)
{
f->left_server_conn = dbus_connection_ref (server_conn);
if (!dbus_connection_add_filter (server_conn,
left_server_message_cb, f, NULL))
oom ("adding filter");
}
else
{
g_assert (f->right_server_conn == NULL);
f->right_server_conn = dbus_connection_ref (server_conn);
}
test_connection_setup (f->ctx, server_conn);
}
static void
test_connect (Fixture *f,
gboolean should_support_fds)
{
char *address;
if (f->skip)
return;
g_assert (f->left_server_conn == NULL);
g_assert (f->right_server_conn == NULL);
address = dbus_server_get_address (f->server);
g_assert (address != NULL);
f->left_client_conn = dbus_connection_open_private (address, &f->e);
assert_no_error (&f->e);
g_assert (f->left_client_conn != NULL);
test_connection_setup (f->ctx, f->left_client_conn);
/* The left client connection is allowed to behave abusively. */
dbus_connection_set_max_message_unix_fds (f->left_client_conn, 1000);
dbus_connection_set_max_received_unix_fds (f->left_client_conn, 1000000);
while (f->left_server_conn == NULL)
{
test_progress ('.');
test_main_context_iterate (f->ctx, TRUE);
}
f->right_client_conn = dbus_connection_open_private (address, &f->e);
assert_no_error (&f->e);
g_assert (f->right_client_conn != NULL);
test_connection_setup (f->ctx, f->right_client_conn);
dbus_free (address);
while (f->right_server_conn == NULL)
{
test_progress ('.');
test_main_context_iterate (f->ctx, TRUE);
}
if (!dbus_connection_add_filter (f->right_client_conn,
right_client_message_cb, f, NULL))
oom ("adding filter");
/* The right client connection is allowed to queue all the messages. */
dbus_connection_set_max_message_unix_fds (f->right_client_conn, 1000);
dbus_connection_set_max_received_unix_fds (f->right_client_conn, 1000000);
while (!dbus_connection_get_is_authenticated (f->left_client_conn) ||
!dbus_connection_get_is_authenticated (f->right_client_conn) ||
!dbus_connection_get_is_authenticated (f->left_server_conn) ||
!dbus_connection_get_is_authenticated (f->right_server_conn))
{
test_progress ('*');
test_main_context_iterate (f->ctx, TRUE);
}
if (!should_support_fds)
return;
if (!dbus_connection_can_send_type (f->left_client_conn,
DBUS_TYPE_UNIX_FD))
g_error ("left client connection cannot send Unix fds");
if (!dbus_connection_can_send_type (f->left_server_conn,
DBUS_TYPE_UNIX_FD))
g_error ("left server connection cannot send Unix fds");
if (!dbus_connection_can_send_type (f->right_client_conn,
DBUS_TYPE_UNIX_FD))
g_error ("right client connection cannot send Unix fds");
if (!dbus_connection_can_send_type (f->right_server_conn,
DBUS_TYPE_UNIX_FD))
g_error ("right server connection cannot send Unix fds");
}
static void
setup_common (Fixture *f,
const char *address)
{
f->ctx = test_main_context_get ();
dbus_error_init (&f->e);
g_queue_init (&f->messages);
if ((g_str_has_prefix (address, "tcp:") ||
g_str_has_prefix (address, "nonce-tcp:")) &&
!test_check_tcp_works ())
{
f->skip = TRUE;
return;
}
f->server = dbus_server_listen (address, &f->e);
assert_no_error (&f->e);
g_assert (f->server != NULL);
dbus_server_set_new_connection_function (f->server,
new_conn_cb, f, NULL);
test_server_setup (f->ctx, f->server);
}
static void
setup_unsupported (Fixture *f,
gconstpointer data G_GNUC_UNUSED)
{
setup_common (f, "tcp:host=127.0.0.1");
}
static void
setup (Fixture *f,
gconstpointer data G_GNUC_UNUSED)
{
#ifdef HAVE_UNIX_FD_PASSING
/* We assume that anything with fd-passing supports the unix: transport */
setup_common (f, "unix:tmpdir=/tmp");
f->fd_before = open ("/dev/null", O_RDONLY);
/* this should succeed on any reasonable Unix */
if (f->fd_before < 0)
g_error ("cannot open /dev/null for reading: %s", g_strerror (errno));
_dbus_fd_set_close_on_exec (f->fd_before);
#endif
}
static void
test_unsupported (Fixture *f,
gconstpointer data)
{
if (f->skip)
return;
test_connect (f, FALSE);
if (dbus_connection_can_send_type (f->left_client_conn,
DBUS_TYPE_UNIX_FD))
g_error ("left client connection claims it can send Unix fds");
if (dbus_connection_can_send_type (f->left_server_conn,
DBUS_TYPE_UNIX_FD))
g_error ("left server connection claims it can send Unix fds");
if (dbus_connection_can_send_type (f->right_client_conn,
DBUS_TYPE_UNIX_FD))
g_error ("right client connection claims it can send Unix fds");
if (dbus_connection_can_send_type (f->right_server_conn,
DBUS_TYPE_UNIX_FD))
g_error ("right server connection claims it can send Unix fds");
}
static void
test_relay (Fixture *f,
gconstpointer data)
{
#ifdef HAVE_UNIX_FD_PASSING
/* We assume that any platform with working fd-passing is POSIX,
* and therefore has open() and fstat() */
dbus_uint32_t serial;
DBusMessage *outgoing, *incoming;
int fd_after;
struct stat stat_before;
struct stat stat_after;
if (f->skip)
return;
test_connect (f, TRUE);
outgoing = dbus_message_new_signal ("/com/example/Hello",
"com.example.Hello", "Greeting");
g_assert (outgoing != NULL);
if (!dbus_message_append_args (outgoing,
DBUS_TYPE_UNIX_FD, &f->fd_before,
DBUS_TYPE_INVALID))
oom ("appending fd");
if (!dbus_connection_send (f->left_client_conn, outgoing, &serial))
oom ("sending message");
dbus_message_unref (outgoing);
while (g_queue_get_length (&f->messages) < 1)
{
test_progress ('.');
test_main_context_iterate (f->ctx, TRUE);
}
g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 1);
incoming = g_queue_pop_head (&f->messages);
g_assert (dbus_message_contains_unix_fds (incoming));
g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
"com.example.Hello");
g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
g_assert_cmpstr (dbus_message_get_signature (incoming), ==,
DBUS_TYPE_UNIX_FD_AS_STRING);
g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
if (dbus_set_error_from_message (&f->e, incoming))
g_error ("%s: %s", f->e.name, f->e.message);
else if (!dbus_message_get_args (incoming,
&f->e,
DBUS_TYPE_UNIX_FD, &fd_after,
DBUS_TYPE_INVALID))
g_error ("%s: %s", f->e.name, f->e.message);
assert_no_error (&f->e);
if (fstat (f->fd_before, &stat_before) < 0)
g_error ("%s", g_strerror (errno));
if (fstat (fd_after, &stat_after) < 0)
g_error ("%s", g_strerror (errno));
/* this seems like enough to say "it's the same file" */
g_assert_cmpint (stat_before.st_dev, ==, stat_after.st_dev);
g_assert_cmpint (stat_before.st_ino, ==, stat_after.st_ino);
g_assert_cmpint (stat_before.st_rdev, ==, stat_after.st_rdev);
dbus_message_unref (incoming);
if (close (fd_after) < 0)
g_error ("%s", g_strerror (errno));
g_assert (dbus_connection_get_is_connected (f->right_client_conn));
g_assert (dbus_connection_get_is_connected (f->right_server_conn));
g_assert (dbus_connection_get_is_connected (f->left_client_conn));
g_assert (dbus_connection_get_is_connected (f->left_server_conn));
#else
g_test_skip ("fd-passing not supported on this platform");
#endif
}
static void
test_limit (Fixture *f,
gconstpointer data)
{
#ifdef HAVE_UNIX_FD_PASSING
dbus_uint32_t serial;
DBusMessage *outgoing, *incoming;
int i;
if (f->skip)
return;
test_connect (f, TRUE);
outgoing = dbus_message_new_signal ("/com/example/Hello",
"com.example.Hello", "Greeting");
g_assert (outgoing != NULL);
for (i = 0; i < MAX_MESSAGE_UNIX_FDS; i++)
{
if (!dbus_message_append_args (outgoing,
DBUS_TYPE_UNIX_FD, &f->fd_before,
DBUS_TYPE_INVALID))
oom ("appending fd");
}
if (!dbus_connection_send (f->left_client_conn, outgoing, &serial))
oom ("sending message");
dbus_message_unref (outgoing);
while (g_queue_get_length (&f->messages) < 1)
{
test_progress ('.');
test_main_context_iterate (f->ctx, TRUE);
}
g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 1);
incoming = g_queue_pop_head (&f->messages);
g_assert (dbus_message_contains_unix_fds (incoming));
g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
"com.example.Hello");
g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
dbus_message_unref (incoming);
g_assert (dbus_connection_get_is_connected (f->right_client_conn));
g_assert (dbus_connection_get_is_connected (f->right_server_conn));
g_assert (dbus_connection_get_is_connected (f->left_client_conn));
g_assert (dbus_connection_get_is_connected (f->left_server_conn));
#else
g_test_skip ("fd-passing not supported on this platform");
#endif
}
static void
test_too_many (Fixture *f,
gconstpointer data)
{
#ifdef HAVE_UNIX_FD_PASSING
DBusMessage *outgoing;
unsigned int i;
if (f->skip)
return;
test_connect (f, TRUE);
outgoing = dbus_message_new_signal ("/com/example/Hello",
"com.example.Hello", "Greeting");
g_assert (outgoing != NULL);
for (i = 0; i < MAX_MESSAGE_UNIX_FDS + GPOINTER_TO_UINT (data); i++)
{
if (!dbus_message_append_args (outgoing,
DBUS_TYPE_UNIX_FD, &f->fd_before,
DBUS_TYPE_INVALID))
oom ("appending fd");
}
if (!dbus_connection_send (f->left_client_conn, outgoing, NULL))
oom ("sending message");
dbus_message_unref (outgoing);
/* The sender is unceremoniously disconnected. */
while (dbus_connection_get_is_connected (f->left_client_conn) ||
dbus_connection_get_is_connected (f->left_server_conn))
{
test_progress ('.');
test_main_context_iterate (f->ctx, TRUE);
}
/* The message didn't get through without its fds. */
g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 0);
/* The intended victim is unaffected by the left connection's
* misbehaviour. */
g_assert (dbus_connection_get_is_connected (f->right_client_conn));
g_assert (dbus_connection_get_is_connected (f->right_server_conn));
#else
g_test_skip ("fd-passing not supported on this platform");
#endif
}
static void
test_too_many_split (Fixture *f,
gconstpointer data)
{
#ifdef HAVE_UNIX_FD_PASSING
DBusMessage *outgoing;
int i;
DBusSocket left_client_socket;
char *payload;
int payload_len;
DBusString buffer;
int fds[TOO_MANY_FDS];
int done;
#ifdef HAVE_GETRLIMIT
struct rlimit lim;
#endif
if (f->skip)
return;
/* This test deliberately pushes up against OS limits, so skip it
* if we don't have enough fds. 4 times the maximum per message
* ought to be enough: that will cover the message, the dup'd fds
* we actually send, the copy that we potentially receive, and some
* spare capacity for everything else. */
#ifdef HAVE_GETRLIMIT
if (getrlimit (RLIMIT_NOFILE, &lim) == 0)
{
if (lim.rlim_cur != RLIM_INFINITY &&
lim.rlim_cur < 4 * TOO_MANY_FDS)
{
g_test_skip ("not enough RLIMIT_NOFILE");
return;
}
}
#endif
test_connect (f, TRUE);
outgoing = dbus_message_new_signal ("/com/example/Hello",
"com.example.Hello", "Greeting");
g_assert (outgoing != NULL);
/* TOO_MANY_FDS fds are far too many: in particular, Linux doesn't allow
* sending this many in a single sendmsg(). libdbus never splits
* a message between two sendmsg() calls if it can help it, and
* in particular it always sends all the fds with the first sendmsg(),
* but malicious senders might not be so considerate. */
for (i = 0; i < TOO_MANY_FDS; i++)
{
if (!dbus_message_append_args (outgoing,
DBUS_TYPE_UNIX_FD, &f->fd_before,
DBUS_TYPE_INVALID))
oom ("appending fd");
}
/* This probably shouldn't work for messages with fds, but it does,
* which is convenient for this sort of trickery. */
if (!dbus_message_marshal (outgoing, &payload, &payload_len))
oom ("marshalling message");
_dbus_string_init_const_len (&buffer, payload, payload_len);
for (i = 0; i < TOO_MANY_FDS; i++)
{
fds[i] = dup (f->fd_before);
if (fds[i] < 0)
g_error ("could not dup fd: %s", g_strerror (errno));
}
/* This is blatant cheating, and the API documentation specifically
* tells you not use this function in this way. Never do this
* in application code. */
if (!dbus_connection_get_socket (f->left_client_conn,
&left_client_socket.fd))
g_error ("'unix:' DBusConnection should have had a socket");
/* Just to be sure that we're at a message boundary. */
dbus_connection_flush (f->left_client_conn);
/* We have too many fds for one sendmsg(), so send the first half
* (rounding down if odd) with the first byte... */
done = _dbus_write_socket_with_unix_fds (left_client_socket, &buffer, 0, 1,
&fds[0], TOO_MANY_FDS / 2);
if (done < 0)
g_error ("could not send first byte and first batch of fds: %s",
g_strerror (errno));
/* ... and the second half (rounding up if odd) with the rest of
* the message */
done = _dbus_write_socket_with_unix_fds (left_client_socket, &buffer, 1,
payload_len - 1, &fds[TOO_MANY_FDS / 2],
TOO_MANY_FDS - (TOO_MANY_FDS / 2));
if (done < 0)
{
g_error ("could not send rest of message and rest of fds: %s",
g_strerror (errno));
}
else if (done < payload_len - 1)
{
/* For simplicity, assume the socket buffer is big enough for the
* whole message, which should be < 2 KiB. If this fails on some
* OS, redo this test code to use a proper loop like the real
* libdbus does. */
g_error ("short write in sendmsg(), fix this test: %d/%d",
done, payload_len - 1);
}
dbus_free (payload);
for (i = 0; i < TOO_MANY_FDS; i++)
close (fds[i]);
dbus_message_unref (outgoing);
/* The sender is unceremoniously disconnected. */
while (dbus_connection_get_is_connected (f->left_client_conn) ||
dbus_connection_get_is_connected (f->left_server_conn))
{
test_progress ('.');
test_main_context_iterate (f->ctx, TRUE);
}
/* The message didn't get through without its fds. */
g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 0);
/* The intended victim is unaffected by the left connection's
* misbehaviour. */
g_assert (dbus_connection_get_is_connected (f->right_client_conn));
g_assert (dbus_connection_get_is_connected (f->right_server_conn));
#else
g_test_skip ("fd-passing not supported on this platform");
#endif
}
static void
test_flood (Fixture *f,
gconstpointer data)
{
#ifdef HAVE_UNIX_FD_PASSING
unsigned int i, j;
DBusMessage *outgoing[SOME_MESSAGES];
dbus_uint32_t serial;
if (f->skip)
return;
test_connect (f, TRUE);
for (j = 0; j < SOME_MESSAGES; j++)
{
outgoing[j] = dbus_message_new_signal ("/com/example/Hello",
"com.example.Hello", "Greeting");
g_assert (outgoing[j] != NULL);
for (i = 0; i < GPOINTER_TO_UINT (data); i++)
{
if (!dbus_message_append_args (outgoing[j],
DBUS_TYPE_UNIX_FD, &f->fd_before,
DBUS_TYPE_INVALID))
oom ("appending fd");
}
}
/* This is in its own loop so we do it as fast as possible */
for (j = 0; j < SOME_MESSAGES; j++)
{
if (!dbus_connection_send (f->left_client_conn, outgoing[j], &serial))
oom ("sending message");
}
for (j = 0; j < SOME_MESSAGES; j++)
{
dbus_message_unref (outgoing[j]);
}
while (g_queue_get_length (&f->messages) < SOME_MESSAGES)
{
test_progress ('.');
test_main_context_iterate (f->ctx, TRUE);
}
g_assert_cmpuint (g_queue_get_length (&f->messages), ==, SOME_MESSAGES);
for (j = 0; j < SOME_MESSAGES; j++)
{
DBusMessage *incoming;
incoming = g_queue_pop_head (&f->messages);
g_assert (dbus_message_contains_unix_fds (incoming));
g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
"com.example.Hello");
g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
dbus_message_unref (incoming);
}
g_assert (dbus_connection_get_is_connected (f->right_client_conn));
g_assert (dbus_connection_get_is_connected (f->right_server_conn));
g_assert (dbus_connection_get_is_connected (f->left_client_conn));
g_assert (dbus_connection_get_is_connected (f->left_server_conn));
#else
g_test_skip ("fd-passing not supported on this platform");
#endif
}
static void
test_odd_limit (Fixture *f,
gconstpointer data)
{
#ifdef HAVE_UNIX_FD_PASSING
DBusMessage *outgoing;
int i;
if (f->skip)
return;
test_connect (f, TRUE);
dbus_connection_set_max_message_unix_fds (f->left_server_conn, 7);
dbus_connection_set_max_message_unix_fds (f->right_server_conn, 7);
outgoing = dbus_message_new_signal ("/com/example/Hello",
"com.example.Hello", "Greeting");
g_assert (outgoing != NULL);
for (i = 0; i < 7 + GPOINTER_TO_INT (data); i++)
{
if (!dbus_message_append_args (outgoing,
DBUS_TYPE_UNIX_FD, &f->fd_before,
DBUS_TYPE_INVALID))
oom ("appending fd");
}
if (!dbus_connection_send (f->left_client_conn, outgoing, NULL))
oom ("sending message");
dbus_message_unref (outgoing);
if (GPOINTER_TO_INT (data) > 0)
{
/* The sender is unceremoniously disconnected. */
while (dbus_connection_get_is_connected (f->left_client_conn) ||
dbus_connection_get_is_connected (f->left_server_conn))
{
test_progress ('.');
test_main_context_iterate (f->ctx, TRUE);
}
/* The message didn't get through without its fds. */
g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 0);
/* The intended victim is unaffected by the left connection's
* misbehaviour. */
g_assert (dbus_connection_get_is_connected (f->right_client_conn));
g_assert (dbus_connection_get_is_connected (f->right_server_conn));
}
else
{
DBusMessage *incoming;
/* We're at or under the limit. The message gets through intact. */
while (g_queue_get_length (&f->messages) < 1)
{
test_progress ('.');
test_main_context_iterate (f->ctx, TRUE);
}
g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 1);
incoming = g_queue_pop_head (&f->messages);
g_assert (dbus_message_contains_unix_fds (incoming));
g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
"com.example.Hello");
g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
g_assert_cmpstr (dbus_message_get_path (incoming), ==,
"/com/example/Hello");
dbus_message_unref (incoming);
g_assert (dbus_connection_get_is_connected (f->right_client_conn));
g_assert (dbus_connection_get_is_connected (f->right_server_conn));
g_assert (dbus_connection_get_is_connected (f->left_client_conn));
g_assert (dbus_connection_get_is_connected (f->left_server_conn));
}
#else
g_test_skip ("fd-passing not supported on this platform");
#endif
}
static void
teardown (Fixture *f,
gconstpointer data G_GNUC_UNUSED)
{
if (f->left_client_conn != NULL)
{
test_connection_shutdown (f->ctx, f->left_client_conn);
dbus_connection_close (f->left_client_conn);
dbus_connection_unref (f->left_client_conn);
f->left_client_conn = NULL;
}
if (f->right_client_conn != NULL)
{
test_connection_shutdown (f->ctx, f->right_client_conn);
dbus_connection_close (f->right_client_conn);
dbus_connection_unref (f->right_client_conn);
f->right_client_conn = NULL;
}
if (f->left_server_conn != NULL)
{
test_connection_shutdown (f->ctx, f->left_server_conn);
dbus_connection_close (f->left_server_conn);
dbus_connection_unref (f->left_server_conn);
f->left_server_conn = NULL;
}
if (f->right_server_conn != NULL)
{
test_connection_shutdown (f->ctx, f->right_server_conn);
dbus_connection_close (f->right_server_conn);
dbus_connection_unref (f->right_server_conn);
f->right_server_conn = NULL;
}
if (f->server != NULL)
{
test_server_shutdown (f->ctx, f->server);
dbus_server_unref (f->server);
f->server = NULL;
}
if (f->ctx != NULL)
test_main_context_unref (f->ctx);
#ifdef HAVE_UNIX_FD_PASSING
if (f->fd_before >= 0 && close (f->fd_before) < 0)
g_error ("%s", g_strerror (errno));
#endif
}
int
main (int argc,
char **argv)
{
int ret;
test_init (&argc, &argv);
#ifdef HAVE_GETRLIMIT
{
struct rlimit lim;
if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
g_error ("Failed to get RLIMIT_NOFILE limit: %s", g_strerror (errno));
if (lim.rlim_cur != RLIM_INFINITY &&
/* only run if we have a fairly generous margin of error
* for stdout, stderr, duplicates, the D-Bus connection, etc. */
lim.rlim_cur < 2 * MAX_MESSAGE_UNIX_FDS * SOME_MESSAGES)
{
g_message ("not enough RLIMIT_NOFILE to run this test");
/* Autotools exit code for "all skipped" */
return 77;
}
}
#endif
g_test_add ("/unsupported", Fixture, NULL, setup_unsupported,
test_unsupported, teardown);
g_test_add ("/relay", Fixture, NULL, setup,
test_relay, teardown);
g_test_add ("/limit", Fixture, NULL, setup,
test_limit, teardown);
g_test_add ("/too-many/plus1", Fixture, GUINT_TO_POINTER (1), setup,
test_too_many, teardown);
g_test_add ("/too-many/plus2", Fixture, GUINT_TO_POINTER (2), setup,
test_too_many, teardown);
g_test_add ("/too-many/plus17", Fixture, GUINT_TO_POINTER (17), setup,
test_too_many, teardown);
g_test_add ("/too-many/split", Fixture, NULL, setup,
test_too_many_split, teardown);
g_test_add ("/flood/1", Fixture, GUINT_TO_POINTER (1),
setup, test_flood, teardown);
#if MAX_MESSAGE_UNIX_FDS >= 2
g_test_add ("/flood/half-limit", Fixture,
GUINT_TO_POINTER (MAX_MESSAGE_UNIX_FDS / 2),
setup, test_flood, teardown);
#endif
#if MAX_MESSAGE_UNIX_FDS >= 4
g_test_add ("/flood/over-half-limit", Fixture,
GUINT_TO_POINTER (3 * MAX_MESSAGE_UNIX_FDS / 4),
setup, test_flood, teardown);
#endif
g_test_add ("/flood/limit", Fixture,
GUINT_TO_POINTER (MAX_MESSAGE_UNIX_FDS), setup, test_flood, teardown);
g_test_add ("/odd-limit/minus1", Fixture, GINT_TO_POINTER (-1), setup,
test_odd_limit, teardown);
g_test_add ("/odd-limit/at", Fixture, GINT_TO_POINTER (0), setup,
test_odd_limit, teardown);
g_test_add ("/odd-limit/plus1", Fixture, GINT_TO_POINTER (+1), setup,
test_odd_limit, teardown);
g_test_add ("/odd-limit/plus2", Fixture, GINT_TO_POINTER (+2), setup,
test_odd_limit, teardown);
ret = g_test_run ();
dbus_shutdown ();
return ret;
}
|