diff mbox series

vfio: Fix smatch errors in vfio_combine_iova_ranges()

Message ID 20231002224325.3150842-1-alex.williamson@redhat.com (mailing list archive)
State New, archived
Headers show
Series vfio: Fix smatch errors in vfio_combine_iova_ranges() | expand

Commit Message

Alex Williamson Oct. 2, 2023, 10:43 p.m. UTC
smatch reports:

vfio_combine_iova_ranges() error: uninitialized symbol 'last'.
vfio_combine_iova_ranges() error: potentially dereferencing uninitialized 'comb_end'.
vfio_combine_iova_ranges() error: potentially dereferencing uninitialized 'comb_start'.

These errors are only reachable via invalid input, in the case of
@last when we receive an empty rb-tree or for @comb_{start,end} if the
rb-tree is empty or otherwise fails to produce a second node that
reduces the gap.  Add tests with warnings for these cases.

Reported-by: Cong Liu <liucong2@kylinos.cn>
Link: https://lore.kernel.org/all/20230920095532.88135-1-liucong2@kylinos.cn
Cc: Yishai Hadas <yishaih@nvidia.com>
Cc: Brett Creeley <brett.creeley@amd.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
 drivers/vfio/vfio_main.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

Comments

Brett Creeley Oct. 4, 2023, 3:35 p.m. UTC | #1
On 10/2/2023 3:43 PM, Alex Williamson wrote:
> Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
> 
> 
> smatch reports:
> 
> vfio_combine_iova_ranges() error: uninitialized symbol 'last'.
> vfio_combine_iova_ranges() error: potentially dereferencing uninitialized 'comb_end'.
> vfio_combine_iova_ranges() error: potentially dereferencing uninitialized 'comb_start'.
> 
> These errors are only reachable via invalid input, in the case of
> @last when we receive an empty rb-tree or for @comb_{start,end} if the
> rb-tree is empty or otherwise fails to produce a second node that
> reduces the gap.  Add tests with warnings for these cases.
> 
> Reported-by: Cong Liu <liucong2@kylinos.cn>
> Link: https://lore.kernel.org/all/20230920095532.88135-1-liucong2@kylinos.cn
> Cc: Yishai Hadas <yishaih@nvidia.com>
> Cc: Brett Creeley <brett.creeley@amd.com>
> Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
> ---
>   drivers/vfio/vfio_main.c | 10 ++++++++++
>   1 file changed, 10 insertions(+)
> 
> diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
> index 40732e8ed4c6..e31e1952d7b8 100644
> --- a/drivers/vfio/vfio_main.c
> +++ b/drivers/vfio/vfio_main.c
> @@ -946,6 +946,11 @@ void vfio_combine_iova_ranges(struct rb_root_cached *root, u32 cur_nodes,
>                  unsigned long last;
> 
>                  comb_start = interval_tree_iter_first(root, 0, ULONG_MAX);
> +
> +               /* Empty list */
> +               if (WARN_ON_ONCE(!comb_start))
> +                       return;
> +
>                  curr = comb_start;
>                  while (curr) {
>                          last = curr->last;
> @@ -975,6 +980,11 @@ void vfio_combine_iova_ranges(struct rb_root_cached *root, u32 cur_nodes,
>                          prev = curr;
>                          curr = interval_tree_iter_next(curr, 0, ULONG_MAX);
>                  }
> +
> +               /* Empty list or no nodes to combine */
> +               if (WARN_ON_ONCE(min_gap == ULONG_MAX))
> +                       break;
> +
>                  comb_start->last = comb_end->last;
>                  interval_tree_remove(comb_end, root);
>                  cur_nodes--;
> --
> 2.40.1
> 

LGTM. Thanks for fixing this.

Reviewed-by: Brett Creeley <brett.creeley@amd.com>
Jason Gunthorpe Oct. 6, 2023, 4:56 p.m. UTC | #2
On Mon, Oct 02, 2023 at 04:43:25PM -0600, Alex Williamson wrote:
> smatch reports:
> 
> vfio_combine_iova_ranges() error: uninitialized symbol 'last'.
> vfio_combine_iova_ranges() error: potentially dereferencing uninitialized 'comb_end'.
> vfio_combine_iova_ranges() error: potentially dereferencing uninitialized 'comb_start'.
> 
> These errors are only reachable via invalid input, in the case of
> @last when we receive an empty rb-tree or for @comb_{start,end} if the
> rb-tree is empty or otherwise fails to produce a second node that
> reduces the gap.  Add tests with warnings for these cases.
> 
> Reported-by: Cong Liu <liucong2@kylinos.cn>
> Link: https://lore.kernel.org/all/20230920095532.88135-1-liucong2@kylinos.cn
> Cc: Yishai Hadas <yishaih@nvidia.com>
> Cc: Brett Creeley <brett.creeley@amd.com>
> Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
> ---
>  drivers/vfio/vfio_main.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)

Yeah, this is much clearer

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Thanks,
Jason
diff mbox series

Patch

diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 40732e8ed4c6..e31e1952d7b8 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -946,6 +946,11 @@  void vfio_combine_iova_ranges(struct rb_root_cached *root, u32 cur_nodes,
 		unsigned long last;
 
 		comb_start = interval_tree_iter_first(root, 0, ULONG_MAX);
+
+		/* Empty list */
+		if (WARN_ON_ONCE(!comb_start))
+			return;
+
 		curr = comb_start;
 		while (curr) {
 			last = curr->last;
@@ -975,6 +980,11 @@  void vfio_combine_iova_ranges(struct rb_root_cached *root, u32 cur_nodes,
 			prev = curr;
 			curr = interval_tree_iter_next(curr, 0, ULONG_MAX);
 		}
+
+		/* Empty list or no nodes to combine */
+		if (WARN_ON_ONCE(min_gap == ULONG_MAX))
+			break;
+
 		comb_start->last = comb_end->last;
 		interval_tree_remove(comb_end, root);
 		cur_nodes--;