Skip to content

Commit d263f29

Browse files
committed
Merge branch 'PHP-7.1'
* PHP-7.1: exclude debug code ensure the string for conversion is \0 terminated and integrade additional path length check
2 parents 73bfa26 + 51e1da6 commit d263f29

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

Zend/zend_virtual_cwd.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -848,9 +848,13 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
848848
int bufindex = 0, isabsolute = 0;
849849
wchar_t * reparsetarget;
850850
BOOL isVolume = FALSE;
851-
char *printname = NULL, *substitutename = NULL;
851+
#if VIRTUAL_CWD_DEBUG
852+
char *printname = NULL;
853+
#endif
854+
char *substitutename = NULL;
852855
size_t substitutename_len;
853856
int substitutename_off = 0;
857+
wchar_t tmpsubstname[MAXPATHLEN];
854858

855859
if(++(*ll) > LINK_MAX) {
856860
free_alloca(tmp, use_heap);
@@ -885,44 +889,66 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
885889
if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
886890
reparsetarget = pbuffer->SymbolicLinkReparseBuffer.ReparseTarget;
887891
isabsolute = (pbuffer->SymbolicLinkReparseBuffer.Flags == 0) ? 1 : 0;
892+
#if VIRTUAL_CWD_DEBUG
888893
printname = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR));
889894
if (!printname) {
890895
free_alloca(pbuffer, use_heap_large);
891896
free_alloca(tmp, use_heap);
892897
FREE_PATHW()
893898
return -1;
894899
}
900+
#endif
895901

896902
substitutename_len = pbuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
897-
substitutename = php_win32_cp_conv_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR),
898-
substitutename_len, &substitutename_len);
903+
if (substitutename_len > MAXPATHLEN) {
904+
free_alloca(pbuffer, use_heap_large);
905+
free_alloca(tmp, use_heap);
906+
FREE_PATHW()
907+
return -1;
908+
}
909+
memmove(tmpsubstname, reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR), pbuffer->MountPointReparseBuffer.SubstituteNameLength);
910+
tmpsubstname[substitutename_len] = L'\0';
911+
substitutename = php_win32_cp_conv_w_to_any(tmpsubstname, substitutename_len, &substitutename_len);
899912
if (!substitutename) {
900913
free_alloca(pbuffer, use_heap_large);
901914
free_alloca(tmp, use_heap);
915+
#if VIRTUAL_CWD_DEBUG
902916
free(printname);
917+
#endif
903918
FREE_PATHW()
904919
return -1;
905920
}
906921
}
907922
else if(pbuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
908923
isabsolute = 1;
909924
reparsetarget = pbuffer->MountPointReparseBuffer.ReparseTarget;
925+
#if VIRTUAL_CWD_DEBUG
910926
printname = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR));
911927
if (!printname) {
912928
free_alloca(pbuffer, use_heap_large);
913929
free_alloca(tmp, use_heap);
914930
FREE_PATHW()
915931
return -1;
916932
}
933+
#endif
917934

918935

919936
substitutename_len = pbuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
920-
substitutename = php_win32_cp_conv_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR),
921-
substitutename_len, &substitutename_len);
937+
if (substitutename_len > MAXPATHLEN) {
938+
free_alloca(pbuffer, use_heap_large);
939+
free_alloca(tmp, use_heap);
940+
FREE_PATHW()
941+
return -1;
942+
}
943+
memmove(tmpsubstname, reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR), pbuffer->MountPointReparseBuffer.SubstituteNameLength);
944+
tmpsubstname[substitutename_len] = L'\0';
945+
substitutename = php_win32_cp_conv_w_to_any(tmpsubstname, substitutename_len, &substitutename_len);
922946
if (!substitutename) {
923947
free_alloca(pbuffer, use_heap_large);
924948
free_alloca(tmp, use_heap);
949+
#if VIRTUAL_CWD_DEBUG
925950
free(printname);
951+
#endif
926952
FREE_PATHW()
927953
return -1;
928954
}
@@ -985,9 +1011,9 @@ static int tsrm_realpath_r(char *path, int start, int len, int *ll, time_t *t, i
9851011
fprintf(stderr, "reparse: print: %s ", printname);
9861012
fprintf(stderr, "sub: %s ", substitutename);
9871013
fprintf(stderr, "resolved: %s ", path);
1014+
free(printname);
9881015
#endif
9891016
free_alloca(pbuffer, use_heap_large);
990-
free(printname);
9911017
free(substitutename);
9921018

9931019
if(isabsolute == 1) {

0 commit comments

Comments
 (0)