Skip to content

Commit

Permalink
SFTP Zero Byte Files
Browse files Browse the repository at this point in the history
1. When getting a file with SFTP, the client should check that the
   requested file is a regular file based on its attributes.
2. Add the attributes to check in the permissions.
3. Add a new error for a non-regular file.
  • Loading branch information
ejohnstown committed Dec 5, 2023
1 parent c5cb920 commit 6aad26d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions examples/sftpclient/sftpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,12 @@ static int doCmds(func_args* args)
#endif

if (ret != WS_SUCCESS) {
if (wolfSSH_get_error(ssh) == WS_SFTP_NOT_FILE_E) {
if (SFTP_FPUTS(args, "Not a regular file\n") < 0) {
err_msg("fputs error");
return -1;
}
}
if (SFTP_FPUTS(args, "Error getting file\n") < 0) {
err_msg("fputs error");
return -1;
Expand Down
3 changes: 3 additions & 0 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ const char* GetErrorString(int err)
case WS_KEY_FORMAT_E:
return "key format wrong error";

case WS_SFTP_NOT_FILE_E:
return "not a regular file";

default:
return "Unknown error code";
}
Expand Down
8 changes: 8 additions & 0 deletions src/wolfsftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -8473,6 +8473,14 @@ int wolfSSH_SFTP_Get(WOLFSSH* ssh, char* from,
state->state = STATE_GET_CLEANUP;
continue;
}
if ((state->attrib.per & FILEATRB_PER_MASK_TYPE)
!= FILEATRB_PER_FILE) {
WLOG(WS_LOG_SFTP, "Not a file");
ssh->error = WS_SFTP_NOT_FILE_E;
ret = WS_FATAL_ERROR;
state->state = STATE_GET_CLEANUP;
continue;
}
state->handleSz = WOLFSSH_MAX_HANDLE;
state->state = STATE_GET_OPEN_REMOTE;
NO_BREAK;
Expand Down
3 changes: 2 additions & 1 deletion wolfssh/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ enum WS_ErrorCodes {
WS_KEY_AUTH_MAGIC_E = -1090, /* OpenSSH key auth magic check fail */
WS_KEY_CHECK_VAL_E = -1091, /* OpenSSH key check value fail */
WS_KEY_FORMAT_E = -1092, /* OpenSSH key format fail */
WS_SFTP_NOT_FILE_E = -1093, /* Not a regular file */

WS_LAST_E = -1092 /* Update this to indicate last error */
WS_LAST_E = -1093 /* Update this to indicate last error */
};


Expand Down
7 changes: 7 additions & 0 deletions wolfssh/wolfsftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ struct WS_SFTP_FILEATRB_EX {
WS_SFTP_FILEATRB_EX* next;
};

#define FILEATRB_PER_MASK_TYPE 0770000
#define FILEATRB_PER_FILE 0100000
#define FILEATRB_PER_DEV_CHAR 0020000
#define FILEATRB_PER_DIR 0040000
#define FILEATRB_PER_DEV_BLOCK 0060000
#define FILEATRB_PER_MASK_PERM 0000777

typedef struct WS_SFTP_FILEATRB {
word32 flags;
word32 sz[2]; /* sz[0] being the lower and sz[1] being the upper */
Expand Down

0 comments on commit 6aad26d

Please sign in to comment.