1 /* 2 * Copyright (c) 2013-2019 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 24 #ifndef __CONTENT_FILTER_H__ 25 #define __CONTENT_FILTER_H__ 26 27 #include <sys/param.h> 28 #include <sys/types.h> 29 #include <sys/_types/_timeval64.h> 30 #include <sys/socket.h> 31 #include <sys/syslog.h> 32 #include <netinet/in.h> 33 #include <stdint.h> 34 #include <corecrypto/ccsha2.h> 35 36 #ifdef BSD_KERNEL_PRIVATE 37 #include <sys/mbuf.h> 38 #include <sys/socketvar.h> 39 #endif /* BSD_KERNEL_PRIVATE */ 40 41 #ifndef XNU_KERNEL_PRIVATE 42 #include <TargetConditionals.h> 43 #endif 44 45 __BEGIN_DECLS 46 47 #ifdef PRIVATE 48 49 /* 50 * Kernel control name for an instance of a Content Filter 51 * Use CTLIOCGINFO to find out the corresponding kernel control id 52 * to be set in the sc_id field of sockaddr_ctl for connect(2) 53 * Note: the sc_unit is ephemeral 54 */ 55 #define CONTENT_FILTER_CONTROL_NAME "com.apple.content-filter" 56 57 /* 58 * Opaque socket identifier 59 */ 60 typedef uint64_t cfil_sock_id_t; 61 62 #define CFIL_SOCK_ID_NONE UINT64_MAX 63 64 65 /* 66 * CFIL_OPT_NECP_CONTROL_UNIT 67 * To set or get the NECP filter control unit for the kernel control socket 68 * The option level is SYSPROTO_CONTROL 69 */ 70 #define CFIL_OPT_NECP_CONTROL_UNIT 1 /* uint32_t */ 71 72 /* 73 * CFIL_OPT_GET_SOCKET_INFO 74 * To get information about a given socket that is being filtered. 75 */ 76 #define CFIL_OPT_GET_SOCKET_INFO 2 /* uint32_t */ 77 78 /* 79 * CFIL_OPT_PRESERVE_CONNECTIONS 80 * To set or get the preserve-connections setting for the filter 81 */ 82 #define CFIL_OPT_PRESERVE_CONNECTIONS 3 /* uint32_t */ 83 84 /* 85 * struct cfil_opt_sock_info 86 * 87 * Contains information about a socket that is being filtered. 88 */ 89 struct cfil_opt_sock_info { 90 cfil_sock_id_t cfs_sock_id; 91 int cfs_sock_family; /* e.g. PF_INET */ 92 int cfs_sock_type; /* e.g. SOCK_STREAM */ 93 int cfs_sock_protocol; /* e.g. IPPROTO_TCP */ 94 union sockaddr_in_4_6 cfs_local; 95 union sockaddr_in_4_6 cfs_remote; 96 pid_t cfs_pid; 97 pid_t cfs_e_pid; 98 uuid_t cfs_uuid; 99 uuid_t cfs_e_uuid; 100 }; 101 102 /* 103 * How many filter may be active simultaneously 104 */ 105 106 #ifdef XNU_KERNEL_PRIVATE 107 #if !XNU_TARGET_OS_OSX 108 #define CFIL_MAX_FILTER_COUNT 2 109 #else 110 #define CFIL_MAX_FILTER_COUNT 8 111 #endif /* !XNU_TARGET_OS_OSX */ 112 #else 113 #if !TARGET_OS_OSX 114 #define CFIL_MAX_FILTER_COUNT 2 115 #else 116 #define CFIL_MAX_FILTER_COUNT 8 117 #endif /* !TARGET_OS_OSX */ 118 #endif /* XNU_KERNEL_PRIVATE */ 119 120 121 /* 122 * Crypto Support 123 */ 124 #define CFIL_CRYPTO 1 125 #define CFIL_CRYPTO_SIGNATURE_SIZE 32 126 #define CFIL_CRYPTO_DATA_EVENT 1 127 128 typedef uint8_t cfil_crypto_key[CCSHA256_OUTPUT_SIZE]; 129 typedef uint8_t cfil_crypto_signature[CFIL_CRYPTO_SIGNATURE_SIZE]; 130 131 typedef struct cfil_crypto_state { 132 const struct ccdigest_info *digest_info; 133 cfil_crypto_key key; 134 } *cfil_crypto_state_t; 135 136 typedef struct cfil_crypto_data { 137 uuid_t flow_id; 138 u_int64_t sock_id; 139 u_int32_t direction; 140 union sockaddr_in_4_6 remote; 141 union sockaddr_in_4_6 local; 142 u_int32_t socketProtocol; 143 pid_t pid; 144 pid_t effective_pid; 145 uuid_t uuid; 146 uuid_t effective_uuid; 147 u_int64_t byte_count_in; 148 u_int64_t byte_count_out; 149 } *cfil_crypto_data_t; 150 151 /* 152 * Types of messages 153 * 154 * Event messages flow from kernel to user space while action 155 * messages flow in the reverse direction. 156 * A message in entirely represented by a packet sent or received 157 * on a Content Filter kernel control socket. 158 */ 159 #define CFM_TYPE_EVENT 1 /* message from kernel */ 160 #define CFM_TYPE_ACTION 2 /* message to kernel */ 161 162 /* 163 * Operations associated with events from kernel 164 */ 165 #define CFM_OP_SOCKET_ATTACHED 1 /* a socket has been attached */ 166 #define CFM_OP_SOCKET_CLOSED 2 /* a socket is being closed */ 167 #define CFM_OP_DATA_OUT 3 /* data being sent */ 168 #define CFM_OP_DATA_IN 4 /* data being received */ 169 #define CFM_OP_DISCONNECT_OUT 5 /* no more outgoing data */ 170 #define CFM_OP_DISCONNECT_IN 6 /* no more incoming data */ 171 #define CFM_OP_STATS 7 /* periodic stats report(s) */ 172 173 /* 174 * Operations associated with action from filter to kernel 175 */ 176 #define CFM_OP_DATA_UPDATE 16 /* update pass or peek offsets */ 177 #define CFM_OP_DROP 17 /* shutdown socket, no more data */ 178 #define CFM_OP_BLESS_CLIENT 18 /* mark a client flow as already filtered, passes a uuid */ 179 #define CFM_OP_SET_CRYPTO_KEY 19 /* assign client crypto key for message signing */ 180 181 /* 182 * struct cfil_msg_hdr 183 * 184 * Header common to all messages 185 */ 186 struct cfil_msg_hdr { 187 uint32_t cfm_len; /* total length */ 188 uint32_t cfm_version; 189 uint32_t cfm_type; 190 uint32_t cfm_op; 191 cfil_sock_id_t cfm_sock_id; 192 }; 193 194 #define CFM_VERSION_CURRENT 1 195 196 /* 197 * Connection Direction 198 */ 199 #define CFS_CONNECTION_DIR_IN 0 200 #define CFS_CONNECTION_DIR_OUT 1 201 202 #define CFS_AUDIT_TOKEN 1 203 204 /* 205 * struct cfil_msg_sock_attached 206 * 207 * Information about a new socket being attached to the content filter 208 * 209 * Action: No reply is expected as this does not block the creation of the 210 * TCP/IP but timely action must be taken to avoid user noticeable delays. 211 * 212 * Valid Types: CFM_TYPE_EVENT 213 * 214 * Valid Op: CFM_OP_SOCKET_ATTACHED 215 */ 216 struct cfil_msg_sock_attached { 217 struct cfil_msg_hdr cfs_msghdr; 218 int cfs_sock_family; /* e.g. PF_INET */ 219 int cfs_sock_type; /* e.g. SOCK_STREAM */ 220 int cfs_sock_protocol; /* e.g. IPPROTO_TCP */ 221 int cfs_unused; /* padding */ 222 pid_t cfs_pid; 223 pid_t cfs_e_pid; 224 uuid_t cfs_uuid; 225 uuid_t cfs_e_uuid; 226 union sockaddr_in_4_6 cfs_src; 227 union sockaddr_in_4_6 cfs_dst; 228 int cfs_conn_dir; 229 unsigned int cfs_audit_token[8]; /* Must match audit_token_t */ 230 cfil_crypto_signature cfs_signature; 231 uint32_t cfs_signature_length; 232 }; 233 234 /* 235 * CFIL data flags 236 */ 237 #define CFD_DATA_FLAG_IP_HEADER 0x00000001 /* Data includes IP header */ 238 239 /* 240 * struct cfil_msg_data_event 241 * 242 * Event for the content fiter to act on a span of data 243 * A data span is described by a pair of offsets over the cumulative 244 * number of bytes sent or received on the socket. 245 * 246 * Action: The event must be acted upon but the filter may buffer 247 * data spans until it has enough content to make a decision. 248 * The action must be timely to avoid user noticeable delays. 249 * 250 * Valid Type: CFM_TYPE_EVENT 251 * 252 * Valid Ops: CFM_OP_DATA_OUT, CFM_OP_DATA_IN 253 */ 254 struct cfil_msg_data_event { 255 struct cfil_msg_hdr cfd_msghdr; 256 union sockaddr_in_4_6 cfc_src; 257 union sockaddr_in_4_6 cfc_dst; 258 uint64_t cfd_start_offset; 259 uint64_t cfd_end_offset; 260 cfil_crypto_signature cfd_signature; 261 uint32_t cfd_signature_length; 262 uint32_t cfd_flags; 263 /* Actual content data immediatly follows */ 264 }; 265 266 #define CFI_MAX_TIME_LOG_ENTRY 6 267 /* 268 * struct cfil_msg_sock_closed 269 * 270 * Information about a socket being closed to the content filter 271 * 272 * Action: No reply is expected as this does not block the closing of the 273 * TCP/IP. 274 * 275 * Valid Types: CFM_TYPE_EVENT 276 * 277 * Valid Op: CFM_OP_SOCKET_CLOSED 278 */ 279 struct cfil_msg_sock_closed { 280 struct cfil_msg_hdr cfc_msghdr; 281 struct timeval64 cfc_first_event; 282 uint32_t cfc_op_list_ctr; 283 uint32_t cfc_op_time[CFI_MAX_TIME_LOG_ENTRY]; /* time interval in microseconds since first event */ 284 unsigned char cfc_op_list[CFI_MAX_TIME_LOG_ENTRY]; 285 uint64_t cfc_byte_inbound_count; 286 uint64_t cfc_byte_outbound_count; 287 cfil_crypto_signature cfc_signature; 288 uint32_t cfc_signature_length; 289 } __attribute__((aligned(8))); 290 291 /* 292 * struct cfil_msg_stats_report 293 * 294 * Statistics report for flow(s). 295 * 296 * Action: No reply is expected. 297 * 298 * Valid Types: CFM_TYPE_EVENT 299 * 300 * Valid Op: CFM_OP_STATS 301 */ 302 struct cfil_msg_sock_stats { 303 cfil_sock_id_t cfs_sock_id; 304 uint64_t cfs_byte_inbound_count; 305 uint64_t cfs_byte_outbound_count; 306 union sockaddr_in_4_6 cfs_laddr; 307 } __attribute__((aligned(8))); 308 309 struct cfil_msg_stats_report { 310 struct cfil_msg_hdr cfr_msghdr; 311 uint32_t cfr_count; 312 struct cfil_msg_sock_stats cfr_stats[]; 313 } __attribute__((aligned(8))); 314 315 /* 316 * struct cfil_msg_action 317 * 318 * Valid Type: CFM_TYPE_ACTION 319 * 320 * Valid Ops: CFM_OP_DATA_UPDATE, CFM_OP_DROP 321 * 322 * For CFM_OP_DATA_UPDATE: 323 * 324 * cfa_in_pass_offset and cfa_out_pass_offset indicates how much data is 325 * allowed to pass. A zero value does not modify the corresponding pass offset. 326 * 327 * cfa_in_peek_offset and cfa_out_peek_offset lets the filter specify how much 328 * data it needs to make a decision: the kernel will deliver data up to that 329 * offset (if less than cfa_pass_offset it is ignored). Use CFM_MAX_OFFSET 330 * if you don't value the corresponding peek offset to be updated. 331 */ 332 struct cfil_msg_action { 333 struct cfil_msg_hdr cfa_msghdr; 334 uint64_t cfa_in_pass_offset; 335 uint64_t cfa_in_peek_offset; 336 uint64_t cfa_out_pass_offset; 337 uint64_t cfa_out_peek_offset; 338 uint32_t cfa_stats_frequency; // Statistics frequency in milliseconds 339 }; 340 341 /* 342 * struct cfil_msg_bless_client 343 * 344 * Marks a client UUID as already filtered at a higher level. 345 * 346 * Valid Type: CFM_TYPE_ACTION 347 * 348 * Valid Ops: CFM_OP_BLESS_CLIENT 349 */ 350 struct cfil_msg_bless_client { 351 struct cfil_msg_hdr cfb_msghdr; 352 uuid_t cfb_client_uuid; 353 }; 354 355 /* 356 * struct cfil_msg_set_crypto_key 357 * 358 * Filter assigning client crypto key to CFIL for message signing 359 * 360 * Valid Type: CFM_TYPE_ACTION 361 * 362 * Valid Ops: CFM_OP_SET_CRYPTO_KEY 363 */ 364 struct cfil_msg_set_crypto_key { 365 struct cfil_msg_hdr cfb_msghdr; 366 cfil_crypto_key crypto_key; 367 }; 368 369 #define CFM_MAX_OFFSET UINT64_MAX 370 371 /* 372 * Statistics retrieved via sysctl(3) 373 */ 374 struct cfil_filter_stat { 375 uint32_t cfs_len; 376 uint32_t cfs_filter_id; 377 uint32_t cfs_flags; 378 uint32_t cfs_sock_count; 379 uint32_t cfs_necp_control_unit; 380 }; 381 382 struct cfil_entry_stat { 383 uint32_t ces_len; 384 uint32_t ces_filter_id; 385 uint32_t ces_flags; 386 uint32_t ces_necp_control_unit; 387 struct timeval64 ces_last_event; 388 struct timeval64 ces_last_action; 389 struct cfe_buf_stat { 390 uint64_t cbs_pending_first; 391 uint64_t cbs_pending_last; 392 uint64_t cbs_ctl_first; 393 uint64_t cbs_ctl_last; 394 uint64_t cbs_pass_offset; 395 uint64_t cbs_peek_offset; 396 uint64_t cbs_peeked; 397 } ces_snd, ces_rcv; 398 }; 399 400 struct cfil_sock_stat { 401 uint32_t cfs_len; 402 int cfs_sock_family; 403 int cfs_sock_type; 404 int cfs_sock_protocol; 405 cfil_sock_id_t cfs_sock_id; 406 uint64_t cfs_flags; 407 pid_t cfs_pid; 408 pid_t cfs_e_pid; 409 uuid_t cfs_uuid; 410 uuid_t cfs_e_uuid; 411 struct cfi_buf_stat { 412 uint64_t cbs_pending_first; 413 uint64_t cbs_pending_last; 414 uint64_t cbs_pass_offset; 415 uint64_t cbs_inject_q_len; 416 } cfs_snd, cfs_rcv; 417 struct cfil_entry_stat ces_entries[CFIL_MAX_FILTER_COUNT]; 418 }; 419 420 /* 421 * Global statistics 422 */ 423 struct cfil_stats { 424 int32_t cfs_ctl_connect_ok; 425 int32_t cfs_ctl_connect_fail; 426 int32_t cfs_ctl_disconnect_ok; 427 int32_t cfs_ctl_disconnect_fail; 428 int32_t cfs_ctl_send_ok; 429 int32_t cfs_ctl_send_bad; 430 int32_t cfs_ctl_rcvd_ok; 431 int32_t cfs_ctl_rcvd_bad; 432 int32_t cfs_ctl_rcvd_flow_lift; 433 int32_t cfs_ctl_action_data_update; 434 int32_t cfs_ctl_action_drop; 435 int32_t cfs_ctl_action_bad_op; 436 int32_t cfs_ctl_action_bad_len; 437 438 int32_t cfs_sock_id_not_found; 439 440 int32_t cfs_cfi_alloc_ok; 441 int32_t cfs_cfi_alloc_fail; 442 443 int32_t cfs_sock_userspace_only; 444 int32_t cfs_sock_attach_in_vain; 445 int32_t cfs_sock_attach_already; 446 int32_t cfs_sock_attach_no_mem; 447 int32_t cfs_sock_attach_failed; 448 int32_t cfs_sock_attached; 449 int32_t cfs_sock_detached; 450 451 int32_t cfs_attach_event_ok; 452 int32_t cfs_attach_event_flow_control; 453 int32_t cfs_attach_event_fail; 454 455 int32_t cfs_closed_event_ok; 456 int32_t cfs_closed_event_flow_control; 457 int32_t cfs_closed_event_fail; 458 459 int32_t cfs_data_event_ok; 460 int32_t cfs_data_event_flow_control; 461 int32_t cfs_data_event_fail; 462 463 int32_t cfs_stats_event_ok; 464 int32_t cfs_stats_event_flow_control; 465 int32_t cfs_stats_event_fail; 466 467 int32_t cfs_disconnect_in_event_ok; 468 int32_t cfs_disconnect_out_event_ok; 469 int32_t cfs_disconnect_event_flow_control; 470 int32_t cfs_disconnect_event_fail; 471 472 int32_t cfs_ctl_q_not_started; 473 474 int32_t cfs_close_wait; 475 int32_t cfs_close_wait_timeout; 476 477 int32_t cfs_flush_in_drop; 478 int32_t cfs_flush_out_drop; 479 int32_t cfs_flush_in_close; 480 int32_t cfs_flush_out_close; 481 int32_t cfs_flush_in_free; 482 int32_t cfs_flush_out_free; 483 484 int32_t cfs_inject_q_nomem; 485 int32_t cfs_inject_q_nobufs; 486 int32_t cfs_inject_q_detached; 487 int32_t cfs_inject_q_in_fail; 488 int32_t cfs_inject_q_out_fail; 489 490 int32_t cfs_inject_q_in_retry; 491 int32_t cfs_inject_q_out_retry; 492 493 int32_t cfs_data_in_control; 494 int32_t cfs_data_in_oob; 495 int32_t cfs_data_out_control; 496 int32_t cfs_data_out_oob; 497 498 int64_t cfs_ctl_q_in_enqueued __attribute__((aligned(8))); 499 int64_t cfs_ctl_q_out_enqueued __attribute__((aligned(8))); 500 int64_t cfs_ctl_q_in_peeked __attribute__((aligned(8))); 501 int64_t cfs_ctl_q_out_peeked __attribute__((aligned(8))); 502 503 int64_t cfs_pending_q_in_enqueued __attribute__((aligned(8))); 504 int64_t cfs_pending_q_out_enqueued __attribute__((aligned(8))); 505 506 int64_t cfs_inject_q_in_enqueued __attribute__((aligned(8))); 507 int64_t cfs_inject_q_out_enqueued __attribute__((aligned(8))); 508 int64_t cfs_inject_q_in_passed __attribute__((aligned(8))); 509 int64_t cfs_inject_q_out_passed __attribute__((aligned(8))); 510 }; 511 #endif /* PRIVATE */ 512 513 #ifdef BSD_KERNEL_PRIVATE 514 515 #define M_SKIPCFIL M_PROTO5 516 517 #define CFIL_DGRAM_FILTERED(so) ((so->so_flags & SOF_CONTENT_FILTER) && (so->so_flow_db != NULL)) 518 519 extern int cfil_log_level; 520 521 #define CFIL_LOG(level, fmt, ...) \ 522 do { \ 523 if (cfil_log_level >= level) \ 524 printf("%s:%d " fmt "\n",\ 525 __FUNCTION__, __LINE__, ##__VA_ARGS__); \ 526 } while (0) 527 528 529 extern void cfil_init(void); 530 531 extern boolean_t cfil_filter_present(void); 532 extern boolean_t cfil_sock_connected_pending_verdict(struct socket *so); 533 extern errno_t cfil_sock_attach(struct socket *so, 534 struct sockaddr *local, struct sockaddr *remote, int dir); 535 extern errno_t cfil_sock_detach(struct socket *so); 536 537 extern int cfil_sock_data_out(struct socket *so, struct sockaddr *to, 538 struct mbuf *data, struct mbuf *control, 539 uint32_t flags, struct soflow_hash_entry *); 540 extern int cfil_sock_data_in(struct socket *so, struct sockaddr *from, 541 struct mbuf *data, struct mbuf *control, 542 uint32_t flags, struct soflow_hash_entry *); 543 544 extern int cfil_sock_shutdown(struct socket *so, int *how); 545 extern void cfil_sock_is_closed(struct socket *so); 546 extern void cfil_sock_notify_shutdown(struct socket *so, int how); 547 extern void cfil_sock_close_wait(struct socket *so); 548 549 extern boolean_t cfil_sock_data_pending(struct sockbuf *sb); 550 extern int cfil_sock_data_space(struct sockbuf *sb); 551 extern void cfil_sock_buf_update(struct sockbuf *sb); 552 553 extern cfil_sock_id_t cfil_sock_id_from_socket(struct socket *so); 554 extern cfil_sock_id_t cfil_sock_id_from_datagram_socket(struct socket *so, struct sockaddr *local, struct sockaddr *remote); 555 556 extern struct m_tag *cfil_dgram_get_socket_state(struct mbuf *m, uint32_t *state_change_cnt, 557 uint32_t *options, struct sockaddr **faddr, int *inp_flags); 558 extern boolean_t cfil_dgram_peek_socket_state(struct mbuf *m, int *inp_flags); 559 560 #endif /* BSD_KERNEL_PRIVATE */ 561 562 __END_DECLS 563 564 #endif /* __CONTENT_FILTER_H__ */ 565