Skip to content

Commit ac28329

Browse files
committed
Fix bug #69545 - avoid overflow when reading list
1 parent 95fa727 commit ac28329

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

ext/ftp/ftp.c

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ ftp_close(ftpbuf_t *ftp)
188188
SSL_shutdown(ftp->ssl_handle);
189189
SSL_free(ftp->ssl_handle);
190190
}
191-
#endif
191+
#endif
192192
closesocket(ftp->fd);
193-
}
193+
}
194194
ftp_gc(ftp);
195195
efree(ftp);
196196
return NULL;
@@ -262,23 +262,23 @@ ftp_login(ftpbuf_t *ftp, const char *user, const char *pass TSRMLS_DC)
262262
if (!ftp_getresp(ftp)) {
263263
return 0;
264264
}
265-
265+
266266
if (ftp->resp != 234) {
267267
if (!ftp_putcmd(ftp, "AUTH", "SSL")) {
268268
return 0;
269269
}
270270
if (!ftp_getresp(ftp)) {
271271
return 0;
272272
}
273-
273+
274274
if (ftp->resp != 334) {
275275
return 0;
276276
} else {
277277
ftp->old_ssl = 1;
278278
ftp->use_ssl_for_data = 1;
279279
}
280280
}
281-
281+
282282
ctx = SSL_CTX_new(SSLv23_client_method());
283283
if (ctx == NULL) {
284284
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create the SSL context");
@@ -325,8 +325,8 @@ ftp_login(ftpbuf_t *ftp, const char *user, const char *pass TSRMLS_DC)
325325
if (!ftp_getresp(ftp)) {
326326
return 0;
327327
}
328-
329-
ftp->use_ssl_for_data = (ftp->resp >= 200 && ftp->resp <=299);
328+
329+
ftp->use_ssl_for_data = (ftp->resp >= 200 && ftp->resp <=299);
330330
}
331331
}
332332
#endif
@@ -360,7 +360,7 @@ ftp_reinit(ftpbuf_t *ftp)
360360
{
361361
if (ftp == NULL) {
362362
return 0;
363-
}
363+
}
364364

365365
ftp_gc(ftp);
366366

