Re: [PATCH v2] vfio: Fix uninitialized symbol and potential dereferencing errors in vfio_combine_iova_ranges

From: Alex Williamson
Date: Tue Sep 19 2023 - 14:05:54 EST


On Thu, 14 Sep 2023 17:08:39 +0800
Cong Liu <liucong2@xxxxxxxxxx> wrote:

> when compiling with smatch check, the following errors were encountered:
>
> drivers/vfio/vfio_main.c:957 vfio_combine_iova_ranges() error: uninitialized symbol 'last'.
> drivers/vfio/vfio_main.c:978 vfio_combine_iova_ranges() error: potentially dereferencing uninitialized 'comb_end'.
> drivers/vfio/vfio_main.c:978 vfio_combine_iova_ranges() error: potentially dereferencing uninitialized 'comb_start'.
>
> this patch fix these error.
>
> Signed-off-by: Cong Liu <liucong2@xxxxxxxxxx>
> ---
> drivers/vfio/vfio_main.c | 15 +++++++++++++--
> 1 file changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
> index 40732e8ed4c6..96d2f3030ebb 100644
> --- a/drivers/vfio/vfio_main.c
> +++ b/drivers/vfio/vfio_main.c
> @@ -938,14 +938,17 @@ static int vfio_ioctl_device_feature_migration(struct vfio_device *device,
> void vfio_combine_iova_ranges(struct rb_root_cached *root, u32 cur_nodes,
> u32 req_nodes)
> {
> - struct interval_tree_node *prev, *curr, *comb_start, *comb_end;
> + struct interval_tree_node *prev, *curr;
> + struct interval_tree_node *comb_start = NULL, *comb_end = NULL;
> unsigned long min_gap, curr_gap;
>
> /* Special shortcut when a single range is required */
> if (req_nodes == 1) {
> - unsigned long last;
> + unsigned long last = 0;
>
> comb_start = interval_tree_iter_first(root, 0, ULONG_MAX);
> + if (!comb_start)
> + return;
> curr = comb_start;
> while (curr) {
> last = curr->last;

@last no longer requires initialization with the @comb_start test.

However, all of these are testing for invalid parameters, which I think
we can eliminate if we simply introduce the following at the start of
the function:

if (!cur_nodes || cur_nodes <= req_nodes ||
WARN_ON(!req_nodes || !root->rb_root.rb_node))
return;

At that point we're guaranteed to have any entry for both the above and
below first entry and there must be at least a second entry (or a
driver bug telling us there are more entries than actually exist) for
the next call below. Thanks,

Alex


> @@ -963,6 +966,10 @@ void vfio_combine_iova_ranges(struct rb_root_cached *root, u32 cur_nodes,
> prev = NULL;
> min_gap = ULONG_MAX;
> curr = interval_tree_iter_first(root, 0, ULONG_MAX);
> + if (!curr) {
> + /* No more ranges to combine */
> + break;
> + }
> while (curr) {
> if (prev) {
> curr_gap = curr->start - prev->last;
> @@ -975,6 +982,10 @@ void vfio_combine_iova_ranges(struct rb_root_cached *root, u32 cur_nodes,
> prev = curr;
> curr = interval_tree_iter_next(curr, 0, ULONG_MAX);
> }
> + if (!comb_start || !comb_end) {
> + /* No more ranges to combine */
> + break;
> + }
> comb_start->last = comb_end->last;
> interval_tree_remove(comb_end, root);
> cur_nodes--;