fix: add unrecoverable to avoid out of bound access

Related-To: NEO-9038
Signed-off-by: Mateusz Jablonski <mateusz.jablonski@intel.com>
This commit is contained in:
Mateusz Jablonski 2023-10-09 09:30:26 +00:00 committed by Compute-Runtime-Automation
parent d76145ee65
commit a31dd7b454
9 changed files with 32 additions and 27 deletions

View File

@ -41,6 +41,7 @@
#include "shared/source/os_interface/linux/i915_prelim.h"
#include "shared/source/os_interface/linux/memory_info.h"
#include "shared/source/os_interface/linux/os_context_linux.h"
#include "shared/source/os_interface/linux/sys_calls.h"
#include "shared/source/os_interface/os_interface.h"
#include <cstring>
@ -815,7 +816,7 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromMultipleShared
if (bo == nullptr) {
areBosSharedObjects = false;
size_t size = lseekFunction(handle, 0, SEEK_END);
size_t size = SysCalls::lseek(handle, 0, SEEK_END);
UNRECOVERABLE_IF(size == std::numeric_limits<size_t>::max());
totalSize += size;
@ -964,7 +965,8 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o
}
if (bo == nullptr) {
size_t size = lseekFunction(handle, 0, SEEK_END);
size_t size = SysCalls::lseek(handle, 0, SEEK_END);
UNRECOVERABLE_IF(size == std::numeric_limits<size_t>::max());
auto patIndex = drm.getPatIndex(nullptr, properties.allocationType, CacheRegion::Default, CachePolicy::WriteBack, false);
auto boHandleWrapper = reuseSharedAllocation ? BufferObjectHandleWrapper{boHandle} : tryToGetBoHandleWrapperWithSharedOwnership(boHandle);
@ -2388,7 +2390,8 @@ DrmAllocation *DrmMemoryManager::createUSMHostAllocationFromSharedHandle(osHandl
if (bo == nullptr) {
void *cpuPointer = nullptr;
size_t size = lseekFunction(handle, 0, SEEK_END);
size_t size = SysCalls::lseek(handle, 0, SEEK_END);
UNRECOVERABLE_IF(size == std::numeric_limits<size_t>::max());
bo = new BufferObject(properties.rootDeviceIndex, &drm, patIndex, boHandle, size, maxOsContextCount);

View File

@ -161,7 +161,6 @@ class DrmMemoryManager : public MemoryManager {
std::unique_ptr<DrmGemCloseWorker> gemCloseWorker;
decltype(&mmap) mmapFunction = mmap;
decltype(&munmap) munmapFunction = munmap;
decltype(&lseek) lseekFunction = lseek;
decltype(&close) closeFunction = close;
std::vector<BufferObject *> sharingBufferObjects;
std::mutex mtx;

View File

@ -51,5 +51,6 @@ int unlink(const std::string &pathname);
DIR *opendir(const char *name);
struct dirent *readdir(DIR *dir);
int closedir(DIR *dir);
off_t lseek(int fd, off_t offset, int whence) noexcept;
} // namespace SysCalls
} // namespace NEO

View File

@ -183,5 +183,8 @@ int closedir(DIR *dir) {
return ::closedir(dir);
}
off_t lseek(int fd, off_t offset, int whence) noexcept {
return ::lseek(fd, offset, whence);
}
} // namespace SysCalls
} // namespace NEO

View File

