From 3a39919d31ac2362d18e1f71928c028e6ac33151 Mon Sep 17 00:00:00 2001 From: nabil Otsmane Date: Sun, 24 Nov 2024 16:24:36 +0400 Subject: [PATCH 1/2] protocols: fix shm fd size check before creating or resizing shm_pool --- src/protocols/core/Shm.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/protocols/core/Shm.cpp b/src/protocols/core/Shm.cpp index a8c98bb0ed1..49dae079892 100644 --- a/src/protocols/core/Shm.cpp +++ b/src/protocols/core/Shm.cpp @@ -1,6 +1,7 @@ #include "Shm.hpp" #include #include +#include #include #include "../../render/Texture.hpp" #include "../types/WLBuffer.hpp" @@ -99,10 +100,25 @@ void CSHMPool::resize(size_t size_) { LOGM(ERR, "Couldn't mmap {} bytes from fd {} of shm client", size, fd); } +int shmIsSizeValid(int fd, size_t size) { + struct stat st; + if (fstat(fd, &st) == -1) { + LOGM(ERR, "Couldn't get stat for fd {} of shm client", fd); + return 0; + } + + return (size_t)st.st_size >= size; +} + CWLSHMPoolResource::CWLSHMPoolResource(SP resource_, int fd_, size_t size_) : resource(resource_) { if (!good()) return; + if (!shmIsSizeValid(fd_, size_)) { + resource_->error(-1, "The size of the file is not big enough for the shm pool"); + return; + } + pool = makeShared(fd_, size_); resource->setDestroy([this](CWlShmPool* r) { PROTO::shm->destroyResource(this); }); @@ -113,6 +129,11 @@ CWLSHMPoolResource::CWLSHMPoolResource(SP resource_, int fd_, size_t r->error(-1, "Shrinking a shm pool is illegal"); return; } + if (!shmIsSizeValid(pool->fd, size_)) { + r->error(-1, "The size of the file is not big enough for the shm pool"); + return; + } + pool->resize(size_); }); From 201ad4baad72895e567c40dc9231ff64d02769b8 Mon Sep 17 00:00:00 2001 From: nabil Otsmane Date: Sun, 24 Nov 2024 18:21:25 +0400 Subject: [PATCH 2/2] added static to function --- src/protocols/core/Shm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/protocols/core/Shm.cpp b/src/protocols/core/Shm.cpp index 49dae079892..708d41c3042 100644 --- a/src/protocols/core/Shm.cpp +++ b/src/protocols/core/Shm.cpp @@ -100,7 +100,7 @@ void CSHMPool::resize(size_t size_) { LOGM(ERR, "Couldn't mmap {} bytes from fd {} of shm client", size, fd); } -int shmIsSizeValid(int fd, size_t size) { +static int shmIsSizeValid(int fd, size_t size) { struct stat st; if (fstat(fd, &st) == -1) { LOGM(ERR, "Couldn't get stat for fd {} of shm client", fd);