Fossil SCM
In "fossil import --svn", refuse a negative property block length and check every length inside the block against its size. A block too short for the PROPS-END marker was read before its start, and a negative or oversized length made the import loop for ever or read and write outside the buffer.
Commit
78511fc47c39a36a918cda0268c990dccdd877e3fb816ac63e5624279b22fca4
Parent
5c2919cfc608d85…
1 file changed
+22
-2
+22
-2
| --- src/import.c | ||
| +++ src/import.c | ||
| @@ -942,37 +942,51 @@ | ||
| 942 | 942 | rec->aProps = 0; |
| 943 | 943 | rec->nProps = 0; |
| 944 | 944 | zLen = svn_find_header(*rec, "Prop-content-length"); |
| 945 | 945 | if( zLen ){ |
| 946 | 946 | nRawProps = atoi(zLen); |
| 947 | + if( nRawProps<0 ){ | |
| 948 | + fossil_fatal("svn-dump data format broken"); | |
| 949 | + } | |
| 947 | 950 | } |
| 948 | 951 | if( nRawProps ){ |
| 949 | 952 | int got; |
| 950 | 953 | char *zLine; |
| 954 | + char *zEnd; /* Start of the PROPS-END marker */ | |
| 951 | 955 | rec->pRawProps = pRawProps = fossil_malloc( nRawProps ); |
| 952 | 956 | got = fread(rec->pRawProps, 1, nRawProps, pIn); |
| 953 | 957 | if( got!=nRawProps ){ |
| 954 | 958 | fossil_fatal("short read: got %d of %d bytes", got, nRawProps); |
| 955 | 959 | } |
| 956 | - if( memcmp(&pRawProps[got-10], "PROPS-END\n", 10)!=0 ){ | |
| 960 | + if( got<10 || memcmp(&pRawProps[got-10], "PROPS-END\n", 10)!=0 ){ | |
| 957 | 961 | fossil_fatal("svn-dump data ended unexpectedly"); |
| 958 | 962 | } |
| 959 | 963 | zLine = pRawProps; |
| 960 | - while( zLine<(pRawProps+nRawProps-10) ){ | |
| 964 | + zEnd = pRawProps+nRawProps-10; | |
| 965 | + while( zLine<zEnd ){ | |
| 961 | 966 | char *eol; |
| 962 | 967 | int propLen; |
| 963 | 968 | if( zLine[0]=='D' ){ |
| 964 | 969 | propLen = atoi(&zLine[2]); |
| 965 | 970 | eol = strchr(zLine, '\n'); |
| 971 | + if( eol==0 || eol>=zEnd || propLen<0 || propLen>zEnd-eol ){ | |
| 972 | + fossil_fatal("svn-dump data format broken"); | |
| 973 | + } | |
| 966 | 974 | zLine = eol+1+propLen+1; |
| 967 | 975 | }else{ |
| 968 | 976 | if( zLine[0]!='K' ){ |
| 969 | 977 | fossil_fatal("svn-dump data format broken"); |
| 970 | 978 | } |
| 971 | 979 | propLen = atoi(&zLine[2]); |
| 972 | 980 | eol = strchr(zLine, '\n'); |
| 981 | + if( eol==0 || eol>=zEnd ){ | |
| 982 | + fossil_fatal("svn-dump data format broken"); | |
| 983 | + } | |
| 973 | 984 | zLine = eol+1; |
| 985 | + if( propLen<0 || propLen>=zEnd-zLine ){ | |
| 986 | + fossil_fatal("svn-dump data format broken"); | |
| 987 | + } | |
| 974 | 988 | eol = zLine+propLen; |
| 975 | 989 | if( *eol!='\n' ){ |
| 976 | 990 | fossil_fatal("svn-dump data format broken"); |
| 977 | 991 | } |
| 978 | 992 | *eol = 0; |
| @@ -984,11 +998,17 @@ | ||
| 984 | 998 | if( zLine[0]!='V' ){ |
| 985 | 999 | fossil_fatal("svn-dump data format broken"); |
| 986 | 1000 | } |
| 987 | 1001 | propLen = atoi(&zLine[2]); |
| 988 | 1002 | eol = strchr(zLine, '\n'); |
| 1003 | + if( eol==0 || eol>=zEnd ){ | |
| 1004 | + fossil_fatal("svn-dump data format broken"); | |
| 1005 | + } | |
| 989 | 1006 | zLine = eol+1; |
| 1007 | + if( propLen<0 || propLen>=zEnd-zLine ){ | |
| 1008 | + fossil_fatal("svn-dump data format broken"); | |
| 1009 | + } | |
| 990 | 1010 | eol = zLine+propLen; |
| 991 | 1011 | if( *eol!='\n' ){ |
| 992 | 1012 | fossil_fatal("svn-dump data format broken"); |
| 993 | 1013 | } |
| 994 | 1014 | *eol = 0; |
| 995 | 1015 |
| --- src/import.c | |
| +++ src/import.c | |
| @@ -942,37 +942,51 @@ | |
| 942 | rec->aProps = 0; |
| 943 | rec->nProps = 0; |
| 944 | zLen = svn_find_header(*rec, "Prop-content-length"); |
| 945 | if( zLen ){ |
| 946 | nRawProps = atoi(zLen); |
| 947 | } |
| 948 | if( nRawProps ){ |
| 949 | int got; |
| 950 | char *zLine; |
| 951 | rec->pRawProps = pRawProps = fossil_malloc( nRawProps ); |
| 952 | got = fread(rec->pRawProps, 1, nRawProps, pIn); |
| 953 | if( got!=nRawProps ){ |
| 954 | fossil_fatal("short read: got %d of %d bytes", got, nRawProps); |
| 955 | } |
| 956 | if( memcmp(&pRawProps[got-10], "PROPS-END\n", 10)!=0 ){ |
| 957 | fossil_fatal("svn-dump data ended unexpectedly"); |
| 958 | } |
| 959 | zLine = pRawProps; |
| 960 | while( zLine<(pRawProps+nRawProps-10) ){ |
| 961 | char *eol; |
| 962 | int propLen; |
| 963 | if( zLine[0]=='D' ){ |
| 964 | propLen = atoi(&zLine[2]); |
| 965 | eol = strchr(zLine, '\n'); |
| 966 | zLine = eol+1+propLen+1; |
| 967 | }else{ |
| 968 | if( zLine[0]!='K' ){ |
| 969 | fossil_fatal("svn-dump data format broken"); |
| 970 | } |
| 971 | propLen = atoi(&zLine[2]); |
| 972 | eol = strchr(zLine, '\n'); |
| 973 | zLine = eol+1; |
| 974 | eol = zLine+propLen; |
| 975 | if( *eol!='\n' ){ |
| 976 | fossil_fatal("svn-dump data format broken"); |
| 977 | } |
| 978 | *eol = 0; |
| @@ -984,11 +998,17 @@ | |
| 984 | if( zLine[0]!='V' ){ |
| 985 | fossil_fatal("svn-dump data format broken"); |
| 986 | } |
| 987 | propLen = atoi(&zLine[2]); |
| 988 | eol = strchr(zLine, '\n'); |
| 989 | zLine = eol+1; |
| 990 | eol = zLine+propLen; |
| 991 | if( *eol!='\n' ){ |
| 992 | fossil_fatal("svn-dump data format broken"); |
| 993 | } |
| 994 | *eol = 0; |
| 995 |
| --- src/import.c | |
| +++ src/import.c | |
| @@ -942,37 +942,51 @@ | |
| 942 | rec->aProps = 0; |
| 943 | rec->nProps = 0; |
| 944 | zLen = svn_find_header(*rec, "Prop-content-length"); |
| 945 | if( zLen ){ |
| 946 | nRawProps = atoi(zLen); |
| 947 | if( nRawProps<0 ){ |
| 948 | fossil_fatal("svn-dump data format broken"); |
| 949 | } |
| 950 | } |
| 951 | if( nRawProps ){ |
| 952 | int got; |
| 953 | char *zLine; |
| 954 | char *zEnd; /* Start of the PROPS-END marker */ |
| 955 | rec->pRawProps = pRawProps = fossil_malloc( nRawProps ); |
| 956 | got = fread(rec->pRawProps, 1, nRawProps, pIn); |
| 957 | if( got!=nRawProps ){ |
| 958 | fossil_fatal("short read: got %d of %d bytes", got, nRawProps); |
| 959 | } |
| 960 | if( got<10 || memcmp(&pRawProps[got-10], "PROPS-END\n", 10)!=0 ){ |
| 961 | fossil_fatal("svn-dump data ended unexpectedly"); |
| 962 | } |
| 963 | zLine = pRawProps; |
| 964 | zEnd = pRawProps+nRawProps-10; |
| 965 | while( zLine<zEnd ){ |
| 966 | char *eol; |
| 967 | int propLen; |
| 968 | if( zLine[0]=='D' ){ |
| 969 | propLen = atoi(&zLine[2]); |
| 970 | eol = strchr(zLine, '\n'); |
| 971 | if( eol==0 || eol>=zEnd || propLen<0 || propLen>zEnd-eol ){ |
| 972 | fossil_fatal("svn-dump data format broken"); |
| 973 | } |
| 974 | zLine = eol+1+propLen+1; |
| 975 | }else{ |
| 976 | if( zLine[0]!='K' ){ |
| 977 | fossil_fatal("svn-dump data format broken"); |
| 978 | } |
| 979 | propLen = atoi(&zLine[2]); |
| 980 | eol = strchr(zLine, '\n'); |
| 981 | if( eol==0 || eol>=zEnd ){ |
| 982 | fossil_fatal("svn-dump data format broken"); |
| 983 | } |
| 984 | zLine = eol+1; |
| 985 | if( propLen<0 || propLen>=zEnd-zLine ){ |
| 986 | fossil_fatal("svn-dump data format broken"); |
| 987 | } |
| 988 | eol = zLine+propLen; |
| 989 | if( *eol!='\n' ){ |
| 990 | fossil_fatal("svn-dump data format broken"); |
| 991 | } |
| 992 | *eol = 0; |
| @@ -984,11 +998,17 @@ | |
| 998 | if( zLine[0]!='V' ){ |
| 999 | fossil_fatal("svn-dump data format broken"); |
| 1000 | } |
| 1001 | propLen = atoi(&zLine[2]); |
| 1002 | eol = strchr(zLine, '\n'); |
| 1003 | if( eol==0 || eol>=zEnd ){ |
| 1004 | fossil_fatal("svn-dump data format broken"); |
| 1005 | } |
| 1006 | zLine = eol+1; |
| 1007 | if( propLen<0 || propLen>=zEnd-zLine ){ |
| 1008 | fossil_fatal("svn-dump data format broken"); |
| 1009 | } |
| 1010 | eol = zLine+propLen; |
| 1011 | if( *eol!='\n' ){ |
| 1012 | fossil_fatal("svn-dump data format broken"); |
| 1013 | } |
| 1014 | *eol = 0; |
| 1015 |