Debian Codegraph @

debian/patches/0105-cve-2018-13421.patch

raw symbols SCIP
1 Description: Fix heap-based buffer over-read in io::trim_chars (CVE-2018-13421)
2 Author: Ben Strasser <[email protected]>
3 Origin: https://github.com/ben-strasser/fast-cpp-csv-parser/commit/8cf591aa7397f4372778cc927e184d28ee591093#diff-ef7f3342d40d9604321eba8dd7e59f5d
4 Bug: https://github.com/ben-strasser/fast-cpp-csv-parser/issues/67
5 Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=903247
6 Forwarded: no
7 Last-Update: 2018-07-08
8 ---
9 This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
10 Index: trunk/csv.h
11 ===================================================================
12 --- trunk.orig/csv.h
13 +++ trunk/csv.h
14 @@ -63,7 +63,7 @@ namespace io{
15 return error_message_buffer;
16 }
17
18 - mutable char error_message_buffer[256];
19 + mutable char error_message_buffer[512];
20 };
21
22 const int max_file_name_length = 255;
23 @@ -74,8 +74,12 @@ namespace io{
24 }
25
26 void set_file_name(const char*file_name){
27 - std::strncpy(this->file_name, file_name, max_file_name_length);
28 - this->file_name[max_file_name_length] = '\0';
29 + if(file_name != nullptr){
30 + strncpy(this->file_name, file_name, error::max_file_name_length);
31 + this->file_name[error::max_file_name_length] = '\0';
32 + }else{
33 + this->file_name[0] = '\0';
34 + }
35 }
36
37 char file_name[max_file_name_length+1];
38 @@ -233,7 +237,7 @@ namespace io{
39 }
40
41 bool is_valid()const{
42 - return byte_source != 0;
43 + return byte_source != nullptr;
44 }
45
46 void start_read(char*arg_buffer, int arg_desired_byte_count){
47 @@ -259,7 +263,7 @@ namespace io{
48 }
49
50 ~AsynchronousReader(){
51 - if(byte_source != 0){
52 + if(byte_source != nullptr){
53 {
54 std::unique_lock<std::mutex>guard(lock);
55 termination_requested = true;
56 @@ -293,7 +297,7 @@ namespace io{
57 }
58
59 bool is_valid()const{
60 - return byte_source != 0;
61 + return byte_source != nullptr;
62 }
63
64 void start_read(char*arg_buffer, int arg_desired_byte_count){
65 @@ -314,12 +318,12 @@ namespace io{
66 class LineReader{
67 private:
68 static const int block_len = 1<<24;
69 + std::unique_ptr<char[]>buffer; // must be constructed before (and thus destructed after) the reader!
70 #ifdef CSV_IO_NO_THREAD
71 detail::SynchronousReader reader;
72 #else
73 detail::AsynchronousReader reader;
74 #endif
75 - char*buffer;
76 int data_begin;
77 int data_end;
78
79 @@ -343,22 +347,17 @@ namespace io{
80 void init(std::unique_ptr<ByteSourceBase>byte_source){
81 file_line = 0;
82
83 - buffer = new char[3*block_len];
84 - try{
85 - data_begin = 0;
86 - data_end = byte_source->read(buffer, 2*block_len);
87 -
88 - // Ignore UTF-8 BOM
89 - if(data_end >= 3 && buffer[0] == '\xEF' && buffer[1] == '\xBB' && buffer[2] == '\xBF')
90 - data_begin = 3;
91 -
92 - if(data_end == 2*block_len){
93 - reader.init(std::move(byte_source));
94 - reader.start_read(buffer + 2*block_len, block_len);
95 - }
96 - }catch(...){
97 - delete[]buffer;
98 - throw;
99 + buffer = std::unique_ptr<char[]>(new char[3*block_len]);
100 + data_begin = 0;
101 + data_end = byte_source->read(buffer.get(), 2*block_len);
102 +
103 + // Ignore UTF-8 BOM
104 + if(data_end >= 3 && buffer[0] == '\xEF' && buffer[1] == '\xBB' && buffer[2] == '\xBF')
105 + data_begin = 3;
106 +
107 + if(data_end == 2*block_len){
108 + reader.init(std::move(byte_source));
109 + reader.start_read(buffer.get() + 2*block_len, block_len);
110 }
111 }
112
113 @@ -422,8 +421,12 @@ namespace io{
114 }
115
116 void set_file_name(const char*file_name){
117 - strncpy(this->file_name, file_name, error::max_file_name_length);
118 - this->file_name[error::max_file_name_length] = '\0';
119 + if(file_name != nullptr){
120 + strncpy(this->file_name, file_name, error::max_file_name_length);
121 + this->file_name[error::max_file_name_length] = '\0';
122 + }else{
123 + this->file_name[0] = '\0';
124 + }
125 }
126
127 const char*get_truncated_file_name()const{
128 @@ -448,14 +451,14 @@ namespace io{
129 assert(data_end <= block_len*2);
130
131 if(data_begin >= block_len){
132 - std::memcpy(buffer, buffer+block_len, block_len);
133 + std::memcpy(buffer.get(), buffer.get()+block_len, block_len);
134 data_begin -= block_len;
135 data_end -= block_len;
136 if(reader.is_valid())
137 {
138 data_end += reader.finish_read();
139 - std::memcpy(buffer+block_len, buffer+2*block_len, block_len);
140 - reader.start_read(buffer + 2*block_len, block_len);
141 + std::memcpy(buffer.get()+block_len, buffer.get()+2*block_len, block_len);
142 + reader.start_read(buffer.get() + 2*block_len, block_len);
143 }
144 }
145
146 @@ -484,14 +487,10 @@ namespace io{
147 if(line_end != data_begin && buffer[line_end-1] == '\r')
148 buffer[line_end-1] = '\0';
149
150 - char*ret = buffer + data_begin;
151 + char*ret = buffer.get() + data_begin;
152 data_begin = line_end+1;
153 return ret;
154 }
155 -
156 - ~LineReader(){
157 - delete[] buffer;
158 - }
159 };
160
161
162 @@ -507,8 +506,12 @@ namespace io{
163 }
164
165 void set_column_name(const char*column_name){
166 - std::strncpy(this->column_name, column_name, max_column_name_length);
167 - this->column_name[max_column_name_length] = '\0';
168 + if(column_name != nullptr){
169 + std::strncpy(this->column_name, column_name, max_column_name_length);
170 + this->column_name[max_column_name_length] = '\0';
171 + }else{
172 + this->column_name[0] = '\0';
173 + }
174 }
175
176 char column_name[max_column_name_length+1];
177 @@ -523,8 +526,12 @@ namespace io{
178 }
179
180 void set_column_content(const char*column_content){
181 - std::strncpy(this->column_content, column_content, max_column_content_length);
182 - this->column_content[max_column_content_length] = '\0';
183 + if(column_content != nullptr){
184 + std::strncpy(this->column_content, column_content, max_column_content_length);
185 + this->column_content[max_column_content_length] = '\0';
186 + }else{
187 + this->column_content[0] = '\0';
188 + }
189 }
190
191 char column_content[max_column_content_length+1];
192 @@ -692,9 +699,9 @@ namespace io{
193
194 public:
195 static void trim(char*&str_begin, char*&str_end){
196 - while(is_trim_char(*str_begin, trim_char_list...) && str_begin != str_end)
197 + while(str_begin != str_end && is_trim_char(*str_begin, trim_char_list...))
198 ++str_begin;
199 - while(is_trim_char(*(str_end-1), trim_char_list...) && str_begin != str_end)
200 + while(str_begin != str_end && is_trim_char(*(str_end-1), trim_char_list...))
201 --str_end;
202 *str_end = '\0';
203 }
204 @@ -786,7 +793,7 @@ namespace io{
205 --col_end;
206 char*out = col_begin;
207 for(char*in = col_begin; in!=col_end; ++in){
208 - if(*in == quote && *(in+1) == quote){
209 + if(*in == quote && (in+1) != col_end && *(in+1) == quote){
210 ++in;
211 }
212 *out = *in;
213 @@ -1083,6 +1090,9 @@ namespace io{
214
215 template<class overflow_policy, class T>
216 void parse(char*col, T&x){
217 + // Mute unused variable compiler warning
218 + (void)col;
219 + (void)x;
220 // GCC evalutes "false" when reading the template and
221 // "sizeof(T)!=sizeof(T)" only when instantiating it. This is why
222 // this strange construct is used.
223 @@ -1131,9 +1141,9 @@ namespace io{
224 column_names[i-1] = "col"+std::to_string(i);
225 }
226
227 - char*next_line(){
228 - return in.next_line();
229 - }
230 + char*next_line(){
231 + return in.next_line();
232 + }
233
234 template<class ...ColNames>
235 void read_header(ignore_column ignore_policy, ColNames...cols){
236 @@ -1255,4 +1265,3 @@ namespace io{
237 };
238 }
239 #endif
240 -