Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp291687lqh; Thu, 28 Mar 2024 02:04:33 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVo1BdZ+vDCuswZ+lFrN+RoO56OV6KCs7AK7TlDJikzJoPHUXnKgIhiQZ35hrBMJ7IcqCC5It5MzJVK3VQFRJnFp3/E25gTzScZII8eTw== X-Google-Smtp-Source: AGHT+IFmwSdeUpNbIvhzimEtjwFwAgoor8EVd90Cu8CVEYAqUgAHYGCXBTXib3jn9S+orTMkkhvv X-Received: by 2002:a17:903:32cb:b0:1e0:384:ecb9 with SMTP id i11-20020a17090332cb00b001e00384ecb9mr2632356plr.8.1711616672787; Thu, 28 Mar 2024 02:04:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711616672; cv=pass; d=google.com; s=arc-20160816; b=KWoP6eCGmGiAIv5f9anS2YOfr+5uGKqTJDlZk4O+aFbiCJPlgKtciu9K9jPa64DENy CNXeCgMyna3dRdG19ZPycJ2v1VinCM9o63K/7eJz3FNPd/SOPmkSzFST3dhJ+2tGEF7V EClklgcv3RVFNFA5qY5JlfabX475yWQZEtlL0zklsjeaxBdhfTee5DfZHFRgUebYYUfI 6YpF6bTYbPai1dxAh49KQqaiWzrCywBD8Am6VvmxaGBa9CraCkJ8Cc7okinIcjh7PeeT WK56SZPC9BaseQXGZfcK5XXOhU6x+bf1GwIrORR5KlYIyvD7e9BzBOxBqyYhvy/rABuX pf+w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id; bh=9bqYXlCwIs2wM5lxl+u/GBLyORU2sWUj1TtKfTDIg6o=; fh=3XC/XLjFBz+bnaQjf+BDF7jCUhIJ1mNeytvF6QJne1Q=; b=pyIhBd22oEejWAViJvE/vjZnYGqSMbfnOn0rMtHdAehdDnvPvSpRRQl/3OM7DOwxjT Nf5gkZ9fKlFzgHijiOV/sV0L+gVKWr4QIUBIt+wJzJCGlS8Dyt7I/PyhJ7WyDST6sbU1 1R0Q9dqdipDEnx8H3RWHoqKw/q/8+Nfxz7aNhkIeJBIsY+dYc2TcajxTjh6gSOPrtbus UCBM55YmH3dMCJoHZuCuiXhn9ypi2wsNQtHGktP+1PedjHhfLtsz62Drj+jLYM2TChlA yKN9FY3rwju0M5oEnxOagcsE72hmFJr8c1oHh8GdD3mCZx/O4hcNmRBSjOKQed/XSAc0 GFqQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-122630-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122630-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e12-20020a17090301cc00b001dddb014623si1073358plh.211.2024.03.28.02.04.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 02:04:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122630-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-122630-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122630-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C2DFB29D54E for ; Thu, 28 Mar 2024 09:03:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 79E555A115; Thu, 28 Mar 2024 09:02:49 +0000 (UTC) Received: from mail-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E6B45024E; Thu, 28 Mar 2024 09:02:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711616568; cv=none; b=eneVz9lrttgX0QqePcc+GcAF15mxMaTgfEHfR21ZEysLq54xeQBwWC5fvYIgfW0gRJ+AR+q8IegjqWGO1oNoqf3pD5ayLdIvfQivlm4l0uXj5KYPIq2F+p0hnOi64K95TJ6JZlSJa4HOBhb8CrwZXQnYOcVWCGrRs1DI8cmfAn4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711616568; c=relaxed/simple; bh=xXKAKa461tS7heJrO1tiCk3yWmnxZb0FebuFA1FpwBY=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=P7/zz4PAf1qUIquPZnQoxIlfrII7VK0JM6KpwsJqls+ubg3YUW0ti6PdlsjnFl6D+wp7283qMncxYcQHfe947AfdILbiaVp8gxIlXLysLZHJTtHJ/92m2ffsxRO1AIPjbu9oqi/8Ngjgb7/7b/urYC/vbLtMB0JZS1/2oDr+YaU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.208.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-f44.google.com with SMTP id 4fb4d7f45d1cf-56c4ca65dc6so795866a12.1; Thu, 28 Mar 2024 02:02:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711616565; x=1712221365; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=9bqYXlCwIs2wM5lxl+u/GBLyORU2sWUj1TtKfTDIg6o=; b=n2HYUwQ0HERc6rsn6DeiD89NfvtL6sloFypSDnKFWBS7HLPczqDL9p7VYshnTug+eC rYKlIrxubHI+QhAkCmzYV23jHXah2pRgcQSfJfnIq6+1b/1n2ppVCIH+N8ybQXIFKpxG SbufMR5D+ARxKSX3Qs3lgCyT9up1NpwDnZ1zEazC28eQPCIibife0283DndOwsPVOS7w +DL0BOkhCOCEce/kY6MI9UGW9pVBvMxXJ58nGSFj16QCngbTRABYH3rgRTCiSoSCUnl4 VtmBPdVcJk7YEW68FNbwhkaciaRDd9+238T2KbhYvxUXeP72naFIL5IgHuUC6yUOT9OJ Krug== X-Forwarded-Encrypted: i=1; AJvYcCUrNAlE6+I0tlpUWabqmBrIVjlDsnYfaBt0AKRu7ieYXuOl42lNTd53KHkt0fclXiB9oOwBRdILOpO38iFHoPgewRiZhOc5V+tjLpE6FlMKHOsNIsMUzB1x6oCyFP76+Z/eKGRqSL4v749dz7trXw/zfRD+a5+rqI/rQ08iN1++ X-Gm-Message-State: AOJu0Yz+Zj7di3JMqkne1fXgoRSiQvzRDAnHMV6TRPyAbRmZ1JyEzW7P wRuJrXMOssInEwopsYQZbOYhSsK2K/vdJNRj8oGxburgEGBYt/c+ X-Received: by 2002:a50:d604:0:b0:568:231e:5e9f with SMTP id x4-20020a50d604000000b00568231e5e9fmr1537821edi.26.1711616565282; Thu, 28 Mar 2024 02:02:45 -0700 (PDT) Received: from ?IPV6:2a0b:e7c0:0:107::aaaa:69? ([2a0b:e7c0:0:107::aaaa:69]) by smtp.gmail.com with ESMTPSA id t35-20020a056402242300b0056c2d0052c0sm578654eda.60.2024.03.28.02.02.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 28 Mar 2024 02:02:44 -0700 (PDT) Message-ID: Date: Thu, 28 Mar 2024 10:02:43 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] usb: gadget: u_serial: Add null pointer checks after RX/TX submission To: Kuen-Han Tsai , Greg KH Cc: quic_prashk@quicinc.com, stern@rowland.harvard.edu, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20240116141801.396398-1-khtsai@google.com> <02bec7b8-7754-4b9d-84ae-51621d6aa7ec@kernel.org> <2024012724-chirpy-google-51bb@gregkh> Content-Language: en-US From: Jiri Slaby Autocrypt: addr=jirislaby@kernel.org; keydata= xsFNBE6S54YBEACzzjLwDUbU5elY4GTg/NdotjA0jyyJtYI86wdKraekbNE0bC4zV+ryvH4j rrcDwGs6tFVrAHvdHeIdI07s1iIx5R/ndcHwt4fvI8CL5PzPmn5J+h0WERR5rFprRh6axhOk rSD5CwQl19fm4AJCS6A9GJtOoiLpWn2/IbogPc71jQVrupZYYx51rAaHZ0D2KYK/uhfc6neJ i0WqPlbtIlIrpvWxckucNu6ZwXjFY0f3qIRg3Vqh5QxPkojGsq9tXVFVLEkSVz6FoqCHrUTx wr+aw6qqQVgvT/McQtsI0S66uIkQjzPUrgAEtWUv76rM4ekqL9stHyvTGw0Fjsualwb0Gwdx ReTZzMgheAyoy/umIOKrSEpWouVoBt5FFSZUyj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w/JVfEJW9JXp3BFAVzRQ5nVrrLDAJZ8Y5ioWcm99JtSIIxXxt9FJaGc1Bgsi5K/+dyTKL wLMJgiBzbVx8G+fCJJ9YtlNOPWhbKPlrQ8+AY52Aagi9WNhe6XfJdh5g6ptiOILm330mkR4g W6nEgZVyIyTq3ekOuruftWL99qpP5zi+eNrMmLRQx9iecDNgFr342R9bTDlb1TLuRb+/tJ98 f/bIWIr0cqQmqQ33FgRhrG1+Xml6UXyJ2jExmlO8JljuOGeXYh6ZkIEyzqzffzBLXZCujlYQ DFXpyMNVJ2ZwPmX2mWEoYuaBU0JN7wM+/zWgOf2zRwhEuD3A2cO2PxoiIfyUEfB9SSmffaK/ S4xXoB6wvGENZ85Hg37C7WDNdaAt6Xh2uQIly5grkgvWppkNy4ZHxE+jeNsU7tg= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 08. 03. 24, 12:47, Kuen-Han Tsai wrote: > Hi Greg & Jiri, > > On Sun, Jan 28, 2024 at 9:29 AM Greg KH wrote: >> >> On Thu, Jan 18, 2024 at 10:27:54AM +0100, Jiri Slaby wrote: >>> On 16. 01. 24, 15:16, Kuen-Han Tsai wrote: >>>> Commit ffd603f21423 ("usb: gadget: u_serial: Add null pointer check in >>>> gs_start_io") adds null pointer checks to gs_start_io(), but it doesn't >>>> fully fix the potential null pointer dereference issue. While >>>> gserial_connect() calls gs_start_io() with port_lock held, gs_start_rx() >>>> and gs_start_tx() release the lock during endpoint request submission. >>>> This creates a window where gs_close() could set port->port_tty to NULL, >>>> leading to a dereference when the lock is reacquired. >>>> >>>> This patch adds a null pointer check for port->port_tty after RX/TX >>>> submission, and removes the initial null pointer check in gs_start_io() >>>> since the caller must hold port_lock and guarantee non-null values for >>>> port_usb and port_tty. >>> >>> Or you switch to tty_port refcounting and need not fiddling with this at all >>> ;). >> >> I agree, Kuen-Han, why not do that instead? > > The u_serial driver has already maintained the usage count of a TTY > structure for open and close. While the driver tracks the usage count > via open/close, it doesn't fully eliminate race conditions. Below are > two potential scenarios: > > Case 1 (Observed): > 1. gs_open() sets usage count to 1. > 2. gserial_connect(), gs_start_io(), and gs_start_rx() execute in > sequence (lock held). > 3. Lock released, usb_ep_queue() called. > 4. In parallel, gs_close() executes, sees count of 1, clears TTY, releases lock. > 5. Original thread resumes in gs_start_rx(), potentially leading to > kernel panic on an invalid TTY. If it used refcounting -- tty_port_tty_get(), how comes? thanks, -- js suse labs