Skip to content

Commit a768c0f

Browse files
Merge pull request #785 from wolfSSL/devin/1740508226-sftp-state-var
Fix SFTP data truncation by moving sentSzSave to state structure
2 parents da85e49 + 502b5a6 commit a768c0f

File tree

2 files changed

+144
-3
lines changed

2 files changed

+144
-3
lines changed

.github/workflows/sftp-test.yml

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
name: wolfSSH SFTP Test
2+
3+
on:
4+
push:
5+
branches: [ '*' ]
6+
pull_request:
7+
branches: [ '*' ]
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
create_matrix:
15+
runs-on: ubuntu-latest
16+
outputs:
17+
versions: ${{ steps.json.outputs.versions }}
18+
steps:
19+
- name: Create wolfSSL version matrix
20+
id: json
21+
run: |
22+
current=`curl -s https://api.github.com/repos/wolfssl/wolfssl/releases | grep tag_name | cut -d : -f 2,3 | tr -d \" | tr -d , | tr -d ' ' | head -1`
23+
last=`curl -s https://api.github.com/repos/wolfssl/wolfssl/releases | grep tag_name | cut -d : -f 2,3 | tr -d \" | tr -d , | tr -d ' ' | head -2 | tail -1`
24+
VERSIONS=$(echo "[ \"master\", \"$current\", \"$last\" ]")
25+
echo "wolfSSL versions found: $VERSIONS"
26+
echo "versions=$VERSIONS" >> $GITHUB_OUTPUT
27+
28+
build_wolfssl:
29+
needs: create_matrix
30+
strategy:
31+
fail-fast: false
32+
matrix:
33+
os: [ ubuntu-latest ]
34+
wolfssl: ${{ fromJson(needs.create_matrix.outputs['versions']) }}
35+
name: Build wolfssl
36+
runs-on: ${{ matrix.os }}
37+
timeout-minutes: 4
38+
steps:
39+
- name: Checking cache for wolfssl
40+
uses: actions/cache@v4
41+
id: cache-wolfssl
42+
with:
43+
path: build-dir/
44+
key: wolfssh-sftp-wolfssl-${{ matrix.wolfssl }}-${{ matrix.os }}
45+
lookup-only: true
46+
47+
- name: Checkout, build, and install wolfssl
48+
if: steps.cache-wolfssl.outputs.cache-hit != 'true'
49+
uses: wolfSSL/actions-build-autotools-project@v1
50+
with:
51+
repository: wolfssl/wolfssl
52+
ref: ${{ matrix.wolfssl }}
53+
path: wolfssl
54+
configure: --enable-ssh
55+
check: false
56+
install: true
57+
58+
build_wolfssh:
59+
needs:
60+
- build_wolfssl
61+
- create_matrix
62+
strategy:
63+
fail-fast: false
64+
matrix:
65+
os: [ ubuntu-latest ]
66+
wolfssl: ${{ fromJson(needs.create_matrix.outputs['versions']) }}
67+
name: Build and test wolfsftp
68+
runs-on: ${{ matrix.os }}
69+
timeout-minutes: 10
70+
steps:
71+
- name: Checking cache for wolfssl
72+
uses: actions/cache@v4
73+
with:
74+
path: build-dir/
75+
key: wolfssh-sftp-wolfssl-${{ matrix.wolfssl }}-${{ matrix.os }}
76+
fail-on-cache-miss: true
77+
78+
- uses: actions/checkout@v4
79+
with:
80+
path: wolfssh/
81+
82+
- name: autogen
83+
working-directory: ./wolfssh/
84+
run: ./autogen.sh
85+
86+
- name: configure
87+
working-directory: ./wolfssh/
88+
run : |
89+
./configure --enable-sftp LDFLAGS="-L${{ github.workspace }}/build-dir/lib" CPPFLAGS="-I${{ github.workspace }}/build-dir/include -DWOLFSSH_NO_FPKI"
90+
91+
- name: make
92+
working-directory: ./wolfssh/
93+
run: make
94+
95+
- name: Create test file
96+
run: |
97+
dd if=/dev/urandom of=/tmp/test.dat bs=1M count=2
98+
echo "Created 2MB test file at /tmp/test.dat"
99+
md5sum /tmp/test.dat > /tmp/test.md5
100+
101+
- name: Start echoserver
102+
working-directory: ./wolfssh/
103+
run: |
104+
./examples/echoserver/echoserver -f &
105+
echo "Echoserver started with PID: $!"
106+
sleep 2 # Give the server time to start
107+
108+
- name: Run SFTP test
109+
working-directory: ./wolfssh/
110+
run: |
111+
mkdir -p /tmp/sftp_test_dir
112+
# Create expect script to automate the SFTP client interaction
113+
cat > /tmp/sftp_test.exp << 'EOF'
114+
#!/usr/bin/expect -f
115+
set timeout 60
116+
spawn ./examples/sftpclient/wolfsftp -N -h 127.0.0.1 -p 22222 -u jill
117+
expect "Password:"
118+
send "upthehill\r"
119+
expect "wolfSSH sftp>"
120+
send "put /tmp/test.dat /tmp/sftp_test_dir/test_received.dat\r"
121+
expect "wolfSSH sftp>"
122+
send "exit\r"
123+
expect eof
124+
EOF
125+
chmod +x /tmp/sftp_test.exp
126+
127+
# Install expect
128+
sudo apt-get update && sudo apt-get install -y expect
129+
130+
# Run the expect script
131+
/tmp/sftp_test.exp
132+
133+
# Verify the files match
134+
echo "Verifying file integrity..."
135+
if cmp -s /tmp/test.dat /tmp/sftp_test_dir/test_received.dat; then
136+
echo "SFTP Test PASSED: Files match"
137+
else
138+
echo "SFTP Test FAILED: Files do not match"
139+
exit 1
140+
fi

