sgx_create_report.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. /**
  32. * File: sgx_create_report.cpp
  33. * Description:
  34. * Wrapper for EREPORT instruction
  35. */
  36. #include "sgx_utils.h"
  37. #include "util.h"
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "se_memcpy.h"
  41. #include "sgx_trts.h"
  42. #include "trts_inst.h"
  43. #include "se_cdefs.h"
  44. // add a version to tservice.
  45. SGX_ACCESS_VERSION(tservice, 1)
  46. sgx_status_t sgx_create_report(const sgx_target_info_t *target_info, const sgx_report_data_t *report_data, sgx_report_t *report)
  47. {
  48. int i = 0;
  49. // check parameters
  50. //
  51. // target_info is allowed to be NULL, but if it is not NULL, it must be within the enclave
  52. if(target_info)
  53. {
  54. if (!sgx_is_within_enclave(target_info, sizeof(*target_info)))
  55. return SGX_ERROR_INVALID_PARAMETER;
  56. for(i=0; i<SGX_TARGET_INFO_RESERVED1_BYTES; ++i)
  57. {
  58. if (target_info->reserved1[i] != 0)
  59. return SGX_ERROR_INVALID_PARAMETER;
  60. }
  61. for(i=0; i<SGX_TARGET_INFO_RESERVED2_BYTES; ++i)
  62. {
  63. if (target_info->reserved2[i] != 0)
  64. return SGX_ERROR_INVALID_PARAMETER;
  65. }
  66. }
  67. // report_data is allowed to be NULL, but if it is not NULL, it must be within the enclave
  68. if(report_data && !sgx_is_within_enclave(report_data, sizeof(*report_data)))
  69. {
  70. return SGX_ERROR_INVALID_PARAMETER;
  71. }
  72. // report must be within the enclave
  73. if(!report || !sgx_is_within_enclave(report, sizeof(*report)))
  74. {
  75. return SGX_ERROR_INVALID_PARAMETER;
  76. }
  77. // allocate memory
  78. //
  79. // To minimize the effort of memory management, the three elements allocation
  80. // are combined in a single malloc. The calculation for the required size has
  81. // an assumption, that
  82. // the elements should be allocated in descending order of the alignment size.
  83. //
  84. // If the alignment requirements are changed, the allocation order needs to
  85. // change accordingly.
  86. //
  87. // Current allocation order is:
  88. // report -> target_info -> report_data
  89. //
  90. // target_info: 512-byte aligned, 512-byte length
  91. // report_data: 128-byte aligned, 64-byte length
  92. // report: 512-byte aligned, 432-byte length
  93. //
  94. size_t size = ROUND_TO(sizeof(sgx_target_info_t), TARGET_INFO_ALIGN_SIZE) +
  95. ROUND_TO(sizeof(sgx_report_data_t), REPORT_DATA_ALIGN_SIZE) +
  96. ROUND_TO(sizeof(sgx_report_t), REPORT_ALIGN_SIZE);
  97. size += MAX(MAX(TARGET_INFO_ALIGN_SIZE, REPORT_DATA_ALIGN_SIZE), REPORT_ALIGN_SIZE) - 1;
  98. void *buffer = malloc(size);
  99. if(buffer == NULL)
  100. {
  101. return SGX_ERROR_OUT_OF_MEMORY;
  102. }
  103. memset(buffer, 0, size);
  104. size_t buf_ptr = reinterpret_cast<size_t>(buffer);
  105. buf_ptr = ROUND_TO(buf_ptr, REPORT_ALIGN_SIZE);
  106. sgx_report_t *tmp_report = reinterpret_cast<sgx_report_t *>(buf_ptr);
  107. buf_ptr += sizeof(*tmp_report);
  108. buf_ptr = ROUND_TO(buf_ptr, TARGET_INFO_ALIGN_SIZE);
  109. sgx_target_info_t *tmp_target_info = reinterpret_cast<sgx_target_info_t *>(buf_ptr);
  110. buf_ptr += sizeof(*tmp_target_info);
  111. buf_ptr = ROUND_TO(buf_ptr, REPORT_DATA_ALIGN_SIZE);
  112. sgx_report_data_t *tmp_report_data = reinterpret_cast<sgx_report_data_t *>(buf_ptr);
  113. // Copy data from user buffer to the aligned memory
  114. if(target_info)
  115. {
  116. memcpy_s(tmp_target_info, sizeof(*tmp_target_info), target_info, sizeof(*target_info));
  117. }
  118. if(report_data)
  119. {
  120. memcpy_s(tmp_report_data, sizeof(*tmp_report_data), report_data, sizeof(*report_data));
  121. }
  122. // Do EREPORT
  123. do_ereport(tmp_target_info, tmp_report_data, tmp_report);
  124. // Copy data to the user buffer
  125. memcpy_s(report, sizeof(*report), tmp_report, sizeof(*tmp_report));
  126. // cleanup
  127. memset_s(buffer, size, 0, size);
  128. free(buffer);
  129. return SGX_SUCCESS;
  130. }