Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4492512pxb; Mon, 21 Feb 2022 23:11:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJzJs9N0IPODj/IvNada+qJpjdbrk7oFun5uP3c6jrNumqDSXbqKw1zhZsA7wYvM1wahGTLt X-Received: by 2002:a63:7150:0:b0:372:e0e0:f1a4 with SMTP id b16-20020a637150000000b00372e0e0f1a4mr18801344pgn.507.1645513889408; Mon, 21 Feb 2022 23:11:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645513889; cv=none; d=google.com; s=arc-20160816; b=irvoBwCAcbsgKgPIaJj0uuYqLjdRSI+uABmq6UHG7AKexw/UyZTgCXxKsHKGiXm0Ls 59xGRW3kbHUEGG96/3H1opbffpPbeRa7tLWu9UElLTA6KorYzlxZp+HDyrjuRa+48bmF ouXxHBXDxCCBCvvrL1UYZvmNrlTXPLlg4O2Uv/hlkEwe75wjs+1kyYs/bvU2wsh79I4x wDstxUdzSpiGDqHj1cNkbLC36rwqkLRVLPmJi2uXgKga4tAaDinM9bJ7HtIEv+h61FQ/ RWewXOYsfboi0VLlxQIRAgsD21ASCZo6ooOtmXrevmFtujGmWNAYjFyOcUyRmi1EqmyX 1yuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=7FSv/oEcapgvDoGQHzrlqg4yIRZgWGWLxeotDRNQuUU=; b=kdTlA83sT/iHsS/7wnaDLWjolxZhkMSjrHzQk2v2oH8nuH1UmI1v3FLq8obsTXzrzJ B0Z1BLyxDPWJZgYphY3g9ifcNJAT1VcgILTydmz91n6pT0vrGoWPHPrBpXDKEe35OTMu ZxqfPjIoDcapZ8+hIvSPc0m1YSWBvQgrumwo7punANA6Lwjd7rPZAjgkmsEKcBHl5/Jd /sUd0BQgLc9aLeThFIQkrncIXz9H0KELrm8qCti6hQljwTmguEokfJOAU41UYv8zbaVZ iZQpNDp8jjuSuJgCeGt8wUlQzhbXpSHeO8HzQwUgYceJ34WTIPDnEuKTsbsonayywT6x KbxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QfYMeUWd; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id x186si3629425pgb.787.2022.02.21.23.11.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Feb 2022 23:11:29 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QfYMeUWd; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AD56C1160C0; Mon, 21 Feb 2022 23:11:27 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230436AbiBVHLt (ORCPT + 99 others); Tue, 22 Feb 2022 02:11:49 -0500 Received: from gmail-smtp-in.l.google.com ([23.128.96.19]:36352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230438AbiBVHLs (ORCPT ); Tue, 22 Feb 2022 02:11:48 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5772BB1098 for ; Mon, 21 Feb 2022 23:11:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645513882; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=7FSv/oEcapgvDoGQHzrlqg4yIRZgWGWLxeotDRNQuUU=; b=QfYMeUWdbHNfKBqd2cikNJgC6ukjCBCDCH4DoFuZ8YCYw3eXgJEoFLMIz8op/hX9qqcqk6 aD6WjMYVqPg5hSreHPiFZ/+IbNP/l4j4ShxRgf+Kkc6BF76trHJbU6SIMX2Vh0Ou+nwzeF 9BGGFr2og7MkCum1ooZZdwraODGB5kM= Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-441-rla05-UmNcqZQGRcof4XkA-1; Tue, 22 Feb 2022 02:11:20 -0500 X-MC-Unique: rla05-UmNcqZQGRcof4XkA-1 Received: by mail-lj1-f197.google.com with SMTP id o1-20020a2eb301000000b0024640251ba9so2336410lja.12 for ; Mon, 21 Feb 2022 23:11:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=7FSv/oEcapgvDoGQHzrlqg4yIRZgWGWLxeotDRNQuUU=; b=HIRCeoy9JqYJYpi67Uld+Q4UYx3uUWd5Rb2tcGK3xN9KUXzQfdrNaDDe8BErksn801 g7Aro2Fu6wevCbUu/D956a8OGah3av3Yds3qDEdUbdm8Ek5v4bWd/gjHFFHp0kCIKtew udDX0EH7Tj3Wh2WPvKC01m+SwseQd341zB74D3OATeNJ86vTKB5qGxaJ85DxvYVo1+gP jJyjILqNSJcnEIw6m8GR3fJTtghSpBB/RtJiZe8DjlwpVERdxaoNML/9u8tH/tbjuKLw T9QT2p5L/iIADK6Ec0Ynp7M4PwqHdaH8DWRTkDZCD/Ia5odo1MJgjmu9PtVAlCfKyURj jKMg== X-Gm-Message-State: AOAM530nakpRQs5G+N6/LlkXWtGI9zz3gOX6fkOygXEeH67Sn5CigVHd XAh/kgh7lrkEB2ctZnuMxqQZdsulvCiXmwr8Yd9vekpO5Rb3f3I8wp+5+RIG3aHOPscqMDS2WNt tzjoUIaIH4pzZjUCfZEgBhSbqHlaoYPZxOL7P7hbe X-Received: by 2002:ac2:5dc9:0:b0:443:5db1:244c with SMTP id x9-20020ac25dc9000000b004435db1244cmr16719971lfq.84.1645513878929; Mon, 21 Feb 2022 23:11:18 -0800 (PST) X-Received: by 2002:ac2:5dc9:0:b0:443:5db1:244c with SMTP id x9-20020ac25dc9000000b004435db1244cmr16719955lfq.84.1645513878661; Mon, 21 Feb 2022 23:11:18 -0800 (PST) MIME-Version: 1.0 References: <20220221195303.13560-1-mail@anirudhrb.com> In-Reply-To: From: Jason Wang Date: Tue, 22 Feb 2022 15:11:07 +0800 Message-ID: Subject: Re: [PATCH] vhost: validate range size before adding to iotlb To: Anirudh Rayabharam Cc: "Michael S. Tsirkin" , syzbot+0abd373e2e50d704db87@syzkaller.appspotmail.com, kvm , virtualization , netdev , linux-kernel Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Feb 22, 2022 at 12:57 PM Anirudh Rayabharam wrote: > > On Tue, Feb 22, 2022 at 10:50:20AM +0800, Jason Wang wrote: > > On Tue, Feb 22, 2022 at 3:53 AM Anirudh Rayabharam wrote: > > > > > > In vhost_iotlb_add_range_ctx(), validate the range size is non-zero > > > before proceeding with adding it to the iotlb. > > > > > > Range size can overflow to 0 when start is 0 and last is (2^64 - 1). > > > One instance where it can happen is when userspace sends an IOTLB > > > message with iova=size=uaddr=0 (vhost_process_iotlb_msg). So, an > > > entry with size = 0, start = 0, last = (2^64 - 1) ends up in the > > > iotlb. Next time a packet is sent, iotlb_access_ok() loops > > > indefinitely due to that erroneous entry: > > > > > > Call Trace: > > > > > > iotlb_access_ok+0x21b/0x3e0 drivers/vhost/vhost.c:1340 > > > vq_meta_prefetch+0xbc/0x280 drivers/vhost/vhost.c:1366 > > > vhost_transport_do_send_pkt+0xe0/0xfd0 drivers/vhost/vsock.c:104 > > > vhost_worker+0x23d/0x3d0 drivers/vhost/vhost.c:372 > > > kthread+0x2e9/0x3a0 kernel/kthread.c:377 > > > ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:295 > > > > > > > > > Reported by syzbot at: > > > https://syzkaller.appspot.com/bug?extid=0abd373e2e50d704db87 > > > > > > Reported-by: syzbot+0abd373e2e50d704db87@syzkaller.appspotmail.com > > > Tested-by: syzbot+0abd373e2e50d704db87@syzkaller.appspotmail.com > > > Signed-off-by: Anirudh Rayabharam > > > --- > > > drivers/vhost/iotlb.c | 6 ++++-- > > > 1 file changed, 4 insertions(+), 2 deletions(-) > > > > > > diff --git a/drivers/vhost/iotlb.c b/drivers/vhost/iotlb.c > > > index 670d56c879e5..b9de74bd2f9c 100644 > > > --- a/drivers/vhost/iotlb.c > > > +++ b/drivers/vhost/iotlb.c > > > @@ -53,8 +53,10 @@ int vhost_iotlb_add_range_ctx(struct vhost_iotlb *iotlb, > > > void *opaque) > > > { > > > struct vhost_iotlb_map *map; > > > + u64 size = last - start + 1; > > > > > > - if (last < start) > > > + // size can overflow to 0 when start is 0 and last is (2^64 - 1). > > > + if (last < start || size == 0) > > > return -EFAULT; > > > > I'd move this check to vhost_chr_iter_write(), then for the device who > > has its own msg handler (e.g vDPA) can benefit from it as well. > > Thanks for reviewing! > > I kept the check here thinking that all devices would benefit from it > because they would need to call vhost_iotlb_add_range() to add an entry > to the iotlb. Isn't that correct? Correct for now but not for the future, it's not guaranteed that the per device iotlb message handler will use vhost iotlb. But I agree that we probably don't need to care about it too much now. > Do you see any other benefit in moving > it to vhost_chr_iter_write()? > > One concern I have is that if we move it out some future caller to > vhost_iotlb_add_range() might forget to handle this case. Yes. Rethink the whole fix, we're basically rejecting [0, ULONG_MAX] range which seems a little bit odd. I wonder if it's better to just remove the map->size. Having a quick glance at the the user, I don't see any blocker for this. Thanks > > Thanks! > > - Anirudh. > > > > > Thanks > > > > > > > > if (iotlb->limit && > > > @@ -69,7 +71,7 @@ int vhost_iotlb_add_range_ctx(struct vhost_iotlb *iotlb, > > > return -ENOMEM; > > > > > > map->start = start; > > > - map->size = last - start + 1; > > > + map->size = size; > > > map->last = last; > > > map->addr = addr; > > > map->perm = perm; > > > -- > > > 2.35.1 > > > > > >