Skip to content

Commit b9dfa04

Browse files
yosuke-wolfsslejohnstown
authored andcommitted
Add scpDirDepth as depth guard and Add unit tests
1 parent 3b867e7 commit b9dfa04

4 files changed

Lines changed: 349 additions & 0 deletions

File tree

src/internal.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,7 @@ WOLFSSH* SshInit(WOLFSSH* ssh, WOLFSSH_CTX* ctx)
13021302
ssh->scpRequestType = WOLFSSH_SCP_SINGLE_FILE_REQUEST;
13031303
ssh->scpIsRecursive = 0;
13041304
ssh->scpDirection = WOLFSSH_SCP_DIR_NONE;
1305+
ssh->scpDirDepth = 0;
13051306
#endif
13061307

13071308
#ifdef WOLFSSH_SFTP

src/wolfscp.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,13 +2005,19 @@ int wsScpRecvCallback(WOLFSSH* ssh, int state, const char* basePath,
20052005
NU_Done(&stat);
20062006
}
20072007
}
2008+
if (ret != WS_SCP_ABORT) {
2009+
ssh->scpDirDepth = 0;
2010+
}
20082011
#else
20092012
if (WCHDIR(ssh->fs, basePath) != 0) {
20102013
WLOG(WS_LOG_ERROR,
20112014
"scp: invalid destination directory, abort");
20122015
wolfSSH_SetScpErrorMsg(ssh, "invalid destination directory");
20132016
ret = WS_SCP_ABORT;
20142017
}
2018+
else {
2019+
ssh->scpDirDepth = 0;
2020+
}
20152021
#endif
20162022
break;
20172023

@@ -2138,30 +2144,48 @@ int wsScpRecvCallback(WOLFSSH* ssh, int state, const char* basePath,
21382144
WSTRNCAT((char*)basePath, "/", sizeof("/"));
21392145
WSTRNCAT((char*)basePath, fileName, WOLFSSH_MAX_FILENAME);
21402146
wolfSSH_CleanPath(ssh, (char*)basePath, WOLFSSH_MAX_FILENAME);
2147+
ssh->scpDirDepth++;
21412148
#else
21422149
if (WCHDIR(ssh->fs, fileName) != 0) {
21432150
WLOG(WS_LOG_ERROR,
21442151
"scp: unable to cd into directory, abort");
21452152
wolfSSH_SetScpErrorMsg(ssh, "unable to cd into directory");
21462153
ret = WS_SCP_ABORT;
21472154
}
2155+
else {
2156+
ssh->scpDirDepth++;
2157+
}
21482158
#endif
21492159
}
21502160
break;
21512161

21522162
case WOLFSSH_SCP_END_DIR:
21532163

2164+
/* abort if peer sent END_DIR without a matching NEW_DIR */
2165+
if (ssh->scpDirDepth == 0) {
2166+
WLOG(WS_LOG_ERROR,
2167+
"scp: end directory without matching start, abort");
2168+
wolfSSH_SetScpErrorMsg(ssh,
2169+
"end directory without matching start");
2170+
ret = WS_SCP_ABORT;
2171+
break;
2172+
}
2173+
21542174
/* cd out of directory */
21552175
#ifdef WOLFSSL_NUCLEUS
21562176
WSTRNCAT((char*)basePath, "/..", WOLFSSH_MAX_FILENAME - 1);
21572177
wolfSSH_CleanPath(ssh, (char*)basePath, WOLFSSH_MAX_FILENAME);
2178+
ssh->scpDirDepth--;
21582179
#else
21592180
if (WCHDIR(ssh->fs, "..") != 0) {
21602181
WLOG(WS_LOG_ERROR,
21612182
"scp: unable to cd out of directory, abort");
21622183
wolfSSH_SetScpErrorMsg(ssh, "unable to cd out of directory");
21632184
ret = WS_SCP_ABORT;
21642185
}
2186+
else {
2187+
ssh->scpDirDepth--;
2188+
}
21652189
#endif
21662190
break;
21672191

0 commit comments

Comments
 (0)