Bug 1693 - ssh prompts for passphrase even when identity file is unreadable
Summary: ssh prompts for passphrase even when identity file is unreadable
Status: CLOSED FIXED
Alias: None
Product: Portable OpenSSH
Classification: Unclassified
Component: ssh (show other bugs)
Version: 5.3p1
Hardware: All All
: P2 minor
Assignee: Assigned to nobody
URL:
Keywords:
Depends on:
Blocks: V_5_4
  Show dependency treegraph
 
Reported: 2010-01-08 03:29 AEDT by TJ Saunders
Modified: 2010-03-26 10:51 AEDT (History)
1 user (show)

See Also:


Attachments
Uses access(2) to check readability of identity file before loading (1008 bytes, patch)
2010-01-08 03:29 AEDT, TJ Saunders
no flags Details | Diff
log (to debug) the reason for failing to load a private key (1.45 KB, patch)
2010-01-11 15:16 AEDT, Darren Tucker
djm: ok+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description TJ Saunders 2010-01-08 03:29:21 AEDT
Created attachment 1767 [details]
Uses access(2) to check readability of identity file before loading

When using publickey authentication, the private key is loaded from an IdentityFile using ssh2connect.c's load_identity_file() function.  A stat(2) is used to ensure that the file exists, and then the key is loaded.

If the file exists, but the process does not have read permissions on that file, then ssh will prompt the user for a passphrase.  This is particularly confusing if the private key in question is not passphrase-protected.  The fact that the ssh client prompts for the passphrase is misleading; the underlying inability to load the private key stems not from the need for a passphrase, but from the lack of readability.  It would be better to display a "Permission denied" message to the user, and/or move on to the next authentication method, rather than needlessly prompting the user for passphrases in this case.

The attached patch attempts to remedy this situation by using access(2) after the stat(2) call.  If the file is not readable, then the private key cannot be loaded, and thus the call to key_load_private_type() is skipped.
Comment 1 Darren Tucker 2010-01-11 12:43:56 AEDT
Look at this for 5.4
Comment 2 Darren Tucker 2010-01-11 15:16:15 AEDT
Created attachment 1774 [details]
log (to debug) the reason for failing to load a private key

OK, so this is interesting: it doesn't happen on OpenBSD, but I do see it on Linux.

sshconnect2.c:load_identity_file() calls key_load_private_type.  If that fails and if we're either or in batchmode or if the key has bad perms then we quit, otherwise we prompt for a passphrase.  Or at least that's what it's supposed to do.

So why doesn't this actually happen on Linux?  perm_ok is on the stack, and if the open() in key_load_private_type fails then it returns early without setting (or unsetting) perm_ok.  Thus, whether or not you get prompted depends on the initial value of perm_ok from whatever happened to be on the stack.  Nasty.

This patch reports the reason if the open() fails and sets perms_ok appropriately, no need for the extra access() calls.  It also initializes perms_ok to the safe default.
Comment 3 Darren Tucker 2010-01-11 15:47:32 AEDT
Patch has been applied and will be in 5.4.  Thanks for the report.
Comment 4 TJ Saunders 2010-01-12 04:59:20 AEDT
Thanks for the analysis; I'd missed the reliance upon the stack-based value of perm_ok when trying to figure this out.  Cheers!
Comment 5 Darren Tucker 2010-03-26 10:51:47 AEDT
With the release of 5.4p1, this bug is now considered closed.