Skip to content

Commit

Permalink
Wrong NamedPipe name was used when elevating as another user. Fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerardo Grignoli committed Jan 8, 2020
1 parent a057b41 commit f1b999c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/gsudo/Commands/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Principal;
using System.Threading.Tasks;

namespace gsudo.Commands
Expand Down Expand Up @@ -104,6 +105,7 @@ public async Task<int> Execute()
{
Logger.Instance.Log($"Using Console mode {elevationRequest.Mode}", LogLevel.Debug);
var callingPid = GetCallingPid(currentProcess);
var callingSid = WindowsIdentity.GetCurrent().User.Value;
Logger.Instance.Log($"Caller ProcessId is {callingPid}", LogLevel.Debug);

var cmd = CommandToRun.FirstOrDefault();
Expand All @@ -130,7 +132,7 @@ public async Task<int> Execute()
Logger.Instance.Log("Elevating process...", LogLevel.Debug);

var dbg = GlobalSettings.Debug ? "--debug " : string.Empty;
using (var process = ProcessFactory.StartElevatedDetached(currentProcess.MainModule.FileName, $"{dbg}gsudoservice {callingPid} {GlobalSettings.LogLevel}", !GlobalSettings.Debug))
using (var process = ProcessFactory.StartElevatedDetached(currentProcess.MainModule.FileName, $"{dbg}gsudoservice {callingPid} {callingSid} {GlobalSettings.LogLevel}", !GlobalSettings.Debug))
{
Logger.Instance.Log("Elevated instance started.", LogLevel.Debug);
}
Expand All @@ -157,7 +159,6 @@ public async Task<int> Execute()
connection?.Dispose();
}
}

}

private static int GetCallingPid(Process currentProcess)
Expand Down
3 changes: 2 additions & 1 deletion src/gsudo/Commands/ServiceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace gsudo.Commands
class ServiceCommand : ICommand
{
public int allowedPid { get; set; }
public string allowedSid { get; set; }

public LogLevel? LogLvl { get; set; }

Expand Down Expand Up @@ -78,7 +79,7 @@ private static IProcessHost CreateProcessHost(ElevationRequest request)

private IRpcServer CreateServer()
{
return new NamedPipeServer(allowedPid);
return new NamedPipeServer(allowedPid, allowedSid);
}

private async Task<ElevationRequest> ReadElevationRequest(Stream dataPipe)
Expand Down
5 changes: 3 additions & 2 deletions src/gsudo/Helpers/ArgumentsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,15 @@ internal static ICommand ParseCommand(string[] args)
{
bool hasLoglevel = false;
LogLevel logLevel = LogLevel.Info;
if (args.Length>2)
if (args.Length>3)
{
hasLoglevel = Enum.TryParse<LogLevel>(args[2], true, out logLevel);
hasLoglevel = Enum.TryParse<LogLevel>(args[3], true, out logLevel);
}

return new ServiceCommand()
{
allowedPid = int.Parse(args[1], CultureInfo.InvariantCulture),
allowedSid = args[2],
LogLvl = hasLoglevel ? logLevel : (LogLevel?)null,
};
}
Expand Down
8 changes: 5 additions & 3 deletions src/gsudo/Rpc/NamedPipeServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,30 @@ namespace gsudo.Rpc
class NamedPipeServer : IRpcServer
{
private readonly int _allowedPid;
private readonly string _allowedSid;
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

public event EventHandler<Connection> ConnectionAccepted;
public event EventHandler<Connection> ConnectionClosed;

const int MAX_SERVER_INSTANCES = 20;

public NamedPipeServer(int AllowedPid)
public NamedPipeServer(int AllowedPid, string AllowedSid)
{
_allowedPid = AllowedPid;
_allowedSid = AllowedSid;
}

public async Task Listen()
{
var ps = new PipeSecurity();

ps.AddAccessRule(new PipeAccessRule(
WindowsIdentity.GetCurrent().User,
new SecurityIdentifier(_allowedSid),
PipeAccessRights.ReadWrite | PipeAccessRights.CreateNewInstance,
AccessControlType.Allow));

var pipeName = GetPipeName(_allowedPid);
var pipeName = GetPipeName(_allowedSid, _allowedPid);
Logger.Instance.Log($"Using named pipe {pipeName}.", LogLevel.Debug);

Logger.Instance.Log($"Access allowed only for ProcessID {_allowedPid} and childs", LogLevel.Debug);
Expand Down

0 comments on commit f1b999c

Please sign in to comment.