Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions keepercommander/plugins/awskey/aws_accesskey.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import logging
import shutil
from configparser import RawConfigParser
import os
from os.path import expandvars, expanduser, isfile

from botocore.exceptions import ClientError
Expand Down Expand Up @@ -137,6 +138,8 @@ def sync_with_creds_file(self):
shutil.copy2(creds_filename, backup_file)
with open(creds_filename, 'w') as f:
cp.write(f)
os.chmod(creds_filename, 0o600)
Copy link
Contributor

Choose a reason for hiding this comment

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

One concern — race condition (TOCTOU): The file is opened, written, closed, and then chmod'd. There is a brief window where the file exists at 0644 (or whatever the process umask dictates) before permissions are restricted. On a paranoid multi-user system, the correct fix would be to either:

Set a restrictive umask before open() and restore it afterward,
or
Open the file descriptor with os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) and wrap it with open(fd, 'w').

os.chmod(backup_file, 0o600)
logging.info(
f'Synced AWS key rotation with AWS credential file "{creds_filename}"'
f' and backed up original file to "{backup_file}"'
Expand Down
8 changes: 4 additions & 4 deletions keepercommander/plugins/ssh/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,27 @@ def rotate_ssh(host, port, user, old_password, new_password, timeout=5, revert=F
ia.send('passwd')
answer = ia.expect([r'(?i).*current.*password.*', r'(?i).*old.*password.*', r'(?i).*new.*password.*'])
result = ia.current_output
logging.debug('Output from passwd command: \"%s\"', result)
logging.debug('Rotation command responded (%d bytes)', len(result))
if answer < 0:
logging.debug('Unexpected response from the passwd command. Old password is assumed.')
if answer < 2:
ia.send(old_password)
logging.debug('Old Password sent')
ia.expect(r'(?i).*new.*password.*')
result = ia.current_output
logging.debug('Output from Old Password: \"%s\"', result)
logging.debug('Old credential prompt responded (%d bytes)', len(result))
ia.send(new_password)
logging.debug('New Password sent')
ia.expect(r'(?i).*new.*password.*')
result = ia.current_output
logging.debug('Output from New Password: \"%s\"', result)
logging.debug('New credential prompt responded (%d bytes)', len(result))
try:
ia.send(new_password)
logging.debug('New Password Again sent')
time.sleep(0.2)
ia.expect('.+')
result = ia.current_output
logging.debug('Output from New Password Again: \"%s\"', result)
logging.debug('Credential confirmation responded (%d bytes)', len(result))
results = []
lines = [x for x in result.splitlines() if x]
has_prompt = False
Expand Down
Loading