Skip to content

Commit 31ea49b

Browse files
takaswietiwai
authored andcommitted
ALSA: firewire-lib: fix buffer-over-run when detecting packet discontinuity
When detecting packet discontinuity, handle_in_packet() returns minus value and this value is assigned to unsigned int variable, then the variable has huge value. As a result, the variable causes buffer-over-run in handle_out_packet(). This brings invalid page request and system hangup. This commit fixes the bug to add a new argument into handle_in_packet() and the number of handled data blocks is assignd to it. The function return value is just used to check error. I also considered to change the type of local variable to 'int' in in_stream_callback(). This idea is based on type-conversion in C standard, while it may cause future problems when adding more works. Thus, I dropped this idea. Fixes: 6fc6b9c('ALSA: firewire-lib: pass the number of data blocks in incoming packets to outgoing packets') Reported-by: Dan Carpenter <dan.carpenter@oracle.com> Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp> Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 0d769a5 commit 31ea49b

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

‎sound/firewire/amdtp.c‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,10 @@ static int handle_out_packet(struct amdtp_stream *s, unsigned int data_blocks,
688688
}
689689

690690
static int handle_in_packet(struct amdtp_stream *s,
691-
unsigned int payload_quadlets, __be32 *buffer)
691+
unsigned int payload_quadlets, __be32 *buffer,
692+
unsigned int *data_blocks)
692693
{
693694
u32 cip_header[2];
694-
unsigned int data_blocks;
695695
unsigned int data_block_quadlets, data_block_counter, dbc_interval;
696696
struct snd_pcm_substream *pcm = NULL;
697697
bool lost;
@@ -709,15 +709,15 @@ static int handle_in_packet(struct amdtp_stream *s,
709709
dev_info_ratelimited(&s->unit->device,
710710
"Invalid CIP header for AMDTP: %08X:%08X\n",
711711
cip_header[0], cip_header[1]);
712-
data_blocks = 0;
712+
*data_blocks = 0;
713713
goto end;
714714
}
715715

716716
/* Calculate data blocks */
717717
if (payload_quadlets < 3 ||
718718
((cip_header[1] & CIP_FDF_MASK) ==
719719
(AMDTP_FDF_NO_DATA << CIP_FDF_SHIFT))) {
720-
data_blocks = 0;
720+
*data_blocks = 0;
721721
} else {
722722
data_block_quadlets =
723723
(cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
@@ -731,12 +731,12 @@ static int handle_in_packet(struct amdtp_stream *s,
731731
if (s->flags & CIP_WRONG_DBS)
732732
data_block_quadlets = s->data_block_quadlets;
733733

734-
data_blocks = (payload_quadlets - 2) / data_block_quadlets;
734+
*data_blocks = (payload_quadlets - 2) / data_block_quadlets;
735735
}
736736

737737
/* Check data block counter continuity */
738738
data_block_counter = cip_header[0] & CIP_DBC_MASK;
739-
if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
739+
if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
740740
s->data_block_counter != UINT_MAX)
741741
data_block_counter = s->data_block_counter;
742742

@@ -746,10 +746,10 @@ static int handle_in_packet(struct amdtp_stream *s,
746746
} else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
747747
lost = data_block_counter != s->data_block_counter;
748748
} else {
749-
if ((data_blocks > 0) && (s->tx_dbc_interval > 0))
749+
if ((*data_blocks > 0) && (s->tx_dbc_interval > 0))
750750
dbc_interval = s->tx_dbc_interval;
751751
else
752-
dbc_interval = data_blocks;
752+
dbc_interval = *data_blocks;
753753

754754
lost = data_block_counter !=
755755
((s->data_block_counter + dbc_interval) & 0xff);
@@ -762,30 +762,30 @@ static int handle_in_packet(struct amdtp_stream *s,
762762
return -EIO;
763763
}
764764

765-
if (data_blocks > 0) {
765+
if (*data_blocks > 0) {
766766
buffer += 2;
767767

768768
pcm = ACCESS_ONCE(s->pcm);
769769
if (pcm)
770-
s->transfer_samples(s, pcm, buffer, data_blocks);
770+
s->transfer_samples(s, pcm, buffer, *data_blocks);
771771

772772
if (s->midi_ports)
773-
read_midi_messages(s, buffer, data_blocks);
773+
read_midi_messages(s, buffer, *data_blocks);
774774
}
775775

776776
if (s->flags & CIP_DBC_IS_END_EVENT)
777777
s->data_block_counter = data_block_counter;
778778
else
779779
s->data_block_counter =
780-
(data_block_counter + data_blocks) & 0xff;
780+
(data_block_counter + *data_blocks) & 0xff;
781781
end:
782782
if (queue_in_packet(s) < 0)
783783
return -EIO;
784784

785785
if (pcm)
786-
update_pcm_pointers(s, pcm, data_blocks);
786+
update_pcm_pointers(s, pcm, *data_blocks);
787787

788-
return data_blocks;
788+
return 0;
789789
}
790790

791791
static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
@@ -853,8 +853,8 @@ static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
853853
break;
854854
}
855855

856-
data_blocks = handle_in_packet(s, payload_quadlets, buffer);
857-
if (data_blocks < 0) {
856+
if (handle_in_packet(s, payload_quadlets, buffer,
857+
&data_blocks) < 0) {
858858
s->packet_index = -1;
859859
break;
860860
}

0 commit comments

Comments
 (0)