Loading...
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 | /* * Copyright (c) 2000-2007 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * @OSF_COPYRIGHT@ */ /* * Mach Operating System * Copyright (c) 1991,1990,1989 Carnegie Mellon University * All Rights Reserved. * * Permission to use, copy, modify and distribute this software and its * documentation is hereby granted, provided that both the copyright * notice and this permission notice appear in all copies of the * software, derivative works or modified versions, and any portions * thereof, and that both notices appear in supporting documentation. * * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. * * Carnegie Mellon requests users of this software to return to * * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU * School of Computer Science * Carnegie Mellon University * Pittsburgh PA 15213-3890 * * any improvements or extensions that they make and grant Carnegie Mellon * the rights to redistribute these changes. */ /* * NOTICE: This file was modified by McAfee Research in 2004 to introduce * support for mandatory and extensible security protections. This notice * is included in support of clause 2.2 (b) of the Apple Public License, * Version 2.0. * Copyright (c) 2005 SPARTA, Inc. */ /* */ /* * File: ipc/mach_msg.c * Author: Rich Draves * Date: 1989 * * Exported message traps. See mach/message.h. */ #include <mach/mach_types.h> #include <mach/kern_return.h> #include <mach/port.h> #include <mach/message.h> #include <mach/mig_errors.h> #include <mach/mach_traps.h> #include <kern/kern_types.h> #include <kern/assert.h> #include <kern/cpu_number.h> #include <kern/ipc_kobject.h> #include <kern/ipc_mig.h> #include <kern/task.h> #include <kern/thread.h> #include <kern/sched_prim.h> #include <kern/exception.h> #include <kern/misc_protos.h> #include <kern/processor.h> #include <kern/syscall_subr.h> #include <kern/policy_internal.h> #include <kern/mach_filter.h> #include <vm/vm_map_xnu.h> #include <ipc/port.h> #include <ipc/ipc_types.h> #include <ipc/ipc_kmsg.h> #include <ipc/ipc_mqueue.h> #include <ipc/ipc_object.h> #include <ipc/ipc_notify.h> #include <ipc/ipc_policy.h> #include <ipc/ipc_port.h> #include <ipc/ipc_pset.h> #include <ipc/ipc_space.h> #include <ipc/ipc_entry.h> #include <ipc/ipc_importance.h> #include <ipc/ipc_voucher.h> #include <machine/machine_routines.h> #include <security/mac_mach_internal.h> #include <sys/kdebug.h> #include <sys/proc_ro.h> #ifndef offsetof #define offsetof(type, member) ((size_t)(&((type *)0)->member)) #endif /* offsetof */ /* * Forward declarations - kernel internal routines */ static mach_msg_return_t mach_msg_rcv_link_special_reply_port( ipc_port_t special_reply_port, mach_port_name_t dest_name_port); void mach_msg_receive_results_complete(ipc_object_t object); const security_token_t KERNEL_SECURITY_TOKEN = KERNEL_SECURITY_TOKEN_VALUE; const audit_token_t KERNEL_AUDIT_TOKEN = KERNEL_AUDIT_TOKEN_VALUE; #define mach_copyout_field(kaddr, uaddr, type_t, field) \ mach_copyout((kaddr), (uaddr) + offsetof(type_t, field), \ sizeof(((type_t *)0)->field)) /* * Routine: mach_msg_receive_too_large() * Purpose: * Helper for mach_msg_receive_results() to handle * the MACH_RCV_TOO_LARGE error when the MACH64_RCV_LARGE * option is set. * Returns: * MACH_RCV_INVALID_DATA or the passed in mr. */ static mach_msg_return_t mach_msg_receive_too_large( mach_msg_recv_bufs_t *recv_bufs, mach_msg_recv_result_t *msgr, mach_msg_option64_t options, mach_msg_return_t mr) { assert(mr == MACH_RCV_TOO_LARGE); assert(options & MACH_RCV_LARGE); if (options & MACH64_RCV_LINEAR_VECTOR) { /* * If MACH64_RCV_LINEAR_VECTOR is set, kevent is calling * from filt_machportprocess() and the reporting of name * and sizes happen via the knote in filt_machportprocess() * rather than a receive operation, there's nothing for us * to do here. */ return mr; } /* * For the regular case, just copyout the size * (and optional port name) in a fake header. */ if ((options & MACH64_RCV_LARGE_IDENTITY) && recv_bufs->recv_msg_size >= offsetof(mach_msg_user_header_t, msgh_voucher_port)) { /* * If MACH64_RCV_LARGE_IDENTITY is set, * we monkey patch the msgh_local_port field. */ if (mach_copyout_field(&msgr->msgr_recv_name, recv_bufs->recv_msg_addr, mach_msg_user_header_t, msgh_local_port)) { mr = MACH_RCV_INVALID_DATA; } } if (recv_bufs->recv_msg_size >= offsetof(mach_msg_user_header_t, msgh_remote_port)) { /* * For all cases, we monkey patch the msgh_size * field with how much size is needed. */ if (mach_copyout_field(&msgr->msgr_msg_size, recv_bufs->recv_msg_addr, mach_msg_user_header_t, msgh_size)) { mr = MACH_RCV_INVALID_DATA; } } if (recv_bufs->recv_aux_addr) { /* * Then we report the incoming aux size as well, * if the caller has an aux buffer. */ assert(recv_bufs->recv_aux_size >= sizeof(mach_msg_aux_header_t)); if (mach_copyout_field(&msgr->msgr_aux_size, recv_bufs->recv_aux_addr, mach_msg_aux_header_t, msgdh_size)) { mr = MACH_RCV_INVALID_DATA; } } return mr; } /* * Routine: mach_msg_receive_error [internal] * Purpose: * Builds a minimal header/trailer and copies it to * the user message buffer. Invoked when in the case of a * MACH_RCV_TOO_LARGE or MACH_RCV_BODY_ERROR error. * Conditions: * Nothing locked. kmsg is freed upon return. * Returns: * the incoming "mr" minimal header/trailer copied * MACH_RCV_INVALID_DATA copyout to user buffer failed */ static mach_msg_return_t mach_msg_receive_error( ipc_kmsg_t kmsg, mach_msg_recv_bufs_t *recv_bufs, mach_msg_recv_result_t *msgr, mach_msg_option64_t options, ipc_space_t space, vm_map_t map, mach_msg_return_t mr) { /* * Copy out the destination port in the message. * * Destroy all other rights and memory in the message, * and turn it into a very simple bare message with just a header. */ ipc_kmsg_copyout_dest_to_user(kmsg, space); /* * Copy the message to user space and return the size * (note that ipc_kmsg_put_to_user may also adjust the actual * msg and aux size copied out to user-space). */ return ipc_kmsg_put_to_user(kmsg, recv_bufs, msgr, options, map, mr); } /* * Routine: mach_msg_receive_results * Purpose: * Receive a message. * Conditions: * Nothing locked. * * Returns: * sizep (out): copied out size of message proper * aux_sizep (out): copied out size of aux data * MACH_MSG_SUCCESS Received a message. * MACH_RCV_INVALID_NAME The name doesn't denote a right, * or the denoted right is not receive or port set. * MACH_RCV_IN_SET Receive right is a member of a set. * MACH_RCV_TOO_LARGE Message wouldn't fit into buffer. * MACH_RCV_TIMED_OUT Timeout expired without a message. * MACH_RCV_INTERRUPTED Reception interrupted. * MACH_RCV_PORT_DIED Port/set died while receiving. * MACH_RCV_PORT_CHANGED Port moved into set while receiving. * MACH_RCV_INVALID_DATA Couldn't copy to user buffer. * MACH_RCV_INVALID_NOTIFY Bad notify port. * MACH_RCV_HEADER_ERROR */ mach_msg_return_t mach_msg_receive_results( mach_msg_recv_result_t *msgr_out) { thread_t self = current_thread(); ipc_space_t space = current_space(); vm_map_t map = current_map(); /* * /!\IMPORTANT/!\: Pull out values we stashed on thread struct now. * Values may be stomped over if copyio operations in this function * trigger kernel IPC calls. */ ipc_object_t object = self->ith_object; mach_msg_return_t mr = self->ith_state; mach_msg_option64_t options = self->ith_option; ipc_kmsg_t kmsg = self->ith_kmsg; mach_msg_recv_bufs_t recv_bufs = self->ith_recv_bufs; mach_msg_recv_result_t msgr = { .msgr_seqno = self->ith_seqno, .msgr_context = 0, }; /* * unlink the special_reply_port before releasing reference to object. * get the thread's turnstile, if the thread donated it's turnstile to the port */ mach_msg_receive_results_complete(object); io_release(object); if (options & MACH64_RCV_LINEAR_VECTOR) { assert(recv_bufs.recv_aux_addr == 0); assert(recv_bufs.recv_aux_size == 0); } if (mr == MACH_RCV_TOO_LARGE) { /* these ith_* fields are only set for MACH_RCV_TOO_LARGE */ msgr.msgr_msg_size = self->ith_msize; msgr.msgr_aux_size = self->ith_asize; msgr.msgr_recv_name = self->ith_receiver_name; /* * If the receive operation occurs with MACH_RCV_LARGE set * then no message was extracted from the queue, and the size * and (optionally) receiver names were the only thing captured. */ if (options & MACH64_RCV_LARGE) { mr = mach_msg_receive_too_large(&recv_bufs, &msgr, options, mr); } else { /* discard importance in message */ ipc_importance_clean(kmsg); mr = mach_msg_receive_error(kmsg, &recv_bufs, &msgr, options, space, map, mr); /* kmsg freed */ } } if (mr != MACH_MSG_SUCCESS) { goto out; } #if IMPORTANCE_INHERITANCE /* adopt/transform any importance attributes carried in the message */ ipc_importance_receive(kmsg, options); #endif /* IMPORTANCE_INHERITANCE */ /* auto redeem the voucher in the message */ ipc_voucher_receive_postprocessing(kmsg, options); /* Save destination port context for the trailer before copyout */ msgr.msgr_context = ikm_header(kmsg)->msgh_remote_port->ip_context; /* * restore the recv_bufs values that are used by * ipc_kmsg_copyout_guarded_port_descriptor() */ self->ith_recv_bufs = recv_bufs; mr = ipc_kmsg_copyout(kmsg, space, map, options); if (mr != MACH_MSG_SUCCESS) { /* already received importance, so have to undo that here */ ipc_importance_unreceive(kmsg, options); /* if we had a body error copyout what we have, otherwise a simple header/trailer */ if ((mr & ~MACH_MSG_MASK) == MACH_RCV_BODY_ERROR) { mr = ipc_kmsg_put_to_user(kmsg, &recv_bufs, &msgr, options, map, mr); } else { mr = mach_msg_receive_error(kmsg, &recv_bufs, &msgr, options, space, map, mr); } } else { msgr.msgr_priority = kmsg->ikm_ppriority; msgr.msgr_qos_ovrd = kmsg->ikm_qos_override; mr = ipc_kmsg_put_to_user(kmsg, &recv_bufs, &msgr, options, map, mr); } /* kmsg freed */ out: if (msgr_out) { *msgr_out = msgr; } return mr; } void mach_msg_receive_continue(void) { mach_msg_return_t mr; ipc_port_thread_group_unblocked(); mr = mach_msg_receive_results(NULL); thread_syscall_return(mr); } /* * Routine: mach_msg_validate_data_vectors * Purpose: * Perform validations on message and auxiliary data vectors * we have copied in. */ static mach_msg_return_t mach_msg_validate_data_vectors( mach_msg_vector_t *msg_vec, mach_msg_vector_t *aux_vec, mach_msg_size_t vec_count, __unused mach_msg_option64_t option64, bool sending) { mach_msg_size_t msg_size = 0, aux_size = 0; /* user size */ assert(vec_count <= MACH_MSGV_MAX_COUNT); assert(option64 & MACH64_MSG_VECTOR); assert(msg_vec != NULL); assert(aux_vec != NULL); if (vec_count == 0) { /* * can't use MACH_RCV_TOO_LARGE or MACH_RCV_INVALID_DATA here because * they imply a message has been dropped. use a new error code that * suggests an early error and that message is still queued. */ return sending ? MACH_SEND_MSG_TOO_SMALL : MACH_RCV_INVALID_ARGUMENTS; } /* * Validate first (message proper) data vector. * * Since we are using mach_msg2_trap() to shim existing mach_msg() calls, * we unfortunately cannot validate message rcv address or message rcv size * at this point for compatibility reasons. * * (1) If rcv address is invalid, we will destroy the incoming message during * ipc_kmsg_put_to_user(), instead of returning an error before receive * is attempted. * (2) If rcv size is smaller than the minimal message header and trailer * that mach_msg_receive_error() builds, we will truncate the message * and copy out a partial message. * * See: ipc_kmsg_put_vector_to_user(). */ if (sending) { if (msg_vec->msgv_data == 0) { return MACH_SEND_INVALID_DATA; } msg_size = msg_vec->msgv_send_size; if ((msg_size < sizeof(mach_msg_user_header_t)) || (msg_size & 3)) { return MACH_SEND_MSG_TOO_SMALL; } if (msg_size > IPC_KMSG_MAX_BODY_SPACE) { return MACH_SEND_TOO_LARGE; } } /* Validate second (optional auxiliary) data vector */ if (vec_count == MACH_MSGV_MAX_COUNT) { if (sending) { aux_size = aux_vec->msgv_send_size; if (aux_size != 0 && aux_vec->msgv_data == 0) { return MACH_SEND_INVALID_DATA; } if (aux_size != 0 && aux_size < sizeof(mach_msg_aux_header_t)) { return MACH_SEND_AUX_TOO_SMALL; } if (aux_size > IPC_KMSG_MAX_AUX_DATA_SPACE) { return MACH_SEND_AUX_TOO_LARGE; } } else { mach_vm_address_t rcv_addr = aux_vec->msgv_rcv_addr ? aux_vec->msgv_rcv_addr : aux_vec->msgv_data; if (rcv_addr == 0) { return MACH_RCV_INVALID_ARGUMENTS; } /* * We are using this aux vector to receive, kernel will at * least copy out an empty aux data header. * * See: ipc_kmsg_put_vector_to_user() */ aux_size = aux_vec->msgv_rcv_size; if (aux_size < sizeof(mach_msg_aux_header_t)) { return MACH_RCV_INVALID_ARGUMENTS; } } } else { if (sending) { /* * Not sending aux data vector, but we still might have copied it * in if doing a combined send/receive. Nil out the send size. */ aux_vec->msgv_send_size = 0; } else { /* Do the same for receive */ aux_vec->msgv_rcv_size = 0; } } return MACH_MSG_SUCCESS; } /* * Routine: mach_msg_copyin_data_vectors * Purpose: * Copy in and message user data vectors. */ static mach_msg_return_t mach_msg_copyin_data_vectors( mach_vm_address_t data_addr,/* user address */ mach_msg_size_t cpin_count, mach_msg_option64_t option64, mach_msg_vector_t *msg_vecp,/* out */ mach_msg_vector_t *aux_vecp)/* out */ { mach_msg_vector_t data_vecs[MACH_MSGV_MAX_COUNT] = {}; static_assert(MACH_MSGV_MAX_COUNT == 2); assert(option64 & MACH64_MSG_VECTOR); if (cpin_count > MACH_MSGV_MAX_COUNT) { return (option64 & MACH64_SEND_MSG) ? MACH_SEND_INVALID_DATA : MACH_RCV_INVALID_ARGUMENTS; } if (cpin_count == 0) { return (option64 & MACH64_SEND_MSG) ? MACH_SEND_MSG_TOO_SMALL : MACH_RCV_INVALID_ARGUMENTS; } if (mach_copyin(data_addr, data_vecs, cpin_count * sizeof(mach_msg_vector_t))) { return (option64 & MACH64_SEND_MSG) ? MACH_SEND_INVALID_DATA : MACH_RCV_INVALID_ARGUMENTS; } memcpy(msg_vecp, &data_vecs[MACH_MSGV_IDX_MSG], sizeof(mach_msg_vector_t)); if (cpin_count == MACH_MSGV_MAX_COUNT) { memcpy(aux_vecp, &data_vecs[MACH_MSGV_IDX_AUX], sizeof(mach_msg_vector_t)); } return MACH_MSG_SUCCESS; } #if IPC_HAS_LEGACY_MACH_MSG_TRAP /* * Routine: mach_msg_copyin_user_header * Purpose: * Copy in the message header, or up until message body if message is * large enough. Returns the header of the message and number of descriptors. * Used for mach_msg_overwrite_trap() only. Not available on embedded. * Returns: * MACH_MSG_SUCCESS - Copyin succeeded, msg_addr and msg_size are validated. * MACH_SEND_MSG_TOO_SMALL * MACH_SEND_TOO_LARGE * MACH_SEND_INVALID_DATA */ static mach_msg_return_t mach_msg_copyin_user_header( mach_msg_send_uctx_t *send_uctx, mach_msg_option64_t options) { mach_msg_return_t mr = MACH_MSG_SUCCESS; if (send_uctx->send_msg_size < sizeof(mach_msg_user_header_t) || (send_uctx->send_msg_size & 3)) { return MACH_SEND_MSG_TOO_SMALL; } if (send_uctx->send_msg_size > IPC_KMSG_MAX_BODY_SPACE) { return MACH_SEND_TOO_LARGE; } if (send_uctx->send_msg_size < sizeof(mach_msg_user_base_t)) { static_assert(offsetof(mach_msg_send_uctx_t, send_dsc_count) == offsetof(mach_msg_user_base_t, body.msgh_descriptor_count)); mr = copyinmsg(send_uctx->send_msg_addr, &send_uctx->send_header, sizeof(mach_msg_user_header_t)); } else { mr = copyinmsg(send_uctx->send_msg_addr, &send_uctx->send_header, sizeof(mach_msg_user_base_t)); } if (mr != KERN_SUCCESS) { return MACH_SEND_INVALID_DATA; } /* * If the message claims to be complex, it must at least * have the length of a "base" message (header + dsc_count). */ if (send_uctx->send_header.msgh_bits & MACH_MSGH_BITS_COMPLEX) { if (send_uctx->send_msg_size < sizeof(mach_msg_user_base_t)) { return MACH_SEND_MSG_TOO_SMALL; } } else { send_uctx->send_dsc_count = 0; } send_uctx->send_header.msgh_size = send_uctx->send_msg_size; return ipc_policy_allow_legacy_send_trap(send_uctx->send_header.msgh_id, options); } #endif /* IPC_HAS_LEGACY_MACH_MSG_TRAP */ __attribute__((noinline, cold)) static mach_msg_return_t mach_msg_receive_pseudo( ipc_kmsg_t kmsg, mach_msg_send_uctx_t *send_uctx, mach_msg_option64_t options, ipc_space_t space, vm_map_t map) { mach_msg_recv_bufs_t recv_bufs = { .recv_msg_addr = send_uctx->send_msg_addr, .recv_aux_addr = send_uctx->send_aux_addr, .recv_msg_size = send_uctx->send_msg_size, .recv_aux_size = send_uctx->send_aux_size, }; mach_msg_recv_result_t msgr = { .msgr_recv_name = MSGR_PSEUDO_RECEIVE, }; mach_msg_return_t mr; /* * set the recv_bufs values that are used by * ipc_kmsg_copyout_guarded_port_descriptor() */ current_thread()->ith_recv_bufs = recv_bufs; mr = ipc_kmsg_copyout_pseudo(kmsg, space, map); (void)ipc_kmsg_put_to_user(kmsg, &recv_bufs, &msgr, options, map, mr); return mr; } /* * Routine: mach_msg_trap_send [internal] * Purpose: * Send a message. * Conditions: * MACH_SEND_MSG is set. aux_send_size is bound checked. * send_aux_{addr, size} are 0 if not vector send. * send_msg_size needs additional bound checks. * Returns: * All of mach_msg_send error codes. */ static mach_msg_return_t mach_msg_trap_send( /* shared args between send and receive */ mach_msg_send_uctx_t *send_uctx, mach_msg_option64_t options, mach_msg_timeout_t msg_timeout, mach_msg_priority_t priority) { ipc_space_t space = current_space(); vm_map_t map = current_map(); ipc_kmsg_t kmsg; mach_msg_return_t mr; assert(options & MACH64_SEND_MSG); /* * Validate the send sizes and header carefuly. */ if (send_uctx->send_msg_size < sizeof(mach_msg_user_header_t) || (send_uctx->send_msg_size & 3)) { return MACH_SEND_MSG_TOO_SMALL; } if (send_uctx->send_msg_size > IPC_KMSG_MAX_BODY_SPACE) { return MACH_SEND_TOO_LARGE; } /* * Complex message must have a body, also do a bound check on descriptor count * (more in ikm_check_descriptors()). */ if (send_uctx->send_header.msgh_bits & MACH_MSGH_BITS_COMPLEX) { if (send_uctx->send_msg_size < sizeof(mach_msg_user_base_t)) { return MACH_SEND_MSG_TOO_SMALL; } if (send_uctx->send_dsc_count > (send_uctx->send_msg_size - sizeof(mach_msg_user_base_t)) / sizeof(mach_msg_type_descriptor_t)) { return MACH_SEND_MSG_TOO_SMALL; } } else if (send_uctx->send_dsc_count != 0) { /* * Simple message cannot contain descriptors. * * This invalid config can only happen from mach_msg2_trap() * since desc_count is passed as its own trap argument. */ assert(options & MACH64_MACH_MSG2); return MACH_SEND_TOO_LARGE; } /* * Now that we have validated send_msg_size, send_aux_size and * send_dsc_count, copy in the message. */ kmsg = ipc_kmsg_alloc(send_uctx->send_msg_size, send_uctx->send_aux_size, send_uctx->send_dsc_count, IPC_KMSG_ALLOC_USER); if (kmsg == IKM_NULL) { return MACH_SEND_NO_BUFFER; } KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_LINK) | DBG_FUNC_NONE, (uintptr_t)send_uctx->send_msg_addr, VM_KERNEL_ADDRPERM((uintptr_t)kmsg), 0, 0, 0); /* * holding kmsg ref * may add MACH64_SEND_ALWAYS to options */ mr = ipc_kmsg_copyin_from_user(kmsg, send_uctx, space, map, priority, &options); if (mr != MACH_MSG_SUCCESS) { ipc_kmsg_free(kmsg); return mr; } mr = ipc_kmsg_send(kmsg, options, msg_timeout); if (mr != MACH_MSG_SUCCESS) { mr |= mach_msg_receive_pseudo(kmsg, send_uctx, options, space, map); /* kmsg is freed */ } return mr; } /* * Routine: mach_msg_trap_receive [internal] * Purpose: * Receive a message. * Conditions: * MACH_RCV_MSG is set. * max_{msg, aux}_rcv_size are already validated. * Returns: * All of mach_msg_receive error codes. */ static mach_msg_return_t mach_msg_trap_receive( /* shared args between send and receive */ mach_vm_address_t msg_addr, mach_vm_address_t aux_addr, /* 0 if not vector send/rcv */ mach_msg_option64_t option64, mach_msg_timeout_t msg_timeout, mach_port_name_t sync_send, /* msg receive args */ mach_msg_size_t max_msg_rcv_size, mach_msg_size_t max_aux_rcv_size, /* 0 if not vector send/rcv */ mach_port_name_t rcv_name) { ipc_object_t object; thread_t self = current_thread(); ipc_space_t space = current_space(); mach_msg_return_t mr = MACH_MSG_SUCCESS; assert(option64 & MACH64_RCV_MSG); mr = ipc_mqueue_copyin(space, rcv_name, &object); if (mr != MACH_MSG_SUCCESS) { return mr; } /* hold ref for object */ if (sync_send != MACH_PORT_NULL) { ipc_port_t special_reply_port = ip_object_to_port(object); /* link the special reply port to the destination */ mr = mach_msg_rcv_link_special_reply_port(special_reply_port, sync_send); if (mr != MACH_MSG_SUCCESS) { io_release(object); return mr; } } /* Set up message proper receive params on thread */ bzero(&self->ith_receive, sizeof(self->ith_receive)); self->ith_recv_bufs = (mach_msg_recv_bufs_t){ .recv_msg_addr = msg_addr, .recv_msg_size = max_msg_rcv_size, .recv_aux_addr = max_aux_rcv_size ? aux_addr : 0, .recv_aux_size = max_aux_rcv_size, }; self->ith_object = object; self->ith_option = option64; self->ith_knote = ITH_KNOTE_NULL; /* not part of ith_receive */ ipc_mqueue_receive(io_waitq(object), msg_timeout, THREAD_ABORTSAFE, self, /* continuation ? */ true); /* NOTREACHED if thread started waiting */ if ((option64 & MACH_RCV_TIMEOUT) && msg_timeout == 0) { thread_poll_yield(self); } mr = mach_msg_receive_results(NULL); /* release ref on ith_object */ return mr; } /* * Routine: mach_msg_overwrite_trap [mach trap] * Purpose: * Possibly send a message; possibly receive a message. * * /!\ Deprecated /!\ * No longer supported on embedded and will be removed from macOS. * Use mach_msg2_trap() instead. * Conditions: * Nothing locked. * The 'priority' is only a QoS if MACH_SEND_OVERRIDE is passed - * otherwise, it is a port name. * Returns: * All of mach_msg_send and mach_msg_receive error codes. */ mach_msg_return_t mach_msg_overwrite_trap( struct mach_msg_overwrite_trap_args *args) { #if IPC_HAS_LEGACY_MACH_MSG_TRAP mach_msg_option64_t options = args->option; mach_msg_return_t mr = MACH_MSG_SUCCESS; options = ipc_current_msg_options(current_task(), options); KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START); if (options & MACH_SEND_MSG) { mach_msg_send_uctx_t send_uctx = { .send_msg_addr = args->msg, .send_msg_size = args->send_size, }; /* * Perform a crude copyin of the mach_msg_user_header_t, * including the next 4 bytes in case it is a complex message, * in order to mimic mach_msg2_trap() behavior of synthesizing * the mach_msg_user_base_t via the trap arguments. * * mach_msg_trap_send() will do more thorough validation of * sizes and header. */ mr = mach_msg_copyin_user_header(&send_uctx, options); if (mr != MACH_MSG_SUCCESS) { return mr; } mr = mach_msg_trap_send(&send_uctx, options, args->timeout, args->priority); } /* If send failed, skip receive */ if (mr != MACH_MSG_SUCCESS) { KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr); goto end; } if (options & MACH_RCV_MSG) { mach_vm_address_t msg_addr = args->rcv_msg ?: args->msg; mach_port_name_t sync_send = MACH_PORT_NULL; /* * Although just the presence of MACH_RCV_SYNC_WAIT and absence of * MACH_SEND_OVERRIDE should imply that 'priority' is a valid port name * to link, in practice older userspace is dependent on * MACH_SEND_SYNC_OVERRIDE also excluding this path. */ if ((options & MACH_RCV_SYNC_WAIT) && !(options & (MACH_SEND_OVERRIDE | MACH_SEND_MSG)) && !(options & MACH_SEND_SYNC_OVERRIDE)) { sync_send = (mach_port_name_t)args->priority; } mr = mach_msg_trap_receive(msg_addr, 0, options, args->timeout, sync_send, args->rcv_size, 0, args->rcv_name); } end: /* unblock call is idempotent */ ipc_port_thread_group_unblocked(); return mr; #else (void)args; return KERN_NOT_SUPPORTED; #endif /* IPC_HAS_LEGACY_MACH_MSG_TRAP */ } /* * Routine: mach_msg_trap [mach trap] * Purpose: * Possibly send a message; possibly receive a message. * * /!\ Deprecated /!\ * No longer supported on embedded and will be removed from macOS. * Use mach_msg2_trap() instead. * Conditions: * Nothing locked. * Returns: * All of mach_msg_send and mach_msg_receive error codes. */ mach_msg_return_t mach_msg_trap( struct mach_msg_overwrite_trap_args *args) { args->rcv_msg = (mach_vm_address_t)0; return mach_msg_overwrite_trap(args); } /* * Routine: mach_msg2_trap [mach trap] * Purpose: * Modern mach_msg_trap() with vector message and CFI support. * Conditions: * Nothing locked. * Returns: * All of mach_msg_send and mach_msg_receive error codes. */ mach_msg_return_t mach_msg2_trap( struct mach_msg2_trap_args *args) { /* packed arguments, LO_BITS_and_HI_BITS */ uint64_t mb_ss = args->msgh_bits_and_send_size; uint64_t mr_lp = args->msgh_remote_and_local_port; uint64_t mv_id = args->msgh_voucher_and_id; uint64_t dc_rn = args->desc_count_and_rcv_name; uint64_t rs_pr = args->rcv_size_and_priority; mach_msg_timeout_t msg_timeout = (mach_msg_timeout_t)args->timeout; mach_msg_vector_t msg_vec = {}, aux_vec = {}; mach_msg_size_t vec_snd_count = 0; mach_msg_size_t vec_rcv_count = 0; mach_msg_option64_t option64; mach_msg_return_t mr = MACH_MSG_SUCCESS; option64 = ipc_current_msg_options(current_task(), args->options) | MACH64_MACH_MSG2; KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_START); mr = ipc_preflight_msg_option64(option64); if (mr != MACH_MSG_SUCCESS) { goto send_fail; } if (option64 & MACH64_MSG_VECTOR) { vec_snd_count = (mach_msg_size_t)(mb_ss >> 32); vec_rcv_count = (mach_msg_size_t)rs_pr; mr = mach_msg_copyin_data_vectors(args->data, MAX(vec_snd_count, vec_rcv_count), option64, &msg_vec, &aux_vec); if (mr != MACH_MSG_SUCCESS) { goto send_fail; } } if (option64 & MACH64_SEND_MSG) { mach_msg_send_uctx_t send_uctx = { .send_header = { .msgh_bits = (mach_msg_bits_t) (mb_ss), .msgh_size = 0, .msgh_remote_port = (mach_port_name_t)(mr_lp), .msgh_local_port = (mach_port_name_t)(mr_lp >> 32), .msgh_voucher_port = (mach_port_name_t)(mv_id), .msgh_id = (mach_msg_id_t) (mv_id >> 32), }, .send_dsc_count = (mach_msg_size_t)dc_rn, }; mach_msg_priority_t priority = (mach_msg_priority_t)(rs_pr >> 32); if (option64 & MACH64_MSG_VECTOR) { /* * only validate msg send related arguments. * * bad receive args do not stop us from sending * during combined send/rcv. */ mr = mach_msg_validate_data_vectors(&msg_vec, &aux_vec, vec_snd_count, option64, /* sending? */ TRUE); if (mr != MACH_MSG_SUCCESS) { goto send_fail; } send_uctx.send_msg_addr = msg_vec.msgv_data; send_uctx.send_msg_size = msg_vec.msgv_send_size; send_uctx.send_aux_size = aux_vec.msgv_send_size; if (send_uctx.send_aux_size) { send_uctx.send_aux_addr = aux_vec.msgv_data; } } else { send_uctx.send_msg_addr = args->data; send_uctx.send_msg_size = (mach_msg_size_t)(mb_ss >> 32); } mr = mach_msg_trap_send(&send_uctx, option64, msg_timeout, priority); } send_fail: /* if send failed, skip receive */ if (mr != MACH_MSG_SUCCESS) { KDBG(MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_KMSG_INFO) | DBG_FUNC_END, mr); goto end; } if (option64 & MACH64_RCV_MSG) { mach_port_name_t rcv_name = (mach_port_name_t)(dc_rn >> 32); mach_port_name_t sync_send = MACH_PORT_NULL; mach_vm_address_t msg_addr = 0; mach_msg_size_t msg_size = 0; mach_vm_address_t aux_addr = 0; mach_msg_size_t aux_size = 0; if (option64 & MACH64_MSG_VECTOR) { /* only validate msg receive related arguments */ mr = mach_msg_validate_data_vectors(&msg_vec, &aux_vec, vec_rcv_count, option64, /* sending? */ FALSE); if (mr != MACH_MSG_SUCCESS) { goto end; } msg_addr = msg_vec.msgv_rcv_addr ?: msg_vec.msgv_data; msg_size = msg_vec.msgv_rcv_size; aux_size = aux_vec.msgv_rcv_size; if (aux_size) { aux_addr = aux_vec.msgv_rcv_addr ?: aux_vec.msgv_data; } } else { msg_addr = args->data; msg_size = (mach_msg_size_t)rs_pr; } if (option64 & MACH64_RCV_SYNC_WAIT) { /* use msgh_remote_port as sync send boosting port */ sync_send = (mach_port_name_t)mr_lp; } mr = mach_msg_trap_receive(msg_addr, aux_addr, option64, msg_timeout, sync_send, msg_size, aux_size, rcv_name); } end: /* unblock call is idempotent */ ipc_port_thread_group_unblocked(); return mr; } /* * Routine: mach_msg_rcv_link_special_reply_port * Purpose: * Link the special reply port(rcv right) to the * other end of the sync ipc channel. * Conditions: * Nothing locked. * Returns: * None. */ static mach_msg_return_t mach_msg_rcv_link_special_reply_port( ipc_port_t special_reply_port, mach_port_name_t dest_name_port) { ipc_port_t dest_port = IP_NULL; kern_return_t kr; if (current_thread()->ith_special_reply_port != special_reply_port) { return MACH_RCV_INVALID_NOTIFY; } /* Copyin the destination port */ if (!MACH_PORT_VALID(dest_name_port)) { return MACH_RCV_INVALID_NOTIFY; } kr = ipc_port_translate_send(current_space(), dest_name_port, &dest_port); if (kr == KERN_SUCCESS) { ip_reference(dest_port); ip_mq_unlock(dest_port); /* * The receive right of dest port might have gone away, * do not fail the receive in that case. */ ipc_port_link_special_reply_port(special_reply_port, dest_port, FALSE); ip_release(dest_port); } return MACH_MSG_SUCCESS; } /* * Routine: mach_msg_receive_results_complete * Purpose: * Get thread's turnstile back from the object and * if object is a special reply port then reset its * linkage. * Condition: * Nothing locked. * Returns: * None. */ void mach_msg_receive_results_complete(ipc_object_t object) { thread_t self = current_thread(); ipc_port_t port = IP_NULL; boolean_t get_turnstile = (self->turnstile == TURNSTILE_NULL); if (io_is_pset(object)) { assert(self->turnstile != TURNSTILE_NULL); return; } uint8_t flags = IPC_PORT_ADJUST_SR_ALLOW_SYNC_LINKAGE; /* * Don't clear the ip_srp_msg_sent bit if... */ if (!((self->ith_state == MACH_RCV_TOO_LARGE && self->ith_option & MACH_RCV_LARGE) || //msg was too large and the next receive will get it self->ith_state == MACH_RCV_INTERRUPTED || self->ith_state == MACH_RCV_TIMED_OUT || self->ith_state == MACH_RCV_PORT_CHANGED)) { flags |= IPC_PORT_ADJUST_SR_RECEIVED_MSG; } port = ip_object_to_port(object); if (ip_is_special_reply_port(port) || get_turnstile) { ip_mq_lock(port); ipc_port_adjust_special_reply_port_locked(port, NULL, flags, get_turnstile); /* port unlocked */ } assert(self->turnstile != TURNSTILE_NULL); /* thread now has a turnstile */ } SECURITY_READ_ONLY_LATE(struct mach_msg_filter_callbacks) mach_msg_filter_callbacks; kern_return_t mach_msg_filter_register_callback( const struct mach_msg_filter_callbacks *callbacks) { size_t size = 0; if (callbacks == NULL) { return KERN_INVALID_ARGUMENT; } if (mach_msg_filter_callbacks.fetch_filter_policy != NULL) { /* double init */ return KERN_FAILURE; } if (callbacks->version >= MACH_MSG_FILTER_CALLBACKS_VERSION_0) { /* check for missing v0 callbacks */ if (callbacks->fetch_filter_policy == NULL) { return KERN_INVALID_ARGUMENT; } size = offsetof(struct mach_msg_filter_callbacks, alloc_service_port_sblabel); } if (callbacks->version >= MACH_MSG_FILTER_CALLBACKS_VERSION_1) { if (callbacks->alloc_service_port_sblabel == NULL || callbacks->dealloc_service_port_sblabel == NULL || callbacks->derive_sblabel_from_service_port == NULL || callbacks->get_connection_port_filter_policy == NULL || callbacks->retain_sblabel == NULL) { return KERN_INVALID_ARGUMENT; } size = sizeof(struct mach_msg_filter_callbacks); } if (callbacks->version > MACH_MSG_FILTER_CALLBACKS_VERSION_1) { /* invalid version */ return KERN_INVALID_ARGUMENT; } memcpy(&mach_msg_filter_callbacks, callbacks, size); return KERN_SUCCESS; } /* This function should only be called if the task and port allow message filtering */ boolean_t mach_msg_fetch_filter_policy( void *port_label, mach_msg_id_t msgh_id, mach_msg_filter_id *fid) { boolean_t ret = TRUE; if (mach_msg_fetch_filter_policy_callback == NULL) { *fid = MACH_MSG_FILTER_POLICY_ALLOW; return true; } ret = mach_msg_fetch_filter_policy_callback(current_task(), port_label, msgh_id, fid); return ret; } |