Pointers.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /* Test Pointer Auttributes */
  32. #include <sys/types.h>
  33. #include <string.h>
  34. #include "sgx_trts.h"
  35. #include "sgx_lfence.h"
  36. #include "../Enclave.h"
  37. #include "Enclave_t.h"
  38. /* checksum_internal:
  39. * get simple checksum of input buffer and length
  40. */
  41. int32_t checksum_internal(char *buf, size_t count)
  42. {
  43. register int32_t sum = 0;
  44. int16_t *ptr = (int16_t *)buf;
  45. /* Main summing loop */
  46. while(count > 1) {
  47. sum = sum + *ptr++;
  48. count = count - 2;
  49. }
  50. /* Add left-over byte, if any */
  51. if (count > 0)
  52. sum = sum + *((char *)ptr);
  53. return ~sum;
  54. }
  55. /* ecall_pointer_user_check, ecall_pointer_in, ecall_pointer_out, ecall_pointer_in_out:
  56. * The root ECALLs to test [in], [out], [user_check] attributes.
  57. */
  58. size_t ecall_pointer_user_check(void *val, size_t sz)
  59. {
  60. /* check if the buffer is allocated outside */
  61. if (sgx_is_outside_enclave(val, sz) != 1)
  62. abort();
  63. /*fence after sgx_is_outside_enclave check*/
  64. sgx_lfence();
  65. char tmp[100] = {0};
  66. size_t len = sz>100?100:sz;
  67. /* copy the memory into the enclave to make sure 'val'
  68. * is not being changed in checksum_internal() */
  69. memcpy(tmp, val, len);
  70. int32_t sum = checksum_internal((char *)tmp, len);
  71. printf("Checksum(0x%p, %zu) = 0x%x\n",
  72. val, len, (unsigned int)sum);
  73. /* modify outside memory directly */
  74. memcpy(val, "SGX_SUCCESS", len>12?12:len);
  75. return len;
  76. }
  77. /* ecall_pointer_in:
  78. * the buffer of val is copied to the enclave.
  79. */
  80. void ecall_pointer_in(int *val)
  81. {
  82. if (sgx_is_within_enclave(val, sizeof(int)) != 1)
  83. abort();
  84. *val = 1234;
  85. }
  86. /* ecall_pointer_out:
  87. * the buffer of val is copied to the untrusted side.
  88. */
  89. void ecall_pointer_out(int *val)
  90. {
  91. if (sgx_is_within_enclave(val, sizeof(int)) != 1)
  92. abort();
  93. assert(*val == 0);
  94. *val = 1234;
  95. }
  96. /* ecall_pointer_in_out:
  97. * the buffer of val is double-copied.
  98. */
  99. void ecall_pointer_in_out(int *val)
  100. {
  101. if (sgx_is_within_enclave(val, sizeof(int)) != 1)
  102. abort();
  103. *val = 1234;
  104. }
  105. /* ocall_pointer_attr:
  106. * The root ECALL that test OCALL [in], [out], [user_check].
  107. */
  108. void ocall_pointer_attr(void)
  109. {
  110. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  111. int val = 0;
  112. ret = ocall_pointer_user_check(&val);
  113. if (ret != SGX_SUCCESS)
  114. abort();
  115. val = 0;
  116. ret = ocall_pointer_in(&val);
  117. if (ret != SGX_SUCCESS)
  118. abort();
  119. assert(val == 0);
  120. val = 0;
  121. ret = ocall_pointer_out(&val);
  122. if (ret != SGX_SUCCESS)
  123. abort();
  124. assert(val == 1234);
  125. val = 0;
  126. ret = ocall_pointer_in_out(&val);
  127. if (ret != SGX_SUCCESS)
  128. abort();
  129. assert(val == 1234);
  130. return;
  131. }
  132. /* ecall_pointer_string:
  133. * [string] defines a string.
  134. */
  135. void ecall_pointer_string(char *str)
  136. {
  137. strncpy(str, "0987654321", strlen(str));
  138. }
  139. /* ecall_pointer_string_const:
  140. * const [string] defines a string that cannot be modified.
  141. */
  142. void ecall_pointer_string_const(const char *str)
  143. {
  144. char* temp = new char[strlen(str)];
  145. strncpy(temp, str, strlen(str));
  146. delete []temp;
  147. }
  148. /* ecall_pointer_size:
  149. * 'len' needs to be specified to tell Edger8r the length of 'str'.
  150. */
  151. void ecall_pointer_size(void *ptr, size_t len)
  152. {
  153. strncpy((char*)ptr, "0987654321", len);
  154. }
  155. /* ecall_pointer_count:
  156. * 'cnt' needs to be specified to tell Edger8r the number of elements in 'arr'.
  157. */
  158. void ecall_pointer_count(int *arr, int cnt)
  159. {
  160. for (int i = (cnt - 1); i >= 0; i--)
  161. arr[i] = (cnt - 1 - i);
  162. }
  163. /* ecall_pointer_isptr_readonly:
  164. * 'buf' is user defined type, shall be tagged with [isptr].
  165. * if it's not writable, [readonly] shall be specified.
  166. */
  167. void ecall_pointer_isptr_readonly(buffer_t buf, size_t len)
  168. {
  169. strncpy((char*)buf, "0987654321", len);
  170. }