Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1141971rdh; Mon, 25 Sep 2023 04:45:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE6BxpFv6juaNkZlxmg9UdmnN5RYBD+NDLWE/2+jAG9ad5wwunjInKfab4XqEvk+XUtVeTy X-Received: by 2002:a05:6808:1823:b0:3ae:100d:5320 with SMTP id bh35-20020a056808182300b003ae100d5320mr6896132oib.2.1695642302299; Mon, 25 Sep 2023 04:45:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695642302; cv=none; d=google.com; s=arc-20160816; b=AzziExWnTo7PfBoIYhGLCr6hH1GNHZ+WmCxqDWIQi/+sEm7NT9dLZBJsaw9wVyx5py sT5AluWpJcMWcsM79jOEb2vtN0STkGTa9KcEyJC3Fytdjvb+CMkAVXhlnVEPWb2J5WIR ThuRJbBAjucgLFFODqFFCb0VAS4gZtkNpdXhabxY8BMknmNjlVTYrnOtunmcxFgHJeAO 0sTDtMotEzf6z86wJr6nVeDmkPPMKtAabyKA4DeOnE1EtReFmbd8hI0J/9dBl32lOZQW PdScrmDqUyrqoc6y8b4UH8Z8Bz+E3b+aK/lqhxNvXF0Au99CGtl5ZJHiXo/KtHTdAvuE YJGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=3SOX1sUAjrW5/F0GPz8qWm4h1iURYcsVmq2K+RtdMls=; fh=4h8vZJTHX0sT2HOGFDoE4f8IK0l3amwtU9pmfp1rfdU=; b=geA8wAzX50i0eoBxtDn9DoufQSe1pa6NO/WBZOn/r2lIR7eScDzcWQHSdYLfMa3aow 2P2HluyE7gltIKlI17bXw7HMxHuSnnwQMEBtPrOzEThWoMK+Plemu1C60i+M8PV7xXpK mOG8jvNbT0Wgkj5AtlE+M2fu1PwK32yNwo4/JxjNH92GUoOiFvB3nsUYjn6bKhOYECBT 80cKLyduoL7bLNFGf+VQECz6LV3aEJNgJ8lziMGPOOop+pmqMF3mJ1MNfViGXlxxNoaf Rh6GkpG1i/FzNAxcEBfqIX70ebzA7NBvi4Ul7NCBxDH0KfsMEskNhDcSYwPNaNwFUd+w reug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=UktWqUJI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id m16-20020a63fd50000000b00563dfffe7b9si10147862pgj.810.2023.09.25.04.45.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 04:45:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=UktWqUJI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 1080D82B92AC; Mon, 25 Sep 2023 00:59:59 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232573AbjIYH76 (ORCPT + 99 others); Mon, 25 Sep 2023 03:59:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232560AbjIYH7y (ORCPT ); Mon, 25 Sep 2023 03:59:54 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E20ED3; Mon, 25 Sep 2023 00:59:48 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-690d2441b95so4309504b3a.1; Mon, 25 Sep 2023 00:59:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695628787; x=1696233587; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=3SOX1sUAjrW5/F0GPz8qWm4h1iURYcsVmq2K+RtdMls=; b=UktWqUJIZF7NCG5qgnZX9pzpTBm/pRAcf2tWVVfzQz6r5ogKO1r47p6eRcACIRqmfu qBhogYLilnnMTfHjLZFc3qvPzNUb9NyarvOTsxuKvW99LChry84ZAUBCV2PFkdJrBAbs gumNjGVSjuGtEewRMUTdsPEP9sAbkn1ZEXTjkUZ66hE8z4WESfEysxJV8tRDkOtC23Y+ C70bIZKfTcHeDR+SnKrNkWW3LHBGW+iKNaytJOZghE5r+lTwKkswCTfD2LxIBfn9JMTw hArckVaOnRAjCNki7JR3dxPpprKovzNsAon4ZpKf/HYZZj/DnssjFy4KgAYXG8wH/584 ENFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695628787; x=1696233587; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3SOX1sUAjrW5/F0GPz8qWm4h1iURYcsVmq2K+RtdMls=; b=i2p+FvDVgLG6JKrqNBdkKbrtZ4IRRTSKlQ/O6QxYi6Q//Y2X7EyU+KSna+9AGZJm8o GyebVHdjK5PoRA6dhoqUtznTwz1qHRGMdMsx7FzwRZLb02L6T8981m4BF3B730piAygH W21FF+2Hg6dlNYpztm+13a6MBAuHaDWYqW8wgyc/OVqZIq/S6O0YgMgCzLOgUrJ+brT6 1U6SNpA/FFqYZQrJsM3tf7irCp035ObXp100AuKOhtqNXiZbxB7aNR+KlA+vSb1rWzrR 0pIRTQt6CNXZRaFbqmhn0PXqKNydB4hmRBxOkMhbjqpdyYB18Iexwa0F2Romquk9izxW H3BA== X-Gm-Message-State: AOJu0YzDyv7ci9tjmv+MPg0bMs5L/6RoMbUpR6jCGPeU2jXNczlUy7Bq 2b56cDHRCmtCzI0pFIT1WIGNFm0xESvuFw== X-Received: by 2002:a05:6a20:431a:b0:15c:8555:a21c with SMTP id h26-20020a056a20431a00b0015c8555a21cmr9019477pzk.8.1695628787450; Mon, 25 Sep 2023 00:59:47 -0700 (PDT) Received: from [192.168.255.10] ([103.7.29.32]) by smtp.gmail.com with ESMTPSA id i14-20020aa787ce000000b006900cb919b8sm7438827pfo.53.2023.09.25.00.59.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Sep 2023 00:59:47 -0700 (PDT) Message-ID: <74dc2c4a-077d-a538-4c4d-9f2141702ada@gmail.com> Date: Mon, 25 Sep 2023 15:59:40 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH v2 1/2] KVM: eventfd: Fix NULL deref irqbypass producer Content-Language: en-US To: Alex Williamson , Sean Christopherson Cc: Paolo Bonzini , linux-kernel@vger.kernel.org, kvm@vger.kernel.org References: <20230802051700.52321-1-likexu@tencent.com> <20230802051700.52321-2-likexu@tencent.com> From: Like Xu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 25 Sep 2023 00:59:59 -0700 (PDT) Hi Alex, what do you think of Sean's proposal diff below ? By the way, could anyone to accept patch 1/2, thanks. On 17/8/2023 2:37 am, Sean Christopherson wrote: > On Wed, Aug 02, 2023, Like Xu wrote: >> From: Like Xu >> >> Adding guard logic to make irq_bypass_register/unregister_producer() >> looks for the producer entry based on producer pointer itself instead >> of pure token matching. >> >> As was attempted commit 4f3dbdf47e15 ("KVM: eventfd: fix NULL deref >> irqbypass consumer"), two different producers may occasionally have two >> identical eventfd's. In this case, the later producer may unregister >> the previous one after the registration fails (since they share the same >> token), then NULL deref incurres in the path of deleting producer from >> the producers list. >> >> Registration should also fail if a registered producer changes its >> token and registers again via the same producer pointer. >> >> Cc: Alex Williamson >> Signed-off-by: Like Xu >> Reviewed-by: Alex Williamson >> --- >> virt/lib/irqbypass.c | 4 ++-- >> 1 file changed, 2 insertions(+), 2 deletions(-) >> >> diff --git a/virt/lib/irqbypass.c b/virt/lib/irqbypass.c >> index 28fda42e471b..e0aabbbf27ec 100644 >> --- a/virt/lib/irqbypass.c >> +++ b/virt/lib/irqbypass.c >> @@ -98,7 +98,7 @@ int irq_bypass_register_producer(struct irq_bypass_producer *producer) >> mutex_lock(&lock); >> >> list_for_each_entry(tmp, &producers, node) { >> - if (tmp->token == producer->token) { >> + if (tmp->token == producer->token || tmp == producer) { >> ret = -EBUSY; >> goto out_err; >> } >> @@ -148,7 +148,7 @@ void irq_bypass_unregister_producer(struct irq_bypass_producer *producer) >> mutex_lock(&lock); >> >> list_for_each_entry(tmp, &producers, node) { >> - if (tmp->token != producer->token) >> + if (tmp != producer) > > What are the rules for using these APIs? E.g. is doing unregister without > first doing a register actually allowed? Ditto for having multiple in-flight > calls to (un)register the exact same producer or consumer. > > E.g. can we do something like the below, and then remove the list iteration to > find the passed in pointer (which is super odd IMO). Obviously not a blocker > for this patch, but it seems like we could achieve a simpler and more performant > implementation if we first sanitize the rules and the usage. > > diff --git a/virt/lib/irqbypass.c b/virt/lib/irqbypass.c > index 28fda42e471b..be0ba4224a23 100644 > --- a/virt/lib/irqbypass.c > +++ b/virt/lib/irqbypass.c > @@ -90,6 +90,9 @@ int irq_bypass_register_producer(struct irq_bypass_producer *producer) > if (!producer->token) > return -EINVAL; > > + if (WARN_ON_ONCE(producer->node.prev && !list_empty(&producer->node))) > + return -EINVAL; > + > might_sleep(); > > if (!try_module_get(THIS_MODULE)) > @@ -140,6 +143,9 @@ void irq_bypass_unregister_producer(struct irq_bypass_producer *producer) > if (!producer->token) > return; > > + if (WARN_ON_ONCE(!producer->node.prev || list_empty(&producer->node))) > + return; > + > might_sleep(); > > if (!try_module_get(THIS_MODULE)) >