src/wolfsftp.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ typedef struct WS_SFTP_SEND_WRITE_STATE {
346346
WS_SFTP_BUFFER buffer;
347347
int maxSz;
348348
int sentSz;
349+
int sentSzSave;
349350
} WS_SFTP_SEND_WRITE_STATE;
350351

351352

@@ -7167,7 +7168,6 @@ int wolfSSH_SFTP_SendWritePacket(WOLFSSH* ssh, byte* handle, word32 handleSz,
71677168
WS_SFTP_SEND_WRITE_STATE* state = NULL;
71687169
int ret = WS_FATAL_ERROR;
71697170
int status;
7170-
int sentSzSave = 0;
71717171
byte type;
71727172

71737173
WLOG(WS_LOG_SFTP, "Entering wolfSSH_SFTP_SendWritePacket()");
@@ -7195,6 +7195,7 @@ int wolfSSH_SFTP_SendWritePacket(WOLFSSH* ssh, byte* handle, word32 handleSz,
71957195
case STATE_SEND_WRITE_INIT:
71967196
WLOG(WS_LOG_SFTP, "SFTP SEND_WRITE STATE: INIT");
71977197
state->sentSz = 0;
7198+
state->sentSzSave = 0;
71987199
if (wolfSSH_SFTP_buffer_create(ssh, &state->buffer,
71997200
handleSz + WOLFSSH_SFTP_HEADER + UINT32_SZ * 4) !=
72007201
WS_SUCCESS) {
@@ -7267,7 +7268,7 @@ int wolfSSH_SFTP_SendWritePacket(WOLFSSH* ssh, byte* handle, word32 handleSz,
72677268
continue;
72687269
}
72697270

7270-
sentSzSave += state->sentSz;
7271+
state->sentSzSave += state->sentSz;
72717272
if (inSz > (word32)state->sentSz) {
72727273
in += state->sentSz;
72737274
inSz -= state->sentSz;
@@ -7351,7 +7352,7 @@ int wolfSSH_SFTP_SendWritePacket(WOLFSSH* ssh, byte* handle, word32 handleSz,
73517352
ret = WS_SFTP_STATUS_NOT_OK;
73527353
}
73537354
if (ret >= WS_SUCCESS)
7354-
ret = sentSzSave;
7355+
ret = state->sentSzSave;
73557356
state->state = STATE_SEND_WRITE_CLEANUP;
73567357
NO_BREAK;
73577358

0 commit comments

Comments
 (0)