]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/ipc/ipc_voucher.c
xnu-3248.60.10.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_voucher.c
index 47e9bdf3a80bd651e04a7190eab7e64897bca9de..f914dec6f0e90339a9e2c8b0b377b4b05369c273 100644 (file)
@@ -183,6 +183,14 @@ static void ivgt_lookup(iv_index_t,
                        ipc_voucher_attr_manager_t *,
                        ipc_voucher_attr_control_t *);
 
                        ipc_voucher_attr_manager_t *,
                        ipc_voucher_attr_control_t *);
 
+static kern_return_t
+ipc_voucher_prepare_processing_recipe(
+       ipc_voucher_t voucher,
+       ipc_voucher_attr_raw_recipe_array_t recipes,
+       ipc_voucher_attr_raw_recipe_array_size_t *in_out_size,
+       mach_voucher_attr_recipe_command_t command,
+       ipc_voucher_attr_manager_flags flags,
+       int *need_processing);
 
 #if defined(MACH_VOUCHER_ATTR_KEY_USER_DATA) || defined(MACH_VOUCHER_ATTR_KEY_TEST)
 void user_data_attr_manager_init(void);
 
 #if defined(MACH_VOUCHER_ATTR_KEY_USER_DATA) || defined(MACH_VOUCHER_ATTR_KEY_TEST)
 void user_data_attr_manager_init(void);
@@ -535,6 +543,7 @@ convert_voucher_to_port(ipc_voucher_t voucher)
 #define ivace_reset_data(ivace_elem, next_index) {       \
        (ivace_elem)->ivace_value = 0xDEADC0DEDEADC0DE;  \
        (ivace_elem)->ivace_refs = 0;                    \
 #define ivace_reset_data(ivace_elem, next_index) {       \
        (ivace_elem)->ivace_value = 0xDEADC0DEDEADC0DE;  \
        (ivace_elem)->ivace_refs = 0;                    \
+       (ivace_elem)->ivace_persist = 0;                 \
        (ivace_elem)->ivace_made = 0;                    \
        (ivace_elem)->ivace_free = TRUE;                 \
        (ivace_elem)->ivace_releasing = FALSE;           \
        (ivace_elem)->ivace_made = 0;                    \
        (ivace_elem)->ivace_free = TRUE;                 \
        (ivace_elem)->ivace_releasing = FALSE;           \
@@ -546,6 +555,7 @@ convert_voucher_to_port(ipc_voucher_t voucher)
 #define ivace_copy_data(ivace_src_elem, ivace_dst_elem) {  \
        (ivace_dst_elem)->ivace_value = (ivace_src_elem)->ivace_value; \
        (ivace_dst_elem)->ivace_refs = (ivace_src_elem)->ivace_refs;   \
 #define ivace_copy_data(ivace_src_elem, ivace_dst_elem) {  \
        (ivace_dst_elem)->ivace_value = (ivace_src_elem)->ivace_value; \
        (ivace_dst_elem)->ivace_refs = (ivace_src_elem)->ivace_refs;   \
+       (ivace_dst_elem)->ivace_persist = (ivace_src_elem)->ivace_persist; \
        (ivace_dst_elem)->ivace_made = (ivace_src_elem)->ivace_made;   \
        (ivace_dst_elem)->ivace_free = (ivace_src_elem)->ivace_free;   \
        (ivace_dst_elem)->ivace_layered = (ivace_src_elem)->ivace_layered;   \
        (ivace_dst_elem)->ivace_made = (ivace_src_elem)->ivace_made;   \
        (ivace_dst_elem)->ivace_free = (ivace_src_elem)->ivace_free;   \
        (ivace_dst_elem)->ivace_layered = (ivace_src_elem)->ivace_layered;   \
@@ -898,7 +908,11 @@ ivace_reference_by_index(
        assert(0xdeadc0dedeadc0de != ivace->ivace_value);
        assert(0 < ivace->ivace_refs);
        assert(!ivace->ivace_free);
        assert(0xdeadc0dedeadc0de != ivace->ivace_value);
        assert(0 < ivace->ivace_refs);
        assert(!ivace->ivace_free);
-       ivace->ivace_refs++;
+
+       /* Take ref only on non-persistent values */
+       if (!ivace->ivace_persist) {
+               ivace->ivace_refs++;
+       }
        ivac_unlock(ivac);
 }
 
        ivac_unlock(ivac);
 }
 
