Received: by 2002:a05:6358:795:b0:dc:4c66:fc3e with SMTP id n21csp2045704rwj; Sun, 30 Oct 2022 10:19:05 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7QBSNWXt44FLlYOr11oXKOWYgE/RUnDhKS5nta+h6nYTeIkpUKG5JIx7zdLxicsXPB9IJG X-Received: by 2002:a17:902:8bc9:b0:186:e151:6b29 with SMTP id r9-20020a1709028bc900b00186e1516b29mr10418821plo.8.1667150345668; Sun, 30 Oct 2022 10:19:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667150345; cv=none; d=google.com; s=arc-20160816; b=uyPNIuKeigyxCREcWzGCipJdKKbSvKqt4f/BxRO2ebSHrY8FwqSLgq2zLRLShMcFZP 1UpE2rxL24atkFG/SnT7xdDxhc/aemqA556q9g1CrZjUF2CbYMz2HbpIUv7djHIJia64 sqSXvcAM3kWA/ZpKVwgK0hft2A8w9SeUyZ4xa7s9wbjC9LC5fb1PGsDnym6YrF6Y4V3c AdhdTXVx/ygGxnC7lAUSfHw5iYMnBdVhTkRop3qWhmxOCgwKgIvjcpgBVpQJzGM1+I0H nRH1m2HYAZFySNeqHBUcqiKRxLBbHbFzscSZdH/a+aT+/b3RdnePcN4TL2JpZyLK356I O75Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=/OOE1cqrWBSeAoSKe3jvfAQJpwrq8lK+Su8hSKbtMC8=; b=FmRx7b1Oe2I2csj+hemdfs7ySISPLeHXQwtAgNd7Qni/M3Oy91E8NoO9w/8FE1VrI+ Q9wW3eb+fzHFu5t0f/9TNx7mzwb0KtjeOJBFQfqr32L7IoS9blNnxP5SzZubMX4XGzJU TQZnfWmK87FolpxWrQHprfNy+7m6MMknSdYhFdSOCDu7PaXGdxCk+miMTE7QaXvXVC1D AFSgSnaZs0K60o3dEl4wEHEoy1mICgGJlqr3HEc8+IKAo+5faqFPjTMghbSnwhXEPT+k ISnnQy1tpgh2ULvQPVT8NNwUOI1MUJn0uMJKYNlaIBTlH/N1+efJfM0xu/djT39WXDcd grng== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l9-20020a170903120900b00186e9ad55c1si5873979plh.434.2022.10.30.10.18.54; Sun, 30 Oct 2022 10:19:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229520AbiJ3QXX (ORCPT + 99 others); Sun, 30 Oct 2022 12:23:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229441AbiJ3QXV (ORCPT ); Sun, 30 Oct 2022 12:23:21 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 57F8018B for ; Sun, 30 Oct 2022 09:23:20 -0700 (PDT) Received: (qmail 162170 invoked by uid 1000); 30 Oct 2022 12:23:19 -0400 Date: Sun, 30 Oct 2022 12:23:19 -0400 From: Alan Stern To: Eli Billauer Cc: gregkh@linuxfoundation.org, arnd@arndb.de, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, imv4bel@gmail.com Subject: Re: [PATCH v2] char: xillybus: Prevent use-after-free due to race condition Message-ID: References: <20221030094209.65916-1-eli.billauer@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221030094209.65916-1-eli.billauer@gmail.com> X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS 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 Sun, Oct 30, 2022 at 11:42:09AM +0200, Eli Billauer wrote: > The driver for XillyUSB devices maintains a kref reference count on each > xillyusb_dev structure, which represents a physical device. This reference > count reaches zero when the device has been disconnected and there are no > open file descriptors that are related to the device. When this occurs, > kref_put() calls cleanup_dev(), which clears up the device's data, > including the structure itself. > > However, when xillyusb_open() is called, this reference count becomes > tricky: This function needs to obtain the xillyusb_dev structure that > relates to the inode's major and minor (as there can be several such). > xillybus_find_inode() (which is defined in xillybus_class.c) is called > for this purpose. xillybus_find_inode() holds a mutex that is global in > xillybus_class.c to protect the list of devices, and releases this > mutex before returning. As a result, nothing protects the xillyusb_dev's > reference counter from being decremented to zero before xillyusb_open() > increments it on its own behalf. Hence the structure can be freed > due to a rare race condition. > > To solve this, a mutex is added. It is locked by xillyusb_open() before > the call to xillybus_find_inode() and is released only after the kref > counter has been incremented on behalf of the newly opened inode. This > protects the kref reference counters of all xillyusb_dev structs from > being decremented by xillyusb_disconnect() during this time segment, as > the call to kref_put() in this function is done with the same lock held. > > There is no need to hold the lock on other calls to kref_put(), because > if xillybus_find_inode() finds a struct, xillyusb_disconnect() has not > made the call to remove it, and hence not made its call to kref_put(), > which takes place afterwards. Hence preventing xillyusb_disconnect's > call to kref_put() is enough to ensure that the reference doesn't reach > zero before it's incremented by xillyusb_open(). > > It would have been more natural to increment the reference count in > xillybus_find_inode() of course, however this function is also called by > Xillybus' driver for PCIe / OF, which registers a completely different > structure. Therefore, xillybus_find_inode() treats these structures as > void pointers, and accordingly can't make any changes. > > Reported-by: Hyunwoo Kim > Suggested-by: Alan Stern > Signed-off-by: Eli Billauer It looks like the xillybus driver already has a private mutex that would have been very well suited for this task: unit_mutex defined in xillybus_class.c. Of course, there's nothing wrong with using a new mutex instead -- just make sure there aren't any ABBA locking order problems. Alan Stern