|
@@ -168,16 +168,23 @@ int ocall_close (int fd)
|
|
|
return retval;
|
|
|
}
|
|
|
|
|
|
-int ocall_read (int fd, void * buf, unsigned int count)
|
|
|
-{
|
|
|
+int ocall_read(int fd, void* buf, unsigned int count) {
|
|
|
int retval = 0;
|
|
|
- void * obuf = NULL;
|
|
|
- ms_ocall_read_t * ms;
|
|
|
+ void* obuf = NULL;
|
|
|
+ ms_ocall_read_t* ms;
|
|
|
+ void* ms_buf;
|
|
|
|
|
|
if (count > MAX_UNTRUSTED_STACK_BUF) {
|
|
|
retval = ocall_mmap_untrusted(-1, 0, ALLOC_ALIGN_UP(count), PROT_READ | PROT_WRITE, &obuf);
|
|
|
if (IS_ERR(retval))
|
|
|
return retval;
|
|
|
+ ms_buf = obuf;
|
|
|
+ } else {
|
|
|
+ ms_buf = sgx_alloc_on_ustack(count);
|
|
|
+ if (!ms_buf) {
|
|
|
+ retval = -EPERM;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
ms = sgx_alloc_on_ustack(sizeof(*ms));
|
|
@@ -188,15 +195,7 @@ int ocall_read (int fd, void * buf, unsigned int count)
|
|
|
|
|
|
ms->ms_fd = fd;
|
|
|
ms->ms_count = count;
|
|
|
- if (obuf)
|
|
|
- ms->ms_buf = obuf;
|
|
|
- else
|
|
|
- ms->ms_buf = sgx_alloc_on_ustack(count);
|
|
|
-
|
|
|
- if (!ms->ms_buf) {
|
|
|
- retval = -EPERM;
|
|
|
- goto out;
|
|
|
- }
|
|
|
+ ms->ms_buf = ms_buf;
|
|
|
|
|
|
retval = sgx_ocall(OCALL_READ, ms);
|
|
|
|
|
@@ -214,15 +213,15 @@ out:
|
|
|
return retval;
|
|
|
}
|
|
|
|
|
|
-int ocall_write (int fd, const void * buf, unsigned int count)
|
|
|
-{
|
|
|
+int ocall_write(int fd, const void* buf, unsigned int count) {
|
|
|
int retval = 0;
|
|
|
- void * obuf = NULL;
|
|
|
- ms_ocall_write_t * ms;
|
|
|
+ void* obuf = NULL;
|
|
|
+ ms_ocall_write_t* ms;
|
|
|
+ const void* ms_buf;
|
|
|
|
|
|
if (sgx_is_completely_outside_enclave(buf, count)) {
|
|
|
/* buf is in untrusted memory (e.g., allowed file mmaped in untrusted memory) */
|
|
|
- obuf = (void*)buf;
|
|
|
+ ms_buf = buf;
|
|
|
} else if (sgx_is_completely_within_enclave(buf, count)) {
|
|
|
/* typical case of buf inside of enclave memory */
|
|
|
if (count > MAX_UNTRUSTED_STACK_BUF) {
|
|
@@ -231,10 +230,17 @@ int ocall_write (int fd, const void * buf, unsigned int count)
|
|
|
if (IS_ERR(retval))
|
|
|
return retval;
|
|
|
memcpy(obuf, buf, count);
|
|
|
+ ms_buf = obuf;
|
|
|
+ } else {
|
|
|
+ ms_buf = sgx_copy_to_ustack(buf, count);
|
|
|
}
|
|
|
} else {
|
|
|
/* buf is partially in/out of enclave memory */
|
|
|
- return -EPERM;
|
|
|
+ ms_buf = NULL;
|
|
|
+ }
|
|
|
+ if (!ms_buf) {
|
|
|
+ retval = -EPERM;
|
|
|
+ goto out;
|
|
|
}
|
|
|
|
|
|
ms = sgx_alloc_on_ustack(sizeof(*ms));
|
|
@@ -245,21 +251,107 @@ int ocall_write (int fd, const void * buf, unsigned int count)
|
|
|
|
|
|
ms->ms_fd = fd;
|
|
|
ms->ms_count = count;
|
|
|
+ ms->ms_buf = ms_buf;
|
|
|
+
|
|
|
+ retval = sgx_ocall(OCALL_WRITE, ms);
|
|
|
+
|
|
|
+out:
|
|
|
+ sgx_reset_ustack();
|
|
|
if (obuf)
|
|
|
- ms->ms_buf = obuf;
|
|
|
- else
|
|
|
- ms->ms_buf = sgx_copy_to_ustack(buf, count);
|
|
|
+ ocall_munmap_untrusted(obuf, ALLOC_ALIGN_UP(count));
|
|
|
+ return retval;
|
|
|
+}
|
|
|
|
|
|
- if (!ms->ms_buf) {
|
|
|
+ssize_t ocall_pread(int fd, void* buf, size_t count, off_t offset) {
|
|
|
+ long retval = 0;
|
|
|
+ void* obuf = NULL;
|
|
|
+ ms_ocall_pread_t* ms;
|
|
|
+ void* ms_buf;
|
|
|
+
|
|
|
+ if (count > MAX_UNTRUSTED_STACK_BUF) {
|
|
|
+ retval = ocall_mmap_untrusted(-1, 0, ALLOC_ALIGN_UP(count), PROT_READ | PROT_WRITE, &obuf);
|
|
|
+ if (IS_ERR(retval))
|
|
|
+ return retval;
|
|
|
+ ms_buf = obuf;
|
|
|
+ } else {
|
|
|
+ ms_buf = sgx_alloc_on_ustack(count);
|
|
|
+ if (!ms_buf) {
|
|
|
+ retval = -EPERM;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ms = sgx_alloc_on_ustack(sizeof(*ms));
|
|
|
+ if (!ms) {
|
|
|
retval = -EPERM;
|
|
|
goto out;
|
|
|
}
|
|
|
|
|
|
- retval = sgx_ocall(OCALL_WRITE, ms);
|
|
|
+ ms->ms_fd = fd;
|
|
|
+ ms->ms_count = count;
|
|
|
+ ms->ms_offset = offset;
|
|
|
+ ms->ms_buf = ms_buf;
|
|
|
+
|
|
|
+ retval = sgx_ocall(OCALL_PREAD, ms);
|
|
|
+ if (retval > 0) {
|
|
|
+ if (!sgx_copy_to_enclave(buf, count, ms->ms_buf, retval)) {
|
|
|
+ retval = -EPERM;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
out:
|
|
|
sgx_reset_ustack();
|
|
|
- if (obuf && obuf != buf)
|
|
|
+ if (obuf)
|
|
|
+ ocall_munmap_untrusted(obuf, ALLOC_ALIGN_UP(count));
|
|
|
+ return retval;
|
|
|
+}
|
|
|
+
|
|
|
+ssize_t ocall_pwrite(int fd, const void* buf, size_t count, off_t offset) {
|
|
|
+ long retval = 0;
|
|
|
+ void* obuf = NULL;
|
|
|
+ ms_ocall_pwrite_t* ms;
|
|
|
+ const void* ms_buf;
|
|
|
+
|
|
|
+ if (sgx_is_completely_outside_enclave(buf, count)) {
|
|
|
+ /* buf is in untrusted memory (e.g., allowed file mmaped in untrusted memory) */
|
|
|
+ ms_buf = buf;
|
|
|
+ } else if (sgx_is_completely_within_enclave(buf, count)) {
|
|
|
+ /* typical case of buf inside of enclave memory */
|
|
|
+ if (count > MAX_UNTRUSTED_STACK_BUF) {
|
|
|
+ /* buf is too big and may overflow untrusted stack, so use untrusted heap */
|
|
|
+ retval = ocall_mmap_untrusted(-1, 0, ALLOC_ALIGN_UP(count), PROT_READ | PROT_WRITE, &obuf);
|
|
|
+ if (IS_ERR(retval))
|
|
|
+ return retval;
|
|
|
+ memcpy(obuf, buf, count);
|
|
|
+ ms_buf = obuf;
|
|
|
+ } else {
|
|
|
+ ms_buf = sgx_copy_to_ustack(buf, count);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ /* buf is partially in/out of enclave memory */
|
|
|
+ ms_buf = NULL;
|
|
|
+ }
|
|
|
+ if (!ms_buf) {
|
|
|
+ retval = -EPERM;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
+ ms = sgx_alloc_on_ustack(sizeof(*ms));
|
|
|
+ if (!ms) {
|
|
|
+ retval = -EPERM;
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
+ ms->ms_fd = fd;
|
|
|
+ ms->ms_count = count;
|
|
|
+ ms->ms_offset = offset;
|
|
|
+ ms->ms_buf = ms_buf;
|
|
|
+
|
|
|
+ retval = sgx_ocall(OCALL_PWRITE, ms);
|
|
|
+
|
|
|
+out:
|
|
|
+ sgx_reset_ustack();
|
|
|
+ if (obuf)
|
|
|
ocall_munmap_untrusted(obuf, ALLOC_ALIGN_UP(count));
|
|
|
return retval;
|
|
|
}
|
|
@@ -385,26 +477,6 @@ int ocall_ftruncate (int fd, uint64_t length)
|
|
|
return retval;
|
|
|
}
|
|
|
|
|
|
-int ocall_lseek(int fd, uint64_t offset, int whence) {
|
|
|
- int retval = 0;
|
|
|
- ms_ocall_lseek_t* ms;
|
|
|
-
|
|
|
- ms = sgx_alloc_on_ustack(sizeof(*ms));
|
|
|
- if (!ms) {
|
|
|
- sgx_reset_ustack();
|
|
|
- return -EPERM;
|
|
|
- }
|
|
|
-
|
|
|
- ms->ms_fd = fd;
|
|
|
- ms->ms_offset = offset;
|
|
|
- ms->ms_whence = whence;
|
|
|
-
|
|
|
- retval = sgx_ocall(OCALL_LSEEK, ms);
|
|
|
-
|
|
|
- sgx_reset_ustack();
|
|
|
- return retval;
|
|
|
-}
|
|
|
-
|
|
|
int ocall_mkdir (const char * pathname, unsigned short mode)
|
|
|
{
|
|
|
int retval = 0;
|