-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinterceptor_smoke_test.go
More file actions
1853 lines (1588 loc) · 49.8 KB
/
interceptor_smoke_test.go
File metadata and controls
1853 lines (1588 loc) · 49.8 KB
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
"fmt"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"testing"
"time"
)
func interceptSourcePath(t *testing.T) string {
t.Helper()
_, file, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("failed to resolve test source path")
}
return filepath.Join(filepath.Dir(file), "lib", "intercept.c")
}
func TestInjectedLibraryHandshakeAndConnectSmoke(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skipf("unsupported runtime platform for smoke test: %s", runtime.GOOS)
}
cc, err := findCCompiler()
if err != nil {
t.Skipf("skipping smoke test: %v", err)
}
helperDir := t.TempDir()
cfg, err := currentInjectionConfig()
if err != nil {
t.Fatalf("currentInjectionConfig failed: %v", err)
}
libraryPath := filepath.Join(helperDir, cfg.LibraryName)
if err := buildInterceptLibraryForTest(t, cc, libraryPath); err != nil {
t.Fatalf("failed to build intercept library: %v", err)
}
helperBinary := filepath.Join(helperDir, "connect-probe")
if err := buildConnectProbeForTest(t, cc, helperBinary); err != nil {
t.Fatalf("failed to build connect probe: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ipcServer, err := NewIPCServer()
if err != nil {
t.Fatalf("NewIPCServer failed: %v", err)
}
defer ipcServer.Close()
subID, ch := ipcServer.Subscribe()
defer ipcServer.Unsubscribe(subID)
socksPort := reserveUnusedPort(t)
cmd := exec.CommandContext(ctx, helperBinary, "203.0.113.1:443")
cmd.Env = buildChildEnv(os.Environ(), cfg, libraryPath, ipcServer.SocketPath(), socksPort, false, false)
if output, err := cmd.CombinedOutput(); err != nil {
t.Logf("probe exited with %v: %s", err, strings.TrimSpace(string(output)))
}
deadline := time.After(5 * time.Second)
var sawReady bool
var sawConnect bool
for !(sawReady && sawConnect) {
select {
case msg, ok := <-ch:
if !ok {
t.Fatal("ipc subscription closed before expected messages arrived")
}
switch msg.Type {
case "READY":
sawReady = true
case "CONNECT":
sawConnect = true
if msg.Addr == "" {
t.Fatal("CONNECT message did not include address")
}
}
case <-deadline:
t.Fatalf("timed out waiting for READY and CONNECT messages (ready=%v connect=%v)", sawReady, sawConnect)
}
}
}
func TestInjectedLibraryBypassesLocalhostConnects(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skipf("unsupported runtime platform for smoke test: %s", runtime.GOOS)
}
cc, err := findCCompiler()
if err != nil {
t.Skipf("skipping localhost bypass test: %v", err)
}
helperDir := t.TempDir()
cfg, err := currentInjectionConfig()
if err != nil {
t.Fatalf("currentInjectionConfig failed: %v", err)
}
libraryPath := filepath.Join(helperDir, cfg.LibraryName)
if err := buildInterceptLibraryForTest(t, cc, libraryPath); err != nil {
t.Fatalf("failed to build intercept library: %v", err)
}
helperBinary := filepath.Join(helperDir, "connect-probe")
if err := buildConnectProbeForTest(t, cc, helperBinary); err != nil {
t.Fatalf("failed to build connect probe: %v", err)
}
ipcServer, err := NewIPCServer()
if err != nil {
t.Fatalf("NewIPCServer failed: %v", err)
}
defer ipcServer.Close()
subID, ch := ipcServer.Subscribe()
defer ipcServer.Unsubscribe(subID)
socksPort := reserveUnusedPort(t)
cmd := exec.Command(helperBinary, "127.0.0.1:9")
cmd.Env = buildChildEnv(os.Environ(), cfg, libraryPath, ipcServer.SocketPath(), socksPort, false, false)
if output, err := cmd.CombinedOutput(); err != nil {
t.Logf("probe exited with %v: %s", err, strings.TrimSpace(string(output)))
}
deadline := time.After(2 * time.Second)
sawReady := false
for {
select {
case msg, ok := <-ch:
if !ok {
t.Fatal("ipc subscription closed unexpectedly")
}
switch msg.Type {
case "READY":
sawReady = true
case "CONNECT":
t.Fatalf("localhost connect should not be intercepted, saw CONNECT for %q", msg.Addr)
}
case <-deadline:
if !sawReady {
t.Fatal("timed out waiting for READY message")
}
return
}
}
}
func TestInterceptorSourceKeepsUnixDomainConnectsBypassed(t *testing.T) {
data, err := os.ReadFile(interceptSourcePath(t))
if err != nil {
t.Fatalf("failed to read interceptor source: %v", err)
}
content := string(data)
requiredSnippets := []string{
"if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6) {",
"return 0; // Only intercept IP connections",
"case AF_UNIX:",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(content, snippet) {
t.Fatalf("interceptor source missing expected unix-domain bypass snippet: %q", snippet)
}
}
}
func TestInjectedLibraryReportsBindSmoke(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skipf("unsupported runtime platform for smoke test: %s", runtime.GOOS)
}
cc, err := findCCompiler()
if err != nil {
t.Skipf("skipping bind smoke test: %v", err)
}
helperDir := t.TempDir()
cfg, err := currentInjectionConfig()
if err != nil {
t.Fatalf("currentInjectionConfig failed: %v", err)
}
libraryPath := filepath.Join(helperDir, cfg.LibraryName)
if err := buildInterceptLibraryForTest(t, cc, libraryPath); err != nil {
t.Fatalf("failed to build intercept library: %v", err)
}
helperBinary := filepath.Join(helperDir, "bind-probe")
if err := buildBindProbeForTest(t, cc, helperBinary); err != nil {
t.Fatalf("failed to build bind probe: %v", err)
}
ipcServer, err := NewIPCServer()
if err != nil {
t.Fatalf("NewIPCServer failed: %v", err)
}
defer ipcServer.Close()
subID, ch := ipcServer.Subscribe()
defer ipcServer.Unsubscribe(subID)
socksPort := reserveUnusedPort(t)
cmd := exec.Command(helperBinary)
cmd.Env = buildChildEnv(os.Environ(), cfg, libraryPath, ipcServer.SocketPath(), socksPort, false, false)
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("bind probe failed: %v: %s", err, strings.TrimSpace(string(output)))
}
deadline := time.After(5 * time.Second)
var sawReady bool
var sawBind bool
for !(sawReady && sawBind) {
select {
case msg, ok := <-ch:
if !ok {
t.Fatal("ipc subscription closed before expected messages arrived")
}
switch msg.Type {
case "READY":
sawReady = true
case "BIND":
sawBind = true
if msg.Port == 0 {
t.Fatal("BIND message did not include a concrete port")
}
}
case <-deadline:
t.Fatalf("timed out waiting for READY and BIND messages (ready=%v bind=%v)", sawReady, sawBind)
}
}
}
func TestInjectedLibraryHandlesNonBlockingConnectSmoke(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skipf("unsupported runtime platform for smoke test: %s", runtime.GOOS)
}
cc, err := findCCompiler()
if err != nil {
t.Skipf("skipping non-blocking smoke test: %v", err)
}
helperDir := t.TempDir()
cfg, err := currentInjectionConfig()
if err != nil {
t.Fatalf("currentInjectionConfig failed: %v", err)
}
libraryPath := filepath.Join(helperDir, cfg.LibraryName)
if err := buildInterceptLibraryForTest(t, cc, libraryPath); err != nil {
t.Fatalf("failed to build intercept library: %v", err)
}
helperBinary := filepath.Join(helperDir, "nonblocking-connect-probe")
if err := buildNonBlockingConnectProbeForTest(t, cc, helperBinary); err != nil {
t.Fatalf("failed to build non-blocking connect probe: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ipcServer, err := NewIPCServer()
if err != nil {
t.Fatalf("NewIPCServer failed: %v", err)
}
defer ipcServer.Close()
subID, ch := ipcServer.Subscribe()
defer ipcServer.Unsubscribe(subID)
socksPort := startSOCKSSuccessServer(t)
cmd := exec.CommandContext(ctx, helperBinary, "203.0.113.1:443")
cmd.Env = buildChildEnv(os.Environ(), cfg, libraryPath, ipcServer.SocketPath(), socksPort, false, false)
output, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
t.Fatalf("non-blocking connect probe timed out, output: %s", strings.TrimSpace(string(output)))
}
if err != nil {
t.Fatalf("non-blocking probe failed: %v: %s", err, strings.TrimSpace(string(output)))
}
deadline := time.After(5 * time.Second)
var sawReady bool
var sawConnect bool
for !(sawReady && sawConnect) {
select {
case msg, ok := <-ch:
if !ok {
t.Fatal("ipc subscription closed before expected messages arrived")
}
switch msg.Type {
case "READY":
sawReady = true
case "CONNECT":
sawConnect = true
if msg.Addr == "" {
t.Fatal("CONNECT message did not include address")
}
}
case <-deadline:
t.Fatalf("timed out waiting for READY and CONNECT messages (ready=%v connect=%v)", sawReady, sawConnect)
}
}
}
func TestInjectedLibraryVirtualizesGetpeernameAfterSOCKSConnect(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skipf("getpeername virtualization is only exercised on Linux now, runtime=%s", runtime.GOOS)
}
cc, err := findCCompiler()
if err != nil {
t.Skipf("skipping getpeername virtualization test: %v", err)
}
helperDir := t.TempDir()
cfg, err := currentInjectionConfig()
if err != nil {
t.Fatalf("currentInjectionConfig failed: %v", err)
}
libraryPath := filepath.Join(helperDir, cfg.LibraryName)
if err := buildInterceptLibraryForTest(t, cc, libraryPath); err != nil {
t.Fatalf("failed to build intercept library: %v", err)
}
helperBinary := filepath.Join(helperDir, "getpeername-probe")
if err := buildGetPeerNameProbeForTest(t, cc, helperBinary); err != nil {
t.Fatalf("failed to build getpeername probe: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ipcServer, err := NewIPCServer()
if err != nil {
t.Fatalf("NewIPCServer failed: %v", err)
}
defer ipcServer.Close()
subID, ch := ipcServer.Subscribe()
defer ipcServer.Unsubscribe(subID)
socksPort := startSOCKSSuccessServer(t)
cmd := exec.CommandContext(ctx, helperBinary, "203.0.113.1:443")
cmd.Env = buildChildEnv(os.Environ(), cfg, libraryPath, ipcServer.SocketPath(), socksPort, false, false)
output, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
t.Fatalf("getpeername probe timed out, output: %s", strings.TrimSpace(string(output)))
}
if err != nil {
t.Fatalf("getpeername probe failed: %v: %s", err, strings.TrimSpace(string(output)))
}
deadline := time.After(5 * time.Second)
var sawReady bool
var sawConnect bool
for !(sawReady && sawConnect) {
select {
case msg, ok := <-ch:
if !ok {
t.Fatal("ipc subscription closed before expected messages arrived")
}
switch msg.Type {
case "READY":
sawReady = true
case "CONNECT":
sawConnect = true
}
case <-deadline:
t.Fatalf("timed out waiting for READY and CONNECT messages (ready=%v connect=%v)", sawReady, sawConnect)
}
}
}
func TestInterceptorSourceClearsVirtualPeerStateBeforeFallbackConnects(t *testing.T) {
data, err := os.ReadFile(interceptSourcePath(t))
if err != nil {
t.Fatalf("failed to read interceptor source: %v", err)
}
content := string(data)
requiredSnippets := []string{
"static void forget_virtual_peer(int sockfd)",
"forget_virtual_peer(sockfd);\n errno = EHOSTUNREACH;",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(content, snippet) {
t.Fatalf("interceptor source missing expected virtual-peer cleanup snippet: %q", snippet)
}
}
fallbacks := []string{
"forget_virtual_peer(sockfd);\n#ifdef __APPLE__\n return raw_connect_call(sockfd, addr, addrlen);",
"forget_virtual_peer(sockfd);\n#ifdef __APPLE__\n return raw_connect_call(sockfd, addr, addrlen);\n#else\n return call_real_connect(sockfd, addr, addrlen);",
}
foundFallback := false
for _, snippet := range fallbacks {
if strings.Contains(content, snippet) {
foundFallback = true
break
}
}
if !foundFallback {
t.Fatalf("interceptor source missing expected virtual-peer fallback connect cleanup")
}
}
func TestInterceptorSourceDeclaresMacOSInterpositionEntryPoints(t *testing.T) {
data, err := os.ReadFile(interceptSourcePath(t))
if err != nil {
t.Fatalf("failed to read interceptor source: %v", err)
}
content := string(data)
requiredSnippets := []string{
"#ifdef __APPLE__",
"DYLD_INTERPOSE(wrapguard_connect, connect)",
"DYLD_INTERPOSE(wrapguard_bind, bind)",
"DYLD_INTERPOSE(wrapguard_connectx, connectx)",
"DYLD_INTERPOSE(wrapguard_sendto, sendto)",
"DYLD_INTERPOSE(wrapguard_sendmsg, sendmsg)",
"return raw_bind_call(sockfd, addr, addrlen);",
"dlsym(RTLD_NEXT, \"connect\")",
"dlsym(RTLD_NEXT, \"bind\")",
"dlsym(RTLD_NEXT, \"getpeername\")",
"dlsym(RTLD_NEXT, \"connectx\")",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(content, snippet) {
t.Fatalf("interceptor source missing expected macOS interposition snippet: %q", snippet)
}
}
}
func TestInterceptorSourceUsesMacOSSafeDebugIPCForQUICSuppression(t *testing.T) {
data, err := os.ReadFile(interceptSourcePath(t))
if err != nil {
t.Fatalf("failed to read interceptor source: %v", err)
}
content := string(data)
requiredSnippets := []string{
"send_ipc_message(\"DEBUG\"",
"log_debugf(\"Blocking likely QUIC UDP sendto() to %s\", addr_str);",
"log_debugf(\"Blocking likely QUIC UDP sendmsg() to %s\", addr_str);",
"DYLD_INTERPOSE(wrapguard_sendto, sendto)",
"DYLD_INTERPOSE(wrapguard_sendmsg, sendmsg)",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(content, snippet) {
t.Fatalf("interceptor source missing expected macOS QUIC observability snippet: %q", snippet)
}
}
}
func TestInterceptorSourceBlocksConnectedDarwinUDPSendPathsViaPeerLookup(t *testing.T) {
data, err := os.ReadFile(interceptSourcePath(t))
if err != nil {
t.Fatalf("failed to read interceptor source: %v", err)
}
content := string(data)
requiredSnippets := []string{
"if (target == NULL || target_len < (socklen_t)sizeof(sa_family_t)) {",
"if (call_real_getpeername(sockfd, (struct sockaddr *)&target_storage, &target_len) != 0) {",
"if (!should_block_udp_target(target)) {",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(content, snippet) {
t.Fatalf("interceptor source missing expected connected-UDP suppression snippet: %q", snippet)
}
}
}
func TestInterceptorSourceKeepsDarwinGetpeernameOutOfTheInterposeTable(t *testing.T) {
data, err := os.ReadFile(interceptSourcePath(t))
if err != nil {
t.Fatalf("failed to read interceptor source: %v", err)
}
content := string(data)
if strings.Contains(content, "DYLD_INTERPOSE(wrapguard_getpeername, getpeername)") {
t.Fatal("Darwin should not interpose getpeername anymore; that regression breaks browser socket-thread behavior")
}
requiredSnippets := []string{
"#ifndef __APPLE__",
"int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(content, snippet) {
t.Fatalf("interceptor source missing expected Darwin getpeername guard snippet: %q", snippet)
}
}
}
func TestInterceptorSourceKeepsExpectReadyOneShotForDarwinChildren(t *testing.T) {
data, err := os.ReadFile(interceptSourcePath(t))
if err != nil {
t.Fatalf("failed to read interceptor source: %v", err)
}
content := string(data)
requiredSnippets := []string{
"expect_ready_cached = expect_ready_enabled();",
"if (expect_ready_cached) {\n unsetenv(\"WRAPGUARD_EXPECT_READY\");\n }",
"if (expect_ready_cached && ipc_path != NULL && socks_port != 0) {\n send_ipc_message(\"READY\", -1, socks_port, NULL);\n }",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(content, snippet) {
t.Fatalf("interceptor source missing expected one-shot READY snippet: %q", snippet)
}
}
}
func TestInterceptorSourcePreservesErrnoAcrossMacOSDebugIPC(t *testing.T) {
data, err := os.ReadFile(interceptSourcePath(t))
if err != nil {
t.Fatalf("failed to read interceptor source: %v", err)
}
content := string(data)
requiredSnippets := []string{
"int saved_errno = errno;",
"send_ipc_message(\"DEBUG\", -1, 0, message);",
"send_ipc_message(\"ERROR\", -1, 0, message);",
"errno = saved_errno;",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(content, snippet) {
t.Fatalf("interceptor source missing expected errno-preservation snippet: %q", snippet)
}
}
}
func TestInterceptorSourceAvoidsPreMutatingConnectxOutputsBeforeFallback(t *testing.T) {
data, err := os.ReadFile(interceptSourcePath(t))
if err != nil {
t.Fatalf("failed to read interceptor source: %v", err)
}
content := string(data)
requiredSnippets := []string{
"if (endpoints == NULL || endpoints->sae_dstaddr == NULL || endpoints->sae_dstaddrlen < (socklen_t)sizeof(sa_family_t)) {\n return call_real_connectx(sockfd, endpoints, associd, flags, iov, iovcnt, len, connid);\n }",
"if (len != NULL) {\n *len = 0;\n }",
"if (connid != NULL) {\n *connid = SAE_CONNID_ANY;\n }",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(content, snippet) {
t.Fatalf("interceptor source missing expected connectx compatibility snippet: %q", snippet)
}
}
}
func TestInterceptorSourcePinsMozillaHelperPassthroughPolicyOnMacOS(t *testing.T) {
data, err := os.ReadFile(interceptSourcePath(t))
if err != nil {
t.Fatalf("failed to read interceptor source: %v", err)
}
content := string(data)
requiredSnippets := []string{
"#include <crt_externs.h>",
"static int should_passthrough_mozilla_process(char *const *argv)",
"if (str_equals(base, \"plugin-container\")) {",
"if (argc > 1 && str_equals(argv[argc - 1], \"socket\")) {",
"passthrough_mode_cached = should_passthrough_current_process();",
"if (passthrough_mode_cached) {",
"return raw_connect_call(sockfd, addr, addrlen);",
"return raw_connectx_call(sockfd, endpoints, associd, flags, iov, iovcnt, len, connid);",
"int suppress_debug_log = addr->sa_family == AF_UNIX && sock_type == SOCK_DGRAM;",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(content, snippet) {
t.Fatalf("interceptor source missing expected Mozilla helper passthrough snippet: %q", snippet)
}
}
}
func TestInterceptorSourcePinsVirtualPeerBookkeepingForGetpeername(t *testing.T) {
data, err := os.ReadFile(interceptSourcePath(t))
if err != nil {
t.Fatalf("failed to read interceptor source: %v", err)
}
content := string(data)
requiredSnippets := []string{
"static int wrapguard_getpeername_impl(int sockfd, struct sockaddr *addr, socklen_t *addrlen)",
"if (lookup_virtual_peer(sockfd, addr, addrlen)) {",
"remember_virtual_peer(sockfd, addr, addrlen);",
"#ifndef __APPLE__",
"int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {",
}
for _, snippet := range requiredSnippets {
if !strings.Contains(content, snippet) {
t.Fatalf("interceptor source missing expected virtual-peer snippet: %q", snippet)
}
}
}
func TestInjectedLibraryBlocksLikelyQUICUDPConnectOnDarwin(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("macOS-only QUIC suppression smoke test")
}
cc, err := findCCompiler()
if err != nil {
t.Skipf("skipping UDP suppression smoke test: %v", err)
}
helperDir := t.TempDir()
cfg, err := currentInjectionConfig()
if err != nil {
t.Fatalf("currentInjectionConfig failed: %v", err)
}
libraryPath := filepath.Join(helperDir, cfg.LibraryName)
if err := buildInterceptLibraryForTest(t, cc, libraryPath); err != nil {
t.Fatalf("failed to build intercept library: %v", err)
}
helperBinary := filepath.Join(helperDir, "udp-connect-probe")
if err := buildUDPConnectProbeForTest(t, cc, helperBinary); err != nil {
t.Fatalf("failed to build udp connect probe: %v", err)
}
ipcServer, err := NewIPCServer()
if err != nil {
t.Fatalf("NewIPCServer failed: %v", err)
}
defer ipcServer.Close()
subID, ch := ipcServer.Subscribe()
defer ipcServer.Unsubscribe(subID)
socksPort := reserveUnusedPort(t)
cmd := exec.Command(helperBinary, "203.0.113.1:443")
cmd.Env = buildChildEnv(os.Environ(), cfg, libraryPath, ipcServer.SocketPath(), socksPort, false, false)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("udp connect probe failed: %v: %s", err, strings.TrimSpace(string(output)))
}
deadline := time.After(2 * time.Second)
sawReady := false
for {
select {
case msg, ok := <-ch:
if !ok {
t.Fatal("ipc subscription closed unexpectedly")
}
switch msg.Type {
case "READY":
sawReady = true
case "CONNECT":
t.Fatalf("UDP/443 connect should not have been tunneled through SOCKS, saw CONNECT for %q", msg.Addr)
}
case <-deadline:
if !sawReady {
t.Fatal("timed out waiting for READY message")
}
return
}
}
}
func TestInjectedLibraryInterceptsConnectxOnDarwin(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("macOS-only connectx smoke test")
}
cc, err := findCCompiler()
if err != nil {
t.Skipf("skipping connectx smoke test: %v", err)
}
helperDir := t.TempDir()
cfg, err := currentInjectionConfig()
if err != nil {
t.Fatalf("currentInjectionConfig failed: %v", err)
}
libraryPath := filepath.Join(helperDir, cfg.LibraryName)
if err := buildInterceptLibraryForTest(t, cc, libraryPath); err != nil {
t.Fatalf("failed to build intercept library: %v", err)
}
helperBinary := filepath.Join(helperDir, "connectx-probe")
if err := buildConnectxProbeForTest(t, cc, helperBinary); err != nil {
t.Fatalf("failed to build connectx probe: %v", err)
}
ipcServer, err := NewIPCServer()
if err != nil {
t.Fatalf("NewIPCServer failed: %v", err)
}
defer ipcServer.Close()
subID, ch := ipcServer.Subscribe()
defer ipcServer.Unsubscribe(subID)
socksPort := reserveUnusedPort(t)
cmd := exec.Command(helperBinary, "203.0.113.1:443")
cmd.Env = buildChildEnv(os.Environ(), cfg, libraryPath, ipcServer.SocketPath(), socksPort, true, false)
if output, err := cmd.CombinedOutput(); err != nil {
t.Logf("connectx probe exited with %v: %s", err, strings.TrimSpace(string(output)))
}
deadline := time.After(5 * time.Second)
var sawReady bool
var sawConnect bool
for !(sawReady && sawConnect) {
select {
case msg, ok := <-ch:
if !ok {
t.Fatal("ipc subscription closed before expected messages arrived")
}
switch msg.Type {
case "READY":
sawReady = true
case "CONNECT":
sawConnect = true
if msg.Addr == "" {
t.Fatal("CONNECT message did not include address")
}
}
case <-deadline:
t.Fatalf("timed out waiting for READY and CONNECT messages from connectx probe (ready=%v connect=%v)", sawReady, sawConnect)
}
}
}
func TestInjectedLibraryAppliesMozillaRolePolicyOnDarwin(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("macOS-only Mozilla role policy smoke test")
}
cc, err := findCCompiler()
if err != nil {
t.Skipf("skipping Mozilla role policy smoke test: %v", err)
}
helperDir := t.TempDir()
cfg, err := currentInjectionConfig()
if err != nil {
t.Fatalf("currentInjectionConfig failed: %v", err)
}
libraryPath := filepath.Join(helperDir, cfg.LibraryName)
if err := buildInterceptLibraryForTest(t, cc, libraryPath); err != nil {
t.Fatalf("failed to build intercept library: %v", err)
}
helperBinary := filepath.Join(helperDir, "nonblocking-role-probe")
if err := buildNonBlockingRoleProbeForTest(t, cc, helperBinary); err != nil {
t.Fatalf("failed to build non-blocking role probe: %v", err)
}
tests := []struct {
name string
linkName string
args []string
wantConnect bool
}{
{
name: "socket-process-stays-intercepted",
linkName: "plugin-container",
args: []string{"203.0.113.1:443", "socket"},
wantConnect: true,
},
{
name: "gpu-helper-stays-passthrough",
linkName: "gpu-helper",
args: []string{"203.0.113.1:443"},
wantConnect: false,
},
{
name: "librewolf-main-process-stays-intercepted",
linkName: "librewolf",
args: []string{"203.0.113.1:443"},
wantConnect: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
roleBinary := filepath.Join(helperDir, tt.linkName)
if err := os.Link(helperBinary, roleBinary); err != nil {
if err := os.Symlink(helperBinary, roleBinary); err != nil {
t.Fatalf("failed to create role probe alias: %v / %v", err, err)
}
}
ipcServer, err := NewIPCServer()
if err != nil {
t.Fatalf("NewIPCServer failed: %v", err)
}
defer ipcServer.Close()
subID, ch := ipcServer.Subscribe()
defer ipcServer.Unsubscribe(subID)
socksPort := startSOCKSSuccessServer(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, roleBinary, tt.args...)
cmd.Env = buildChildEnv(os.Environ(), cfg, libraryPath, ipcServer.SocketPath(), socksPort, true, false)
output, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
t.Fatalf("role probe timed out, output: %s", strings.TrimSpace(string(output)))
}
if err != nil {
t.Fatalf("role probe failed: %v: %s", err, strings.TrimSpace(string(output)))
}
deadline := time.After(5 * time.Second)
sawReady := false
sawConnect := false
for {
select {
case msg, ok := <-ch:
if !ok {
t.Fatal("ipc subscription closed unexpectedly")
}
switch msg.Type {
case "READY":
sawReady = true
case "CONNECT":
sawConnect = true
}
case <-deadline:
if !sawReady {
t.Fatal("timed out waiting for READY from role probe")
}
if sawConnect != tt.wantConnect {
t.Fatalf("CONNECT visibility mismatch for %s: got %v want %v", tt.linkName, sawConnect, tt.wantConnect)
}
return
}
}
})
}
}
func TestInjectedLibraryStripsMacOSInjectionEnvForDescendantsInCompatMode(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("macOS-only GUI compatibility smoke test")
}
cc, err := findCCompiler()
if err != nil {
t.Skipf("skipping GUI compatibility smoke test: %v", err)
}
helperDir := t.TempDir()
cfg, err := currentInjectionConfig()
if err != nil {
t.Fatalf("currentInjectionConfig failed: %v", err)
}
libraryPath := filepath.Join(helperDir, cfg.LibraryName)
if err := buildInterceptLibraryForTest(t, cc, libraryPath); err != nil {
t.Fatalf("failed to build intercept library: %v", err)
}
envDumpBinary := filepath.Join(helperDir, "env-dump-probe")
if err := buildEnvDumpProbeForTest(t, cc, envDumpBinary); err != nil {
t.Fatalf("failed to build env dump probe: %v", err)
}
spawnBinary := filepath.Join(helperDir, "spawn-child-probe")
if err := buildSpawnChildProbeForTest(t, cc, spawnBinary); err != nil {
t.Fatalf("failed to build spawn child probe: %v", err)
}
ipcServer, err := NewIPCServer()
if err != nil {
t.Fatalf("NewIPCServer failed: %v", err)
}
defer ipcServer.Close()
subID, ch := ipcServer.Subscribe()
defer ipcServer.Unsubscribe(subID)
outputPath := filepath.Join(t.TempDir(), "child-env.txt")
socksPort := reserveUnusedPort(t)
cmd := exec.Command(spawnBinary, envDumpBinary, outputPath)
cmd.Env = buildChildEnv(os.Environ(), cfg, libraryPath, ipcServer.SocketPath(), socksPort, true, true)
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("spawn child probe failed: %v: %s", err, strings.TrimSpace(string(output)))
}
sawReady := false
deadline := time.After(5 * time.Second)
for !sawReady {
select {
case msg, ok := <-ch:
if !ok {
t.Fatal("ipc subscription closed before READY arrived")
}
if msg.Type == "READY" {
sawReady = true
}
case <-deadline:
t.Fatal("timed out waiting for READY from injected parent")
}
}
data, err := os.ReadFile(outputPath)
if err != nil {
t.Fatalf("failed to read child env output: %v", err)
}
got := string(data)
for _, key := range []string{
"DYLD_INSERT_LIBRARIES",
"DYLD_FORCE_FLAT_NAMESPACE",
envWrapGuardExpectRDY,
envWrapGuardIPCPath,
envWrapGuardSOCKSPort,
envWrapGuardDebug,
envWrapGuardDebugIPC,
envWrapGuardBlockUDP,
envWrapGuardNoInherit,
} {
if strings.Contains(got, key+"=") && !strings.Contains(got, key+"=\n") {
t.Fatalf("child unexpectedly inherited %s: %q", key, got)
}
}
}
func TestInjectedLibrarySuppressesMacOSUDP443SendtoAndSendmsgSmoke(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("macOS-only QUIC send-path suppression smoke test")
}
cc, err := findCCompiler()
if err != nil {
t.Skipf("skipping UDP send-path suppression smoke test: %v", err)
}
helperDir := t.TempDir()
cfg, err := currentInjectionConfig()
if err != nil {
t.Fatalf("currentInjectionConfig failed: %v", err)
}
libraryPath := filepath.Join(helperDir, cfg.LibraryName)
if err := buildInterceptLibraryForTest(t, cc, libraryPath); err != nil {
t.Fatalf("failed to build intercept library: %v", err)
}
helperBinary := filepath.Join(helperDir, "udp-send-probe")
if err := buildUDPSendProbeForTest(t, cc, helperBinary); err != nil {
t.Fatalf("failed to build udp send probe: %v", err)
}
for _, mode := range []string{"sendto", "sendmsg", "connected-sendto", "connected-sendmsg"} {
t.Run(mode, func(t *testing.T) {
ipcServer, err := NewIPCServer()
if err != nil {
t.Fatalf("NewIPCServer failed: %v", err)
}
defer ipcServer.Close()
subID, ch := ipcServer.Subscribe()
defer ipcServer.Unsubscribe(subID)
socksPort := reserveUnusedPort(t)
cmd := exec.Command(helperBinary, "203.0.113.1:443", mode)
cmd.Env = buildChildEnv(os.Environ(), cfg, libraryPath, ipcServer.SocketPath(), socksPort, true, false)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("udp %s probe failed: %v: %s", mode, err, strings.TrimSpace(string(output)))
}
if strings.Contains(string(output), "WrapGuard DYLD:") || strings.Contains(string(output), "WrapGuard LD_PRELOAD:") {