1*e3723e1fSApple OSS DistributionsMach Port Guard exceptions 2*e3723e1fSApple OSS Distributions========================== 3*e3723e1fSApple OSS Distributions 4*e3723e1fSApple OSS DistributionsMach Port Guard exceptions are used to denote various misuses of Mach IPC. 5*e3723e1fSApple OSS DistributionsThis document covers their meaning, as well as hints on how to debug issues. 6*e3723e1fSApple OSS Distributions 7*e3723e1fSApple OSS Distributions## Anatomy of a Mach Port Guard Exception 8*e3723e1fSApple OSS Distributions 9*e3723e1fSApple OSS DistributionsMach Port Guard Exception is delivered via a Mach exception. These constructs 10*e3723e1fSApple OSS Distributionsare described in the `<kern/exc_guard.h>` header: 11*e3723e1fSApple OSS Distributions 12*e3723e1fSApple OSS Distributions``` 13*e3723e1fSApple OSS Distributions/* 14*e3723e1fSApple OSS Distributions * Mach port guards use the exception codes like this: 15*e3723e1fSApple OSS Distributions * 16*e3723e1fSApple OSS Distributions * code: 17*e3723e1fSApple OSS Distributions * +-----------------------------+----------------+-----------------+ 18*e3723e1fSApple OSS Distributions * |[63:61] GUARD_TYPE_MACH_PORT | [60:32] flavor | [31:0] target | 19*e3723e1fSApple OSS Distributions * +-----------------------------+----------------+-----------------+ 20*e3723e1fSApple OSS Distributions * 21*e3723e1fSApple OSS Distributions * subcode: 22*e3723e1fSApple OSS Distributions * +----------------------------------------------------------------+ 23*e3723e1fSApple OSS Distributions * |[63:0] payload | 24*e3723e1fSApple OSS Distributions * +----------------------------------------------------------------+ 25*e3723e1fSApple OSS Distributions */ 26*e3723e1fSApple OSS Distributions 27*e3723e1fSApple OSS Distributions#define GUARD_TYPE_MACH_PORT 0x1 /* guarded mach port */ 28*e3723e1fSApple OSS Distributions``` 29*e3723e1fSApple OSS Distributions 30*e3723e1fSApple OSS DistributionsThis description is mostly useful to implementors of a Mach exception handler, 31*e3723e1fSApple OSS Distributionsmost clients will instead receive a crash log where ReportCrash tried its best 32*e3723e1fSApple OSS Distributionsto decode the message above, however sometimes it lags behind the introduction 33*e3723e1fSApple OSS Distributionsof new guards, and this document helps decoding the matrix. 34*e3723e1fSApple OSS Distributions 35*e3723e1fSApple OSS DistributionsA properly decoded crashlog will contain information like this: 36*e3723e1fSApple OSS Distributions 37*e3723e1fSApple OSS Distributions``` 38*e3723e1fSApple OSS DistributionsException Type: EXC_GUARD (SIGKILL) 39*e3723e1fSApple OSS DistributionsException Subtype: GUARD_TYPE_MACH_PORT 40*e3723e1fSApple OSS DistributionsException Message: INVALID_NAME on mach port 9987 (guarded with 0x0000000000000000) 41*e3723e1fSApple OSS DistributionsException Codes: 0x0000000000002703, 0x0000000000000000 42*e3723e1fSApple OSS DistributionsTermination Reason: GUARD 2305845208236959491 43*e3723e1fSApple OSS Distributions``` 44*e3723e1fSApple OSS Distributions 45*e3723e1fSApple OSS DistributionsHowever, in some cases, the decoding is partial and one needs to decode 46*e3723e1fSApple OSS Distributionsthese by hand. In all cases, ReportCrash will always recognize Mach Port Guards, 47*e3723e1fSApple OSS Distributionsand the `Exception Codes` and `Termination Reason` are the lines we need, 48*e3723e1fSApple OSS Distributionsin terms of the kernel defined fields, they follow this template: 49*e3723e1fSApple OSS Distributions 50*e3723e1fSApple OSS Distributions``` 51*e3723e1fSApple OSS DistributionsException Codes: $(code.target), $(subcode.payload) 52*e3723e1fSApple OSS DistributionsTermination Reason: GUARD $(code) 53*e3723e1fSApple OSS Distributions``` 54*e3723e1fSApple OSS Distributions 55*e3723e1fSApple OSS DistributionsWhen applying it to the example above, we can see that: 56*e3723e1fSApple OSS Distributions 57*e3723e1fSApple OSS Distributions- the code is `2305845208236959491 == 0x2000020000002703` which decodes into 58*e3723e1fSApple OSS Distributions `{ guard_type: GUARD_TYPE_MACH_PORT, flavor: 0x200, target: 0x2703 }` 59*e3723e1fSApple OSS Distributions- the payload is `0`. 60*e3723e1fSApple OSS Distributions 61*e3723e1fSApple OSS Distributions 62*e3723e1fSApple OSS Distributions## Typical Mach Port bugs, and how to reason about them 63*e3723e1fSApple OSS Distributions 64*e3723e1fSApple OSS DistributionsIn this section, we will describe the most common cases of Mach IPC issues 65*e3723e1fSApple OSS Distributionsthat are reported by Mach Port Guard exceptions, and how to reason about them. 66*e3723e1fSApple OSS Distributions 67*e3723e1fSApple OSS Distributions### Port right mismanagement 68*e3723e1fSApple OSS Distributions 69*e3723e1fSApple OSS DistributionsThis is by far the most common source of issues with Mach IPC. Unlike file 70*e3723e1fSApple OSS Distributionsdescriptors which have a really simple lifecycle (you destroy them with 71*e3723e1fSApple OSS Distributions`close()`), Mach ports have several associated rights that must be managed 72*e3723e1fSApple OSS Distributionsproperly. 73*e3723e1fSApple OSS Distributions 74*e3723e1fSApple OSS DistributionsPort right mismanagement usually happens when some subsystem has a dangling 75*e3723e1fSApple OSS Distributionsreference to a Mach port name that it has already destroyed, and keeps using it. 76*e3723e1fSApple OSS DistributionsThis is the analogous to a use-after-free for allocated memory. 77*e3723e1fSApple OSS Distributions 78*e3723e1fSApple OSS DistributionsThe most common sources of issues are: 79*e3723e1fSApple OSS Distributions 80*e3723e1fSApple OSS Distributions- confusions in accounting of the receive right and send rights, due to the 81*e3723e1fSApple OSS Distributions arcane `mach_port_mod_refs` API being misused. We recommend using 82*e3723e1fSApple OSS Distributions `mach_port_destruct()` for receive rights, and `mach_port_deallocate()` 83*e3723e1fSApple OSS Distributions for send or send-once rights. 84*e3723e1fSApple OSS Distributions 85*e3723e1fSApple OSS Distributions- dangling port names: the port name management was correct, but the reference 86*e3723e1fSApple OSS Distributions to the port wasn't reset to `MACH_PORT_NULL`, leading to eventual over-releases. 87*e3723e1fSApple OSS Distributions 88*e3723e1fSApple OSS Distributions- threading safety related issues where the port management isn't properly 89*e3723e1fSApple OSS Distributions synchronized. 90*e3723e1fSApple OSS Distributions 91*e3723e1fSApple OSS Distributions 92*e3723e1fSApple OSS Distributions## List of fatal Mach Port Guard Exceptions 93*e3723e1fSApple OSS Distributions 94*e3723e1fSApple OSS DistributionsSome of the exceptions are always fatal (hitting them will cause the process to 95*e3723e1fSApple OSS Distributionsbe terminated) regardless of the process kind. 96*e3723e1fSApple OSS Distributions 97*e3723e1fSApple OSS Distributions 98*e3723e1fSApple OSS Distributions### `kGUARD_EXC_DESTROY` 0x00000001 99*e3723e1fSApple OSS Distributions 100*e3723e1fSApple OSS Distributions- **ReportCrash Name**: DESTROY, 101*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 102*e3723e1fSApple OSS Distributions- **Payload meaning**: the correct value of the context guarding the Mach Port. 103*e3723e1fSApple OSS Distributions 104*e3723e1fSApple OSS DistributionsThis exception is thrown when a guarded receive right is being destroyed 105*e3723e1fSApple OSS Distributionswith an incorrect or missing context presented by the caller. 106*e3723e1fSApple OSS Distributions 107*e3723e1fSApple OSS DistributionsReceive rights can be guarded with a caller chosen context via mechanisms such 108*e3723e1fSApple OSS Distributionsas: 109*e3723e1fSApple OSS Distributions 110*e3723e1fSApple OSS Distributions- `mach_port_guard()` being called, 111*e3723e1fSApple OSS Distributions- the right being made using the `MPO_CONTEXT_AS_GUARD` flag of 112*e3723e1fSApple OSS Distributions `mach_port_construct()`, 113*e3723e1fSApple OSS Distributions- the usage of a guarded port descriptor in a Mach message. 114*e3723e1fSApple OSS Distributions 115*e3723e1fSApple OSS Distributions 116*e3723e1fSApple OSS DistributionsExamples of such ports are the receive rights used for XPC Connections, 117*e3723e1fSApple OSS Distributionshitting such a bug usually is a sign of port right mismanagement. 118*e3723e1fSApple OSS Distributions 119*e3723e1fSApple OSS Distributions 120*e3723e1fSApple OSS Distributions### `kGUARD_EXC_MOD_REFS` 0x00000002 121*e3723e1fSApple OSS Distributions 122*e3723e1fSApple OSS Distributions- **ReportCrash Name**: OVER\_DEALLOC or MOD\_REFS, 123*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 124*e3723e1fSApple OSS Distributions- **Payload meaning**: 125*e3723e1fSApple OSS Distributions - `0x0100000000000000`: a `deallocate` function, 126*e3723e1fSApple OSS Distributions - `0x0200000000000000`: a `destroy` function, 127*e3723e1fSApple OSS Distributions - `0x0300000000000000`: via the side effect of a message send (port copyin). 128*e3723e1fSApple OSS Distributions 129*e3723e1fSApple OSS DistributionsThis exception is thrown when the last send right of a pinned mach port is being 130*e3723e1fSApple OSS Distributionsdestroyed. Pinned ports must never have a send right count going to zero in a 131*e3723e1fSApple OSS Distributionsgiven IPC space. 132*e3723e1fSApple OSS Distributions 133*e3723e1fSApple OSS DistributionsExamples of such ports are thread and task control ports. This is usually a 134*e3723e1fSApple OSS Distributionssign of port-right mismanagement. 135*e3723e1fSApple OSS Distributions 136*e3723e1fSApple OSS Distributions 137*e3723e1fSApple OSS Distributions### `kGUARD_EXC_INVALID_OPTIONS` 0x00000003 138*e3723e1fSApple OSS Distributions 139*e3723e1fSApple OSS Distributions- **ReportCrash Name**: INVALID\_OPTIONS, 140*e3723e1fSApple OSS Distributions- **Target meaning**: the message ID of a rejected message via the legacy 141*e3723e1fSApple OSS Distributions `mach_msg_trap()` or zero, 142*e3723e1fSApple OSS Distributions- **Payload meaning**: the kernel sanitized (see `ipc_current_user_policy()`) 143*e3723e1fSApple OSS Distributions for the rejected `mach_msg2()` call. 144*e3723e1fSApple OSS Distributions 145*e3723e1fSApple OSS DistributionsThere are several policies dictating the shape of options passed to calls of the 146*e3723e1fSApple OSS Distributions`mach_msg()` family. These correspond to programming mistakes. 147*e3723e1fSApple OSS Distributions 148*e3723e1fSApple OSS Distributions 149*e3723e1fSApple OSS Distributions### `kGUARD_EXC_SET_CONTEXT` 0x00000004 150*e3723e1fSApple OSS Distributions 151*e3723e1fSApple OSS Distributions- **ReportCrash Name**: SET\_CONTEXT, 152*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 153*e3723e1fSApple OSS Distributions- **Payload meaning**: the value of the context guarding the Mach Port. 154*e3723e1fSApple OSS Distributions 155*e3723e1fSApple OSS DistributionsThis exception is thrown when `mach_port_set_context()` or 156*e3723e1fSApple OSS Distributions`mach_port_swap_guard()` is used against a Mach Port using a strict guard (a 157*e3723e1fSApple OSS DistributionsMach Port constructed with the `MPO_STRICT | MPO_CONTEXT_AS_GUARD` flags). 158*e3723e1fSApple OSS Distributions 159*e3723e1fSApple OSS DistributionsExamples of such ports are the client-side receive right for XPC connections. 160*e3723e1fSApple OSS DistributionsThis is usually a sign of port right mismanagement. 161*e3723e1fSApple OSS Distributions 162*e3723e1fSApple OSS Distributions 163*e3723e1fSApple OSS Distributions### `kGUARD_EXC_THREAD_SET_STATE` 0x00000005 164*e3723e1fSApple OSS Distributions 165*e3723e1fSApple OSS Distributions- **ReportCrash Name**: N/A, 166*e3723e1fSApple OSS Distributions- **Target meaning**: always zero, 167*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 168*e3723e1fSApple OSS Distributions 169*e3723e1fSApple OSS DistributionsThis exception is thrown when a process is trying to use the 170*e3723e1fSApple OSS Distributions`thread_set_state()` interface, or any interface leading to it (such as trying 171*e3723e1fSApple OSS Distributionsto change thread state via replying to a Mach exception message), and that this 172*e3723e1fSApple OSS Distributionsis disallowed by policy for this process. 173*e3723e1fSApple OSS Distributions 174*e3723e1fSApple OSS Distributions 175*e3723e1fSApple OSS Distributions### `kGUARD_EXC_EXCEPTION_BEHAVIOR_ENFORCE` 0x00000006 176*e3723e1fSApple OSS Distributions 177*e3723e1fSApple OSS Distributions- **ReportCrash Name**: N/A, 178*e3723e1fSApple OSS Distributions- **Target meaning**: always zero, 179*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 180*e3723e1fSApple OSS Distributions 181*e3723e1fSApple OSS DistributionsThis exception is thrown when a process is trying to register an exception port 182*e3723e1fSApple OSS Distributionsfor a behavior not using a task identity port, and that this is disallowed by 183*e3723e1fSApple OSS Distributionspolicy for this process. 184*e3723e1fSApple OSS Distributions 185*e3723e1fSApple OSS Distributions 186*e3723e1fSApple OSS Distributions### `kGUARD_EXC_SERVICE_PORT_VIOLATION_FATAL` 0x00000007 187*e3723e1fSApple OSS Distributions 188*e3723e1fSApple OSS Distributions- **ReportCrash Name**: N/A, 189*e3723e1fSApple OSS Distributions- **Target meaning**: the type of service port defense violation, 190*e3723e1fSApple OSS Distributions- **Payload meaning**: violator port name if we have it, zero otherwise. 191*e3723e1fSApple OSS Distributions 192*e3723e1fSApple OSS DistributionsThis exception is thrown when the `service_port_defense_enabled` bootarg is set and a 193*e3723e1fSApple OSS Distributionsprocess violates service port defense policy, which includes copyin a service port receive 194*e3723e1fSApple OSS Distributionsright from process other than launchd, arming pd notification on a service port if the 195*e3723e1fSApple OSS Distributionsprocess is not launchd, and setting a service port as exception port. 196*e3723e1fSApple OSS Distributions 197*e3723e1fSApple OSS Distributions### `kGUARD_EXC_UNGUARDED` 0x00000008 198*e3723e1fSApple OSS Distributions 199*e3723e1fSApple OSS Distributions- **ReportCrash Name**: UNGUARDED, 200*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 201*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 202*e3723e1fSApple OSS Distributions 203*e3723e1fSApple OSS DistributionsThis exception is thrown when a process is trying to perform an operation (such 204*e3723e1fSApple OSS Distributionsas `mach_port_unguard()` on a port that isn't guarded. 205*e3723e1fSApple OSS Distributions 206*e3723e1fSApple OSS DistributionsThis is usually a sign of port right mismanagement. 207*e3723e1fSApple OSS Distributions 208*e3723e1fSApple OSS Distributions 209*e3723e1fSApple OSS Distributions### `kGUARD_EXC_INCORRECT_GUARD` 0x00000010 210*e3723e1fSApple OSS Distributions 211*e3723e1fSApple OSS Distributions- **ReportCrash Name**: INCORRECT\_GUARD, 212*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 213*e3723e1fSApple OSS Distributions- **Payload meaning**: the value of the context guarding the Mach Port. 214*e3723e1fSApple OSS Distributions 215*e3723e1fSApple OSS DistributionsThis exception is thrown when a process is attemtping a guarded operation but 216*e3723e1fSApple OSS Distributionspassed a context to the call that doesn't match the context actually guarding 217*e3723e1fSApple OSS Distributionsthis port. 218*e3723e1fSApple OSS Distributions 219*e3723e1fSApple OSS DistributionsThis is usually a sign of port right mismanagement. 220*e3723e1fSApple OSS Distributions 221*e3723e1fSApple OSS Distributions 222*e3723e1fSApple OSS Distributions### `kGUARD_EXC_IMMOVABLE` 0x00000020 223*e3723e1fSApple OSS Distributions 224*e3723e1fSApple OSS Distributions- **ReportCrash Name**: ILLEGAL\_MOVE, 225*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 226*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 227*e3723e1fSApple OSS Distributions 228*e3723e1fSApple OSS DistributionsThis exception is thrown when a process is attempting to move a port right, 229*e3723e1fSApple OSS Distributionsand this has been disallowed by policy for this port type and process. 230*e3723e1fSApple OSS Distributions 231*e3723e1fSApple OSS DistributionsThis is usually a programming mistake (or legacy code that hasn't been updated 232*e3723e1fSApple OSS Distributionsto the most recent Mach IPC policies). 233*e3723e1fSApple OSS Distributions 234*e3723e1fSApple OSS Distributions 235*e3723e1fSApple OSS Distributions### `kGUARD_EXC_STRICT_REPLY` 0x00000040 236*e3723e1fSApple OSS Distributions 237*e3723e1fSApple OSS DistributionsThis exception is thrown for reply port semantics mistakes, if the 238*e3723e1fSApple OSS Distributions`enforce_strict_reply` boot-arg is set. As this is not a default config at this 239*e3723e1fSApple OSS Distributionspoint, and that this is likely going to be phased out in favor of tracking reply 240*e3723e1fSApple OSS Distributionsports at the port type level, this is left mostly undocumented on purpose. 241*e3723e1fSApple OSS Distributions 242*e3723e1fSApple OSS Distributions 243*e3723e1fSApple OSS Distributions### `kGUARD_EXC_MSG_FILTERED` 0x00000080 244*e3723e1fSApple OSS Distributions 245*e3723e1fSApple OSS Distributions- **ReportCrash Name**: MSG\_FILTERED, 246*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 247*e3723e1fSApple OSS Distributions- **Payload meaning**: the message ID of the filtered message. 248*e3723e1fSApple OSS Distributions 249*e3723e1fSApple OSS DistributionsThis exception is thrown when a process is not allowed to send the specified 250*e3723e1fSApple OSS Distributionsmessage due to sandboxing, and that a hard failure has been requested by either 251*e3723e1fSApple OSS Distributionsthe client or the Sandbox. 252*e3723e1fSApple OSS Distributions 253*e3723e1fSApple OSS Distributions 254*e3723e1fSApple OSS Distributions## List of optionally fatal Mach Port Guard Exceptions 255*e3723e1fSApple OSS Distributions 256*e3723e1fSApple OSS DistributionsSome of the exceptions are optionally fatal. Hitting them will only terminate 257*e3723e1fSApple OSS Distributionsthe process when it is opted for a hardened Mach IPC environment. Such processes 258*e3723e1fSApple OSS Distributionsare: 259*e3723e1fSApple OSS Distributions 260*e3723e1fSApple OSS Distributions- platform binaries, 261*e3723e1fSApple OSS Distributions- processes with a browser entitlement (`com.apple.developer.web-browser-engine.*`). 262*e3723e1fSApple OSS Distributions 263*e3723e1fSApple OSS DistributionsThe `task_exc_guard_default` boot-arg can be used to change these defaults. 264*e3723e1fSApple OSS Distributions 265*e3723e1fSApple OSS DistributionsNote: using the `amfi=-1` or similar boot-args will make all processes appear to 266*e3723e1fSApple OSS Distributionsbe platform binaries, which in turn will turn a lot of bugs in 3p software into 267*e3723e1fSApple OSS Distributionshard crashes. Most notably at this time, electron apps cause several guard 268*e3723e1fSApple OSS Distributionsexceptions in the Mach IPC and VM world. This is not a supported configuration. 269*e3723e1fSApple OSS Distributions 270*e3723e1fSApple OSS Distributions 271*e3723e1fSApple OSS Distributions### `kGUARD_EXC_INVALID_RIGHT` 0x00000100 272*e3723e1fSApple OSS Distributions 273*e3723e1fSApple OSS Distributions- **ReportCrash Name**: INVALID\_RIGHT, 274*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 275*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 276*e3723e1fSApple OSS Distributions 277*e3723e1fSApple OSS DistributionsThis exception is thrown when an operation is targetting a port which rights do 278*e3723e1fSApple OSS Distributionsnot match the caller's expectations. Examples of such mistakes are: 279*e3723e1fSApple OSS Distributions 280*e3723e1fSApple OSS Distributions- performing an operation expecting a port-set name, but passing a port name 281*e3723e1fSApple OSS Distributions instead, 282*e3723e1fSApple OSS Distributions- trying to receive a message and not owning the receive right for it. 283*e3723e1fSApple OSS Distributions 284*e3723e1fSApple OSS DistributionsThese correspond to cases leading to the `KERN_INVALID_RIGHT` or 285*e3723e1fSApple OSS Distributions`KERN_INVALID_CAPABILITY` error codes of most Mach IPC interfaces. 286*e3723e1fSApple OSS Distributions 287*e3723e1fSApple OSS DistributionsThis is usually a sign of port right mismanagement. 288*e3723e1fSApple OSS Distributions 289*e3723e1fSApple OSS Distributions 290*e3723e1fSApple OSS Distributions### `kGUARD_EXC_INVALID_NAME` 0x00000200 291*e3723e1fSApple OSS Distributions 292*e3723e1fSApple OSS Distributions- **ReportCrash Name**: INVALID\_NAME, 293*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 294*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 295*e3723e1fSApple OSS Distributions 296*e3723e1fSApple OSS DistributionsThis exception is thrown when an operation is targetting a name for which 297*e3723e1fSApple OSS Distributionsthe caller holds no right. 298*e3723e1fSApple OSS Distributions 299*e3723e1fSApple OSS DistributionsThese correspond to cases leading to the `KERN_INVALID_NAME` error code of most 300*e3723e1fSApple OSS DistributionsMach IPC interfaces. 301*e3723e1fSApple OSS Distributions 302*e3723e1fSApple OSS DistributionsThis is usually a sign of port right mismanagement. 303*e3723e1fSApple OSS Distributions 304*e3723e1fSApple OSS Distributions 305*e3723e1fSApple OSS Distributions### `kGUARD_EXC_INVALID_VALUE` 0x00000400 306*e3723e1fSApple OSS Distributions 307*e3723e1fSApple OSS Distributions- **ReportCrash Name**: INVALID\_VALUE, 308*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 309*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 310*e3723e1fSApple OSS Distributions 311*e3723e1fSApple OSS DistributionsThis exception is thrown when: 312*e3723e1fSApple OSS Distributions 313*e3723e1fSApple OSS Distributions- the caller is trying to apply a delta to the number of send rights of a port 314*e3723e1fSApple OSS Distributions name, and this would overflow the send right count, which is usually a sign of 315*e3723e1fSApple OSS Distributions port right mismanagement, 316*e3723e1fSApple OSS Distributions 317*e3723e1fSApple OSS Distributions- the trailer related arguments to `mach_port_peek()` are invalid, which is 318*e3723e1fSApple OSS Distributions typicaly a programming mistake. 319*e3723e1fSApple OSS Distributions 320*e3723e1fSApple OSS DistributionsThese correspond to cases leading to the `KERN_INVALID_VALUE` error code of most 321*e3723e1fSApple OSS DistributionsMach IPC interfaces. 322*e3723e1fSApple OSS Distributions 323*e3723e1fSApple OSS Distributions 324*e3723e1fSApple OSS Distributions### `kGUARD_EXC_INVALID_ARGUMENT` 0x00000800 325*e3723e1fSApple OSS Distributions 326*e3723e1fSApple OSS Distributions- **ReportCrash Name**: INVALID\_ARGUMENT, 327*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 328*e3723e1fSApple OSS Distributions- **Payload meaning**: the correct value of the context guarding the Mach Port. 329*e3723e1fSApple OSS Distributions 330*e3723e1fSApple OSS DistributionsThis exception is thrown when a caller is trying to guard an already guarded 331*e3723e1fSApple OSS Distributionsport. This should really have been named `kGUARD_EXC_ALREADY_GUARDED`. 332*e3723e1fSApple OSS Distributions 333*e3723e1fSApple OSS DistributionsThis is usually a sign of port right mismanagement. 334*e3723e1fSApple OSS Distributions 335*e3723e1fSApple OSS Distributions 336*e3723e1fSApple OSS Distributions### `kGUARD_EXC_KERN_FAILURE` 0x00004000 337*e3723e1fSApple OSS Distributions 338*e3723e1fSApple OSS Distributions- **ReportCrash Name**: KERN\_FAILURE, 339*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 340*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 341*e3723e1fSApple OSS Distributions 342*e3723e1fSApple OSS DistributionsThis exception is thrown when a caller is trying to request a port-destroyed 343*e3723e1fSApple OSS Distributionsnotification that is disallowed by system policy. This should really have been 344*e3723e1fSApple OSS Distributionsnamed `kGUARD_EXC_INVALID_PDREQUEST`. 345*e3723e1fSApple OSS Distributions 346*e3723e1fSApple OSS DistributionsThis is usually a sign of port right mismanagement. 347*e3723e1fSApple OSS Distributions 348*e3723e1fSApple OSS Distributions 349*e3723e1fSApple OSS Distributions### `kGUARD_EXC_SEND_INVALID_REPLY` 0x00010000 350*e3723e1fSApple OSS Distributions 351*e3723e1fSApple OSS Distributions- **ReportCrash Name**: SEND\_INVALID\_REPLY, 352*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 353*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 354*e3723e1fSApple OSS Distributions 355*e3723e1fSApple OSS DistributionsThis exception is thrown when a caller is trying to send a message whose reply 356*e3723e1fSApple OSS Distributionsport (the `msgh_local_port` field of a Mach message) violates policies around 357*e3723e1fSApple OSS Distributionsreply ports or its disposition. 358*e3723e1fSApple OSS Distributions 359*e3723e1fSApple OSS DistributionsThis is usually a sign of port right mismanagement. 360*e3723e1fSApple OSS Distributions 361*e3723e1fSApple OSS Distributions 362*e3723e1fSApple OSS Distributions### `kGUARD_EXC_SEND_INVALID_RIGHT` 0x00020000 363*e3723e1fSApple OSS Distributions 364*e3723e1fSApple OSS Distributions- **ReportCrash Name**: SEND\_INVALID\_RIGHT, 365*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 366*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 367*e3723e1fSApple OSS Distributions 368*e3723e1fSApple OSS DistributionsThis exception is thrown when a caller is trying to send a message where one of 369*e3723e1fSApple OSS Distributionsthe port descriptors denotes a right that doesn't match the requested 370*e3723e1fSApple OSS Distributionsdisposition (for example, a make-send disposition for a port where the process 371*e3723e1fSApple OSS Distributionsdoesn't own a receive right). 372*e3723e1fSApple OSS Distributions 373*e3723e1fSApple OSS DistributionsThis is usually a sign of port right mismanagement. 374*e3723e1fSApple OSS Distributions 375*e3723e1fSApple OSS Distributions 376*e3723e1fSApple OSS Distributions### `kGUARD_EXC_SEND_INVALID_VOUCHER` 0x00040000 377*e3723e1fSApple OSS Distributions 378*e3723e1fSApple OSS Distributions- **ReportCrash Name**: SEND\_INVALID\_VOUCHER, 379*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 380*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 381*e3723e1fSApple OSS Distributions 382*e3723e1fSApple OSS DistributionsThis exception is thrown when a caller is trying to send a message whose voucher 383*e3723e1fSApple OSS Distributionsport (the `msgh_voucher_port` field of a Mach message) violates policies around 384*e3723e1fSApple OSS Distributionsvoucher ports or its disposition. 385*e3723e1fSApple OSS Distributions 386*e3723e1fSApple OSS DistributionsThis is usually a sign of port right mismanagement. 387*e3723e1fSApple OSS Distributions 388*e3723e1fSApple OSS Distributions 389*e3723e1fSApple OSS Distributions### `kGUARD_EXC_RCV_INVALID_NAME` 0x00080000 390*e3723e1fSApple OSS Distributions 391*e3723e1fSApple OSS Distributions- **ReportCrash Name**: RCV\_INVALID\_NAME, 392*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 393*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 394*e3723e1fSApple OSS Distributions 395*e3723e1fSApple OSS DistributionsThis exception is thrown when a caller is trying to receive a message on a mach 396*e3723e1fSApple OSS Distributionsport name for which it doesn't hold a port-set or receive right. 397*e3723e1fSApple OSS Distributions 398*e3723e1fSApple OSS DistributionsThis is usually a sign of port right mismanagement. 399*e3723e1fSApple OSS Distributions 400*e3723e1fSApple OSS Distributions 401*e3723e1fSApple OSS Distributions## List of soft Mach Port Guard Exceptions 402*e3723e1fSApple OSS Distributions 403*e3723e1fSApple OSS DistributionsSome of the exceptions are never fatal (hitting them will only emit a simulated 404*e3723e1fSApple OSS Distributionscrash log, and the process will keep going). 405*e3723e1fSApple OSS Distributions 406*e3723e1fSApple OSS Distributions 407*e3723e1fSApple OSS Distributions### `kGUARD_EXC_RCV_GUARDED_DESC` 0x00100000 408*e3723e1fSApple OSS Distributions 409*e3723e1fSApple OSS Distributions- **ReportCrash Name**: RCV\_GUARDED\_DESC, 410*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 411*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 412*e3723e1fSApple OSS Distributions 413*e3723e1fSApple OSS DistributionsThis exception is thrown when a caller is trying to receive a message containing 414*e3723e1fSApple OSS Distributionsguarded port descriptors and it hasn't indicated that it knows how to parse them 415*e3723e1fSApple OSS Distributions(by passing the `MACH_RCV_GUARDED_DESC` flag to `mach_msg()`). 416*e3723e1fSApple OSS Distributions 417*e3723e1fSApple OSS DistributionsThe usage of guarded port descriptor is a protocol agreement between the client 418*e3723e1fSApple OSS Distributionsand the server, and a disagreement here is a programming mistake. 419*e3723e1fSApple OSS Distributions 420*e3723e1fSApple OSS DistributionsThis guard is only enabled on development kernels at this time. 421*e3723e1fSApple OSS Distributions 422*e3723e1fSApple OSS Distributions### `kGUARD_EXC_SERVICE_PORT_VIOLATION_NON_FATAL` 0x00100001 423*e3723e1fSApple OSS Distributions 424*e3723e1fSApple OSS Distributions- **ReportCrash Name**: N/A, 425*e3723e1fSApple OSS Distributions- **Target meaning**: the type of service port defense violation, 426*e3723e1fSApple OSS Distributions- **Payload meaning**: violator port name if we have it, zero otherwise. 427*e3723e1fSApple OSS Distributions 428*e3723e1fSApple OSS DistributionsThis is the non fatal version of `kGUARD_EXC_SERVICE_PORT_VIOLATION_FATAL`, which is 429*e3723e1fSApple OSS Distributionsused when the `service_port_defense_enabled` bootarg is not set. 430*e3723e1fSApple OSS Distributions 431*e3723e1fSApple OSS Distributions 432*e3723e1fSApple OSS Distributions### `kGUARD_EXC_PROVISIONAL_REPLY_PORT` 0x00100002 433*e3723e1fSApple OSS Distributions 434*e3723e1fSApple OSS Distributions- **ReportCrash Name**: N/A, 435*e3723e1fSApple OSS Distributions- **Target meaning**: always zero, 436*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 437*e3723e1fSApple OSS Distributions 438*e3723e1fSApple OSS DistributionsThis exception is thrown when a 1p process is trying to create a provisional 439*e3723e1fSApple OSS Distributionsreply port on iOS. It is currently a soft crash to collect telemetry before 440*e3723e1fSApple OSS Distributionsthe actual enforcement. 441*e3723e1fSApple OSS Distributions 442*e3723e1fSApple OSS Distributions 443*e3723e1fSApple OSS Distributions### `kGUARD_EXC_MOD_REFS_NON_FATAL` 0x00200000 444*e3723e1fSApple OSS Distributions 445*e3723e1fSApple OSS Distributions- **ReportCrash Name**: OVERDEALLOC\_SOFT, 446*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 447*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 448*e3723e1fSApple OSS Distributions 449*e3723e1fSApple OSS DistributionsThis is the same as `kGUARD_EXC_MOD_REFS`, except that this is delivered as a 450*e3723e1fSApple OSS Distributionssoft error. 451*e3723e1fSApple OSS Distributions 452*e3723e1fSApple OSS Distributions 453*e3723e1fSApple OSS Distributions### `kGUARD_EXC_IMMOVABLE_NON_FATAL` 0x00400000 454*e3723e1fSApple OSS Distributions 455*e3723e1fSApple OSS Distributions- **ReportCrash Name**: ILLEGALMOVE\_SOFT. 456*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 457*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 458*e3723e1fSApple OSS Distributions 459*e3723e1fSApple OSS DistributionsThis is the same as `kGUARD_EXC_IMMOVABLE`, except that this is delivered as a 460*e3723e1fSApple OSS Distributionssoft error. 461*e3723e1fSApple OSS Distributions 462*e3723e1fSApple OSS Distributions 463*e3723e1fSApple OSS Distributions### `kGUARD_EXC_REQUIRE_REPLY_PORT_SEMANTICS` 0x00800000 464*e3723e1fSApple OSS Distributions 465*e3723e1fSApple OSS Distributions- **ReportCrash Name**: REQUIRE\_REPLY\_PORT\_SEMANTICS, 466*e3723e1fSApple OSS Distributions- **Target meaning**: the mach port name the incorrect operation targets, 467*e3723e1fSApple OSS Distributions- **Payload meaning**: always zero. 468*e3723e1fSApple OSS Distributions 469*e3723e1fSApple OSS DistributionsThis exception is thrown when a caller is violating the reply port semantics in 470*e3723e1fSApple OSS Distributionsa process where this is disallowed by policy. This is used to gather telemetry 471*e3723e1fSApple OSS Distributionsaround violators pending enforcement in a future release. 472*e3723e1fSApple OSS Distributions 473*e3723e1fSApple OSS DistributionsThis is usually a sign of a programming mistake (violation of the reply port 474*e3723e1fSApple OSS Distributionssemantics rules). 475*e3723e1fSApple OSS Distributions 476