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

KDSingleApplication: Do not exceed maximum socket name length #45

Merged
merged 4 commits into from
Dec 28, 2024

Conversation

jonaski
Copy link
Contributor

@jonaski jonaski commented Nov 15, 2024

In cases where there is a long name given (usually the application name) or a long username, QLocalServer::listen fails because sockaddr_un.sun_path has a char array of 108 on Linux, and 104 on BSD/macOS. On macOS, the TMPDIR path is long, ie.: /var/folders/zg/b242mhvd125344h5zrnpkzk80000gn/T/, so 49 characters just for the temp path alone. To fix this, use the uid instead of the username if the total length of the socket name is expected to exceed the maximum length. If it still exceeds the maximum allowed length, ie.: the given name is too long, chop the socket name which cuts off the last part of the name.

I've tested this to work on both Linux and macOS, Windows should be unaffected by these changes.

Fixes strawberrymusicplayer/strawberry#1603

@jonaski jonaski force-pushed the socketname_length branch 3 times, most recently from 6a7b309 to 62df868 Compare November 15, 2024 23:10
@jonaski
Copy link
Contributor Author

jonaski commented Nov 15, 2024

The macOS CI is broken because macos-latest is arm64 based now, while you install x86_64 based Qt. It needs to be changed to macos-13, or install arm64 based Qt.

@jonaski
Copy link
Contributor Author

jonaski commented Nov 15, 2024

#46 fixes the macOS build

@iamsergio
Copy link
Contributor

any chance to get a minimal repro committed as a test ? (something that fails without this patch)

jonaski and others added 2 commits December 12, 2024 02:29
In cases where there is a long name given (usually the application name) or a long username, `QLocalServer::listen` fails because `sockaddr_un.sun_path` has a char array of 108 on Linux, and 104 on BSD/macOS.
On macOS, the TMPDIR path is long, ie.: `/var/folders/zg/b242mhvd125344h5zrnpkzk80000gn/T/`, so 49 characters just for the temp path alone.
To fix this, use the uid instead of the username if the total length of the socket name is expected to exceed the maximum length.
If it still exceeds the maximum allowed length, ie.: the given name is too long, chop the socket name which cuts off the last part of the name.

I've tested this to work on both Linux and macOS, Windows should be unaffected by these changes.

Fixes strawberrymusicplayer/strawberry#1603
@jonaski
Copy link
Contributor Author

jonaski commented Dec 12, 2024

@iamsergio I added a test now

@iamsergio
Copy link
Contributor

thanks! seems to be failing though ? on windows Qt 5

@jonaski
Copy link
Contributor Author

jonaski commented Dec 12, 2024

thanks! seems to be failing though ? on windows Qt 5

Fixed now

@jonaski
Copy link
Contributor Author

jonaski commented Dec 28, 2024

Ping?

@nicolasfella nicolasfella merged commit 8690284 into KDAB:master Dec 28, 2024
7 checks passed
@nicolasfella
Copy link
Contributor

Thanks!

@jonaski jonaski deleted the socketname_length branch December 28, 2024 21:51
Copy link
Contributor

@dangelog dangelog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a complete review, just some comments.

@@ -30,6 +30,9 @@
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#if defined(Q_OS_LINUX)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be made generic for Unix, rather than Linux-specific? How about #include <sys/un.h> (man 7 unix I guess).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#53

constexpr int maxSocketNameLength = UNIX_PATH_MAX - 1;
#else
constexpr int maxSocketNameLength = 103; // BSD and macOS
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This then could become sizeof(sockaddr_un::sun_path)

m_socketName += sessionId;
}
sessionId = qEnvironmentVariable("XDG_SESSION_ID");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(style) AFAICS, no braces on single line "bodies" (if/for/while/...) is used around the rest of the project...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix

m_socketName += QStringLiteral("-");
m_socketName += name;

int fullSocketNameLength = tempPathLength + m_socketName.length();
#if defined(Q_OS_LINUX) || defined(Q_OS_QNX)
fullSocketNameLength += 1; // PlatformSupportsAbstractNamespace, see qlocalserver_unix.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using the abstract namespace?

qCDebug(kdsaLocalSocket) << "Chopping socket name because it is longer than" << maxSocketNameLength;
m_socketName.chop(fullSocketNameLength - maxSocketNameLength);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruminations.

  • Is it better to chop and possibly getting a clash (multiple independent applications may accidentally use the same socket), or fail to listen?
  • Although, if we fail to listen, it's unclear what the user could do to address the problem.
  • We're doing a lot of work in here, but this "determining the max length" should really be exposed as an API on QLocalServer/QLocalSocket...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Strawberry 1.1.3 crashes on launch (Paid)
4 participants