Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/protocols/core/Shm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Shm.hpp"
#include <algorithm>
#include <sys/mman.h>
#include <sys/stat.h>
#include <drm_fourcc.h>
#include "../../render/Texture.hpp"
#include "../types/WLBuffer.hpp"
Expand Down Expand Up @@ -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) {
nabil-otsmane marked this conversation as resolved.
Show resolved Hide resolved
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_) {
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<CSHMPool>(fd_, size_);

resource->setDestroy([this](CWlShmPool* r) { PROTO::shm->destroyResource(this); });
Expand All @@ -113,6 +129,11 @@ CWLSHMPoolResource::CWLSHMPoolResource(SP<CWlShmPool> 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_);
});

Expand Down