From patchwork Tue Mar 12 15:48:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Stern X-Patchwork-Id: 13590241 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by smtp.subspace.kernel.org (Postfix) with SMTP id E82707A70A for ; Tue, 12 Mar 2024 15:48:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.131.102.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710258512; cv=none; b=pkTur11jX1FHnjz18xKuq63GgIycK6odBJJ1jHE8HnO64z4Nex27XD4Dey0Al8tYnviAHG/jWO8c0Qk2bUsYd3uCUvtKFHpN9iZdYysIBS5AACDuzKSEtuTU5qcS36+mQ1++9wrV0JeRTgcXJh+GZ+xNrW4aA6pMnWwV/9Lgpwo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710258512; c=relaxed/simple; bh=OAxgWubk8b2LNJG5CSJSfPS+XTxB9mLiy3tW8lBp940=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition; b=JL2NBTprA3I8+01ppVEbqziB/J0aIG/Gk3Idh6MKRCNOi3PhZrLGa1Xt07GX4LIzqyltjqqrTmWSDRWwqOZmAipMfQW2PDECXN8MGfLvNbNWpr+TeE7MjVSG1YGqollSamygMVKD9HNfVWozB1fJ7ihB4+odEkOHKVmF0vPPkzc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=rowland.harvard.edu; spf=pass smtp.mailfrom=netrider.rowland.org; arc=none smtp.client-ip=192.131.102.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=rowland.harvard.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netrider.rowland.org Received: (qmail 386421 invoked by uid 1000); 12 Mar 2024 11:48:23 -0400 Date: Tue, 12 Mar 2024 11:48:23 -0400 From: Alan Stern To: Greg KH Cc: Yue Sun , xingwei lee , USB mailing list Subject: [PATCH] USB: core: Fix deadlock in usb_deauthorize_interface() Message-ID: <1c37eea1-9f56-4534-b9d8-b443438dc869@rowland.harvard.edu> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline Among the attribute file callback routines in drivers/usb/core/sysfs.c, the interface_authorized_store() function is the only one which acquires a device lock on an ancestor device: It calls usb_deauthorize_interface(), which locks the interface's parent USB device. The will lead to deadlock if another process already owns that lock and tries to remove the interface, whether through a configuration change or because the device has been disconnected. As part of the removal procedure, device_del() waits for all ongoing sysfs attribute callbacks to complete. But usb_deauthorize_interface() can't complete until the device lock has been released, and the lock won't be released until the removal has finished. The mechanism provided by sysfs to prevent this kind of deadlock is to use the sysfs_break_active_protection() function, which tells sysfs not to wait for the attribute callback. Reported-and-tested by: Yue Sun Reported by: xingwei lee Signed-off-by: Alan Stern Link: https://lore.kernel.org/linux-usb/CAEkJfYO6jRVC8Tfrd_R=cjO0hguhrV31fDPrLrNOOHocDkPoAA@mail.gmail.com/#r Fixes: 310d2b4124c0 ("usb: interface authorization: SysFS part of USB interface authorization") Cc: --- drivers/usb/core/sysfs.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) Index: usb-devel/drivers/usb/core/sysfs.c =================================================================== --- usb-devel.orig/drivers/usb/core/sysfs.c +++ usb-devel/drivers/usb/core/sysfs.c @@ -1168,14 +1168,24 @@ static ssize_t interface_authorized_stor { struct usb_interface *intf = to_usb_interface(dev); bool val; + struct kernfs_node *kn; if (kstrtobool(buf, &val) != 0) return -EINVAL; - if (val) + if (val) { usb_authorize_interface(intf); - else - usb_deauthorize_interface(intf); + } else { + /* + * Prevent deadlock if another process is concurrently + * trying to unregister intf. + */ + kn = sysfs_break_active_protection(&dev->kobj, &attr->attr); + if (kn) { + usb_deauthorize_interface(intf); + sysfs_unbreak_active_protection(kn); + } + } return count; }