From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796363629; b=IFHAHj2+QnPjCkQfCcxsggQl4S90a61fhiQnCgQPA2HTa+hpjIOz2o7sECyMyuqVltyo tbDANcNMMgbWKrzJcIDuV1JEQ6JRrfnwz6k7E6lsCJZVemGUa3eg6deEpXY76anXY/ygU OoaZsjqh8QinLYFl0bvjKG/KaE028zocO26zACeYZXqV1EjFdxSUXRU0h9yp8Y4+NQymN pmn0sgjGr2/QIGZvZtyT2Hqr8w0gXGcKXZWE3fjzSlqmK94Tfq/mC6arYGjsf1UyTz3cb JzmyKG/BfMDPO5E2wzdwPu6PMtN+gCxdHjfSX/lkBgEQXrdXauXI2QchKGfz7Dox+Hg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796363629; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=1JsTnQMUgAh4Ljc6otKG0IBmdfXMkZGS2WFe1QAxbIE=; b=3HPDiPoUj0sftQi1xO9fAI4pdvU7G5IbQlVwS3n2BeWADpAAxmGALtIZoA8yO/OVxLGu toirWK4R47XeojUgd9ztH12Bu5uAm6plhtr+kP9qZEHIx359iKIxBblcspjmUVSYARf+6 FAPN0CotH75WrU+au2FyUYklu//yzgGKaPjKbM3ftPjXXNcJLZYhd2uqhQ+quQ4Sn5lN0 Z0Mn/28lZFy7rJzIdM9WzSjKy6mCQLplKkcBy4mMw6XznfEfYHsRBv01OFjobl8bef3q1 vz1RQbIjEK7qdsLujgtRh3KjC8pPp8+yVId7byT/XFMCToQKr7fRG4xM83A4ouh6Icg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Fm8cDKWl; arc=none smtp.remote-ip=209.85.221.42 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3AAB606A32C for ; Fri, 4 Dec 2026 05:53:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E3AAB606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Fm8cDKWl Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-4a7b8abe209so345819f8f.0 for ; Thu, 03 Dec 2026 21:53:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796363627; x=1796968427; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=1JsTnQMUgAh4Ljc6otKG0IBmdfXMkZGS2WFe1QAxbIE=; b=Fm8cDKWlr7mZKDkhtbo0B+8iDL3EYIiL/wpRYtcUufefeFmbYPuPS0RtXTaanldl0g T1Dup5Sb1a40MY5/wOQ2lKKOg2gNpwxHlo8Qp327GT/DNKaAKMIrrc368xr8FTnu/X/o 859A64apfJU7BoSaa7El4Oruc4P1upZlX7RCkhc1ushlfpanPuaowwyG4aPEt5trxKVx F7C+sDRKBwK/PgV1EHm3eY97VUWhvqVRx2R8xAPyBW6xOD0ffXzkpBLFiPO9kFD/Nsxd 4vp/k/ZmMkvPxH+aIgmLyUWUu7+WJa6ewhBFgtPNk70wdPgZl4T6zpacgRG3wB8phr8A iYdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796363627; x=1796968427; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=1JsTnQMUgAh4Ljc6otKG0IBmdfXMkZGS2WFe1QAxbIE=; b=QUOEV25tgwa0xQjHSY7elqJuFUW1EEGtEzH1pDjvSg7/tIoOhAk931U/Hm0JqSeOv/ lqrGaH2f070Kr6SmGERngQ4vqm2m9+upuYGH4KaWrgdxf1tYbft+LdHaEuOkPO4pPVod Aq53lp6fKv/Jv4vKxO/IaIL69bR6fwO8EaG1VirMU/Ox5EtqKM1WDYt1QZjcSWcxKiEl niTy5LLyRB6SWVVZ/Ko78gp881hd9lFO93LybOKbW+oArqO9y4Frdr8rWvOznjNEBMzb ZLUuqd/DDhFdOKSkCq9VRqwIogvzUtFfR6f0o/trDXHKFIF4d8eGCvl//n3BTWtW7vHY S26Q== X-Forwarded-Encrypted: i=1; AL+hErHCogrZ0fTo/aui7UDrQB2XmxruYsgDOhBWFRNKx1ZsAfVRVjCz1d4493QZPCVkoF73/M2zIyPEJJOD8FE=@vger.kernel.org X-Gm-Message-State: APuU4Z95pNB0qUVYBHabaOrbytdD29IxY4lykRQzS0wFs0+v0ORGZVML /aL827G5AoDIvomOAz7HvwI9s0Tzh3KQc1HNMJEbQbv3vz2j02ndupmy X-Gm-Gg: AZFdTZdRFoPYB1aCBP8E2DTfUzPrxn9bkoxV8+bCEZKvZzVcPENZux+tPGMLCYZY5ad 4bCtww33fxisGFAbCXfSeuylOM0pRN2cRrlOJAKbQaquTVHufKPDQ5y4pc5XBaoOfond7nNefuT Z3KKDpce44j0Yl5KkNoVDbfgL6bl2kQJpWZoqWnS8O+1QR8rLX/RZ5WDvC0DsF8M1iW9eFnixiZ x6MTQWFVevwguwLrq9KmOF1aPavK5ZTqZZWW4rhGIhGXTJstdnqC4Lxj9TiCzPKjMNco7DTwS06 IzCk7aYt3gfKslPtWeFAx1ifpGrBj9L6JdqhLRXpWk9fyEIW32DhEgUBc+/0IXuvQA5X3QI0Pjk 19JX1nNfzDqC30Oixlb1a5ZBJgDFPYaWzy/dw0TZXJQsUeHvY1V5WU/EpL2Jtpdxe/kNto0mR10 MtNALJ4ePq9awCN+g0W3ma5E4q1B3FaPRb0WPiIHnmGZ/1NlRqC9Xt++S0kicfT6C3DfMpswBWK e3VJouArW9KK0dT6VD6wdNIWODkrTfLw4zMzy2H5msWzs99ffIJIBCLfP/9jEA2fBtfhemU/xqw IMV2CIXSXLbDN4B9OBxWnqu+Txd/rVopSYieLW08rh4EEz8fDHBRgUItwicB/Fyw3uxzsjDO3FV 6wE/yw6Niutj2TyIN5CHscnA= X-Received: by 2002:a05:600c:3113:b0:4bb:5e6b:49e with SMTP id 5b1f17b1804b1-4bbeb87716bmr14325345e9.6.1796363627291; Thu, 03 Dec 2026 21:53:47 -0800 (PST) Received: from Raghu007.. (sgyl-44-b2-v4wan-174108-cust110.vm6.cable.virginm.net. [80.1.81.111]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbe0030c6fsm142431965e9.2.2026.12.03.21.53.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 21:53:47 -0800 (PST) From: Palla Raghunath To: Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org, Neill Kapron , =?UTF-8?q?Micha=C5=82=20Nazarewicz?= , Shuangpeng Bai , Shuah Khan , Brigham Campbell , linux-kernel-mentees@lists.linux.dev, linux-kernel@vger.kernel.org, stable@vger.kernel.org, syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com, raghunathpalla.0209@gmail.com Subject: [PATCH RESEND v2] usb: gadget: f_fs: fix use-after-free of ffs_dev in ffs_free_inst() Date: Fri, 4 Dec 2026 05:53:46 +0000 Message-Id: <20261204055346.42890-1-raghunathpalla.0209@gmail.com> X-Mailer: git-send-email 2.34.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a function instance is removed through configfs, ffs_free_inst() first calls ffs_release_dev(), which takes ffs_dev_lock, detaches the dev from its mount and drops the lock again. Only then does it retake the lock and free the dev with _ffs_free_dev(). Between those two steps the dev is still on the ffs_devices list. If someone mounts functionfs with the same instance name right then, ffs_acquire_dev() finds the dev, marks it mounted and points the new ffs_data at it. The dev is freed a moment later, and when that mount is torn down, ffs_closed() writes to freed memory. syzbot hit this: BUG: KASAN: slab-use-after-free in ffs_closed drivers/usb/gadget/function/f_fs.c:4427 [inline] BUG: KASAN: slab-use-after-free in ffs_data_clear+0x543/0x5b0 drivers/usb/gadget/function/f_fs.c:2305 Write of size 1 at addr ffff8880237b1e4a by task syz-executor/11910 Call Trace: ffs_closed drivers/usb/gadget/function/f_fs.c:4427 [inline] ffs_data_clear+0x543/0x5b0 drivers/usb/gadget/function/f_fs.c:2305 ffs_data_reset drivers/usb/gadget/function/f_fs.c:2336 [inline] ffs_fs_kill_sb+0x84/0x370 drivers/usb/gadget/function/f_fs.c:2180 deactivate_locked_super+0xbe/0x110 fs/super.c:586 cleanup_mnt+0x3d3/0x460 fs/namespace.c:1329 ... Freed by task 17408: kfree+0x1c5/0x650 mm/slub.c:6923 _ffs_free_dev drivers/usb/gadget/function/f_fs.c:4336 [inline] ffs_free_inst+0x233/0x2c0 drivers/usb/gadget/function/f_fs.c:4152 usb_put_function_instance+0x95/0xc0 drivers/usb/gadget/functions.c:77 config_item_release+0x13a/0x2d0 fs/configfs/item.c:137 configfs_rmdir+0x885/0x950 fs/configfs/dir.c:1580 Fix it by doing the release and the free while holding the lock once. To allow that, move the body of ffs_release_dev() into a new _ffs_release_dev() that expects ffs_lock to be held already, annotated with __must_hold() so the compiler can check that, and keep ffs_release_dev() as a wrapper for ffs_data_put(). A racing mount now either gets the dev before ffs_free_inst() runs, in which case the release clears its pointer, or doesn't find the dev at all. I was able to reproduce this in QEMU with a small program that keeps creating and removing an ffs function directory in configfs while a second thread mounts and unmounts functionfs with the same name. On an unpatched kernel it hit the same KASAN report within about ten minutes. With this patch applied, the same program ran for 30 minutes without any problem. Fixes: ecfbd7b9054b ("usb: gadget: f_fs: Fix setting of device and driver data cross-references") Cc: stable@vger.kernel.org Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0 Acked-by: Michał Nazarewicz Signed-off-by: Palla Raghunath --- RESEND v2: no code change. Greg asked whether Shuangpeng Bai's patch "usb: gadget: f_fs: fix use-after-free in ffs_func_unbind" [1] already fixes this. It doesn't. That patch is about struct ffs_data being used after functionfs_unbind() in ffs_func_unbind(). This one is about struct ffs_dev: syzbot's report shows the object allocated in ffs_alloc_inst() and freed by _ffs_free_dev() from ffs_free_inst(), and the race is in the window where ffs_free_inst() drops ffs_dev_lock while the dev is still on ffs_devices. No gadget bind or unbind is involved. ffs_func_unbind() isn't in the report, and my reproducer only races configfs mkdir/rmdir against mount/umount without binding a UDC. The other patch doesn't touch ffs_free_inst(), so the window stays. [1] https://lore.kernel.org/r/20261120210616.2293426-1-shuangpeng.kernel@gmail.com v2: - Annotate _ffs_release_dev() with __must_hold(&ffs_lock) so the locking rule is checked by the compiler (Greg) - Fix the comment to name ffs_lock, the actual mutex - Add Michał's Acked-by v1: https://lore.kernel.org/all/20261006204202.59604-1-raghunathpalla.0209@gmail.com/ drivers/usb/gadget/function/f_fs.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c index c64a268e98a4..996fa0767de2 100644 --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -288,6 +288,8 @@ static struct ffs_dev *_ffs_find_dev(const char *name); static struct ffs_dev *_ffs_alloc_dev(void); static void _ffs_free_dev(struct ffs_dev *dev); static int ffs_acquire_dev(const char *dev_name, struct ffs_data *ffs_data); +static void _ffs_release_dev(struct ffs_dev *ffs_dev) + __must_hold(&ffs_lock); static void ffs_release_dev(struct ffs_dev *ffs_dev); static int ffs_ready(struct ffs_data *ffs); static void ffs_closed(struct ffs_data *ffs); @@ -4147,8 +4149,8 @@ static void ffs_free_inst(struct usb_function_instance *f) struct f_fs_opts *opts; opts = to_f_fs_opts(f); - ffs_release_dev(opts->dev); ffs_dev_lock(); + _ffs_release_dev(opts->dev); _ffs_free_dev(opts->dev); ffs_dev_unlock(); kfree(opts); @@ -4363,10 +4365,12 @@ static int ffs_acquire_dev(const char *dev_name, struct ffs_data *ffs_data) return ret; } -static void ffs_release_dev(struct ffs_dev *ffs_dev) +/* + * Same as ffs_release_dev(), for callers that already hold ffs_lock. + */ +static void _ffs_release_dev(struct ffs_dev *ffs_dev) + __must_hold(&ffs_lock) { - ffs_dev_lock(); - if (ffs_dev && ffs_dev->mounted) { ffs_dev->mounted = false; if (ffs_dev->ffs_data) { @@ -4377,7 +4381,12 @@ static void ffs_release_dev(struct ffs_dev *ffs_dev) if (ffs_dev->ffs_release_dev_callback) ffs_dev->ffs_release_dev_callback(ffs_dev); } +} +static void ffs_release_dev(struct ffs_dev *ffs_dev) +{ + ffs_dev_lock(); + _ffs_release_dev(ffs_dev); ffs_dev_unlock(); } -- 2.34.1