@@ -914,7 +928,8 @@ ivace_reference_by_index(
 static iv_index_t
 ivace_reference_by_value(
        ipc_voucher_attr_control_t      ivac,
 static iv_index_t
 ivace_reference_by_value(
        ipc_voucher_attr_control_t      ivac,
-       mach_voucher_attr_value_handle_t        value)
+       mach_voucher_attr_value_handle_t        value,
+       mach_voucher_attr_value_flags_t          flag)
 {
        ivac_entry_t ivace = IVACE_NULL;
        iv_index_t hash_index;
 {
        ivac_entry_t ivace = IVACE_NULL;
        iv_index_t hash_index;
@@ -942,8 +957,8 @@ restart:
 
        /* found it? */
        if (index != IV_HASH_END) { 
 
        /* found it? */
        if (index != IV_HASH_END) { 
-               /* only add reference on non-default value */
-               if (IV_UNUSED_VALINDEX != index) {
+               /* only add reference on non-persistent value */
+               if (!ivace->ivace_persist) {
                        ivace->ivace_refs++;
                        ivace->ivace_made++;
                }
                        ivace->ivace_refs++;
                        ivace->ivace_made++;
                }
@@ -970,6 +985,7 @@ restart:
        ivace->ivace_refs = 1;
        ivace->ivace_made = 1;
        ivace->ivace_free = FALSE;
        ivace->ivace_refs = 1;
        ivace->ivace_made = 1;
        ivace->ivace_free = FALSE;
+       ivace->ivace_persist = (flag & MACH_VOUCHER_ATTR_VALUE_FLAGS_PERSIST) ? TRUE : FALSE;
 
        /* insert the new entry in the proper hash chain */
        ivace->ivace_next = ivac->ivac_table[hash_index].ivace_index;
 
        /* insert the new entry in the proper hash chain */
        ivace->ivace_next = ivac->ivac_table[hash_index].ivace_index;
@@ -1015,6 +1031,12 @@ static void ivace_release(
 
        assert(0 < ivace->ivace_refs);
 
 
        assert(0 < ivace->ivace_refs);
 
+       /* cant release persistent values */
+       if (ivace->ivace_persist) {
+               ivac_unlock(ivac);
+               return;
+       }
+
        if (0 < --ivace->ivace_refs) {
                ivac_unlock(ivac);
                return;
        if (0 < --ivace->ivace_refs) {
                ivac_unlock(ivac);
                return;
@@ -1187,6 +1209,7 @@ ipc_replace_voucher_value(
        mach_voucher_attr_value_handle_t previous_vals[MACH_VOUCHER_ATTR_VALUE_MAX_NESTED];
        mach_voucher_attr_value_handle_array_size_t previous_vals_count;
        mach_voucher_attr_value_handle_t new_value;
        mach_voucher_attr_value_handle_t previous_vals[MACH_VOUCHER_ATTR_VALUE_MAX_NESTED];
        mach_voucher_attr_value_handle_array_size_t previous_vals_count;
        mach_voucher_attr_value_handle_t new_value;
+       mach_voucher_attr_value_flags_t new_flag;
        ipc_voucher_t new_value_voucher;
        ipc_voucher_attr_manager_t ivam;
        ipc_voucher_attr_control_t ivac;
        ipc_voucher_t new_value_voucher;
        ipc_voucher_attr_manager_t ivam;
        ipc_voucher_attr_control_t ivac;
@@ -1226,7 +1249,7 @@ ipc_replace_voucher_value(
                                    ivam, key, command,
                                    previous_vals, previous_vals_count,
                                    content, content_size,
                                    ivam, key, command,
                                    previous_vals, previous_vals_count,
                                    content, content_size,
-                                   &new_value, &new_value_voucher);
+                                   &new_value, &new_flag, &new_value_voucher);
        if (KERN_SUCCESS != kr) {
                ivac_release(ivac);
                return kr;
        if (KERN_SUCCESS != kr) {
                ivac_release(ivac);
                return kr;
@@ -1242,7 +1265,7 @@ ipc_replace_voucher_value(
         * is transferred to a new value, or consumed if
         * we find a matching existing value.
         */
         * is transferred to a new value, or consumed if
         * we find a matching existing value.
         */
-       val_index = ivace_reference_by_value(ivac, new_value);
+       val_index = ivace_reference_by_value(ivac, new_value, new_flag);
        iv_set(voucher, key_index, val_index);
 
        /*
        iv_set(voucher, key_index, val_index);
 
        /*
@@ -1296,7 +1319,8 @@ ipc_directly_replace_voucher_value(
         * is transferred to a new value, or consumed if
         * we find a matching existing value.
         */
         * is transferred to a new value, or consumed if
         * we find a matching existing value.
         */
-       val_index = ivace_reference_by_value(ivac, new_value);
+       val_index = ivace_reference_by_value(ivac, new_value,
+                               MACH_VOUCHER_ATTR_VALUE_FLAGS_NONE);
        iv_set(voucher, key_index, val_index);
 
        /*
        iv_set(voucher, key_index, val_index);
 
        /*
@@ -1909,6 +1933,7 @@ ipc_register_well_known_mach_voucher_attr_manager(
        new_control->ivac_table[IV_UNUSED_VALINDEX].ivace_value = default_value;
        new_control->ivac_table[IV_UNUSED_VALINDEX].ivace_refs = IVACE_REFS_MAX;
        new_control->ivac_table[IV_UNUSED_VALINDEX].ivace_made = IVACE_REFS_MAX;
        new_control->ivac_table[IV_UNUSED_VALINDEX].ivace_value = default_value;
        new_control->ivac_table[IV_UNUSED_VALINDEX].ivace_refs = IVACE_REFS_MAX;
        new_control->ivac_table[IV_UNUSED_VALINDEX].ivace_made = IVACE_REFS_MAX;
+       new_control->ivac_table[IV_UNUSED_VALINDEX].ivace_persist = TRUE;
        assert(IV_HASH_END == new_control->ivac_table[IV_UNUSED_VALINDEX].ivace_next);
 
        ivgt_lock();
        assert(IV_HASH_END == new_control->ivac_table[IV_UNUSED_VALINDEX].ivace_next);
 
        ivgt_lock();
@@ -2333,7 +2358,6 @@ mach_voucher_attr_control_get_values(
        return KERN_SUCCESS;
 }
 
        return KERN_SUCCESS;
 }
 
-
 /*
  *     Routine:        mach_voucher_attr_control_create_mach_voucher
  *     Purpose:
 /*
  *     Routine:        mach_voucher_attr_control_create_mach_voucher
  *     Purpose:
@@ -2584,6 +2608,183 @@ host_register_mach_voucher_attr_manager(
        return KERN_NOT_SUPPORTED;
 }
 
        return KERN_NOT_SUPPORTED;
 }
 
+/*
+ *     Routine:        ipc_voucher_send_preprocessing
+ *     Purpose:
+ *             Processing of the voucher in the kmsg before sending it.
+ *             Currently use to switch PERSONA_TOKEN in case of process with
+ *             no com.apple.private.personas.propagate entitlement.
+ */
+void
+ipc_voucher_send_preprocessing(ipc_kmsg_t kmsg)
+{
+       uint8_t recipes[(MACH_VOUCHER_ATTR_KEY_NUM_WELL_KNOWN + 1) * sizeof(ipc_voucher_attr_recipe_data_t)];
+       ipc_voucher_attr_raw_recipe_array_size_t recipe_size = (MACH_VOUCHER_ATTR_KEY_NUM_WELL_KNOWN + 1) * 
+                                               sizeof(ipc_voucher_attr_recipe_data_t);
+       ipc_voucher_t pre_processed_voucher;
+       ipc_voucher_t voucher_to_send;
+       kern_return_t kr;
+       int need_preprocessing = FALSE;
+
+       if (!IP_VALID(kmsg->ikm_voucher) || current_task() == kernel_task) {
+               return;
+       }
+
+       /* setup recipe for preprocessing of all the attributes. */
+       pre_processed_voucher = (ipc_voucher_t)kmsg->ikm_voucher->ip_kobject;
+
+       kr = ipc_voucher_prepare_processing_recipe(pre_processed_voucher,
+               (mach_voucher_attr_raw_recipe_array_t)recipes,
+               &recipe_size, MACH_VOUCHER_ATTR_SEND_PREPROCESS,
+               IVAM_FLAGS_SUPPORT_SEND_PREPROCESS, &need_preprocessing);
+
+       assert(KERN_SUCCESS == kr);
+       /*
+        * Only do send preprocessing if the voucher needs any pre processing.
+        */
+       if (need_preprocessing) {
+               kr = ipc_create_mach_voucher(recipes,
+                                            recipe_size,
+                                            &voucher_to_send);
+               assert(KERN_SUCCESS == kr);
+               ipc_port_release_send(kmsg->ikm_voucher);
+               kmsg->ikm_voucher = convert_voucher_to_port(voucher_to_send);
+       }
+}
+
+/*
+ *     Routine:        ipc_voucher_receive_postprocessing
+ *     Purpose:
+ *             Redeems the voucher attached to the kmsg.
+ *     Note:
+ *             Although it is possible to call ipc_importance_receive
+ *             here, it is called in mach_msg_receive_results and not here
+ *             in order to maintain symmetry with ipc_voucher_send_preprocessing.
+ */
+void
+ipc_voucher_receive_postprocessing(
+       ipc_kmsg_t              kmsg,
+       mach_msg_option_t       option)
+{
+       uint8_t recipes[(MACH_VOUCHER_ATTR_KEY_NUM_WELL_KNOWN + 1) * sizeof(ipc_voucher_attr_recipe_data_t)];
+       ipc_voucher_attr_raw_recipe_array_size_t recipe_size = (MACH_VOUCHER_ATTR_KEY_NUM_WELL_KNOWN + 1) * 
+                                               sizeof(ipc_voucher_attr_recipe_data_t);
+       ipc_voucher_t recv_voucher;
+       ipc_voucher_t sent_voucher;
+       kern_return_t kr;
+       int need_postprocessing = FALSE;
+
+       if ((option & MACH_RCV_VOUCHER) == 0 || (!IP_VALID(kmsg->ikm_voucher)) ||
+            current_task() == kernel_task) {
+               return;
+       }
+
+       /* setup recipe for auto redeem of all the attributes. */
+       sent_voucher = (ipc_voucher_t)kmsg->ikm_voucher->ip_kobject;
+
+       kr = ipc_voucher_prepare_processing_recipe(sent_voucher,
+               (mach_voucher_attr_raw_recipe_array_t)recipes,
+               &recipe_size, MACH_VOUCHER_ATTR_AUTO_REDEEM,
+               IVAM_FLAGS_SUPPORT_RECEIVE_POSTPROCESS, &need_postprocessing);
+
+       assert(KERN_SUCCESS == kr);
+
+       /*
+        * Only do receive postprocessing if the voucher needs any post processing.
+        */
+       if (need_postprocessing) {
+               kr = ipc_create_mach_voucher(recipes,
+                                            recipe_size,
+                                            &recv_voucher);
+               assert(KERN_SUCCESS == kr);
+               /* swap the voucher port (and set voucher bits in case it didn't already exist) */
+               kmsg->ikm_header->msgh_bits |= (MACH_MSG_TYPE_MOVE_SEND << 16);
+               ipc_port_release_send(kmsg->ikm_voucher);
+               kmsg->ikm_voucher = convert_voucher_to_port(recv_voucher);
+       }
+}
+
+/*
+ *     Routine:        ipc_voucher_prepare_processing_recipe
+ *     Purpose:
+ *             Check if the given voucher has an attribute which supports
+ *             the given flag and prepare a recipe to apply that supported
+ *             command.
+ */
+static kern_return_t
+ipc_voucher_prepare_processing_recipe(
+       ipc_voucher_t voucher,
+       ipc_voucher_attr_raw_recipe_array_t recipes,
+       ipc_voucher_attr_raw_recipe_array_size_t *in_out_size,
+       mach_voucher_attr_recipe_command_t command,
+       ipc_voucher_attr_manager_flags flags,
+       int *need_processing)
+{
+       ipc_voucher_attr_raw_recipe_array_size_t recipe_size = *in_out_size;
+       ipc_voucher_attr_raw_recipe_array_size_t recipe_used = 0;
+       iv_index_t key_index;
+       ipc_voucher_attr_recipe_t recipe;
+       
+       if (IV_NULL == voucher)
+               return KERN_INVALID_ARGUMENT;
+       
+       /* Setup a recipe to copy all attributes. */
+       if (recipe_size < sizeof(*recipe))
+                       return KERN_NO_SPACE;
+
+       *need_processing = FALSE;
+       recipe = (ipc_voucher_attr_recipe_t)(void *)&recipes[recipe_used];
+       recipe->key = MACH_VOUCHER_ATTR_KEY_ALL;
+       recipe->command = MACH_VOUCHER_ATTR_COPY;
+       recipe->previous_voucher = voucher;
+       recipe->content_size = 0;
+       recipe_used += sizeof(*recipe) + recipe->content_size;
+
+       for (key_index = 0; key_index < voucher->iv_table_size; key_index++) {
+               ipc_voucher_attr_manager_t manager;
+               mach_voucher_attr_key_t key;
+               iv_index_t value_index;
+
+               /* don't output anything for a default value */
+               value_index = iv_lookup(voucher, key_index);
+               if (IV_UNUSED_VALINDEX == value_index)
+                       continue;
+
+               if (recipe_size - recipe_used < sizeof(*recipe))
+                       return KERN_NO_SPACE;
+
+               recipe = (ipc_voucher_attr_recipe_t)(void *)&recipes[recipe_used];
+               
+               /*
+                * Get the manager for this key_index. The
+                * existence of a non-default value for this
+                * slot within our voucher will keep the
+                * manager referenced during the callout.
+                */
+               ivgt_lookup(key_index, FALSE, &manager, NULL);
+               assert(IVAM_NULL != manager);
+               if (IVAM_NULL == manager) {
+                       continue;
+               }
+
+               /* Check if the supported flag is set in the manager */
+               if ((manager->ivam_flags & flags) == 0)
+                       continue;
+               
+               key = iv_index_to_key(key_index);
+
+               recipe->key = key;
+               recipe->command = command;
+               recipe->content_size = 0;
+               recipe->previous_voucher = voucher;
+
+               recipe_used += sizeof(*recipe) + recipe->content_size;
+               *need_processing = TRUE;
+       }
+
+       *in_out_size = recipe_used;
+       return KERN_SUCCESS;
+}
 
 #if defined(MACH_VOUCHER_ATTR_KEY_USER_DATA) || defined(MACH_VOUCHER_ATTR_KEY_TEST)
 
 
 #if defined(MACH_VOUCHER_ATTR_KEY_USER_DATA) || defined(MACH_VOUCHER_ATTR_KEY_TEST)
 
@@ -2640,6 +2841,7 @@ user_data_get_value(
        mach_voucher_attr_content_t                     content,
        mach_voucher_attr_content_size_t                content_size,
        mach_voucher_attr_value_handle_t                *out_value,
        mach_voucher_attr_content_t                     content,
        mach_voucher_attr_content_size_t                content_size,
        mach_voucher_attr_value_handle_t                *out_value,
+       mach_voucher_attr_value_flags_t                 *out_flags,
        ipc_voucher_t                                   *out_value_voucher);
 
 static kern_return_t
        ipc_voucher_t                                   *out_value_voucher);
 
 static kern_return_t
@@ -2674,6 +2876,7 @@ struct ipc_voucher_attr_manager user_data_manager = {
        .ivam_extract_content = user_data_extract_content,
        .ivam_command =         user_data_command,
        .ivam_release =         user_data_release,
        .ivam_extract_content = user_data_extract_content,
        .ivam_command =         user_data_command,
        .ivam_release =         user_data_release,
+       .ivam_flags =           IVAM_FLAGS_NONE,
 };
 
 ipc_voucher_attr_control_t user_data_control;
 };
 
 ipc_voucher_attr_control_t user_data_control;
@@ -2829,6 +3032,7 @@ user_data_get_value(
        mach_voucher_attr_content_t                     content,
        mach_voucher_attr_content_size_t                content_size,
        mach_voucher_attr_value_handle_t                *out_value,
        mach_voucher_attr_content_t                     content,
        mach_voucher_attr_content_size_t                content_size,
        mach_voucher_attr_value_handle_t                *out_value,
+       mach_voucher_attr_value_flags_t                 *out_flags,
        ipc_voucher_t                                   *out_value_voucher)
 {
        user_data_element_t elem;
        ipc_voucher_t                                   *out_value_voucher)
 {
        user_data_element_t elem;
@@ -2838,6 +3042,7 @@ user_data_get_value(
 
        /* never an out voucher */
        *out_value_voucher = IPC_VOUCHER_NULL;
 
        /* never an out voucher */
        *out_value_voucher = IPC_VOUCHER_NULL;
+       *out_flags = MACH_VOUCHER_ATTR_VALUE_FLAGS_NONE;
 
        switch (command) {
 
 
        switch (command) {