Skip to content

Commit 14bf354

Browse files
committed
Fixing issue #258, bad keyfile permissions.
This spits out a warning to the console, but ideally it would also issue a warning to the GUI for those who didn't start it from the console. N.B. the warning is a one shot thing, since it fixes the problem in a way essentially undetectable in the future, so it should be done right if it is to be done at all. Maybe we should even disable all keys automatically if the keyfile is found in an insecure state.
1 parent 94835ab commit 14bf354

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

src/shared.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import random
2222
import highlevelcrypto
2323
import shared
24+
import stat
2425

2526
config = ConfigParser.SafeConfigParser()
2627
myECCryptorObjects = {}
@@ -196,8 +197,10 @@ def reloadMyAddressHashes():
196197
myAddressesByHash.clear()
197198
#myPrivateKeys.clear()
198199
configSections = config.sections()
200+
hasExistingKeys = False
199201
for addressInKeysFile in configSections:
200202
if addressInKeysFile <> 'bitmessagesettings':
203+
hasExistingKeys = True
201204
isEnabled = config.getboolean(addressInKeysFile, 'enabled')
202205
if isEnabled:
203206
status,addressVersionNumber,streamNumber,hash = decodeAddress(addressInKeysFile)
@@ -208,6 +211,7 @@ def reloadMyAddressHashes():
208211
myAddressesByHash[hash] = addressInKeysFile
209212
else:
210213
sys.stderr.write('Error in reloadMyAddressHashes: Can\'t handle address versions other than 2 or 3.\n')
214+
fixKeyfilePermissions(appdata + 'keys.dat', hasExistingKeys)
211215

212216
def reloadBroadcastSendersForWhichImWatching():
213217
printLock.acquire()
@@ -298,3 +302,26 @@ def fixPotentiallyInvalidUTF8Data(text):
298302
except:
299303
output = 'Part of the message is corrupt. The message cannot be displayed the normal way.\n\n' + repr(text)
300304
return output
305+
306+
# Fix keyfile permissions due to inappropriate umask during keys.dat creation.
307+
def fixKeyfilePermissions(keyfile, hasExistingKeys):
308+
present_keyfile_permissions = os.stat(keyfile)[0]
309+
keyfile_disallowed_permissions = stat.S_IRWXG | stat.S_IRWXO
310+
if (present_keyfile_permissions & keyfile_disallowed_permissions) != 0:
311+
allowed_keyfile_permissions = ((1<<32)-1) ^ keyfile_disallowed_permissions
312+
new_keyfile_permissions = (
313+
allowed_keyfile_permissions & present_keyfile_permissions)
314+
os.chmod(keyfile, new_keyfile_permissions)
315+
if hasExistingKeys:
316+
print
317+
print '******************************************************************'
318+
print '** !! WARNING !! **'
319+
print '******************************************************************'
320+
print '** Possibly major security problem: **'
321+
print '** Your keyfiles were vulnerable to being read by other users **'
322+
print '** (including some untrusted daemons). You may wish to consider **'
323+
print '** generating new keys and discontinuing use of your old ones. **'
324+
print '** The problem has been automatically fixed. **'
325+
print '******************************************************************'
326+
print
327+

0 commit comments

Comments
 (0)