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

Fix arbitrary command injection in the Unix version of PlatformOpenInShellFn_DefaultImpl #7772

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
37 changes: 29 additions & 8 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14353,11 +14353,18 @@ static void SetClipboardTextFn_DefaultImpl(void* user_data_ctx, const char* text

//-----------------------------------------------------------------------------

#if defined(__APPLE__) && defined(TARGET_OS_IPHONE) && !defined(IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS)
#ifndef IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS
#if defined(__APPLE__) && TARGET_OS_IPHONE
#define IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS
#endif

#if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS)
#if defined(_WIN32) && defined(IMGUI_DISABLE_WIN32_FUNCTIONS)
#define IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS
#endif
#endif

#ifndef IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS
#ifdef _WIN32
#include <shellapi.h> // ShellExecuteA()
#ifdef _MSC_VER
#pragma comment(lib, "shell32")
Expand All @@ -14366,18 +14373,32 @@ static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext*, const char* path)
{
return (INT_PTR)::ShellExecuteA(NULL, "open", path, NULL, NULL, SW_SHOWDEFAULT) > 32;
}
#elif !defined(IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS)
#else
#include <sys/wait.h>
#include <unistd.h>
static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext*, const char* path)
{
#if __APPLE__
const char* open_executable = "open";
const char* args[] { "open", "--", path, NULL };
#else
const char* open_executable = "xdg-open";
const char* args[] { "xdg-open", path, NULL };
#endif
ImGuiTextBuffer buf;
buf.appendf("%s \"%s\"", open_executable, path);
return system(buf.c_str()) != -1;
pid_t pid = fork();
if (pid < 0)
return false;
else if (!pid)
{
execvp(args[0], const_cast<char **>(args));
exit(-1);
}
else
{
int status;
waitpid(pid, &status, 0);
return WEXITSTATUS(status) == 0;
}
}
#endif
#else
static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext*, const char*) { return false; }
#endif // Default shell handlers
Expand Down
Loading