diff --git a/src/ioslaves/file/fdreceiver.cpp b/src/ioslaves/file/fdreceiver.cpp --- a/src/ioslaves/file/fdreceiver.cpp +++ b/src/ioslaves/file/fdreceiver.cpp @@ -70,9 +70,28 @@ { int client = ::accept(m_socketDes, NULL, NULL); if (client > 0) { - FDMessageHeader msg; - if (::recvmsg(client, msg.message(), 0) == 2) { - ::memcpy(&m_fileDes, CMSG_DATA(msg.cmsgHeader()), sizeof m_fileDes); + // Receive fd only if socket owner is root + bool acceptConnection = false; +#if defined(__linux__) + ucred cred; + socklen_t len = sizeof(cred); + if (getsockopt(client, SOL_SOCKET, SO_PEERCRED, &cred, &len) == 0 && cred.uid == 0) { + acceptConnection = true; + } +#elif defined(__FreeBSD__) || defined(__APPLE__) + uid_t uid; + gid_t gid; + if (getpeereid(m_socketDes, &uid, &gid) == 0 && uid == 0) { + acceptConnection = true; + } +#else +#warning Cannot get socket credentials! +#endif + if (acceptConnection) { + FDMessageHeader msg; + if (::recvmsg(client, msg.message(), 0) == 2) { + ::memcpy(&m_fileDes, CMSG_DATA(msg.cmsgHeader()), sizeof m_fileDes); + } } ::close(client); } diff --git a/src/ioslaves/file/file_unix.cpp b/src/ioslaves/file/file_unix.cpp --- a/src/ioslaves/file/file_unix.cpp +++ b/src/ioslaves/file/file_unix.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -40,6 +41,7 @@ #include #include +#include #include "fdreceiver.h" @@ -69,7 +71,8 @@ static const QString socketPath() { - return QStringLiteral("org_kde_kio_file_helper_%1").arg(getpid()); + const QString runtimeDir = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation); + return QStringLiteral("%1/filehelper%2%3").arg(runtimeDir).arg(KRandom::randomString(6)).arg(getpid()); } bool FileProtocol::privilegeOperationUnitTestMode()