Skip to content

Commit

Permalink
compositor: more fs safety around tmp directories
Browse files Browse the repository at this point in the history
HIS now includes a random bit, and hyprland will bail if /tmp/hypr is not a directory or if /tmp/hypr/his exists
  • Loading branch information
vaxerski committed Apr 28, 2024
1 parent b164e67 commit f7815da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion hyprctl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ std::vector<SInstanceData> instances() {
data->id = data->id.substr(data->id.find_last_of('/') + 1, data->id.find(".lock") - data->id.find_last_of('/') - 1);

try {
data->time = std::stoull(data->id.substr(data->id.find_first_of('_') + 1));
data->time = std::stoull(data->id.substr(data->id.find_first_of('_') + 1, data->id.find_last_of('_') - (data->id.find_first_of('_') + 1)));
} catch (std::exception& e) { continue; }

// read file
Expand Down
21 changes: 19 additions & 2 deletions src/Compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,32 @@ void handleUserSignal(int sig) {
CCompositor::CCompositor() {
m_iHyprlandPID = getpid();

m_szInstanceSignature = GIT_COMMIT_HASH + std::string("_") + std::to_string(time(NULL));
std::random_device dev;
std::mt19937 engine(dev());
std::uniform_int_distribution<> distribution(0, INT32_MAX);

m_szInstanceSignature = GIT_COMMIT_HASH + std::string("_") + std::to_string(time(NULL)) + "_" + std::to_string(distribution(engine));

setenv("HYPRLAND_INSTANCE_SIGNATURE", m_szInstanceSignature.c_str(), true);

if (!std::filesystem::exists("/tmp/hypr"))
mkdir("/tmp/hypr", S_IRWXU | S_IRWXG | S_IRWXO | S_ISVTX);
else if (!std::filesystem::is_directory("/tmp/hypr")) {
std::cout << "Bailing out, /tmp/hypr is not a directory\n";
return;
}

const auto INSTANCEPATH = "/tmp/hypr/" + m_szInstanceSignature;
mkdir(INSTANCEPATH.c_str(), S_IRWXU | S_IRWXG);

if (std::filesystem::exists(INSTANCEPATH)) {
std::cout << "Bailing out, /tmp/hypr/$HIS exists??\n";
return;
}

if (mkdir(INSTANCEPATH.c_str(), S_IRWXU) < 0) {
std::cout << "Bailing out, couldn't create /tmp/hypr/$HIS\n";
return;
}

Debug::init(m_szInstanceSignature);

Expand Down

0 comments on commit f7815da

Please sign in to comment.