@@ -348,10 +348,19 @@ pci_vt9p_thread(void *vsc)
348348 command = buf [4 ];
349349 tag = (uint16_t )((uint16_t )buf [5 ] | ((uint16_t )buf [6 ] << 8 ));
350350 DPRINTF (("[thread]Got response for tag %d command %d len %d\r\n" , (int )tag , (int )command , (int )len ));
351+ if (len > BUFSIZE ) {
352+ fprintf (stderr , "virtio-9p: command too long, maximum is %d\n" , BUFSIZE );
353+ /* Fatal error, crash VM, let us be restarted */
354+ _exit (1 );
355+ }
356+ if (len < minlen ) {
357+ fprintf (stderr , "virtio-9p: command too short, must be over 7 bytes\n" );
358+ /* Fatal error, crash VM, let us be restarted */
359+ _exit (1 );
360+ }
351361 n = (size_t )(len - minlen );
352362 ptr = buf + minlen ;
353363 while (n ) {
354- assert (len <= BUFSIZE );
355364 ret = read (sc -> v9sc_sock , ptr , n );
356365 if (ret <= 0 ) {
357366 fprintf (stderr , "virtio-9p: unexpected EOF reading-- did the 9P server crash?\n" );
@@ -362,9 +371,12 @@ pci_vt9p_thread(void *vsc)
362371 ptr += ret ;
363372 }
364373 DPRINTF (("[thread]got complete response for tag %d len %d\r\n" , (int )tag , (int )len ));
365- if (command == 107 ) {
374+ if (command == 107 ) { /* Rerror */
366375 char msg [128 ];
367376 uint16_t slen = (uint16_t )((uint16_t )buf [7 ] | ((uint16_t )buf [8 ] << 8 ));
377+ if (slen > 128 ) {
378+ slen = 128 ; /* truncate overlong error message if required */
379+ }
368380 memcpy (msg , & buf [9 ], slen );
369381 msg [slen ] = 0 ;
370382 DPRINTF (("[thread]Rerror: %s\r\n" , msg ));
0 commit comments