-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Restrict socket connections #9779
Draft
varjolintu
wants to merge
4
commits into
keepassxreboot:develop
Choose a base branch
from
varjolintu:feature/restrict_socket_connections
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "BrowserClientRestrictions.h" | ||
#include "core/FileHash.h" | ||
#include "core/Metadata.h" | ||
#include <QCryptographicHash> | ||
|
||
#ifdef Q_OS_MACOS | ||
#include <libproc.h> | ||
#include <sys/un.h> | ||
#elif Q_OS_LINUX | ||
#include <limits.h> | ||
#elif Q_OS_WIN | ||
#include <psapi.h> | ||
#elif Q_OS_FREEBSD | ||
#include <sys/socket.h> | ||
#include <sys/types.h> | ||
#include <sys/ucred.h> | ||
#include <sys/un.h> | ||
#include <sys/sysctl.h> | ||
#endif | ||
|
||
bool BrowserClientRestrictions::isClientProcessAllowed(const QSharedPointer<Database>& db, | ||
const ClientProcess& clientProcess) | ||
{ | ||
for (const auto& key : db->metadata()->customData()->keys()) { | ||
if (key.startsWith(CustomData::OptionPrefix + CustomData::BrowserAllowedProcessPrefix)) { | ||
auto strippedKey = key; | ||
strippedKey.remove(CustomData::OptionPrefix + CustomData::BrowserAllowedProcessPrefix); | ||
|
||
const auto value = db->metadata()->customData()->value(key); | ||
if (strippedKey == clientProcess.hash && value == clientProcess.path) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
ClientProcess BrowserClientRestrictions::getProcessPathAndHash(QLocalSocket* socket) | ||
{ | ||
if (!socket) { | ||
return {}; | ||
} | ||
|
||
ClientProcess clientProcess; | ||
int socketDesc = socket->socketDescriptor(); | ||
|
||
#ifdef Q_OS_MACOS | ||
pid_t pid; | ||
socklen_t pidSize = sizeof(pid); | ||
auto ret = getsockopt(socketDesc, SOL_LOCAL, LOCAL_PEERPID, &pid, &pidSize); | ||
if (ret == -1) { | ||
return {}; | ||
} | ||
|
||
char fullPath[PROC_PIDPATHINFO_MAXSIZE]; | ||
ret = proc_pidpath(pid, fullPath, sizeof(fullPath)); | ||
if (ret <= 0) { | ||
return {}; | ||
} | ||
|
||
clientProcess.path = fullPath; | ||
#elif Q_OS_WIN | ||
ULONG pid = 0; | ||
HANDLE socketHandle = reinterpret_cast<HANDLE>(socketDesc); | ||
auto res = GetNamedPipeClientProcessId(socketHandle, &pid); | ||
if (res == 0) { | ||
return {}; | ||
} | ||
|
||
HANDLE processHandle = NULL; | ||
processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); | ||
if (!processHandle) { | ||
return {}; | ||
} | ||
|
||
TCHAR fullPath[MAX_PATH]; | ||
if (GetModuleFileNameEx(processHandle, NULL, fullPath, MAX_PATH) == 0) { | ||
return {}; | ||
} | ||
|
||
CloseHandle(processHandle); | ||
clientProcess.path = fullPath; | ||
#elif Q_OS_LINUX | ||
pid_t pid; | ||
socklen_t pidSize = sizeof(pid); | ||
auto ret = getsockopt(socketDesc, SOL_SOCKET, SO_PEERCRED, &pid, &pidSize); | ||
if (ret == -1) { | ||
return {}; | ||
} | ||
|
||
// Get process path from PID | ||
char buf[PATH_MAX]; | ||
auto procPath = QString("/proc/%1/exe").arg(pid); | ||
auto fullPath = realpath(procPath.toStdString().c_str(), buf); | ||
if (!fullPath) { | ||
return {}; | ||
} | ||
|
||
clientProcess.path = fullPath; | ||
#elif Q_OS_FREEBSD | ||
struct xucred xucred; | ||
socklen_t xucredSize = sizeof(xucred); | ||
auto ret = getsockopt(socketDesc, SOL_LOCAL, LOCAL_PEERCRED, &xucred, &xucredSize); | ||
if (ret < 0) { | ||
return {}; | ||
} | ||
|
||
int mib[4]; | ||
mib[0] = CTL_KERN; | ||
mib[1] = KERN_PROC; | ||
mib[2] = KERN_PROC_PATHNAME; | ||
mib[3] = xucred.cr_pid; | ||
size_t bufSize; | ||
|
||
auto res = sysctl(mib, 4, nullptr, &bufSize, nullptr, 0); | ||
if (res < 0) { | ||
return {}; | ||
} | ||
|
||
char buf[bufSize + 1]; | ||
res = sysctl(mib, 4, &buf, &bufSize, nullptr, 0); | ||
if (res < 0) { | ||
return {}; | ||
} | ||
|
||
clientProcess.path = buf; | ||
#else | ||
|
||
#endif | ||
if (clientProcess.path.isEmpty()) { | ||
return {}; | ||
} | ||
|
||
clientProcess.hash = FileHash::getFileHash(clientProcess.path, QCryptographicHash::Md5, 8192); | ||
return clientProcess; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef BROWSERCLIENTRESTRICTIONS_H | ||
#define BROWSERCLIENTRESTRICTIONS_H | ||
|
||
#include "core/Database.h" | ||
#include <QLocalSocket> | ||
|
||
struct ClientProcess | ||
{ | ||
QString path; | ||
QString hash; | ||
}; | ||
|
||
class BrowserClientRestrictions | ||
{ | ||
public: | ||
explicit BrowserClientRestrictions() = default; | ||
|
||
static bool isClientProcessAllowed(const QSharedPointer<Database>& db, const ClientProcess& clientProcess); | ||
static ClientProcess getProcessPathAndHash(QLocalSocket* socket); | ||
}; | ||
|
||
#endif // BROWSERCLIENTRESTRICTIONS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, FdoSecrets does something similar for its client authorization. You can search its code for "proc" to find where that is. This should probably be moved to a common function in OSutils.
This whole PR is similar in concept to #6458, so there is likely to be some common functionality. Maybe a
ClientAunthenticator
class?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OSUtils is a fine place for this, yes. And I agree common functions should have just one class. Going to look at this again after those two required PR's for this one are merged.