shm: fix shm fd size check before creating or resizing shm_pool (#8572)

* protocols: fix shm fd size check before creating or resizing shm_pool

* added static to function
This commit is contained in:
Nabil Otsmane 2024-11-25 01:50:35 +01:00 committed by GitHub
parent cc38e7e18f
commit 1930a95000
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,7 @@
#include "Shm.hpp" #include "Shm.hpp"
#include <algorithm> #include <algorithm>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h>
#include <drm_fourcc.h> #include <drm_fourcc.h>
#include "../../render/Texture.hpp" #include "../../render/Texture.hpp"
#include "../types/WLBuffer.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); LOGM(ERR, "Couldn't mmap {} bytes from fd {} of shm client", size, fd);
} }
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);
return 0;
}
return (size_t)st.st_size >= size;
}
CWLSHMPoolResource::CWLSHMPoolResource(SP<CWlShmPool> resource_, int fd_, size_t size_) : resource(resource_) { CWLSHMPoolResource::CWLSHMPoolResource(SP<CWlShmPool> resource_, int fd_, size_t size_) : resource(resource_) {
if (!good()) if (!good())
return; return;
if (!shmIsSizeValid(fd_, size_)) {
resource_->error(-1, "The size of the file is not big enough for the shm pool");
return;
}
pool = makeShared<CSHMPool>(fd_, size_); pool = makeShared<CSHMPool>(fd_, size_);
resource->setDestroy([this](CWlShmPool* r) { PROTO::shm->destroyResource(this); }); resource->setDestroy([this](CWlShmPool* r) { PROTO::shm->destroyResource(this); });
@ -113,6 +129,11 @@ CWLSHMPoolResource::CWLSHMPoolResource(SP<CWlShmPool> resource_, int fd_, size_t
r->error(-1, "Shrinking a shm pool is illegal"); r->error(-1, "Shrinking a shm pool is illegal");
return; 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_); pool->resize(size_);
}); });