Skip to content

Commit 49d7363

Browse files
Chenyuan Mijic23
Chenyuan Mi
authored andcommittedJul 29, 2023
tools: iio: iio_generic_buffer: Fix some integer type and calculation
In function size_from_channelarray(), the return value 'bytes' is defined as int type. However, the calcution of 'bytes' in this function is designed to use the unsigned int type. So it is necessary to change 'bytes' type to unsigned int to avoid integer overflow. The size_from_channelarray() is called in main() function, its return value is directly multipled by 'buf_len' and then used as the malloc() parameter. The 'buf_len' is completely controllable by user, thus a multiplication overflow may occur here. This could allocate an unexpected small area. Signed-off-by: Chenyuan Mi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 9afc8c6 commit 49d7363

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed
 

‎tools/iio/iio_generic_buffer.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ enum autochan {
5151
* Has the side effect of filling the channels[i].location values used
5252
* in processing the buffer output.
5353
**/
54-
static int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
54+
static unsigned int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
5555
{
56-
int bytes = 0;
56+
unsigned int bytes = 0;
5757
int i = 0;
5858

5959
while (i < num_channels) {
@@ -348,7 +348,7 @@ int main(int argc, char **argv)
348348
ssize_t read_size;
349349
int dev_num = -1, trig_num = -1;
350350
char *buffer_access = NULL;
351-
int scan_size;
351+
unsigned int scan_size;
352352
int noevents = 0;
353353
int notrigger = 0;
354354
char *dummy;
@@ -674,7 +674,16 @@ int main(int argc, char **argv)
674674
}
675675

676676
scan_size = size_from_channelarray(channels, num_channels);
677-
data = malloc(scan_size * buf_len);
677+
678+
size_t total_buf_len = scan_size * buf_len;
679+
680+
if (scan_size > 0 && total_buf_len / scan_size != buf_len) {
681+
ret = -EFAULT;
682+
perror("Integer overflow happened when calculate scan_size * buf_len");
683+
goto error;
684+
}
685+
686+
data = malloc(total_buf_len);
678687
if (!data) {
679688
ret = -ENOMEM;
680689
goto error;

0 commit comments

Comments
 (0)
Please sign in to comment.