From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796384278; b=FZQR6fEi5KEWeA6BqqUAgpJ7STH59Esv957wlpwvfEczBBzos7SwN2swEI0fyW/Xvx4I CLc+93xntBcKfKYdzR/+I6IKl0M81H7XJi4IlRZqh40lnl814WUrAAhNgFkuIdtDtuhS9 zD7g3zm3Am6+9ruk9lHWcw5sYtmiP3YH8CBmbVWVpyNzb43NNUpIlBTotOWr75rd8A8oL uIidiYlMCOQZOGfu4LlfipF1mv22KOxbE1ILSNtC6gqBQZF/EkOcmv5CH14gmhs+A/UG5 bkKQ6H/DQEpacqGr1bkB2oYgR3pAgvNBzJDCX3NtA92jP1suVdUHwgbhzjrHohsH/NQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796384278; 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:In-Reply-To:References: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=mAG7S27OPetLIxNnINd/cEQ6B4cN5hSLJa0uRD9mg2k=; b=33Au7+56C1oOq8WJX0Z/YHDSaq7v+XB4qZXM95IMaBkTz1ge2rCK7O9zBxDFdhJLv2mm fKcrYuOmIoYSeNYvslCN7FLPwQPB7SteMWbWFq8FBs/Vg0DttCldpThUVKYZIwiaw6ElC 6r+jikZbTP7tyWc3oNBUTH5Swr+V5HpAX2DLcD+4cFoTdJhuB9/glzJOgBOEZKjygKQQS EmTKTI4VHDIVKzWW8UcKEaMdPucuJgUUhBpY/f+o3b4kTtiRT+JP366r+ccFemHSvDRRV rpLI9uaaAmUN5gPYT1ENZ74x4OeV5o15nEhWTAYLH2/c1UOf5cvPci8t18756So+Osg== 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=BjANc+zo; arc=none smtp.remote-ip=209.85.216.41 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (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 B2FB5606A32D for ; Fri, 4 Dec 2026 11:37:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B2FB5606A32D 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=BjANc+zo Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-3b3fb193c03so538172a91.2 for ; Fri, 04 Dec 2026 03:37:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796384277; x=1796989077; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=mAG7S27OPetLIxNnINd/cEQ6B4cN5hSLJa0uRD9mg2k=; b=BjANc+zofyt8HylCQSXa9f7+FT+padZnLmRZYz5yfkLFLzRt4XMDCkb0pVfQPYfrTg cn0ibRdlFKAIGacPDhQhxufDkU5s1+1nIdsdhCKjrFkabjger3y+aU6f6oudjEshDndi uE4v25YQHeve+JbISYTw/akxn81rUO8WaIgDFyIT2yOzAAg/wn1HAuD+4XutG2leEQB9 ++smgV/82pQqm+Yg1yvVeTwD6MoN8nt9P6iYP0E4KDUE4nrgNBiz8L3e3cllMFSl+tBg z8Fsmq6MFWe98z0nsSlAZLojObffz86wTXlhZV1vUZ1eTee42k5IijouCw1Y/TfX0OTf /Flg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796384277; x=1796989077; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to: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=mAG7S27OPetLIxNnINd/cEQ6B4cN5hSLJa0uRD9mg2k=; b=zwZixKjkYM6rp9tWaUxINFUk5MnHwO5wS+g+5oDNJDeodcgJ60tLo+l4qdUJXWgUKd EBpWYFbZPE76Fg8T1O+AVjcd9/SxHvvMrWuO+sRugLWwkzsKi2OJSKi9fDIGb4AsLEfg 87rUuwEUgRTLrnFD9LZfv2DuAW9zTfP7CrS49xVN+z6boNBf/vP75TlMJQcxFF4BEAGS JpLEPFV/Ltg4ABNTL5ruGZFPFbUGhfnhlxoKJDdWCteLOYE1RuUmeVg8VZ8x06xw9UCo oBe4lYiPpGys3gkF3B/CLz7Cw61HmVYBO2BsRzmrhZ8RyWEAVgW/ZAyanSvEy9IZ1Yo6 fPJA== X-Forwarded-Encrypted: i=1; AL+hErGMFxv+7QNFBc/mXRT36AbXo3fwi8Glz/kn8JUe6viZwfbr6EuV00+E2k6Ua+N2qOe3RjlkuYwAfZo=@vger.kernel.org X-Gm-Message-State: AB4q+uEcm7ELXXg9+8Q2/ktf/Pwi8swyDxdep/KH2hQRI+cBycbyEuvH tfbvWN8O4H7Z89brn/TZVTejqFAy/6gcnZXFma127uF5hKLaycWQKAWm X-Gm-Gg: AZFdTZcm64wk02IiNnBAydoNPvh5os54Y9mRtkiHNLN2b+nXFKZolV23EtJZRC0F2cs Q/RKt5YOr0KFoSkNznq2XC2alqDLAXF0VYXpPBlGFeGZDfkPv4TTapPmlwjHXp2wJSxIyUGGgbF Uxq/35TmqToXv4ZeaYyKt6bqR/91Y7oWZ3ktO1A4qzvCyIf41sPyALnEDPG3zEOIo6tZOkiV5S8 WRGuAdU+039S/ueRb4x4/1hzLY5huIZKs6wc36xNCRVBVbQo7XLl1ryVirtraNkzVyrfyqZw5iM WaI02q3PGSRINZUr9uBf/Kof78c5ojnUyprfA8GYG8jMO4ArEaSZU9Vj3cO2KrbHlQW5n5Y+GaT rsySIIaAr6amGz30Xw38xDWHn/oEB9PLqcDkaB+VvGIgM7bP3b6NvhHpFRHiZLlK2xFL+9YZR4P mKaYAOzenvOcUiP+oT9M9C1Szn/IichWtk1/bUEI3yZKHNSWkf2WMAE9qzleIl360suIKHhYlQS KzR9yplfDRvx9qHKQL5E7u/mf/pI+uqJIXPzkpGgpD2qMbZgBHCj1ICCmmhiefLf16s8HxQR4FG PCD+kX/T6c3x+IRfFDdjP2wC09p9cQVUk2AD+BiuK/kdlBuVQkpRMQRGEZCRSA4ieU+jdmzPQhc JSFYNLgG0wjXRe8Epfdh5twNtN+RJGevotWFb1VCFDXpGeivFvYmakUzakQ== X-Received: by 2002:a17:90b:5306:b0:3b0:d070:3c46 with SMTP id 98e67ed59e1d1-3b5e060052dmr1417798a91.12.1796384276779; Fri, 04 Dec 2026 03:37:56 -0800 (PST) Received: from ssslab.. (ssslab.skku.edu. [115.145.172.159]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8de121f3f95sm1347146b3a.11.2026.12.04.03.37.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 03:37:56 -0800 (PST) From: Jaegyun Lee To: gregkh@linuxfoundation.org Cc: Jaegyun Lee , nkapron@google.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v4] usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl Date: Fri, 4 Dec 2026 20:37:50 +0900 Message-Id: <179638424483.1056529.4365760725322283852@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-Mailing-List: linux-usb@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 ffs_ep0_ioctl() snapshots ffs->func without any lock or reference: struct ffs_function *func = ffs->func; ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV; ffs_func_revmap_intf() then walks func->interfaces_nums for interfaces_count iterations. ffs_func_unbind() first sets ffs->func = NULL, then after drain_workqueue() kfrees func->eps -- the allocation chunk that also holds interfaces_nums -- and later still writes func->interfaces_nums = NULL. An ioctl that read ffs->func before the NULL write can therefore be inside the revmap walk while the chunk is freed. John Keeping noted this in 2021: https://lkml.rescloud.iu.edu/2112.0/00724.html The following is the KASAN log: BUG: KASAN: slab-use-after-free in ffs_ep0_ioctl+0x29b/0x300 Read of size 2 at addr ffff888051b4925a by task park/9666 CPU: 1 UID: 0 PID: 9666 Comm: park Not tainted 7.4.0-rc3-g666539eecdf4 #1 PREEMPT(full) Call Trace: ffs_ep0_ioctl+0x29b/0x300 __x64_sys_ioctl+0x18f/0x210 do_syscall_64+0x132/0x7c0 Allocated by task 9675: __kmalloc_noprof+0x357/0x810 ffs_func_bind+0xbd1/0x1b00 usb_add_function+0x219/0x8a0 configfs_composite_bind+0xcd5/0x17f0 gadget_bind_driver+0x288/0xc80 Freed by task 9675: kfree+0x2ca/0x6e0 ffs_func_unbind+0x2f2/0x570 purge_configs_funcs+0x283/0x520 configfs_composite_unbind+0xae/0x210 gadget_unbind_driver+0x211/0x520 ... gadget_dev_desc_UDC_store+0x18a/0x2f0 The buggy address is located 602 bytes inside of freed 2048-byte region (kmalloc-2k) Take ffs->eps_lock around the REVMAP snapshot and walk, and do the ffs->func = NULL store in ffs_func_unbind() under the same lock. A walk that started before the store finishes before it, and every later ioctl sees NULL, so the walk can no longer overlap the free of the eps chunk. Reproduced and tested in QEMU (dummy_hcd) only, not on real UDC hardware. Fixes: ddf8abd25994 ("USB: f_fs: the FunctionFS driver") Cc: stable@vger.kernel.org Assisted-by: ZCode:GLM-5.4 Signed-off-by: Jaegyun Lee --- Changes in v4: - Read ffs->func once into a local under the lock. v3 read it twice, and the unlocked NULL store of ffs_func_disable() or ffs_func_set_alt() could land between the two reads (sashiko-bot). - Link to v3: https://patch.msgid.link/179638268904.917654.4905537541576241323@gmail.com diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -688,8 +688,12 @@ long ret; if (code == FUNCTIONFS_INTERFACE_REVMAP) { - struct ffs_function *func = ffs->func; - ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV; + struct ffs_function *func; + + scoped_guard(spinlock_irqsave, &ffs->eps_lock) { + func = ffs->func; + ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV; + } } else if (gadget && gadget->ops->ioctl) { ret = gadget->ops->ioctl(gadget, code, value); } else { @@ -4221,7 +4225,8 @@ if (ffs->func == func) { ffs_func_eps_disable(func); - ffs->func = NULL; + scoped_guard(spinlock_irqsave, &ffs->eps_lock) + ffs->func = NULL; } /* Drain any pending AIO completions */