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

Copy git ssh-id-wrapper to /tmp only if necessary (Fixes #10582, #19532) #43769

Merged
merged 1 commit into from Oct 10, 2017
Merged

Copy git ssh-id-wrapper to /tmp only if necessary (Fixes #10582, #19532) #43769

merged 1 commit into from Oct 10, 2017

Conversation

mtorromeo
Copy link
Contributor

@mtorromeo mtorromeo commented Sep 27, 2017

This adds a check that only copies the ssh wrapper to a temporary location if git is to be run by a specific user and at the same time the predefined wrapper file is not executable by "others" by verifying every path part for the others executable bit.

By doing this the temp file is kept only as a last resort which should work with salt-ssh as per bug #19532, while avoiding a needless copy on /tmp which could be potentially mounted with noexec as per bug #10582.

@mtorromeo
Copy link
Contributor Author

It would be nice to have this also backported to the stable branches if the fix is good.

Thanks!

Copy link
Contributor

@terminalmage terminalmage left a comment

Choose a reason for hiding this comment

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

One small nitpick, though functionally this looks good.

# Cleanup the temporary ssh wrapper file
if tmp_ssh_wrapper and os.path.exists(tmp_ssh_wrapper):
log.debug('Removing ssh wrapper file {0}'.format(tmp_ssh_wrapper))
__salt__['file.remove'](tmp_ssh_wrapper)
Copy link
Contributor

Choose a reason for hiding this comment

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

EAFP here. We can skip stat-ing the file by doing it like this:

try:
    __salt__['file.remove'](tmp_ssh_wrapper)
    log.debug('Removed ssh wrapper file %s', tmp_ssh_wrapper)
except AttributeError:
    # No wrapper was used
    pass
except (SaltInvocationError, CommandExecutionError) as exc:
    log.warning('Failed to remove ssh wrapper file %s: %s', tmp_ssh_wraper, exc)

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 can't say I like calling a function with an obviously wrong parameter. I would have kept at least a check if tmp_ssh_wrapper which does not require any syscall and would avoid a function call with consequent exception catch but I am happy to oblige.

Note that I wrote it this way because that's exactly how tmp_identity_file is removed a couple of lines below. Should I change that too?

Copy link
Contributor

Choose a reason for hiding this comment

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

I can't say I like calling a function with an obviously wrong parameter.

That's what exception handling is for.

https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Exceptions

@mtorromeo
Copy link
Contributor Author

PR updated

@rallytime
Copy link
Contributor

@mtorromeo There is one lint error here. Can you fix it? https://jenkins.saltstack.com/job/PR/job/salt-pr-lint-n/15309/violations/file/salt/modules/git.py/

Then we can get this in.

…#19532)

This adds a check that only copies the ssh wrapper to a temporary
location if git is to be run by a specific user and at the same time
the predefined wrapper file is not executable by "others" by verifying
every path part for the others executable bit.

By doing this the temp file is kept only as a last resort which should
work with salt-ssh as per bug #19532, while avoiding a needless copy
on /tmp which could be potentially mounted with noexec as per
bug #10582.
@mtorromeo
Copy link
Contributor Author

Oops, I copy-pasted the suggested change without checking the syntax first, sorry.
It should be fine now.

@rallytime rallytime merged commit a928dc9 into saltstack:develop Oct 10, 2017
@mtorromeo mtorromeo deleted the git-noexec-fix branch October 10, 2017 13:26
terminalmage pushed a commit that referenced this pull request Oct 23, 2017
@poliva83
Copy link

@mtorromeo @rallytime @terminalmage This change is introduced with 2017.7.3 and has caused regression in the git module for windows hosts. This is because line https://github.com/saltstack/salt/pull/43769/files#diff-59a19c9338fb59c2d90efccd4a0d618fR298 calls __salt__['file.remove'] passing in None. This causes an uncaught TypeError exception to occur here https://github.com/saltstack/salt/blob/v2017.7.3/salt/modules/win_file.py#L1059. You can produce the same error pretty easy on a windows host (see below).

PS C:\Users\olivap5> C:\salt\bin\python.exe
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> os.path.expanduser(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> import os
>>> os.path.expanduser(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\salt\bin\lib\ntpath.py", line 289, in expanduser
    if path[:1] != '~':
TypeError: 'NoneType' object has no attribute '__getitem__'

@mtorromeo
Copy link
Contributor Author

mtorromeo commented Feb 14, 2018

I had a check on tmp_ssh_wrapper in place but I was asked to remove it in the code review #43769 (review)

I still prefer to use an if tmp_ssh_wrapper: but the alternative is to also catch the TypeError exception

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.

None yet

4 participants