@@ -142,7 +142,7 @@ void rose_write_internal(struct sock *sk, int frametype)
142142 * dptr ++ = ROSE_GFI | lci1 ;
143143 * dptr ++ = lci2 ;
144144 * dptr ++ = frametype ;
145- * dptr ++ = 0xAA ;
145+ * dptr ++ = ROSE_CALL_REQ_ADDR_LEN_VAL ;
146146 memcpy (dptr , & rose -> dest_addr , ROSE_ADDR_LEN );
147147 dptr += ROSE_ADDR_LEN ;
148148 memcpy (dptr , & rose -> source_addr , ROSE_ADDR_LEN );
@@ -246,12 +246,16 @@ static int rose_parse_national(unsigned char *p, struct rose_facilities_struct *
246246 do {
247247 switch (* p & 0xC0 ) {
248248 case 0x00 :
249+ if (len < 2 )
250+ return -1 ;
249251 p += 2 ;
250252 n += 2 ;
251253 len -= 2 ;
252254 break ;
253255
254256 case 0x40 :
257+ if (len < 3 )
258+ return -1 ;
255259 if (* p == FAC_NATIONAL_RAND )
256260 facilities -> rand = ((p [1 ] << 8 ) & 0xFF00 ) + ((p [2 ] << 0 ) & 0x00FF );
257261 p += 3 ;
@@ -260,32 +264,48 @@ static int rose_parse_national(unsigned char *p, struct rose_facilities_struct *
260264 break ;
261265
262266 case 0x80 :
267+ if (len < 4 )
268+ return -1 ;
263269 p += 4 ;
264270 n += 4 ;
265271 len -= 4 ;
266272 break ;
267273
268274 case 0xC0 :
275+ if (len < 2 )
276+ return -1 ;
269277 l = p [1 ];
278+ if (len < 2 + l )
279+ return -1 ;
270280 if (* p == FAC_NATIONAL_DEST_DIGI ) {
271281 if (!fac_national_digis_received ) {
282+ if (l < AX25_ADDR_LEN )
283+ return -1 ;
272284 memcpy (& facilities -> source_digis [0 ], p + 2 , AX25_ADDR_LEN );
273285 facilities -> source_ndigis = 1 ;
274286 }
275287 }
276288 else if (* p == FAC_NATIONAL_SRC_DIGI ) {
277289 if (!fac_national_digis_received ) {
290+ if (l < AX25_ADDR_LEN )
291+ return -1 ;
278292 memcpy (& facilities -> dest_digis [0 ], p + 2 , AX25_ADDR_LEN );
279293 facilities -> dest_ndigis = 1 ;
280294 }
281295 }
282296 else if (* p == FAC_NATIONAL_FAIL_CALL ) {
297+ if (l < AX25_ADDR_LEN )
298+ return -1 ;
283299 memcpy (& facilities -> fail_call , p + 2 , AX25_ADDR_LEN );
284300 }
285301 else if (* p == FAC_NATIONAL_FAIL_ADD ) {
302+ if (l < 1 + ROSE_ADDR_LEN )
303+ return -1 ;
286304 memcpy (& facilities -> fail_addr , p + 3 , ROSE_ADDR_LEN );
287305 }
288306 else if (* p == FAC_NATIONAL_DIGIS ) {
307+ if (l % AX25_ADDR_LEN )
308+ return -1 ;
289309 fac_national_digis_received = 1 ;
290310 facilities -> source_ndigis = 0 ;
291311 facilities -> dest_ndigis = 0 ;
@@ -319,24 +339,32 @@ static int rose_parse_ccitt(unsigned char *p, struct rose_facilities_struct *fac
319339 do {
320340 switch (* p & 0xC0 ) {
321341 case 0x00 :
342+ if (len < 2 )
343+ return -1 ;
322344 p += 2 ;
323345 n += 2 ;
324346 len -= 2 ;
325347 break ;
326348
327349 case 0x40 :
350+ if (len < 3 )
351+ return -1 ;
328352 p += 3 ;
329353 n += 3 ;
330354 len -= 3 ;
331355 break ;
332356
333357 case 0x80 :
358+ if (len < 4 )
359+ return -1 ;
334360 p += 4 ;
335361 n += 4 ;
336362 len -= 4 ;
337363 break ;
338364
339365 case 0xC0 :
366+ if (len < 2 )
367+ return -1 ;
340368 l = p [1 ];
341369
342370 /* Prevent overflows*/
@@ -365,49 +393,44 @@ static int rose_parse_ccitt(unsigned char *p, struct rose_facilities_struct *fac
365393 return n ;
366394}
367395
368- int rose_parse_facilities (unsigned char * p ,
396+ int rose_parse_facilities (unsigned char * p , unsigned packet_len ,
369397 struct rose_facilities_struct * facilities )
370398{
371399 int facilities_len , len ;
372400
373401 facilities_len = * p ++ ;
374402
375- if (facilities_len == 0 )
403+ if (facilities_len == 0 || ( unsigned ) facilities_len > packet_len )
376404 return 0 ;
377405
378- while (facilities_len > 0 ) {
379- if (* p == 0x00 ) {
380- facilities_len -- ;
381- p ++ ;
382-
383- switch (* p ) {
384- case FAC_NATIONAL : /* National */
385- len = rose_parse_national (p + 1 , facilities , facilities_len - 1 );
386- if (len < 0 )
387- return 0 ;
388- facilities_len -= len + 1 ;
389- p += len + 1 ;
390- break ;
391-
392- case FAC_CCITT : /* CCITT */
393- len = rose_parse_ccitt (p + 1 , facilities , facilities_len - 1 );
394- if (len < 0 )
395- return 0 ;
396- facilities_len -= len + 1 ;
397- p += len + 1 ;
398- break ;
399-
400- default :
401- printk (KERN_DEBUG "ROSE: rose_parse_facilities - unknown facilities family %02X\n" , * p );
402- facilities_len -- ;
403- p ++ ;
404- break ;
405- }
406- } else
407- break ; /* Error in facilities format */
406+ while (facilities_len >= 3 && * p == 0x00 ) {
407+ facilities_len -- ;
408+ p ++ ;
409+
410+ switch (* p ) {
411+ case FAC_NATIONAL : /* National */
412+ len = rose_parse_national (p + 1 , facilities , facilities_len - 1 );
413+ break ;
414+
415+ case FAC_CCITT : /* CCITT */
416+ len = rose_parse_ccitt (p + 1 , facilities , facilities_len - 1 );
417+ break ;
418+
419+ default :
420+ printk (KERN_DEBUG "ROSE: rose_parse_facilities - unknown facilities family %02X\n" , * p );
421+ len = 1 ;
422+ break ;
423+ }
424+
425+ if (len < 0 )
426+ return 0 ;
427+ if (WARN_ON (len >= facilities_len ))
428+ return 0 ;
429+ facilities_len -= len + 1 ;
430+ p += len + 1 ;
408431 }
409432
410- return 1 ;
433+ return facilities_len == 0 ;
411434}
412435
413436static int rose_create_facilities (unsigned char * buffer , struct rose_sock * rose )
0 commit comments