@ -19,8 +19,6 @@
#include <atomic>
namespace NEO {
off_t lseekReturn = 4096u;
std::atomic<int> lseekCalledCount(0);
std::atomic<int> closeInputFd(0);
std::atomic<int> closeCalledCount(0);
@ -30,10 +28,7 @@ TestedDrmMemoryManager::TestedDrmMemoryManager(ExecutionEnvironment &executionEn
executionEnvironment) {
this->mmapFunction = SysCalls::mmap;
this->munmapFunction = SysCalls::munmap;
this->lseekFunction = &lseekMock;
this->closeFunction = &closeMock;
lseekReturn = 4096;
lseekCalledCount = 0;
closeInputFd = 0;
closeCalledCount = 0;
hostPtrManager.reset(new MockHostPtrManager);
@ -49,10 +44,7 @@ TestedDrmMemoryManager::TestedDrmMemoryManager(bool enableLocalMemory,
executionEnvironment) {
this->mmapFunction = SysCalls::mmap;
this->munmapFunction = SysCalls::munmap;
this->lseekFunction = &lseekMock;
this->closeFunction = &closeMock;
lseekReturn = 4096;
lseekCalledCount = 0;
closeInputFd = 0;
closeCalledCount = 0;
this->executionEnvironment = &executionEnvironment;

View File

@ -15,18 +15,9 @@
#include <atomic>
namespace NEO {
extern off_t lseekReturn;
extern std::atomic<int> lseekCalledCount;
extern std::atomic<int> closeInputFd;
extern std::atomic<int> closeCalledCount;
inline off_t lseekMock(int fd, off_t offset, int whence) noexcept {
lseekCalledCount++;
if ((fd == closeInputFd) && (closeCalledCount > 0)) {
return 0;
}
return lseekReturn;
}
inline int closeMock(int fd) {
closeInputFd = fd;
closeCalledCount++;

View File

@ -101,6 +101,8 @@ DIR *(*sysCallsOpendir)(const char *name) = nullptr;
struct dirent *(*sysCallsReaddir)(DIR *dir) = nullptr;
int (*sysCallsClosedir)(DIR *dir) = nullptr;
int (*sysCallsGetDevicePath)(int deviceFd, char *buf, size_t &bufSize) = nullptr;
off_t lseekReturn = 4096u;
std::atomic<int> lseekCalledCount(0);
int mkdir(const std::string &path) {
if (sysCallsMkdir != nullptr) {
@ -457,5 +459,10 @@ int closedir(DIR *dir) {
return 0;
}
off_t lseek(int fd, off_t offset, int whence) noexcept {
lseekCalledCount++;
return lseekReturn;
}
} // namespace SysCalls
} // namespace NEO

View File

@ -7,6 +7,7 @@
#pragma once
#include <atomic>
#include <cstdint>
#include <dirent.h>
#include <iostream>
@ -73,5 +74,8 @@ extern bool mmapCaptureExtendedPointers;
extern bool mmapAllowExtendedPointers;
extern uint32_t mmapFuncCalled;
extern uint32_t munmapFuncCalled;
extern off_t lseekReturn;
extern std::atomic<int> lseekCalledCount;
} // namespace SysCalls
} // namespace NEO

View File

@ -2029,6 +2029,7 @@ TEST_F(DrmMemoryManagerTest, given32BitAddressingWhenBufferFromSharedHandleAndBi
mock->ioctlExpected.gemWait = 1;
mock->ioctlExpected.gemClose = 1;
SysCalls::lseekCalledCount = 0;
memoryManager->setForce32BitAllocations(true);
osHandle handle = 1u;
this->mock->outputHandle = 2u;
@ -2038,7 +2039,7 @@ TEST_F(DrmMemoryManagerTest, given32BitAddressingWhenBufferFromSharedHandleAndBi
auto graphicsAllocation = memoryManager->createGraphicsAllocationFromSharedHandle(handle, properties, true, false, true, nullptr);
auto drmAllocation = static_cast<DrmAllocation *>(graphicsAllocation);
EXPECT_TRUE(graphicsAllocation->is32BitAllocation());
EXPECT_EQ(1, lseekCalledCount);
EXPECT_EQ(1, SysCalls::lseekCalledCount);
auto gmmHelper = device->getGmmHelper();
EXPECT_EQ(gmmHelper->canonize(memoryManager->getExternalHeapBaseAddress(graphicsAllocation->getRootDeviceIndex(), drmAllocation->isAllocatedInLocalMemoryPool())), drmAllocation->getGpuBaseAddress());
memoryManager->freeGraphicsMemory(graphicsAllocation);
@ -2048,6 +2049,7 @@ TEST_F(DrmMemoryManagerTest, given32BitAddressingWhenBufferFromSharedHandleIsCre
mock->ioctlExpected.primeFdToHandle = 1;
mock->ioctlExpected.gemWait = 1;
mock->ioctlExpected.gemClose = 1;
SysCalls::lseekCalledCount = 0;
memoryManager->setForce32BitAllocations(true);
osHandle handle = 1u;
@ -2057,7 +2059,7 @@ TEST_F(DrmMemoryManagerTest, given32BitAddressingWhenBufferFromSharedHandleIsCre
auto drmAllocation = static_cast<DrmAllocation *>(graphicsAllocation);
EXPECT_FALSE(graphicsAllocation->is32BitAllocation());
EXPECT_EQ(1, lseekCalledCount);
EXPECT_EQ(1, SysCalls::lseekCalledCount);
EXPECT_EQ(0llu, drmAllocation->getGpuBaseAddress());
@ -2068,6 +2070,7 @@ TEST_F(DrmMemoryManagerTest, givenLimitedRangeAllocatorWhenBufferFromSharedHandl
mock->ioctlExpected.primeFdToHandle = 1;
mock->ioctlExpected.gemWait = 1;
mock->ioctlExpected.gemClose = 1;
SysCalls::lseekCalledCount = 0;
memoryManager->forceLimitedRangeAllocator(0xFFFFFFFFF);
osHandle handle = 1u;
@ -2078,7 +2081,7 @@ TEST_F(DrmMemoryManagerTest, givenLimitedRangeAllocatorWhenBufferFromSharedHandl
auto drmAllocation = static_cast<DrmAllocation *>(graphicsAllocation);
EXPECT_EQ(0llu, drmAllocation->getGpuBaseAddress());
EXPECT_EQ(1, lseekCalledCount);
EXPECT_EQ(1, SysCalls::lseekCalledCount);
memoryManager->freeGraphicsMemory(graphicsAllocation);
}
@ -2086,6 +2089,7 @@ TEST_F(DrmMemoryManagerTest, givenNon32BitAddressingWhenBufferFromSharedHandleIs
mock->ioctlExpected.primeFdToHandle = 1;
mock->ioctlExpected.gemWait = 1;
mock->ioctlExpected.gemClose = 1;
SysCalls::lseekCalledCount = 0;
memoryManager->setForce32BitAllocations(false);
osHandle handle = 1u;
@ -2094,7 +2098,7 @@ TEST_F(DrmMemoryManagerTest, givenNon32BitAddressingWhenBufferFromSharedHandleIs
auto graphicsAllocation = memoryManager->createGraphicsAllocationFromSharedHandle(handle, properties, true, false, true, nullptr);
auto drmAllocation = static_cast<DrmAllocation *>(graphicsAllocation);
EXPECT_FALSE(graphicsAllocation->is32BitAllocation());
EXPECT_EQ(1, lseekCalledCount);
EXPECT_EQ(1, SysCalls::lseekCalledCount);
EXPECT_EQ(0llu, drmAllocation->getGpuBaseAddress());
memoryManager->freeGraphicsMemory(graphicsAllocation);
}
@ -2404,7 +2408,8 @@ TEST_F(DrmMemoryManagerTest, given32BitAllocatorWithHeapAllocatorWhenLargerFragm
TEST_F(DrmMemoryManagerTest, givenSharedAllocationWithSmallerThenRealSizeWhenCreateIsCalledThenRealSizeIsUsed) {
unsigned int realSize = 64 * 1024;
lseekReturn = realSize;
VariableBackup<decltype(SysCalls::lseekReturn)> lseekBackup(&SysCalls::lseekReturn, realSize);
SysCalls::lseekCalledCount = 0;
mock->ioctlExpected.primeFdToHandle = 1;
mock->ioctlExpected.gemWait = 1;
mock->ioctlExpected.gemClose = 1;
@ -2423,7 +2428,7 @@ TEST_F(DrmMemoryManagerTest, givenSharedAllocationWithSmallerThenRealSizeWhenCre
EXPECT_NE(0llu, bo->peekAddress());
EXPECT_EQ(1u, bo->getRefCount());
EXPECT_EQ(realSize, bo->peekSize());
EXPECT_EQ(1, lseekCalledCount);
EXPECT_EQ(1, SysCalls::lseekCalledCount);
memoryManager->freeGraphicsMemory(graphicsAllocation);
}