Files
Don Porter c68cb42e20 Address a TOCTTOU vulnerability in SGX read/map (#131)
* Fix a bug where configuration error ends up doing a huge allocation, rather than catching the error.  Add some documentation to the slabmgr code.

* Add a unit test and some documentation to answer the question in issue #107.  I can't see how offset and map_start would end up being different.

* Rewrite of SGX file_map to remove TOCTTTOU now passes all unit tests

* Apply a similar fix to file_read.

* Factor complicated verification code into a common helper routine.

* Adjust the memory copying strategy so that all bits in the returned buffed are exactly the same bits as verified in the trusted, scratch buffer.

* Fixing the TOCTOU issue in file checking

* Adding comments for load_trusted_file() and copy_and_check_trusted_file(); Deprecate the old design

* Documenting the file checking mechanism
2018-10-24 14:51:06 -04:00

66 lines
2.6 KiB
Python

#!/usr/bin/python
import os, sys, mmap, random, string, binascii
from regression import Regression
loader = os.environ['PAL_LOADER']
def prepare_files(args):
global file_exist
file_exist = open('File', 'rb').read()
if os.path.exists("file_nonexist.tmp"):
os.remove("file_nonexist.tmp")
with open("file_delete.tmp", "w") as f:
f.write(file_exist)
# Running File
regression = Regression(loader, "File", prepare_files)
regression.add_check(name="Basic File Opening",
check=lambda res: "File Open Test 1 OK" in res[0].log and
"File Open Test 2 OK" in res[0].log and
"File Open Test 3 OK" in res[0].log)
regression.add_check(name="Basic File Creation",
check=lambda res: "File Creation Test 1 OK" in res[0].log and
"File Creation Test 2 OK" in res[0].log and
"File Creation Test 3 OK" in res[0].log)
regression.add_check(name="File Reading",
check=lambda res: ("Read Test 1 (0th - 40th): " + binascii.hexlify(file_exist[0:40])) in res[0].log and
("Read Test 2 (0th - 40th): " + binascii.hexlify(file_exist[0:40])) in res[0].log and
("Read Test 3 (200th - 240th): " + binascii.hexlify(file_exist[200:240])) in res[0].log)
def check_write(res):
global file_exist
with open("file_nonexist.tmp", "r") as f:
file_nonexist = f.read()
return file_exist[0:40] == file_nonexist[200:240] and \
file_exist[200:240] == file_nonexist[0:40]
regression.add_check(name="File Writing", check=check_write)
regression.add_check(name="File Attribute Query",
check=lambda res: ("Query: type = 1, size = %d" % (len(file_exist))) in res[0].log)
regression.add_check(name="File Attribute Query by Handle",
check=lambda res: ("Query by Handle: type = 1, size = %d" % (len(file_exist))) in res[0].log)
regression.add_check(name="File Mapping",
check=lambda res: ("Map Test 1 (0th - 40th): " + binascii.hexlify(file_exist[0:40])) in res[0].log and
("Map Test 2 (200th - 240th): " + binascii.hexlify(file_exist[200:240])) in res[0].log and
("Map Test 3 (4096th - 4136th): " + binascii.hexlify(file_exist[4096:4136])) in res[0].log and
("Map Test 4 (4296th - 4336th): " +
binascii.hexlify(file_exist[4296:4336])) in res[0].log)
regression.add_check(name="Set File Length",
check=lambda res: os.stat("file_nonexist.tmp").st_size == mmap.ALLOCATIONGRANULARITY)
regression.add_check(name="File Deletion",
check=lambda res: not os.path.exists("file_delete.tmp"))
rv = regression.run_checks()
if rv: sys.exit(rv)