From e8f334b22006997a5d084cb720435aefb8562ffa Mon Sep 17 00:00:00 2001 From: Chia-Che Tsai Date: Sat, 3 Nov 2018 17:55:00 -0700 Subject: [PATCH] [Linux/SGX PAL] Check NULL handles in DkObjectsWaitAny() --- Pal/src/db_object.c | 2 +- Pal/src/host/Linux-SGX/db_object.c | 13 ++++++++++--- Pal/src/host/Linux/db_object.c | 7 ++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Pal/src/db_object.c b/Pal/src/db_object.c index d40748e2..dd1904fe 100644 --- a/Pal/src/db_object.c +++ b/Pal/src/db_object.c @@ -89,7 +89,7 @@ DkObjectsWaitAny (PAL_NUM count, PAL_HANDLE * handleArray, PAL_NUM timeout) for (int i = 0 ; i < count ; i++) // We modify the caller's handleArray? - if (UNKNOWN_HANDLE(handleArray[i])) + if (handleArray[i] && UNKNOWN_HANDLE(handleArray[i])) handleArray[i] = NULL; PAL_HANDLE polled = NULL; diff --git a/Pal/src/host/Linux-SGX/db_object.c b/Pal/src/host/Linux-SGX/db_object.c index b7476b54..5136a47e 100644 --- a/Pal/src/host/Linux-SGX/db_object.c +++ b/Pal/src/host/Linux-SGX/db_object.c @@ -95,7 +95,7 @@ static int _DkObjectWaitOne (PAL_HANDLE handle, uint64_t timeout) const struct handle_ops * ops = HANDLE_OPS(handle); - if (!ops->wait) + if (!ops || !ops->wait) return -PAL_ERROR_NOTSUPPORT; return ops->wait(handle, timeout); @@ -110,8 +110,15 @@ int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, uint64_t timeout, return 0; if (count == 1) { - *polled = handleArray[0]; - return _DkObjectWaitOne(handleArray[0], timeout); + // It is possible to have NULL pointers in the handle array. + // In this case, assume nothing is polled. + if (!handleArray[0]) + return -PAL_ERROR_TRYAGAIN; + + int rv = _DkObjectWaitOne(handleArray[0], timeout); + if (rv == 0) + *polled = handleArray[0]; + return rv; } int i, j, ret, maxfds = 0, nfds = 0; diff --git a/Pal/src/host/Linux/db_object.c b/Pal/src/host/Linux/db_object.c index eddb6cac..d4521a35 100644 --- a/Pal/src/host/Linux/db_object.c +++ b/Pal/src/host/Linux/db_object.c @@ -117,7 +117,7 @@ static int _DkObjectWaitOne (PAL_HANDLE handle, uint64_t timeout) const struct handle_ops * ops = HANDLE_OPS(handle); - if (!ops->wait) + if (!ops || !ops->wait) return -PAL_ERROR_NOTSUPPORT; return ops->wait(handle, timeout); @@ -132,6 +132,11 @@ int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, uint64_t timeout, return 0; if (count == 1) { + // It is possible to have NULL pointers in the handle array. + // In this case, assume nothing is polled. + if (!handleArray[0]) + return -PAL_ERROR_TRYAGAIN; + int rv = _DkObjectWaitOne(handleArray[0], timeout); if (rv == 0) *polled = handleArray[0];