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

Update LanguageServerHost to use NamedPipe #69816

Closed
wants to merge 7 commits into from

Conversation

beccamc
Copy link
Contributor

@beccamc beccamc commented Sep 5, 2023

Current communication between client and the Rolsyn language server is writing to STDOUT. This makes the connection vulnerable to any code in the process writing to stdout stream and corrupting the data.

We have seen at least one bug with this problem: microsoft/vscode-dotnettools#333

The fix it to use a named pipe to communicate. Two things to note about this...

  • The named pipe connection is created by the client. It's a little confusing, but the language client creates the named pipe server, and the language server actually creates a named pipe client.
  • The connection string format for .NET code is different than the connection string format for nodejs. We pass the named pipe name only, and then construct the appropriate connection string. If you try to use the same connection string it will fail silently.

@beccamc beccamc requested a review from a team as a code owner September 5, 2023 18:12
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Sep 5, 2023
@beccamc
Copy link
Contributor Author

beccamc commented Sep 5, 2023

Do we have any tests for this component? I don't see any...

@dibarbet
Copy link
Member

dibarbet commented Sep 5, 2023

Do we have any tests for this component? I don't see any...

We don't for the program component itself, only for the component that we pass the streams to. We likely should but I haven't gotten to it.

Comment on lines 226 to 229
static string GetDotNetPipeConnectionString(string pipeName)
{
return "\\\\.\\" + pipeName;
}
Copy link
Member

Choose a reason for hiding this comment

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

Does this syntax also work on Mac/Linux pipes? Or should we expect that the host is providing the Win32 name?

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 am not sure! Attempting to test on mac now

var pipeNameOption = new CliOption<string>("--pipe")
{
Description = "The name of the pipe to use for RPC with the parent process.",
Required = true
Copy link
Member

Choose a reason for hiding this comment

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

Should we still allow for stdin/stdout for a bit longer, if only so that way we don't have to merge/flow this simultaneously with the VS Code side of things?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

David and I discussed this, and determined it would be fine to merge both PRs close together. The only risk is that someone else updates roslyn version before I get PR in, correct?

Copy link
Member

@dibarbet dibarbet left a comment

Choose a reason for hiding this comment

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

One thing we should change, otherwise looks good.

@@ -100,7 +100,11 @@ static async Task RunAsync(ServerConfiguration serverConfiguration, Cancellation
// TODO: Remove, the path should match exactly. Workaround for https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1830914.
Microsoft.CodeAnalysis.EditAndContinue.EditAndContinueMethodDebugInfoReader.IgnoreCaseWhenComparingDocumentNames = Path.DirectorySeparatorChar == '\\';

var server = new LanguageServerHost(Console.OpenStandardInput(), Console.OpenStandardOutput(), exportProvider, loggerFactory.CreateLogger(nameof(LanguageServerHost)));
// Named pipe server is actually created from the client, so here we create a client stream.
var pipeServer = new NamedPipeClientStream(serverName: ".", serverConfiguration.PipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dibarbet PipeOptions.CurrentUserOnly isn't working. It connects but doesn't send any messages. I could investigate further (I don't know if the issue is between .net and nodjs?) but I don't know if it's worth the time. I don't think this is less secure than the stdin/out was? Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if the client is not creating a pipe that applies to the current user.

Honestly I'm not super familiar with named pipes - but it seems like a lower privileged user could hijack the pipe name and then get access to files from the server that they normally couldn't get. I'm not sure it would be as easy to hijack stdin/out. But maybe it doesn't matter if the pipe is created in the user's tmp dir?

But I don't really know much about named pipes. Not sure @jaredpar might have additional insight?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is less secure than the stdin/out was?

You can't get a handle to another processes stdin/out, but depending on how the pipe is created, this could be. This is a pretty common source of security bugs in the Linux world, especially since /tmp is world-accessible. The potential attack here is imagine you have two users on a machine, one launches VS Code. The other malicious user sees the pipe get created, and connects to it prior to our LSP connecting to it. In that case, you're now able to send LSP messages to the other user's client, which could be potentially bad/broken. (Imagine getting didOpen for files you can't otherwise read, or sending LSP messages back which executes stuff on the VS Code side...)

It's entirely possible we're OK here if the pipe creation API is already doing the right thing, but we'd need to validate that.

Copy link
Member

Choose a reason for hiding this comment

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

I also don't see a way to configure access on the nodejs side - https://nodejs.org/api/net.html#netcreateserveroptions-connectionlistener

Copy link
Member

Choose a reason for hiding this comment

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

Found it here - https://nodejs.org/api/net.html#serverlistenoptions-callback - looks like the default should be current user, so that looks ok (unless vscode is launching as a different user)

Copy link
Member

Choose a reason for hiding this comment

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

Where do you see the default as current user?

Copy link
Member

@dibarbet dibarbet Sep 7, 2023

Choose a reason for hiding this comment

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

Under server.listen options (from https://nodejs.org/api/net.html#serverlistenoptions-callback ), e.g.
image

@beccamc beccamc closed this Sep 12, 2023
@beccamc
Copy link
Contributor Author

beccamc commented Sep 12, 2023

closed in favor of #69918

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants