forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 11
/
sock.c
1206 lines (1080 loc) · 39 KB
/
sock.c
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
#include <fcntl.h>
#include <netinet/tcp.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include "kernel/calls.h"
#include "fs/fd.h"
#include "fs/inode.h"
#include "fs/path.h"
#include "fs/real.h"
#include "fs/sock.h"
#include "debug.h"
#define SOCKET_TYPE_MASK 0xf
const struct fd_ops socket_fdops;
static lock_t peer_lock = LOCK_INITIALIZER;
static fd_t sock_fd_create(int sock_fd, int domain, int type, int protocol) {
struct fd *fd = adhoc_fd_create(&socket_fdops);
if (fd == NULL)
return _ENOMEM;
fd->stat.mode = S_IFSOCK | 0666;
fd->real_fd = sock_fd;
fd->socket.domain = domain;
fd->socket.type = type & SOCKET_TYPE_MASK;
fd->socket.protocol = protocol;
if (domain == AF_LOCAL_) {
cond_init(&fd->socket.unix_got_peer);
list_init(&fd->socket.unix_scm);
}
return f_install(fd, type & ~SOCKET_TYPE_MASK);
}
int_t sys_socket(dword_t domain, dword_t type, dword_t protocol) {
STRACE("socket(%d, %d, %d)", domain, type, protocol);
int real_domain = sock_family_to_real(domain);
if (real_domain < 0)
return _EINVAL;
int real_type = sock_type_to_real(type, protocol);
if (real_type < 0)
return _EINVAL;
// this hack makes mtr work
if (type == SOCK_RAW_ && protocol == IPPROTO_RAW)
protocol = IPPROTO_ICMP;
int sock = socket(real_domain, real_type, protocol);
if (sock < 0)
return errno_map();
#ifdef __APPLE__
if (domain == AF_INET_ && type == SOCK_DGRAM_) {
// in some cases, such as ICMP, datagram sockets on mac can default to
// including the IP header like raw sockets
int one = 1;
setsockopt(sock, IPPROTO_IP, IP_STRIPHDR, &one, sizeof(one));
}
#endif
fd_t f = sock_fd_create(sock, domain, type, protocol);
if (f < 0)
close(sock);
return f;
}
static void inode_release_if_exist(struct inode_data *inode) {
if (inode != NULL)
inode_release(inode);
}
static struct fd *sock_getfd(fd_t sock_fd) {
struct fd *sock = f_get(sock_fd);
if (sock == NULL || sock->ops != &socket_fdops)
return NULL;
return sock;
}
static uint32_t unix_socket_next_id() {
static uint32_t next_id = 0;
static lock_t next_id_lock = LOCK_INITIALIZER;
lock(&next_id_lock);
uint32_t id = ++next_id;
unlock(&next_id_lock);
return id;
}
static int unix_socket_get(const char *path_raw, struct fd *bind_fd, uint32_t *socket_id) {
char path[MAX_PATH];
int err = path_normalize(AT_PWD, path_raw, path, N_SYMLINK_FOLLOW);
if (err < 0)
return err;
struct mount *mount = find_mount_and_trim_path(path);
struct statbuf stat;
err = mount->fs->stat(mount, path, &stat);
// If bind was called, there are some funny semantics.
if (bind_fd != NULL) {
// If the file exists, fail.
if (err == 0) {
err = _EADDRINUSE;
goto out;
}
// If the file can't be found, try to create it as a socket.
if (err < 0) {
mode_t_ mode = 0777;
struct fs_info *fs = current->fs;
lock(&fs->lock);
mode &= ~fs->umask;
unlock(&fs->lock);
err = mount->fs->mknod(mount, path, S_IFSOCK | mode, 0);
if (err < 0)
goto out;
err = mount->fs->stat(mount, path, &stat);
if (err < 0)
goto out;
}
}
// If something other than bind was called, just do the obvious thing and
// fail if stat failed.
if (bind_fd == NULL && err < 0)
goto out;
if (!S_ISSOCK(stat.mode)) {
err = _ENOTSOCK;
goto out;
}
// Look up the socket ID for the inode number.
struct inode_data *inode = inode_get(mount, stat.inode);
lock(&inode->lock);
if (inode->socket_id == 0)
inode->socket_id = unix_socket_next_id();
unlock(&inode->lock);
*socket_id = inode->socket_id;
mount_release(mount);
if (bind_fd != NULL)
bind_fd->socket.unix_name_inode = inode;
else
inode_release(inode);
return 0;
out:
mount_release(mount);
return err;
}
// Dan Bernstein's simple and decently effective hash function
static uint32_t str_hash(const char *str) {
uint32_t hash = 5381;
for (int i = 0; str[i] != '\0'; i++) {
hash = 33 * hash ^ str[i];
}
return hash;
}
// The abstract socket namespace is a lot simpler than it sounds: if the first
// byte of the path is a null byte, then it gets looked up in this hashtable
// instead of the filesystem.
struct unix_abstract {
unsigned refcount;
uint32_t hash;
uint32_t socket_id;
struct list links;
};
#define ABSTRACT_HASH_SIZE 1024
static struct list abstract_hash[ABSTRACT_HASH_SIZE];
static lock_t unix_abstract_lock = LOCK_INITIALIZER;
static int unix_abstract_get(const char *name, struct fd *bind_fd, uint32_t *socket_id) {
uint32_t hash = str_hash(name);
lock(&unix_abstract_lock);
struct unix_abstract *sock_tmp;
struct unix_abstract *sock = NULL;
struct list *bucket = &abstract_hash[hash % ABSTRACT_HASH_SIZE];
if (list_null(bucket))
list_init(bucket);
list_for_each_entry(bucket, sock_tmp, links) {
if (sock_tmp->hash == hash) {
sock = sock_tmp;
break;
}
}
if (bind_fd != NULL && sock != NULL) {
unlock(&unix_abstract_lock);
return _EEXIST;
}
if (bind_fd == NULL && sock == NULL) {
unlock(&unix_abstract_lock);
return _ENOENT;
}
if (sock == NULL) {
sock = malloc(sizeof(struct unix_abstract));
sock->refcount = 0;
sock->hash = hash;
sock->socket_id = unix_socket_next_id();
list_add(bucket, &sock->links);
}
sock->refcount++;
unlock(&unix_abstract_lock);
*socket_id = sock->socket_id;
if (bind_fd != NULL)
bind_fd->socket.unix_name_abstract = sock;
return 0;
}
static void unix_abstract_release(struct unix_abstract *name) {
lock(&unix_abstract_lock);
if (--name->refcount == 0) {
list_remove(&name->links);
free(name);
}
unlock(&unix_abstract_lock);
}
const char *sock_tmp_prefix = "/tmp/ishsock";
static int sockaddr_read_bind(addr_t sockaddr_addr, void *sockaddr, uint_t *sockaddr_len, struct fd *bind_fd) {
// Make sure we can read things without overflowing buffers
if (*sockaddr_len < sizeof(socklen_t))
return _EINVAL;
if (*sockaddr_len > sizeof(struct sockaddr_max_))
return _EINVAL;
if (user_read(sockaddr_addr, sockaddr, *sockaddr_len))
return _EFAULT;
struct sockaddr *real_addr = sockaddr;
struct sockaddr_ *fake_addr = sockaddr;
real_addr->sa_family = sock_family_to_real(fake_addr->family);
switch (real_addr->sa_family) {
case PF_INET:
if (*sockaddr_len < sizeof(struct sockaddr_in))
return _EINVAL;
break;
case PF_INET6:
if (*sockaddr_len < sizeof(struct sockaddr_in6))
return _EINVAL;
break;
case PF_LOCAL: {
// First pull out the path, being careful to not overflow anything.
char path[sizeof(struct sockaddr_max_) - offsetof(struct sockaddr_max_, data) + 1]; // big enough
size_t path_size = *sockaddr_len - offsetof(struct sockaddr_, data);
memcpy(path, fake_addr->data, path_size);
path[path_size] = '\0';
uint32_t socket_id;
int err;
if (path_size == 0) {
return _ENOENT;
} else if (path[0] != '\0') {
STRACE(" unix socket %s", path);
err = unix_socket_get(path, bind_fd, &socket_id);
} else {
STRACE(" unix abstract socket %s", path + 1);
err = unix_abstract_get(path + 1, bind_fd, &socket_id);
}
if (err < 0)
return err;
struct sockaddr_un *real_addr_un = sockaddr;
size_t path_len = sprintf(real_addr_un->sun_path, "%s%d.%u", sock_tmp_prefix, getpid(), socket_id);
// The call to real bind will fail if the backing socket already
// exists from a previous run or something. We already checked that
// the fake file doesn't exist in unix_socket_get, so try a simple
// solution.
if (bind_fd != NULL)
unlink(real_addr_un->sun_path);
*sockaddr_len = offsetof(struct sockaddr_un, sun_path) + path_len;
break;
}
default:
return _EINVAL;
}
return 0;
}
static int sockaddr_read(addr_t sockaddr_addr, void *sockaddr, uint_t *sockaddr_len) {
struct inode_data *inode = NULL;
int err = sockaddr_read_bind(sockaddr_addr, sockaddr, sockaddr_len, NULL);
inode_release_if_exist(inode);
return err;
}
static int sockaddr_write(addr_t sockaddr_addr, void *sockaddr, uint_t buffer_len, uint_t *sockaddr_len) {
struct sockaddr *real_addr = sockaddr;
struct sockaddr_ *fake_addr = sockaddr;
fake_addr->family = sock_family_from_real(real_addr->sa_family);
switch (fake_addr->family) {
case PF_LOCAL_: {
// Most callers of sockaddr_write use it to return a peer name, and
// since we don't know the peer name in this case, just return the
// default peer name, which is the null address.
static struct sockaddr_ unix_domain_null = {.family = PF_LOCAL_};
sockaddr = &unix_domain_null;
*sockaddr_len = sizeof(unix_domain_null);
break;
}
case PF_INET_:
case PF_INET6_:
break;
default:
return _EINVAL;
}
if (buffer_len > *sockaddr_len)
buffer_len = *sockaddr_len;
// The address is supposed to be truncated if the specified length is too
// short, instead of returning an error.
if (user_write(sockaddr_addr, sockaddr, buffer_len))
return _EFAULT;
return 0;
}
int_t sys_bind(fd_t sock_fd, addr_t sockaddr_addr, uint_t sockaddr_len) {
STRACE("bind(%d, 0x%x, %d)", sock_fd, sockaddr_addr, sockaddr_len);
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
struct sockaddr_max_ sockaddr;
struct inode_data *inode = NULL;
int err = sockaddr_read_bind(sockaddr_addr, &sockaddr, &sockaddr_len, sock);
if (err < 0)
return err;
err = bind(sock->real_fd, (void *) &sockaddr, sockaddr_len);
if (err < 0) {
inode_release_if_exist(sock->socket.unix_name_inode);
if (sock->socket.unix_name_abstract != NULL)
unix_abstract_release(sock->socket.unix_name_abstract);
return errno_map();
}
sock->socket.unix_name_inode = inode;
return 0;
}
static void fill_cred(struct ucred_ *cred) {
cred->pid = current->pid;
cred->uid = current->euid;
cred->gid = current->egid;
}
int_t sys_connect(fd_t sock_fd, addr_t sockaddr_addr, uint_t sockaddr_len) {
STRACE("connect(%d, 0x%x, %d)", sock_fd, sockaddr_addr, sockaddr_len);
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
struct sockaddr_max_ sockaddr;
int err = sockaddr_read(sockaddr_addr, &sockaddr, &sockaddr_len);
if (err < 0)
return err;
err = connect(sock->real_fd, (void *) &sockaddr, sockaddr_len);
if (err < 0)
return errno_map();
if (sock->socket.domain == AF_LOCAL_) {
fill_cred(&sock->socket.unix_cred);
assert(sock->socket.unix_peer == NULL);
// Send a pointer to ourselves to the other end so they can set up the peer pointers.
ssize_t res = write(sock->real_fd, &sock, sizeof(struct fd *));
if (res == sizeof(struct fd *)) {
// Wait for acknowledgement that it happened.
lock(&peer_lock);
while (sock->socket.unix_peer == NULL)
wait_for_ignore_signals(&sock->socket.unix_got_peer, &peer_lock, NULL);
unlock(&peer_lock);
}
}
return err;
}
int_t sys_listen(fd_t sock_fd, int_t backlog) {
STRACE("listen(%d, %d)", sock_fd, backlog);
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
int err = listen(sock->real_fd, backlog);
if (err < 0)
return errno_map();
sockrestart_begin_listen(sock);
return err;
}
int_t sys_accept(fd_t sock_fd, addr_t sockaddr_addr, addr_t sockaddr_len_addr) {
STRACE("accept(%d, 0x%x, 0x%x)", sock_fd, sockaddr_addr, sockaddr_len_addr);
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
dword_t sockaddr_len = 0;
if (sockaddr_addr != 0) {
if (user_get(sockaddr_len_addr, sockaddr_len))
return _EFAULT;
}
char sockaddr[sockaddr_len];
int client;
do {
sockrestart_begin_listen_wait(sock);
errno = 0;
client = accept(sock->real_fd,
sockaddr_addr != 0 ? (void *) sockaddr : NULL,
sockaddr_addr != 0 ? &sockaddr_len : NULL);
sockrestart_end_listen_wait(sock);
} while (sockrestart_should_restart_listen_wait() && errno == EINTR);
if (client < 0)
return errno_map();
if (sockaddr_addr != 0) {
int err = sockaddr_write(sockaddr_addr, sockaddr, sizeof(sockaddr), &sockaddr_len);
if (err < 0)
return client;
if (user_put(sockaddr_len_addr, sockaddr_len))
return _EFAULT;
}
fd_t client_f = sock_fd_create(client,
sock->socket.domain, sock->socket.type, sock->socket.protocol);
if (client_f < 0)
close(client);
if (sock->socket.domain == AF_LOCAL_) {
lock(&peer_lock);
struct fd *client_fd = f_get(client_f);
fill_cred(&client_fd->socket.unix_cred);
struct fd *peer;
ssize_t res = read(client, &peer, sizeof(peer));
if (res == sizeof(peer)) {
client_fd->socket.unix_peer = peer;
peer->socket.unix_peer = client_fd;
notify(&peer->socket.unix_got_peer);
}
unlock(&peer_lock);
}
return client_f;
}
int_t sys_getsockname(fd_t sock_fd, addr_t sockaddr_addr, addr_t sockaddr_len_addr) {
STRACE("getsockname(%d, 0x%x, 0x%x)", sock_fd, sockaddr_addr, sockaddr_len_addr);
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
dword_t sockaddr_len;
if (user_get(sockaddr_len_addr, sockaddr_len))
return _EFAULT;
// TODO if this is a unix socket, return the same string passed to bind
char sockaddr[sockaddr_len];
int res = getsockname(sock->real_fd, (void *) sockaddr, &sockaddr_len);
if (res < 0)
return errno_map();
int err = sockaddr_write(sockaddr_addr, sockaddr, sizeof(sockaddr), &sockaddr_len);
if (err < 0)
return err;
if (user_put(sockaddr_len_addr, sockaddr_len))
return _EFAULT;
return res;
}
int_t sys_getpeername(fd_t sock_fd, addr_t sockaddr_addr, addr_t sockaddr_len_addr) {
STRACE("getpeername(%d, 0x%x, 0x%x)", sock_fd, sockaddr_addr, sockaddr_len_addr);
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
dword_t sockaddr_len;
if (user_get(sockaddr_len_addr, sockaddr_len))
return _EFAULT;
// TODO if this is a unix socket, return the same string the peer passed to
// bind once the peer pointer is available
char sockaddr[sockaddr_len];
int res = getpeername(sock->real_fd, (void *) sockaddr, &sockaddr_len);
if (res < 0)
return errno_map();
int err = sockaddr_write(sockaddr_addr, sockaddr, sizeof(sockaddr), &sockaddr_len);
if (err < 0)
return err;
if (user_put(sockaddr_len_addr, sockaddr_len))
return _EFAULT;
return res;
}
int_t sys_socketpair(dword_t domain, dword_t type, dword_t protocol, addr_t sockets_addr) {
STRACE("socketpair(%d, %d, %d, 0x%x)", domain, type, protocol, sockets_addr);
int real_domain = sock_family_to_real(domain);
if (real_domain < 0)
return _EINVAL;
int real_type = sock_type_to_real(type, protocol);
if (real_type < 0)
return _EINVAL;
int sockets[2];
int err = socketpair(domain, type, protocol, sockets);
if (err < 0)
return errno_map();
lock(&peer_lock);
int fake_sockets[2];
err = fake_sockets[0] = sock_fd_create(sockets[0], domain, type, protocol);
if (fake_sockets[0] < 0) {
unlock(&peer_lock);
goto close_sockets;
}
err = fake_sockets[1] = sock_fd_create(sockets[1], domain, type, protocol);
if (fake_sockets[1] < 0) {
unlock(&peer_lock);
goto close_fake_0;
}
struct fd *sock1 = f_get(fake_sockets[0]);
struct fd *sock2 = f_get(fake_sockets[1]);
sock1->socket.unix_peer = sock2;
sock2->socket.unix_peer = sock1;
unlock(&peer_lock);
err = _EFAULT;
if (user_put(sockets_addr, fake_sockets))
goto close_fake_1;
STRACE(" [%d, %d]", fake_sockets[0], fake_sockets[1]);
return 0;
close_fake_1:
sys_close(fake_sockets[1]);
close_fake_0:
sys_close(fake_sockets[0]);
close_sockets:
close(sockets[0]);
close(sockets[1]);
return err;
}
int_t sys_sendto(fd_t sock_fd, addr_t buffer_addr, dword_t len, dword_t flags, addr_t sockaddr_addr, dword_t sockaddr_len) {
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
char *buffer = malloc(len + 1);
if (user_read(buffer_addr, buffer, len))
return _EFAULT;
buffer[len] = '\0';
STRACE("sendto(%d, \"%.100s\", %d, %d, 0x%x, %d)", sock_fd, buffer, len, flags, sockaddr_addr, sockaddr_len);
int real_flags = sock_flags_to_real(flags);
int err = _EINVAL;
if (real_flags < 0)
goto error;
struct sockaddr_max_ sockaddr;
if (sockaddr_addr) {
err = sockaddr_read(sockaddr_addr, &sockaddr, &sockaddr_len);
if (err < 0)
goto error;
}
ssize_t res = sendto(sock->real_fd, buffer, len, real_flags,
sockaddr_addr ? (void *) &sockaddr : NULL, sockaddr_len);
free(buffer);
if (res < 0)
return errno_map();
return res;
error:
free(buffer);
return err;
}
int_t sys_recvfrom(fd_t sock_fd, addr_t buffer_addr, dword_t len, dword_t flags, addr_t sockaddr_addr, addr_t sockaddr_len_addr) {
STRACE("recvfrom(%d, 0x%x, %d, %d, 0x%x, 0x%x)", sock_fd, buffer_addr, len, flags, sockaddr_addr, sockaddr_len_addr);
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
int real_flags = sock_flags_to_real(flags);
if (real_flags < 0)
return _EINVAL;
uint_t sockaddr_len = 0;
if (sockaddr_len_addr != 0)
if (user_get(sockaddr_len_addr, sockaddr_len))
return _EFAULT;
char *buffer = malloc(len);
char sockaddr[sockaddr_len];
ssize_t res = recvfrom(sock->real_fd, buffer, len, real_flags,
sockaddr_addr != 0 ? (void *) sockaddr : NULL,
sockaddr_len_addr != 0 ? &sockaddr_len : NULL);
if (res < 0) {
free(buffer);
return errno_map();
}
if (user_write(buffer_addr, buffer, len)) {
free(buffer);
return _EFAULT;
}
free(buffer);
if (sockaddr_addr != 0) {
int err = sockaddr_write(sockaddr_addr, sockaddr, sizeof(sockaddr), &sockaddr_len);
if (err < 0)
return err;
}
if (sockaddr_len_addr != 0)
if (user_put(sockaddr_len_addr, sockaddr_len))
return _EFAULT;
return res;
}
int_t sys_send(fd_t sock_fd, addr_t buf, dword_t len, int_t flags) {
return sys_sendto(sock_fd, buf, len, flags, 0, 0);
}
int_t sys_recv(fd_t sock_fd, addr_t buf, dword_t len, int_t flags) {
return sys_recvfrom(sock_fd, buf, len, flags, 0, 0);
}
int_t sys_shutdown(fd_t sock_fd, dword_t how) {
STRACE("shutdown(%d, %d)", sock_fd, how);
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
int err = shutdown(sock->real_fd, how);
if (err < 0)
return errno_map();
return 0;
}
#define DEFAULT_TCP_CONGESTION "cubic"
int_t sys_setsockopt(fd_t sock_fd, dword_t level, dword_t option, addr_t value_addr, dword_t value_len) {
STRACE("setsockopt(%d, %d, %d, 0x%x, %d)", sock_fd, level, option, value_addr, value_len);
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
char value[value_len];
if (user_read(value_addr, value, value_len))
return _EFAULT;
// ICMP6_FILTER can only be set on real SOCK_RAW
if (level == IPPROTO_ICMPV6 && option == ICMP6_FILTER_)
return 0;
// IP_MTU_DISCOVER has no equivalent on Darwin
if (level == IPPROTO_IP && option == IP_MTU_DISCOVER_)
return 0;
// TCP_CONGESTION also has no equivalent on Darwin
#if defined(__APPLE__)
if (level == IPPROTO_TCP && option == TCP_CONGESTION_) {
if (strncmp(value, DEFAULT_TCP_CONGESTION, sizeof(value)) == 0)
return 0;
return _ENOENT;
}
#endif
int real_opt = sock_opt_to_real(option, level);
if (real_opt < 0)
return _EINVAL;
int real_level = sock_level_to_real(level);
if (real_level < 0)
return _EINVAL;
// 0 means the option is not implemented, but things rely on it, so we
// should just ignore attempts to set it.
if (real_opt == 0)
return 0;
int err = setsockopt(sock->real_fd, real_level, real_opt, value, value_len);
if (err < 0)
return errno_map();
return 0;
}
int_t sys_getsockopt(fd_t sock_fd, dword_t level, dword_t option, addr_t value_addr, dword_t len_addr) {
STRACE("getsockopt(%d, %d, %d, %#x, %#x)", sock_fd, level, option, value_addr, len_addr);
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
dword_t value_len;
if (user_get(len_addr, value_len))
return _EFAULT;
char value[value_len];
if (user_read(value_addr, value, value_len))
return _EFAULT;
if (level == SOL_SOCKET_ && (option == SO_DOMAIN_ || option == SO_TYPE_ || option == SO_PROTOCOL_)) {
dword_t *value_p = (dword_t *) value;
if (value_len != sizeof(*value_p))
return _EINVAL;
if (option == SO_DOMAIN_)
*value_p = sock->socket.domain;
else if (option == SO_TYPE_)
*value_p = sock->socket.type;
else if (option == SO_PROTOCOL_)
*value_p = sock->socket.protocol;
} else if (level == SOL_SOCKET_ && option == SO_PEERCRED_) {
struct ucred_ *cred = (struct ucred_ *) value;
if (value_len != sizeof(*cred))
return _EINVAL;
lock(&peer_lock);
if (sock->socket.domain != AF_LOCAL_ || sock->socket.unix_peer == NULL) {
cred->pid = 0;
cred->uid = cred->gid = -1;
} else {
*cred = sock->socket.unix_peer->socket.unix_cred;
}
unlock(&peer_lock);
} else if (level == SOL_SOCKET_ && option == SO_ERROR_) {
if (value_len != sizeof(dword_t))
return _EINVAL;
int real_error;
socklen_t real_error_len = sizeof(real_error);
int err = getsockopt(sock->real_fd, SOL_SOCKET, SO_ERROR, &real_error, &real_error_len);
if (err < 0)
return errno_map();
*(dword_t *) value = real_error == 0 ? 0 : -err_map(real_error);
} else if (level == IPPROTO_TCP && option == TCP_CONGESTION_) {
value_len = strlen(DEFAULT_TCP_CONGESTION);
memcpy(value, DEFAULT_TCP_CONGESTION, value_len);
#if defined(__APPLE__)
} else if (level == IPPROTO_TCP && option == TCP_INFO_) {
// This one's fun. On Linux, the struct is not ABI dependent, so no
// special handling is needed. On Darwin, the struct is completely
// different and has a different sockopt name.
struct tcp_connection_info conn_info;
socklen_t conn_info_size = sizeof(conn_info);
int err = getsockopt(sock->real_fd, IPPROTO_TCP, TCP_CONNECTION_INFO, &conn_info, &conn_info_size);
if (err < 0)
return errno_map();
// The possible keys for this table are in netinet/tcp_fsm.h, but that
// header isn't available on iOS, only macOS.
static const uint8_t tcp_state_table[] = {
7, // TCPS_CLOSED
10, // TCPS_LISTEN
2, // TCPS_SYN_SENT
3, // TCPS_SYN_RECEIVED
1, // TCPS_ESTABLISHED
8, // TCPS_CLOSE_WAIT
4, // TCPS_FIN_WAIT_1
11, // TCPS_CLOSING
9, // TCPS_LAST_ACK
5, // TCPS_FIN_WAIT_2
6, // TCPS_TIME_WAIT
};
struct tcp_info_ info = {
.state = tcp_state_table[conn_info.tcpi_state],
.options = conn_info.tcpi_options,
.snd_wscale = conn_info.tcpi_snd_wscale,
.rcv_wscale = conn_info.tcpi_rcv_wscale,
.rto = conn_info.tcpi_rto * 1000,
.snd_mss = conn_info.tcpi_maxseg,
.rtt = conn_info.tcpi_srtt * 1000,
.rttvar = conn_info.tcpi_rttvar * 1000,
.snd_ssthresh = conn_info.tcpi_snd_ssthresh,
.snd_cwnd = conn_info.tcpi_snd_cwnd / conn_info.tcpi_maxseg,
// https://lkml.org/lkml/2017/4/24/923
.total_retrans = conn_info.tcpi_txretransmitpackets,
};
if (value_len > sizeof(struct tcp_info_))
value_len = sizeof(struct tcp_info_);
memcpy(value, &info, value_len);
#endif
} else {
int real_opt = sock_opt_to_real(option, level);
if (real_opt < 0)
return _EINVAL;
int real_level = sock_level_to_real(level);
if (real_level < 0)
return _EINVAL;
int err = getsockopt(sock->real_fd, real_level, real_opt, value, &value_len);
if (err < 0)
return errno_map();
}
if (user_put(len_addr, value_len))
return _EFAULT;
if (user_put(value_addr, value))
return _EFAULT;
return 0;
}
static void scm_free(struct scm *scm) {
for (unsigned i = 0; i < scm->num_fds; i++)
fd_close(scm->fds[i]);
free(scm);
}
int_t sys_sendmsg(fd_t sock_fd, addr_t msghdr_addr, int_t flags) {
int err;
STRACE("sendmsg(%d, %#x, %d)", sock_fd, msghdr_addr, flags);
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
struct msghdr msg;
struct msghdr_ msg_fake;
if (user_get(msghdr_addr, msg_fake))
return _EFAULT;
// msg_name
struct sockaddr_max_ msg_name;
if (msg_fake.msg_name != 0) {
int err = sockaddr_read(msg_fake.msg_name, &msg_name, &msg_fake.msg_namelen);
if (err < 0)
return err;
msg.msg_name = &msg_name;
msg.msg_namelen = msg_fake.msg_namelen;
} else {
msg.msg_name = NULL;
}
// msg_iovec
struct iovec_ msg_iov_fake[msg_fake.msg_iovlen];
if (user_get(msg_fake.msg_iov, msg_iov_fake))
return _EFAULT;
struct iovec msg_iov[msg_fake.msg_iovlen];
memset(msg_iov, 0, sizeof(msg_iov));
msg.msg_iov = msg_iov;
msg.msg_iovlen = sizeof(msg_iov) / sizeof(msg_iov[0]);
for (size_t i = 0; i < (size_t) msg.msg_iovlen; i++) {
msg_iov[i].iov_len = msg_iov_fake[i].len;
msg_iov[i].iov_base = malloc(msg_iov_fake[i].len);
err = _EFAULT;
if (user_read(msg_iov_fake[i].base, msg_iov[i].iov_base, msg_iov_fake[i].len))
goto out_free_iov;
}
// msg_control
uint8_t msg_control_buf[2048];
uint8_t *msg_control = NULL;
if (msg_fake.msg_control != 0) {
if (msg_fake.msg_controllen > sizeof(msg_control_buf)) {
err = _EINVAL;
goto out_free_iov;
}
msg_control = msg_control_buf;
err = _EFAULT;
if (user_read(msg_fake.msg_control, msg_control, msg_fake.msg_controllen))
goto out_free_iov;
}
msg.msg_control = NULL;
msg.msg_controllen = 0;
struct scm *scm = NULL;
char real_msg_control[CMSG_SPACE(sizeof(int))]; // only used if actually sending an fd
if (sock->socket.domain == AF_LOCAL_ && msg_control != NULL && msg_fake.msg_controllen >= sizeof(struct cmsghdr_)) {
// figure out how many file descriptors we're sending
uint8_t *mhdr_end = msg_control + msg_fake.msg_controllen;
unsigned num_fds = 0;
struct cmsghdr_ *cmsg;
for (cmsg = (void *) msg_control; cmsg != NULL; cmsg = CMSG_NXTHDR_(cmsg, mhdr_end)) {
if (cmsg->level != SOL_SOCKET_)
continue;
if (cmsg->type != SCM_RIGHTS_)
return _EINVAL;
num_fds += (cmsg->len - sizeof(struct cmsghdr_)) / sizeof(fd_t);
}
if (num_fds > 253) // *magic*
return _EINVAL;
if (num_fds > 0) {
// send one (1) real fd and put the rest in a struct scm
static int real_fd = -1;
if (real_fd == -1) {
real_fd = open(".", O_RDONLY);
if (real_fd < 0)
ERRNO_DIE("no");
}
msg.msg_control = real_msg_control;
msg.msg_controllen = sizeof(real_msg_control);
struct cmsghdr *real_cmsg = CMSG_FIRSTHDR(&msg);
real_cmsg->cmsg_level = SOL_SOCKET;
real_cmsg->cmsg_type = SCM_RIGHTS;
real_cmsg->cmsg_len = CMSG_LEN(sizeof(real_fd));
memcpy(CMSG_DATA(real_cmsg), &real_fd, sizeof(real_fd));
scm = malloc(sizeof(struct scm) + num_fds * sizeof(struct fd *));
list_init(&scm->queue);
scm->num_fds = num_fds;
unsigned fd_i = 0;
for (cmsg = (void *) msg_control; cmsg != NULL; cmsg = CMSG_NXTHDR_(cmsg, mhdr_end)) {
if (cmsg->level != SOL_SOCKET_)
continue;
fd_t *fds = (void *) cmsg->data;
for (unsigned i = 0; i < (cmsg->len - sizeof(struct cmsghdr_)) / sizeof(fd_t); i++) {
STRACE(" sending fd %d", fds[i]);
scm->fds[fd_i++] = fd_retain(f_get(fds[i]));
}
}
lock(&peer_lock);
struct fd *peer = sock->socket.unix_peer;
if (peer == NULL) {
unlock(&peer_lock);
err = _EPIPE;
goto out_free_scm;
}
lock(&peer->lock);
list_add_tail(&peer->socket.unix_scm, &scm->queue);
unlock(&peer->lock);
unlock(&peer_lock);
}
}
msg.msg_flags = sock_flags_to_real(msg_fake.msg_flags);
err = _EINVAL;
if (msg.msg_flags < 0)
goto out_free_scm;
int real_flags = sock_flags_to_real(flags);
if (real_flags < 0)
goto out_free_scm;
err = sendmsg(sock->real_fd, &msg, real_flags);
if (err < 0) {
err = errno_map();
goto out_free_scm;
}
goto out_free_iov;
out_free_scm:
if (scm != NULL) {
lock(&peer_lock);
struct fd *peer = sock->socket.unix_peer;
if (peer != NULL) {
lock(&peer->lock);
list_remove_safe(&scm->queue);
unlock(&peer->lock);
}
unlock(&peer_lock);
scm_free(scm);
}
out_free_iov:
for (size_t i = 0; i < (size_t) msg.msg_iovlen; i++)
free(msg_iov[i].iov_base);
return err;
}
int_t sys_recvmsg(fd_t sock_fd, addr_t msghdr_addr, int_t flags) {
STRACE("recvmsg(%d, %#x, %d)", sock_fd, msghdr_addr, flags);
struct fd *sock = sock_getfd(sock_fd);
if (sock == NULL)
return _EBADF;
struct msghdr msg;
struct msghdr_ msg_fake;
if (user_get(msghdr_addr, msg_fake))
return _EFAULT;
// msg_name
char msg_name[msg_fake.msg_namelen];
if (msg_fake.msg_name != 0) {
msg.msg_name = msg_name;
msg.msg_namelen = sizeof(msg_name);
} else {
msg.msg_name = NULL;
msg.msg_namelen = 0;
}
char real_msg_control[CMSG_SPACE(sizeof(int))] = {}; // only used if needed
if (msg_fake.msg_controllen != 0) {
// msg_control, include room for one (1) fd
msg.msg_control = real_msg_control;
msg.msg_controllen = sizeof(real_msg_control);
} else {
msg.msg_control = NULL;
msg.msg_controllen = 0;
}
int real_flags = sock_flags_to_real(flags);
if (real_flags < 0)
return _EINVAL;
// msg_iovec (no initial content)
struct iovec_ msg_iov_fake[msg_fake.msg_iovlen];
if (user_get(msg_fake.msg_iov, msg_iov_fake))
return _EFAULT;
struct iovec msg_iov[msg_fake.msg_iovlen];
msg.msg_iov = msg_iov;
msg.msg_iovlen = sizeof(msg_iov) / sizeof(msg_iov[0]);
for (size_t i = 0; i < (size_t) msg.msg_iovlen; i++) {
msg_iov[i].iov_len = msg_iov_fake[i].len;
msg_iov[i].iov_base = malloc(msg_iov_fake[i].len);
}
ssize_t res = recvmsg(sock->real_fd, &msg, real_flags);
int err = 0;
if (res < 0)
err = errno_map();
// don't return err quite yet, there are outstanding mallocs