Skip to content

Commit

Permalink
libssh2: replace access() with stat()
Browse files Browse the repository at this point in the history
Prefer `stat()` to verify the presence of key files.

This drops the last uses of `access()` in the codebase, which was
reported to cause issues in some cases.

Also add `access()` to the list of banned functions in checksrc.

Ref: curl#13412 (comment)
Ref: curl#13482 (comment)
Ref: curl#13497
Co-authored-by: Jay Satiro
Closes curl#13498
  • Loading branch information
vszakats committed Apr 30, 2024
1 parent 7f7ad97 commit 602fc21
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/vssh/libssh2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1086,19 +1086,20 @@ static CURLcode ssh_statemach_act(struct Curl_easy *data, bool *block)
/* To ponder about: should really the lib be messing about with the
HOME environment variable etc? */
char *home = curl_getenv("HOME");
struct_stat sbuf;

/* If no private key file is specified, try some common paths. */
if(home) {
/* Try ~/.ssh first. */
sshc->rsa = aprintf("%s/.ssh/id_rsa", home);
if(!sshc->rsa)
out_of_memory = TRUE;
else if(access(sshc->rsa, R_OK) != 0) {
else if(stat(sshc->rsa, &sbuf)) {
Curl_safefree(sshc->rsa);
sshc->rsa = aprintf("%s/.ssh/id_dsa", home);
if(!sshc->rsa)
out_of_memory = TRUE;
else if(access(sshc->rsa, R_OK) != 0) {
else if(stat(sshc->rsa, &sbuf)) {
Curl_safefree(sshc->rsa);
}
}
Expand All @@ -1107,10 +1108,10 @@ static CURLcode ssh_statemach_act(struct Curl_easy *data, bool *block)
if(!out_of_memory && !sshc->rsa) {
/* Nothing found; try the current dir. */
sshc->rsa = strdup("id_rsa");
if(sshc->rsa && access(sshc->rsa, R_OK) != 0) {
if(sshc->rsa && stat(sshc->rsa, &sbuf)) {
Curl_safefree(sshc->rsa);
sshc->rsa = strdup("id_dsa");
if(sshc->rsa && access(sshc->rsa, R_OK) != 0) {
if(sshc->rsa && stat(sshc->rsa, &sbuf)) {
Curl_safefree(sshc->rsa);
/* Out of guesses. Set to the empty string to avoid
* surprising info messages. */
Expand Down
3 changes: 2 additions & 1 deletion scripts/checksrc.pl
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,8 @@ sub scanfile {
strtok|
v?sprintf|
(str|_mbs|_tcs|_wcs)n?cat|
LoadLibrary(Ex)?(A|W)?)
LoadLibrary(Ex)?(A|W)?|
access)
\s*\(
/x) {
checkwarn("BANNEDFUNC",
Expand Down

0 comments on commit 602fc21

Please sign in to comment.