@@ -395,7 +395,7 @@ ftp_syst(ftpbuf_t *ftp)
395395
if (!ftp_putcmd(ftp, "SYST", NULL)) {
396396
return NULL;
397397
}
398-
if (!ftp_getresp(ftp) || ftp->resp != 215) {
398+
if (!ftp_getresp(ftp) || ftp->resp != 215) {
399399
return NULL;
400400
}
401401
syst = ftp->inbuf;
@@ -431,14 +431,14 @@ ftp_pwd(ftpbuf_t *ftp)
431431
if (!ftp_putcmd(ftp, "PWD", NULL)) {
432432
return NULL;
433433
}
434-
if (!ftp_getresp(ftp) || ftp->resp != 257) {
434+
if (!ftp_getresp(ftp) || ftp->resp != 257) {
435435
return NULL;
436436
}
437437
/* copy out the pwd from response */
438-
if ((pwd = strchr(ftp->inbuf, '"')) == NULL) {
438+
if ((pwd = strchr(ftp->inbuf, '"')) == NULL) {
439439
return NULL;
440440
}
441-
if ((end = strrchr(++pwd, '"')) == NULL) {
441+
if ((end = strrchr(++pwd, '"')) == NULL) {
442442
return NULL;
443443
}
444444
ftp->pwd = estrndup(pwd, end - pwd);
@@ -608,7 +608,7 @@ ftp_chmod(ftpbuf_t *ftp, const int mode, const char *filename, const int filenam
608608
if (!ftp_getresp(ftp) || ftp->resp != 200) {
609609
return 0;
610610
}
611-
611+
612612
return 1;
613613
}
614614
/* }}} */
@@ -625,7 +625,7 @@ ftp_alloc(ftpbuf_t *ftp, const long size, char **response)
625625
}
626626

627627
snprintf(buffer, sizeof(buffer) - 1, "%ld", size);
628-
628+
629629
if (!ftp_putcmd(ftp, "ALLO", buffer)) {
630630
return 0;
631631
}
@@ -642,7 +642,7 @@ ftp_alloc(ftpbuf_t *ftp, const long size, char **response)
642642
return 0;
643643
}
644644

645-
return 1;
645+
return 1;
646646
}
647647
/* }}} */
648648

@@ -674,7 +674,7 @@ ftp_type(ftpbuf_t *ftp, ftptype_t type)
674674
if (ftp == NULL) {
675675
return 0;
676676
}
677-
if (type == ftp->type) {
677+
if (type == ftp->type) {
678678
return 1;
679679
}
680680
if (type == FTPTYPE_ASCII) {
@@ -765,7 +765,7 @@ ftp_pasv(ftpbuf_t *ftp, int pasv)
765765
if (!ftp_putcmd(ftp, "PASV", NULL)) {
766766
return 0;
767767
}
768-
if (!ftp_getresp(ftp) || ftp->resp != 227) {
768+
if (!ftp_getresp(ftp) || ftp->resp != 227) {
769769
return 0;
770770
}
771771
/* parse out the IP and port */
@@ -808,7 +808,7 @@ ftp_get(ftpbuf_t *ftp, php_stream *outstream, const char *path, ftptype_t type,
808808
if ((data = ftp_getdata(ftp TSRMLS_CC)) == NULL) {
809809
goto bail;
810810
}
811-
811+
812812
ftp->data = data;
813813

814814
if (resumepos > 0) {
@@ -902,7 +902,7 @@ ftp_put(ftpbuf_t *ftp, const char *path, php_stream *instream, ftptype_t type, l
902902
if ((data = ftp_getdata(ftp TSRMLS_CC)) == NULL) {
903903
goto bail;
904904
}
905-
ftp->data = data;
905+
ftp->data = data;
906906

907907
if (startpos > 0) {
908908
snprintf(arg, sizeof(arg), "%ld", startpos);
@@ -1103,7 +1103,7 @@ ftp_putcmd(ftpbuf_t *ftp, const char *cmd, const char *args)
11031103

11041104
if (strpbrk(cmd, "\r\n")) {
11051105
return 0;
1106-
}
1106+
}
11071107
/* build the output buffer */
11081108
if (args && args[0]) {
11091109
/* "cmd args\r\n\0" */
@@ -1252,7 +1252,7 @@ my_send(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len)
12521252
#if HAVE_OPENSSL_EXT
12531253
if (ftp->use_ssl && ftp->fd == s && ftp->ssl_active) {
12541254
sent = SSL_write(ftp->ssl_handle, buf, size);
1255-
} else if (ftp->use_ssl && ftp->fd != s && ftp->use_ssl_for_data && ftp->data->ssl_active) {
1255+
} else if (ftp->use_ssl && ftp->fd != s && ftp->use_ssl_for_data && ftp->data->ssl_active) {
12561256
sent = SSL_write(ftp->data->ssl_handle, buf, size);
12571257
} else {
12581258
#endif
@@ -1292,14 +1292,14 @@ my_recv(ftpbuf_t *ftp, php_socket_t s, void *buf, size_t len)
12921292
#if HAVE_OPENSSL_EXT
12931293
if (ftp->use_ssl && ftp->fd == s && ftp->ssl_active) {
12941294
nr_bytes = SSL_read(ftp->ssl_handle, buf, len);
1295-
} else if (ftp->use_ssl && ftp->fd != s && ftp->use_ssl_for_data && ftp->data->ssl_active) {
1295+
} else if (ftp->use_ssl && ftp->fd != s && ftp->use_ssl_for_data && ftp->data->ssl_active) {
12961296
nr_bytes = SSL_read(ftp->data->ssl_handle, buf, len);
12971297
} else {
12981298
#endif
12991299
nr_bytes = recv(s, buf, len, 0);
13001300
#if HAVE_OPENSSL_EXT
13011301
}
1302-
#endif
1302+
#endif
13031303
return (nr_bytes);
13041304
}
13051305
/* }}} */
@@ -1516,7 +1516,7 @@ data_accept(databuf_t *data, ftpbuf_t *ftp TSRMLS_DC)
15161516

15171517
data_accepted:
15181518
#if HAVE_OPENSSL_EXT
1519-
1519+
15201520
/* now enable ssl if we need to */
15211521
if (ftp->use_ssl && ftp->use_ssl_for_data) {
15221522
ctx = SSL_CTX_new(SSLv23_client_method());
@@ -1536,23 +1536,23 @@ data_accept(databuf_t *data, ftpbuf_t *ftp TSRMLS_DC)
15361536
SSL_CTX_free(ctx);
15371537
return 0;
15381538
}
1539-
1540-
1539+
1540+
15411541
SSL_set_fd(data->ssl_handle, data->fd);
15421542

15431543
if (ftp->old_ssl) {
15441544
SSL_copy_session_id(data->ssl_handle, ftp->ssl_handle);
15451545
}
1546-
1546+
15471547
if (SSL_connect(data->ssl_handle) <= 0) {
15481548
php_error_docref(NULL TSRMLS_CC, E_WARNING, "data_accept: SSL/TLS handshake failed");
15491549
SSL_shutdown(data->ssl_handle);
15501550
SSL_free(data->ssl_handle);
15511551
return 0;
15521552
}
1553-
1553+
15541554
data->ssl_active = 1;
1555-
}
1555+
}
15561556

15571557
#endif
15581558

@@ -1567,24 +1567,24 @@ data_close(ftpbuf_t *ftp, databuf_t *data)
15671567
{
15681568
#if HAVE_OPENSSL_EXT
15691569
SSL_CTX *ctx;
1570-
#endif
1570+
#endif
15711571
if (data == NULL) {
15721572
return NULL;
15731573
}
15741574
if (data->listener != -1) {
15751575
#if HAVE_OPENSSL_EXT
15761576
if (data->ssl_active) {
1577-
1577+
15781578
ctx = SSL_get_SSL_CTX(data->ssl_handle);
15791579
SSL_CTX_free(ctx);
15801580

15811581
SSL_shutdown(data->ssl_handle);
15821582
SSL_free(data->ssl_handle);
15831583
data->ssl_active = 0;
15841584
}
1585-
#endif
1585+
#endif
15861586
closesocket(data->listener);
1587-
}
1587+
}
15881588
if (data->fd != -1) {
15891589
#if HAVE_OPENSSL_EXT
15901590
if (data->ssl_active) {
@@ -1595,9 +1595,9 @@ data_close(ftpbuf_t *ftp, databuf_t *data)
15951595
SSL_free(data->ssl_handle);
15961596
data->ssl_active = 0;
15971597
}
1598-
#endif
1598+
#endif
15991599
closesocket(data->fd);
1600-
}
1600+
}
16011601
if (ftp) {
16021602
ftp->data = NULL;
16031603
}
@@ -1615,8 +1615,8 @@ ftp_genlist(ftpbuf_t *ftp, const char *cmd, const char *path TSRMLS_DC)
16151615
databuf_t *data = NULL;
16161616
char *ptr;
16171617
int ch, lastch;
1618-
int size, rcvd;
1619-
int lines;
1618+
size_t size, rcvd;
1619+
size_t lines;
16201620
char **ret = NULL;
16211621
char **entry;
16221622
char *text;
@@ -1634,7 +1634,7 @@ ftp_genlist(ftpbuf_t *ftp, const char *cmd, const char *path TSRMLS_DC)
16341634
if ((data = ftp_getdata(ftp TSRMLS_CC)) == NULL) {
16351635
goto bail;
16361636
}
1637-
ftp->data = data;
1637+
ftp->data = data;
16381638

16391639
if (!ftp_putcmd(ftp, cmd, path)) {
16401640
goto bail;
@@ -1658,7 +1658,7 @@ ftp_genlist(ftpbuf_t *ftp, const char *cmd, const char *path TSRMLS_DC)
16581658
lines = 0;
16591659
lastch = 0;
16601660
while ((rcvd = my_recv(ftp, data->fd, data->buf, FTP_BUFSIZE))) {
1661-
if (rcvd == -1) {
1661+
if (rcvd == -1 || rcvd > ((size_t)(-1))-size) {
16621662
goto bail;
16631663
}
16641664

@@ -1863,7 +1863,7 @@ ftp_nb_put(ftpbuf_t *ftp, const char *path, php_stream *instream, ftptype_t type
18631863
if (!ftp_getresp(ftp) || (ftp->resp != 150 && ftp->resp != 125)) {
18641864
goto bail;
18651865
}
1866-
if ((data = data_accept(data, ftp TSRMLS_CC)) == NULL) {
1866+
if ((data = data_accept(data, ftp TSRMLS_CC)) == NULL) {
18671867
goto bail;
18681868
}
18691869
ftp->data = data;
@@ -1919,7 +1919,7 @@ ftp_nb_continue_write(ftpbuf_t *ftp TSRMLS_DC)
19191919
goto bail;
19201920
}
19211921
ftp->data = data_close(ftp, ftp->data);
1922-
1922+
19231923
if (!ftp_getresp(ftp) || (ftp->resp != 226 && ftp->resp != 250)) {
19241924
goto bail;
19251925
}

0 commit comments

Comments
 (0)