Repository navigation
Fix of 1211 and 1082 - #349
Conversation
1. fix of 1211 (scp works when powershell and cmd as default shell) 2. fix of 1082, 3. have the escaping logic in posix_spawn only4. Add program dir as part of path in process context so scp and sftp in…
| * we want to launch scp and sftp executables from the same binary directory | ||
| * that sshd is hosted in. This will facilitate hosting and evaluating | ||
| * multiple versions of OpenSSH at the same time. | ||
| */ |
There was a problem hiding this comment.
looks like this function is not prepending the current binary location to command. are these comments obsolete ?
There was a problem hiding this comment.
- we added current binary location to PATH
- If we prepend the path, some shells, like powershell will require shell specific additional characters (& for powershell) to be able to run scp, sftp. powershell -c "&
<full path to scp>"
There was a problem hiding this comment.
I will remove the comments
| return ret; | ||
| } | ||
|
|
||
| if (strstr(path, "system32\\cmd") || strstr(path, "ssh-shellhost")) |
There was a problem hiding this comment.
What if you do:
bash.exe -c c:\windows\system32\cmd.exe /c "echo hi"
There was a problem hiding this comment.
this case is an issue when the path is user define cmd which contains both exe and arguments. in next version, I use the last parameter to define if it is user defined cmd. If so, we don't change anything.
| memcpy(t, tmp, strlen(tmp)); | ||
| t += strlen(tmp); | ||
| } else { | ||
| tmp += strlen(exe_extenstion); /* move the pointer to the end of ".exe" */ |
There was a problem hiding this comment.
can you end up with an input command like the following here:
c:\windows\system32\cmd /c ping.exe localhost
If so, this logic would incorrectly identify the nested binary as the parent executable
There was a problem hiding this comment.
For the existing known shells and ssh executable, the first parts always ends with .exe. I don't see other ways to identify the file path (the first part) in the string.
There was a problem hiding this comment.
What about with proxyCommand or SSH_ASK_PASS ? Also fix spelling mistake in exe_extenstion.
There was a problem hiding this comment.
Yes, you are right.
| error("failed to allocation memory"); | ||
| return -1; | ||
| } | ||
| swprintf_s(path_new_value, path_new_len, L"%s%s%s", __wprogdir, path_value ? L";" : L"", path_value); |
There was a problem hiding this comment.
What's the confirmed order of exe lookup (we figured that what's on msdn is wrong) ? Does this guarantee picking up exe from progdir in all scenarios?
Are we OK with adjusting PATH for user's remote sessions?
There was a problem hiding this comment.
the msdn is correct, but the search order is for the executable, which is the first part of the command. but scp, sftp are the arguments part of the command.
So far, looks like it picked up binaries from progdir in all the scenarios.
I don't see a problem with adjusting PATH for user's remote session.
2. don't touch user defined cmd: no prepending, no escaping, no additional quotes 3. fix line ending
|
Manoj Ampalam (@manojampalam) , when you get time, please review the changes. Thanks! |
|
Manoj Ampalam (@manojampalam) Please review. currently only escape non-user defined commands from common shells. for other shells, no escaping, no -c added if shell option is not specified |
Manoj Ampalam (manojampalam)
left a comment
There was a problem hiding this comment.
comments inline...
| <ClInclude Include="$(OpenSSH-Src-Path)uuencode.h" /> | ||
| <ClInclude Include="$(OpenSSH-Src-Path)version.h" /> | ||
| <ClInclude Include="$(OpenSSH-Src-Path)xmalloc.h" /> | ||
| <ClInclude Include="$(OpenSSH-Src-Path)pal-doexec.c.h" /> |
| @@ -1,473 +1,474 @@ | |||
| <?xml version="1.0" encoding="utf-8"?> | |||
There was a problem hiding this comment.
still shows up different
| PSID get_sid(const char*); | ||
| int am_system(); | ||
| char* build_session_commandline(const char *, const char *, const char *); | ||
| char * build_command_string(const char * ); |
There was a problem hiding this comment.
you could get rid of this since this is internal to logic within w32_doexec.c
There was a problem hiding this comment.
Added back and keep them in misc_internal.h. we need to call them from w32_doexec.c and unit tests
| char * build_command_string(const char * ); | ||
| int is_conpty_supported(); | ||
| int exec_command_with_pty(wchar_t*, STARTUPINFOW*, PROCESS_INFORMATION*, int); | ||
| int exec_command_with_pty(int * pid, char* cmd, int in, int out, int err, unsigned int col, unsigned int row, int ttyfd); |
There was a problem hiding this comment.
This one too, you could get rid of this since this is internal to logic within w32_doexec.c
There was a problem hiding this comment.
I revert it back to misc.internal.h because the definition is in w32_pty.c and the caller is w32_doexe.c
| t += strlen(path); | ||
|
|
||
|
|
||
| /*For user defined cmd, no need to add quotes*/ |
There was a problem hiding this comment.
?? can you add some examples? What's a user defined command ?
| t += strlen(__progdir); | ||
| *t++ = '\\'; | ||
| } | ||
| /* |
There was a problem hiding this comment.
I'm not in favor of this. What issues are we hitting if we don't do this? If the module path has spaces in it, it is expected to be enclosed in quotes. This is a standard practice even on Unix.
There was a problem hiding this comment.
remove the log below this.
| while (*t1) | ||
| cmdline_len += (DWORD)strlen(*t1++) + 1 + 2; | ||
| while (*t1) { | ||
| if (!escape) |
There was a problem hiding this comment.
Recommend pulling all the commandline building logic into a separate utility helper and writing an exhaustive set of unit tests.
There was a problem hiding this comment.
separate the utility helper out.
added unit tests.
|
|
||
| memset(&si, 0, sizeof(STARTUPINFO)); | ||
| si.cb = sizeof(STARTUPINFO); | ||
| si.dwXSize = 5; |
There was a problem hiding this comment.
Why 5 ? Add comments if needed
There was a problem hiding this comment.
remove these two lines. It was from the existing codes.
| /* change current directory to sshd.exe root */ | ||
| _wchdir(__wprogdir); | ||
|
|
||
| _wdupenv_s(&path_value, &len, L"PATH"); |
There was a problem hiding this comment.
Please add comments on why we are doing this
2. Add registry key to indicate escaping is needed or not for shells other than ps, shellhost, cmd, cygwin, bash. Default is escape the arg string
…s createprocess failed. Remove the added quotes as failoever when arg is empty.
| @@ -1,473 +1,474 @@ | |||
| <?xml version="1.0" encoding="utf-8"?> | |||
There was a problem hiding this comment.
still shows up different
| *t = '\0'; | ||
|
|
||
| int ret = -1; | ||
| cmdline = build_commandline_string(cmd, argv, prepend_module_path); |
There was a problem hiding this comment.
what if build_commandline_string fails ?
| verbose("fork is not supported"); | ||
| return -1; | ||
| } | ||
| char * build_commandline_string(const char* cmd, char *const argv[], BOOLEAN prepend_module_path); |
There was a problem hiding this comment.
can this move to misc_internal.h
| else | ||
| b = CreateProcessW(NULL, t, NULL, NULL, TRUE, flags, NULL, NULL, &si, &pi); | ||
| *(cmdline_utf16 + wcslen(cmdline_utf16) - 1) = L'\0'; | ||
| /*failover only when double quotes is potentially added necessary*/ |
There was a problem hiding this comment.
you could simplify the below condition by putting this inside loop and getting rid of "num" and "t":
if ( !b && GetLastError() != ERROR_FILENOTFOUND && (argv == NULL || *argv == NULL) && cmd[0] != '"') {
cmdline_utf16++;
*(cmdline_utf16 + wcslen(cmdline_utf16) - 1) = L'\0';
}
break;
There was a problem hiding this comment.
I can get rid of num, but not t.
I need to free cmdline_utf16 at the end. the pointer can't change
| debug3("pty commandline: %ls", pty_cmdline); | ||
|
|
||
| if (!CreateProcessW(NULL, pty_cmdline, NULL, NULL, TRUE, 0, NULL, NULL, si, pi)) { | ||
| if (CreateProcessW(NULL, pty_cmdline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) { |
There was a problem hiding this comment.
Sorry, I may have missed this earlier but why can't we call posix_spawnp here ?
There was a problem hiding this comment.
for pty, the si setting from session (col, row, and dwflag) are different from posix_spawnp. There is no args for col, row, and dwflag to posix_spawnp. One option is change the signature of posix_spawn_internal to take si info and pass them to spawn_child_internal. and then call posix_spawn_internal at here.
| out = build_session_commandline("c:\\powershell", NULL, "sftp-server -arg"); | ||
| ASSERT_STRING_EQ(out, "\"c:\\powershell\" -c \"sftp-server.exe -arg\""); | ||
| char * | ||
| build_commandline_string(const char* cmd, char *const argv[], BOOLEAN prepend_module_path); |
There was a problem hiding this comment.
you can get rid of this by moving it to misc_internal
|
Manoj Ampalam (@manojampalam), please review the updated code |
PowerShell/Win32-OpenSSH#1211 PowerShell/Win32-OpenSSH#1082 Added support for posix_spawnp that executes the command directly instead of appending path. (SH_ASKPASS and proxy command use this). Refactored posix spawn commandline building logic to automatically account for Windows CRT escaping rules on all arguments.
Uh oh!
There was an error while loading. Please reload this page.