From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796384278; b=t7xLFEF6f4FXpl+U3hlAbGBXXmIgakFoI+pGYB38oywx+nqfia5j5Wrokz4VdedZikga 22z1BnVoPFJdiSXuRV+HQYTrdj7egqGKF/VZT3w8L8E+hBVRAvAon9W2qjJVzy5ozznLC qfIBq6Kyy1jJPWlIizgze/Rgs+vmqyKINc+pDkYYB5buLveUWdpR8ScezlrXnmhJNSXLr DXS1X2zS4Qfu79QXsTBGZoqYvxQQINRq/OMcHDLrm73JHxZsM+/ylk6YiT3j2HqYBjPV+ 28x8lYazTIOWdQnbcOcdRrDd+7aWluSL+K2PbDTWBCIgMxW1G7zQtb1O70XIs5h426Q== 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=opOo9iJvamyqEnwlNEQGxH2hKMQyMEyj4B0Zolo9y5nYJgidifzkx4DF936onDEz9EdH QkA/d+JrPbg24BNcxtA8Q4u23XnIpFBsdRoUhK5+ndszlbjNiLe/suxaUty3kr0qnpsXB d6emED2X8G5RK9fQGsIeZfrbmf74Nt7562LBp8D+XyDo0beu/az78BZiacLVq+4sopqO+ NKKGtRBOCUR3pmTIxuk3cmoPwD94Phb8ZOAHIj5KAhvF9oUnrkwMco9jPbNYw+yiKspsF Zp0vHU3UGqRNV6lZmXPEgQ2lLROaegD85QBJ18nZHnBgPabxfvGu+d3iSbhm+D0VUxQ== 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.210.175 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 BDE1D606A330 for ; Fri, 4 Dec 2026 11:37:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BDE1D606A330 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-pf1-f175.google.com with SMTP id d2e1a72fcca58-8a06e79e152so436067b3a.0 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=SXwEg8d1jKKeaaTKwU1LeO4JI68/7WPXh4d0cBHkIxSIH+Slgehsk9tDNr7w48K0X7 sjcPe2A34hwviCvPBx7HkTn5Llg8S3x4WMnoWVNAtxSnz66FJK36nruy3reaRza33X0E jN315Bb5BTEfmCvXKFSLVQdN0ymHZaun11l5zlrVi0O7wo48HFK1WuWmVJfHmJ3vT6Ms u6lISi57u397glPeY+YX20ADUEZ2IFQt6o3LWhx30MeWU0fGGen1zwvA5uacFv4/oFGx pYKNFMM8ZoB4akKL14VXEIYUA+ur5l5qoo+NPGWskJNLHkoNnJkGlNoxRLkuVGUCVRqp T+sQ== X-Forwarded-Encrypted: i=1; AL+hErGijYU4/4sQsKyWtGSOO7HnGix6iQGEe1OghzFdqxO+LZudpZzE52cDIM/4P2Dr+GqqCXNuEutSfz2kdQ8=@vger.kernel.org X-Gm-Message-State: AB4q+uEPvW8sgyb2f+xfJDFzv9eUHqGltVaYquV41UHBAc9FnLnV9BG8 bToxNMlLgQAfmI9YNF5l1oHyPw41iH4YPMVG/QSP1CBNy9DlVNhYFBS9 X-Gm-Gg: AZFdTZffYBAvOvn4b/Jo8JV81n+mh89BXpTR+vQXQVudmAy6q6ieXYMICoriZBFpheD KKKvzInmO8jhMWAhtBPBVm3BCmAJJCseJo2x8dySnS5vA4/PePxroOGS+MZf2gEo1rsYxVVq0bt ZuXpavdLAjvsa84KP/4Nzrvyl75E3wqNM+H56+wyJgY1C8J7TnPPrk6+NiH9ICsiuUH03j5lhJ5 FoaJOEHF/DXxkAGYRQ6ZLFFw0jW9WGQZ03n8LR2ZXnmjPIpaRrstkTH8mC5C0YauEo2dlGKlemj UNgLCQQcFcFM16YcHzstKgZ0b1koCFLITzhTc5opaB5IRepd3EybZ1Prtqj8nPzLE0tGzG5deov GUwImO3cvXtjZkDL3Ws053f8r1tM89a/6n2MKJ8UHmAUcsXyVffMxRcQLj8KSQ2JvZYVwu8ur88 TW9DGgljrIhPUPh6EcFgZlsnYF2KAn+FYKOHDXAyDJhUtxdkJomI4GG9cRvarJT9Qd2XuurtQ/o trbrg8NR7DKMTMZ2nQfrOngpqedXRU3xcCj95/wSr7nlVxljkNR7JlWjPW10/HJSFE1u9pPYy5k DaG4628dnKDVnFlLEQePkITfnHgm/gZvn/4D+Z5ghXcmd/RevsrcJB26isgrxsWlivMy7YBDuzd BS0n66I2svMYrhJkPtP5mFQMzZeAm5EAJPwWidgV88qaDVrNtxmochJyIBQ== 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-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 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 */