pal_linux.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #ifndef PAL_LINUX_H
  16. #define PAL_LINUX_H
  17. #include "pal_defs.h"
  18. #include "pal_linux_defs.h"
  19. #include "pal.h"
  20. #include "api.h"
  21. #include "linux_types.h"
  22. #include "sgx_arch.h"
  23. #include "sgx_tls.h"
  24. #include "sgx_api.h"
  25. #include "enclave_ocalls.h"
  26. extern struct pal_linux_state {
  27. PAL_NUM parent_process_id;
  28. PAL_NUM process_id;
  29. const char ** environ;
  30. /* credentials */
  31. unsigned int uid, gid;
  32. /* currently enabled signals */
  33. __sigset_t sigset;
  34. __sigset_t blocked_signals;
  35. /* enclave */
  36. const char * runtime_dir;
  37. } linux_state;
  38. #include <asm/mman.h>
  39. #define PRESET_PAGESIZE (1 << 12)
  40. #define DEFAULT_BACKLOG 2048
  41. static inline int HOST_FLAGS (int alloc_type, int prot)
  42. {
  43. return ((alloc_type & PAL_ALLOC_RESERVE) ? MAP_NORESERVE|MAP_UNINITIALIZED : 0) |
  44. ((prot & PAL_PROT_WRITECOPY) ? MAP_PRIVATE : MAP_SHARED);
  45. }
  46. static inline int HOST_PROT (int prot)
  47. {
  48. return prot & (PAL_PROT_READ|PAL_PROT_WRITE|PAL_PROT_EXEC);
  49. }
  50. #define ACCESS_R 4
  51. #define ACCESS_W 2
  52. #define ACCESS_X 1
  53. struct stat;
  54. bool stataccess (struct stat * stats, int acc);
  55. #ifdef IN_ENCLAVE
  56. /* Locking and unlocking of Mutexes */
  57. int _DkMutexCreate (struct mutex_handle * mut);
  58. int _DkMutexAtomicCreate (struct mutex_handle * mut);
  59. int _DkMutexDestroy (struct mutex_handle * mut);
  60. int _DkMutexLock (struct mutex_handle * mut);
  61. int _DkMutexLockTimeout (struct mutex_handle * mut, int timeout);
  62. int _DkMutexUnlock (struct mutex_handle * mut);
  63. int * get_futex (void);
  64. void free_futex (int * futex);
  65. extern char __text_start, __text_end, __data_start, __data_end;
  66. #define TEXT_START (void *) (&__text_start)
  67. #define TEXT_END (void *) (&__text_end)
  68. #define DATA_START (void *) (&__text_start)
  69. #define DATA_END (void *) (&__text_end)
  70. typedef struct { char bytes[32]; } sgx_checksum_t;
  71. typedef struct { char bytes[16]; } sgx_stub_t;
  72. int init_trusted_files (void);
  73. int load_trusted_file
  74. (PAL_HANDLE file, sgx_stub_t ** stubptr, uint64_t * sizeptr);
  75. int verify_trusted_file
  76. (const char * uri, void * mem, unsigned int offset, unsigned int size,
  77. sgx_stub_t * stubs, unsigned int total_size);
  78. int init_trusted_children (void);
  79. int register_trusted_child (const char * uri, const char * mrenclave_str);
  80. /* if a stream is encrypted, its key is 256 bit */
  81. typedef uint8_t PAL_SESSION_KEY [32];
  82. typedef uint8_t PAL_MAC_KEY [16];
  83. static inline
  84. void session_key_to_mac_key (PAL_SESSION_KEY * session_key,
  85. PAL_MAC_KEY * mac_key)
  86. {
  87. uint8_t * s = (void *) session_key;
  88. uint8_t * m = (void *) mac_key;
  89. for (int i = 0 ; i < 16 ; i++)
  90. m[i] = s[i] ^ s[16 + i];
  91. }
  92. /* exchange and establish a 256-bit session key */
  93. int _DkStreamKeyExchange (PAL_HANDLE stream, PAL_SESSION_KEY * key);
  94. /* request and respond for remote attestation */
  95. int _DkStreamAttestationRequest (PAL_HANDLE stream, void * data,
  96. int (*check_mrenclave) (sgx_arch_hash_t *,
  97. void *, void *),
  98. void * check_param);
  99. int _DkStreamAttestationRespond (PAL_HANDLE stream, void * data,
  100. int (*check_mrenclave) (sgx_arch_hash_t *,
  101. void *, void *),
  102. void * check_param);
  103. /* enclave state used for generating report */
  104. #define PAL_ATTESTATION_DATA_SIZE 24
  105. extern struct pal_enclave_state {
  106. uint64_t enclave_flags; /* flags to specify the state of the
  107. enclave */
  108. uint8_t data[PAL_ATTESTATION_DATA_SIZE];
  109. /* reserved for filling other data */
  110. uint8_t enclave_keyhash[32]; /* SHA256 digest of enclave's public key
  111. can also be used as an identifier of the
  112. enclave */
  113. } __attribute__((packed, aligned (128))) pal_enclave_state;
  114. #include "sgx_arch.h"
  115. #define PAL_ENCLAVE_INITIALIZED 0x0001ULL
  116. extern struct pal_enclave_config {
  117. sgx_arch_hash_t mrenclave;
  118. sgx_arch_attributes_t enclave_attributes;
  119. void * enclave_key;
  120. } pal_enclave_config;
  121. static inline __attribute__((always_inline))
  122. char * __hex2str(void * hex, int size)
  123. {
  124. static char * ch = "0123456789abcdef";
  125. char * str = __alloca(size * 2);
  126. for (int i = 0 ; i < size ; i++) {
  127. unsigned char h = ((unsigned char *) hex)[i];
  128. str[i * 2] = ch[h / 16];
  129. str[i * 2 + 1] = ch[h % 16];
  130. }
  131. str[size * 2 - 1] = 0;
  132. return str;
  133. }
  134. #define hex2str(array) __hex2str(array, sizeof(array))
  135. #else
  136. #ifdef DEBUG
  137. # ifndef SIGCHLD
  138. # define SIGCHLD 17
  139. # endif
  140. # define ARCH_VFORK() \
  141. (current_enclave->pal_sec.in_gdb ? \
  142. INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK|SIGCHLD, 0, NULL, NULL) :\
  143. INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK, 0, NULL, NULL))
  144. #else
  145. # define ARCH_VFORK() \
  146. (INLINE_SYSCALL(clone, 4, CLONE_VM|CLONE_VFORK, 0, NULL, NULL))
  147. #endif
  148. #endif /* IN_ENCLAVE */
  149. #define DBG_E 0x01
  150. #define DBG_I 0x02
  151. #define DBG_D 0x04
  152. #define DBG_S 0x08
  153. #define DBG_P 0x10
  154. #define DBG_M 0x20
  155. #ifdef DEBUG
  156. # define DBG_LEVEL (DBG_E|DBG_I|DBG_D|DBG_S)
  157. #else
  158. # define DBG_LEVEL (DBG_E)
  159. #endif
  160. #ifdef IN_ENCLAVE
  161. #define SGX_DBG(class, fmt...) \
  162. do { if ((class) & DBG_LEVEL) printf(fmt); } while (0)
  163. #else
  164. int pal_printf(const char * fmt, ...);
  165. #define SGX_DBG(class, fmt...) \
  166. do { if ((class) & DBG_LEVEL) pal_printf(fmt); } while (0)
  167. #endif
  168. #ifdef __i386__
  169. # define rmb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
  170. # define cpu_relax() asm volatile("rep; nop" ::: "memory");
  171. #endif
  172. #ifdef __x86_64__
  173. # include <unistd.h>
  174. # define rmb() asm volatile("lfence" ::: "memory")
  175. # define cpu_relax() asm volatile("rep; nop" ::: "memory");
  176. #endif
  177. #endif /* PAL_LINUX_H */