Skip to content

Fix of 1211 and 1082 - #349

Merged
Manoj Ampalam (manojampalam) merged 36 commits into
PowerShell:latestw_allfrom
bingbing8:cmdescaping
Nov 5, 2018
Merged

Manoj Ampalam (manojampalam) merged 36 commits into
PowerShell:latestw_allfrom
bingbing8:cmdescaping

Conversation

@bingbing8

@bingbing8 Yanbing (bingbing8) commented Oct 12, 2018 •

Copy link
Copy Markdown
  1. fix of Unable to use scp with -oUserKnownHostsFile that has spaces Win32-OpenSSH#1211
  2. fix of Remote SSH commands require double escaping before hitting the DefaultShell Win32-OpenSSH#1082
  3. scp works when powershell and cmd as default shell
  4. have the escaping logic in posix_spawn only
  5. Add program dir as part of path in process context so scp and sftp in…
  6. only escape double quotes and backslash for common shells

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…
@bingbing8 Yanbing (bingbing8) changed the title (not ready for review yet) Fix of 1211 and 1082 Fix of 1211 and 1082 Oct 17, 2018
Comment thread contrib/win32/win32compat/misc.c Resolved
Comment thread contrib/win32/win32compat/misc.c Outdated
* 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.
*/

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

looks like this function is not prepending the current binary location to command. are these comments obsolete ?

@bingbing8 Yanbing (bingbing8) Oct 19, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

  1. we added current binary location to PATH
  2. 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>"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I will remove the comments

Comment thread contrib/win32/win32compat/w32fd.c Outdated
return ret;
}

if (strstr(path, "system32\\cmd") || strstr(path, "ssh-shellhost"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What if you do:
bash.exe -c c:\windows\system32\cmd.exe /c "echo hi"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch.

@bingbing8 Yanbing (bingbing8) Oct 22, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

Comment thread contrib/win32/win32compat/w32fd.c Outdated
memcpy(t, tmp, strlen(tmp));
t += strlen(tmp);
} else {
tmp += strlen(exe_extenstion); /* move the pointer to the end of ".exe" */

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

@manojampalam Manoj Ampalam (manojampalam) Oct 19, 2018 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What about with proxyCommand or SSH_ASK_PASS ? Also fix spelling mistake in exe_extenstion.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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?

@bingbing8 Yanbing (bingbing8) Oct 19, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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
@bingbing8

Copy link
Copy Markdown
Author

Manoj Ampalam (@manojampalam) , when you get time, please review the changes. Thanks!

@bingbing8

Yanbing (bingbing8) commented Oct 26, 2018 •

Copy link
Copy Markdown
Author

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

comments inline...

Comment thread contrib/win32/openssh/ssh.vcxproj Outdated
<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" />

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

pal-doexec.h

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

removed

@@ -1,473 +1,474 @@
<?xml version="1.0" encoding="utf-8"?>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fix CRLF

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

still shows up different

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

finally fixed it.

PSID get_sid(const char*);
int am_system();
char* build_session_commandline(const char *, const char *, const char *);
char * build_command_string(const char * );

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you could get rid of this since this is internal to logic within w32_doexec.c

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

removed

@bingbing8 Yanbing (bingbing8) Nov 3, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This one too, you could get rid of this since this is internal to logic within w32_doexec.c

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

removed

@bingbing8 Yanbing (bingbing8) Nov 3, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I revert it back to misc.internal.h because the definition is in w32_pty.c and the caller is w32_doexe.c

Comment thread contrib/win32/win32compat/w32fd.c Outdated
t += strlen(path);


/*For user defined cmd, no need to add quotes*/

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

?? can you add some examples? What's a user defined command ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

added

Comment thread contrib/win32/win32compat/w32fd.c Outdated
t += strlen(__progdir);
*t++ = '\\';
}
/*

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

remove the log below this.

Comment thread contrib/win32/win32compat/w32fd.c Outdated
while (*t1)
cmdline_len += (DWORD)strlen(*t1++) + 1 + 2;
while (*t1) {
if (!escape)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Recommend pulling all the commandline building logic into a separate utility helper and writing an exhaustive set of unit tests.

@bingbing8 Yanbing (bingbing8) Oct 31, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

separate the utility helper out.
added unit tests.

Comment thread contrib/win32/win32compat/win32_pty.c Outdated

memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwXSize = 5;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why 5 ? Add comments if needed

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please add comments on why we are doing this

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

added.

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
@@ -1,473 +1,474 @@
<?xml version="1.0" encoding="utf-8"?>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

still shows up different

Comment thread contrib/win32/win32compat/w32fd.c Outdated
*t = '\0';

int ret = -1;
cmdline = build_commandline_string(cmd, argv, prepend_module_path);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

what if build_commandline_string fails ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I will add check

verbose("fork is not supported");
return -1;
}
char * build_commandline_string(const char* cmd, char *const argv[], BOOLEAN prepend_module_path);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

can this move to misc_internal.h

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

sure

Comment thread contrib/win32/win32compat/w32fd.c Outdated
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*/

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sorry, I may have missed this earlier but why can't we call posix_spawnp here ?

@bingbing8 Yanbing (bingbing8) Nov 3, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you can get rid of this by moving it to misc_internal

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

sure

@bingbing8

Copy link
Copy Markdown
Author

Manoj Ampalam (@manojampalam), please review the updated code

@manojampalam
Manoj Ampalam (manojampalam) merged commit a75116b into PowerShell:latestw_all Nov 5, 2018
Hazel Duvall (hazelduvall) pushed a commit to hazelduvall/openssh-portable that referenced this pull request May 6, 2026
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.
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.

2 participants