Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3711401ybv; Mon, 10 Feb 2020 05:08:38 -0800 (PST) X-Google-Smtp-Source: APXvYqy1ScizGvggSaGwvFsAUbZPb65ySVS6NvIQIwMFHWk6a1Z3KQvwvIMXyO1HA+HoXMf8kZYJ X-Received: by 2002:aca:c7ca:: with SMTP id x193mr778286oif.70.1581340118227; Mon, 10 Feb 2020 05:08:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581340118; cv=none; d=google.com; s=arc-20160816; b=lgaA7wxekRK7X9FNExrMa29NjiBAao+QnVXLQhQ2MUlS5xJl+FbiUTsDKxZtBfNqIl XlGzjbK7zPUmlKdwc3Yk8yHvBZC69JzaYwpPf64EXlIiLZiB3KUvKfsqiODfK8s7ZISs DBnpEKkKCTm6HH/D0tJhlIVL7qTHqHmJQ5X9n+nRMTIMxwKWKsXZHPgiN2r8m9Wimq6u 1Z1/CJjsoMsnDsfl3QwbJwyX3mDxmwGYuCD5aTM9hYEMw5MIhxefl6z4stH1eO5l4C+5 Di/pl0laU4zCcr+3QXL0ry9trqRLPTV3WN40Xyo0VsnWU/YpqlOLVhrh+JCyr21E8NFH 9pVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=VhjGSegyF/UAxIX4XYyBHSjjD/YDPF00eGJE3N3KNWo=; b=dkV+M9bqdFtvaflmzO+iP+vyojkN7AwIrwqN/c03AaSG8H2zd5Ii7SNZek91aXtvCM +VhRqwNfKcc4HLqY/kz1yU6Vwvq5wHBH3NlPRj1riHjdTljpB2/mQqbxejHwJlJrBla2 BDS3N3GIcgJkwXKkce3y+vTvs98c2qdx1/FGqtACHczdsuUpA5m2fE/9Wso9dVebRAiP AjWr3bcWcmoJO5bNHp+GyCuF7gZsRImQuVGhzLQ5xN4E1I06zfkFNmtOz9v2Is6yU8Tr PQRxXYEqq2Alj47FUqWzAa2QhCcPV6aeiBN8kaQWUSAITeULXIqvuyxsqTpTKkpQolZc PhIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=A8EII2on; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s5si175334otd.141.2020.02.10.05.08.26; Mon, 10 Feb 2020 05:08:38 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=A8EII2on; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729601AbgBJNIV (ORCPT + 99 others); Mon, 10 Feb 2020 08:08:21 -0500 Received: from mail.kernel.org ([198.145.29.99]:37688 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729451AbgBJMjg (ORCPT ); Mon, 10 Feb 2020 07:39:36 -0500 Received: from localhost (unknown [209.37.97.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 15B1921739; Mon, 10 Feb 2020 12:39:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581338375; bh=qATLWGm8vYOyW9iIYLQ9TEKrHeRMZ7731hcN7k1bZw8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A8EII2ondlbHlIEz1PFeFY3CKS5/1dAXm10lHgFvWhQI1i6HnbYP3+b6s7pG449Hk Z1+Xx9j06qEfAxApD7By3ojCOUoqst7G8rP59YnI3/zNe1BfXMPSjsLWqb74ZVuBPc JJRXVVnQ75CobqswzdEQ4Pr2uKTx0BZXfJ2xkesM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Bryan ODonoghue , Felipe Balbi Subject: [PATCH 5.5 049/367] usb: gadget: f_ncm: Use atomic_t to track in-flight request Date: Mon, 10 Feb 2020 04:29:22 -0800 Message-Id: <20200210122428.567332269@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200210122423.695146547@linuxfoundation.org> References: <20200210122423.695146547@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bryan O'Donoghue commit 5b24c28cfe136597dc3913e1c00b119307a20c7e upstream. Currently ncm->notify_req is used to flag when a request is in-flight. ncm->notify_req is set to NULL and when a request completes it is subsequently reset. This is fundamentally buggy in that the unbind logic of the NCM driver will unconditionally free ncm->notify_req leading to a NULL pointer dereference. Fixes: 40d133d7f542 ("usb: gadget: f_ncm: convert to new function interface with backward compatibility") Cc: stable Signed-off-by: Bryan O'Donoghue Signed-off-by: Felipe Balbi Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/function/f_ncm.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) --- a/drivers/usb/gadget/function/f_ncm.c +++ b/drivers/usb/gadget/function/f_ncm.c @@ -53,6 +53,7 @@ struct f_ncm { struct usb_ep *notify; struct usb_request *notify_req; u8 notify_state; + atomic_t notify_count; bool is_open; const struct ndp_parser_opts *parser_opts; @@ -547,7 +548,7 @@ static void ncm_do_notify(struct f_ncm * int status; /* notification already in flight? */ - if (!req) + if (atomic_read(&ncm->notify_count)) return; event = req->buf; @@ -587,7 +588,8 @@ static void ncm_do_notify(struct f_ncm * event->bmRequestType = 0xA1; event->wIndex = cpu_to_le16(ncm->ctrl_id); - ncm->notify_req = NULL; + atomic_inc(&ncm->notify_count); + /* * In double buffering if there is a space in FIFO, * completion callback can be called right after the call, @@ -597,7 +599,7 @@ static void ncm_do_notify(struct f_ncm * status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC); spin_lock(&ncm->lock); if (status < 0) { - ncm->notify_req = req; + atomic_dec(&ncm->notify_count); DBG(cdev, "notify --> %d\n", status); } } @@ -632,17 +634,19 @@ static void ncm_notify_complete(struct u case 0: VDBG(cdev, "Notification %02x sent\n", event->bNotificationType); + atomic_dec(&ncm->notify_count); break; case -ECONNRESET: case -ESHUTDOWN: + atomic_set(&ncm->notify_count, 0); ncm->notify_state = NCM_NOTIFY_NONE; break; default: DBG(cdev, "event %02x --> %d\n", event->bNotificationType, req->status); + atomic_dec(&ncm->notify_count); break; } - ncm->notify_req = req; ncm_do_notify(ncm); spin_unlock(&ncm->lock); } @@ -1649,6 +1653,11 @@ static void ncm_unbind(struct usb_config ncm_string_defs[0].id = 0; usb_free_all_descriptors(f); + if (atomic_read(&ncm->notify_count)) { + usb_ep_dequeue(ncm->notify, ncm->notify_req); + atomic_set(&ncm->notify_count, 0); + } + kfree(ncm->notify_req->buf); usb_ep_free_request(ncm->notify, ncm->notify_req); }