]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/dev/dtrace/dtrace_subr.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / dev / dtrace / dtrace_subr.c
index ef857094da7cc6c6d055cebd90019ed33d616cdd..cdc074485896cf60d9e39fb65d2b81b7af6ac441 100644 (file)
  * Use is subject to license terms.
  */
 
-/*
- * #pragma ident       "@(#)dtrace_subr.c      1.8     07/06/05 SMI"
- */
-
 #include <stdarg.h>
 #include <string.h>
 #include <sys/malloc.h>
 #if CONFIG_CSR
 #include <sys/codesign.h>
 #include <sys/csr.h>
+
+#if defined(KERNEL_INTEGRITY_KTRR) || defined(KERNEL_INTEGRITY_CTRR)
+extern bool csr_unsafe_kernel_text;
+#endif
 #endif
 
 /*
@@ -108,7 +108,7 @@ dtrace_fasttrap_fork(proc_t *p, proc_t *cp)
  * duty to resume the task.
  */
 
-lck_mtx_t dtrace_procwaitfor_lock;
+LCK_MTX_DECLARE_ATTR(dtrace_procwaitfor_lock, &dtrace_lck_grp, &dtrace_lck_attr);
 
 typedef struct dtrace_proc_awaited_entry {
        struct dtrace_procdesc                  *pdesc;
@@ -207,7 +207,7 @@ dtrace_proc_waitfor(dtrace_procdesc_t* pdesc) {
         * Never trust user input, compute the length of the process name and ensure the
         * string is null terminated.
         */
-       pdesc->p_name_length = strnlen(pdesc->p_name, sizeof(pdesc->p_name));
+       pdesc->p_name_length = (int) strnlen(pdesc->p_name, sizeof(pdesc->p_name));
        if (pdesc->p_name_length >= (int) sizeof(pdesc->p_name))
                return -1;
 
@@ -295,6 +295,124 @@ dtrace_invop_remove(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
        kmem_free(hdlr, sizeof (dtrace_invop_hdlr_t));
 }
 
+void*
+dtrace_ptrauth_strip(void *ptr, uint64_t key)
+{
+#pragma unused(key)
+#if __has_feature(ptrauth_calls)
+       /*
+        * The key argument to ptrauth_strip needs to be a compile-time
+        * constant
+        */
+       switch (key) {
+       case ptrauth_key_asia:
+               return ptrauth_strip(ptr, ptrauth_key_asia);
+       case ptrauth_key_asib:
+               return ptrauth_strip(ptr, ptrauth_key_asib);
+       case ptrauth_key_asda:
+               return ptrauth_strip(ptr, ptrauth_key_asda);
+       case ptrauth_key_asdb:
+               return ptrauth_strip(ptr, ptrauth_key_asdb);
+       default:
+               return ptr;
+       }
+#else
+       return ptr;
+#endif // __has_feature(ptrauth_calls)
+}
+
+int
+dtrace_is_valid_ptrauth_key(uint64_t key)
+{
+#pragma unused(key)
+#if __has_feature(ptrauth_calls)
+       return (key == ptrauth_key_asia) || (key == ptrauth_key_asib) ||
+           (key == ptrauth_key_asda) || (key == ptrauth_key_asdb);
+#else
+       return (0);
+#endif /* __has_feature(ptrauth_calls) */
+}
+
+uint64_t
+dtrace_physmem_read(uint64_t addr, size_t size)
+{
+       switch (size) {
+       case 1:
+               return (uint64_t)ml_phys_read_byte_64((addr64_t)addr);
+       case 2:
+               return (uint64_t)ml_phys_read_half_64((addr64_t)addr);
+       case 4:
+               return (uint64_t)ml_phys_read_64((addr64_t)addr);
+       case 8:
+               return (uint64_t)ml_phys_read_double_64((addr64_t)addr);
+       }
+       DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
+
+       return (0);
+}
+
+void
+dtrace_physmem_write(uint64_t addr, uint64_t data, size_t size)
+{
+       switch (size) {
+       case 1:
+               ml_phys_write_byte_64((addr64_t)addr, (unsigned int)data);
+               break;
+       case 2:
+               ml_phys_write_half_64((addr64_t)addr, (unsigned int)data);
+               break;
+       case 4:
+               ml_phys_write_64((addr64_t)addr, (unsigned int)data);
+               break;
+       case 8:
+               ml_phys_write_double_64((addr64_t)addr, (unsigned long long)data);
+               break;
+       default:
+               DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
+       }
+}
+
+static minor_t next_minor = 0;
+static dtrace_state_t* dtrace_clients[DTRACE_NCLIENTS] = {NULL};
+
+
+minor_t
+dtrace_state_reserve(void)
+{
+       for (int i = 0; i < DTRACE_NCLIENTS; i++) {
+               minor_t minor = os_atomic_inc_orig(&next_minor, relaxed) % DTRACE_NCLIENTS;
+               if (dtrace_clients[minor] == NULL)
+                       return minor;
+       }
+       return 0;
+}
+
+dtrace_state_t*
+dtrace_state_get(minor_t minor)
+{
+       ASSERT(minor < DTRACE_NCLIENTS);
+       return dtrace_clients[minor];
+}
+
+dtrace_state_t*
+dtrace_state_allocate(minor_t minor)
+{
+       dtrace_state_t *state = _MALLOC(sizeof(dtrace_state_t), M_TEMP, M_ZERO | M_WAITOK);
+       if (dtrace_casptr(&dtrace_clients[minor], NULL, state) != NULL) {
+               // We have been raced by another client for this number, abort
+               _FREE(state, M_TEMP);
+               return NULL;
+       }
+       return state;
+}
+
+void
+dtrace_state_free(minor_t minor)
+{
+       dtrace_state_t *state = dtrace_clients[minor];
+       dtrace_clients[minor] = NULL;
+       _FREE(state, M_TEMP);
+}
 
 
 
@@ -317,11 +435,8 @@ dtrace_is_restricted(void)
        return FALSE;
 }
 
-/*
- * Check if DTrace is running on a machine currently configured for Apple Internal development
- */
 boolean_t
-dtrace_is_running_apple_internal(void)
+dtrace_are_restrictions_relaxed(void)
 {
 #if CONFIG_CSR
        if (csr_check(CSR_ALLOW_APPLE_INTERNAL) == 0)
@@ -336,13 +451,20 @@ dtrace_fbt_probes_restricted(void)
 {
 
 #if CONFIG_CSR
-       if (dtrace_is_restricted() && !dtrace_is_running_apple_internal())
+       if (dtrace_is_restricted() && !dtrace_are_restrictions_relaxed())
                return TRUE;
 #endif
 
        return FALSE;
 }
 
+boolean_t
+dtrace_sdt_probes_restricted(void)
+{
+
+       return FALSE;
+}
+
 /*
  * Check if the process can be attached.
  */