Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2503651pxj; Mon, 10 May 2021 04:42:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzKEkU5Ry0S+87S4PMtKkn6156x/feXYkffj050BSbQgg80ist4/MSAG6CgcDzGI8yeL/Ow X-Received: by 2002:aa7:c1d6:: with SMTP id d22mr28474201edp.180.1620646946143; Mon, 10 May 2021 04:42:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620646946; cv=none; d=google.com; s=arc-20160816; b=CnjEBrk0WlW1mevrz31zQTkhDsij2dC5W32rFfZX7YRhEd4sDlnGt31D459YTPhQqU m0jrF4TgRJCJAJ0iA64xa+Lg8dFqexawA4JvDeHOrH1OhUXUd87ymTVOgXeUC5/Kr7td CAyYReVLGlL8IMw50ykDQT/9xyUrfKDg3pAqKuwRUWbF+O1Ud7AW45Rlcs2Zp+7N3U06 Zf8QBwDt/WEiOVqGNpE2V3Rw112YVFeWRt7tlb/hMe9UEn5cNvpOdGPQJWNm/0K87wiy w/qPYXjhpBsv9yRxUSjjBXDDQCMip5PvbXanCPildBTz94gynijiCoqc6/Kls/yDpZd+ rxJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wGOTVorFE8mlzmxS9Urrp3+9Fi63NNYSlOTbePysRuA=; b=Q88lf0YKIBX++P5Yzd7Y7OSrBBIg/1Ng4OT1zn76pl8ghHJV8X61TLcEAOA1xKvWC0 gflzcLk7XUIBWSgxLaPkEOwLaXqbAVtbr3bgVhwVzxQrdq06Cb0/8jGWI8mv5LIFIbmY IpdHR/0vIJDdVwqx9t1Gk7oe2nswtBobkCBwDO4Fdl38kHMng41qCJM6nvibSmSHobDU UxlrbzlcmgQFZ5LnI4iLO0D8zF+7ljJ+ITF8szSfXbLOl1Jw0BamrKUnDPU/tRF+2P/A Au6CmCCQ/O6hWdMQFsTTZiHxoGoFA5Gf8yWWVwXrGHtdPsyjsHTlU8I2B44yiVh0rVcW eHEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Xax19AGR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bh4si16510613ejb.56.2021.05.10.04.42.01; Mon, 10 May 2021 04:42:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Xax19AGR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241727AbhEJLil (ORCPT + 99 others); Mon, 10 May 2021 07:38:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:52158 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234499AbhEJK4f (ORCPT ); Mon, 10 May 2021 06:56:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D950461938; Mon, 10 May 2021 10:46:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620643590; bh=gHK9z7jQUHKGMyq9Dk1nA6jlE7fCAzl1NO77XE5WdZ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xax19AGRjw+G7KjD0rI4zn/w6iH5f36Q3WRvW8sh6Q8Ct30mC+p5vYpYsGlwE/JS2 dpl6qcoIPC+lsURqShba2NLuB4PQQ8xDVyqAjD9OVBtP34LxuFqKFIzr5tFeJsJrwv insu3QBIzCVOJqGOoIh+G+rFdTNe72M/MJiuqYg8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ruslan Bilovol , Sasha Levin Subject: [PATCH 5.11 076/342] usb: gadget: f_uac2: validate input parameters Date: Mon, 10 May 2021 12:17:46 +0200 Message-Id: <20210510102012.631909918@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510102010.096403571@linuxfoundation.org> References: <20210510102010.096403571@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ruslan Bilovol [ Upstream commit 3713d5ceb04d5ab6a5e2b86dfca49170053f3a5e ] Currently user can configure UAC2 function with parameters that violate UAC2 spec or are not supported by UAC2 gadget implementation. This can lead to incorrect behavior if such gadget is connected to the host - like enumeration failure or other issues depending on host's UAC2 driver implementation, bringing user to a long hours of debugging the issue. Instead of silently accept these parameters, throw an error if they are not valid. Signed-off-by: Ruslan Bilovol Link: https://lore.kernel.org/r/1614599375-8803-4-git-send-email-ruslan.bilovol@gmail.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/gadget/function/f_uac2.c | 39 ++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/drivers/usb/gadget/function/f_uac2.c b/drivers/usb/gadget/function/f_uac2.c index 6f03e944e0e3..dd960cea642f 100644 --- a/drivers/usb/gadget/function/f_uac2.c +++ b/drivers/usb/gadget/function/f_uac2.c @@ -14,6 +14,9 @@ #include "u_audio.h" #include "u_uac2.h" +/* UAC2 spec: 4.1 Audio Channel Cluster Descriptor */ +#define UAC2_CHANNEL_MASK 0x07FFFFFF + /* * The driver implements a simple UAC_2 topology. * USB-OUT -> IT_1 -> OT_3 -> ALSA_Capture @@ -604,6 +607,36 @@ static void setup_descriptor(struct f_uac2_opts *opts) hs_audio_desc[i] = NULL; } +static int afunc_validate_opts(struct g_audio *agdev, struct device *dev) +{ + struct f_uac2_opts *opts = g_audio_to_uac2_opts(agdev); + + if (!opts->p_chmask && !opts->c_chmask) { + dev_err(dev, "Error: no playback and capture channels\n"); + return -EINVAL; + } else if (opts->p_chmask & ~UAC2_CHANNEL_MASK) { + dev_err(dev, "Error: unsupported playback channels mask\n"); + return -EINVAL; + } else if (opts->c_chmask & ~UAC2_CHANNEL_MASK) { + dev_err(dev, "Error: unsupported capture channels mask\n"); + return -EINVAL; + } else if ((opts->p_ssize < 1) || (opts->p_ssize > 4)) { + dev_err(dev, "Error: incorrect playback sample size\n"); + return -EINVAL; + } else if ((opts->c_ssize < 1) || (opts->c_ssize > 4)) { + dev_err(dev, "Error: incorrect capture sample size\n"); + return -EINVAL; + } else if (!opts->p_srate) { + dev_err(dev, "Error: incorrect playback sampling rate\n"); + return -EINVAL; + } else if (!opts->c_srate) { + dev_err(dev, "Error: incorrect capture sampling rate\n"); + return -EINVAL; + } + + return 0; +} + static int afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) { @@ -612,11 +645,13 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) struct usb_composite_dev *cdev = cfg->cdev; struct usb_gadget *gadget = cdev->gadget; struct device *dev = &gadget->dev; - struct f_uac2_opts *uac2_opts; + struct f_uac2_opts *uac2_opts = g_audio_to_uac2_opts(agdev); struct usb_string *us; int ret; - uac2_opts = container_of(fn->fi, struct f_uac2_opts, func_inst); + ret = afunc_validate_opts(agdev, dev); + if (ret) + return ret; us = usb_gstrings_attach(cdev, fn_strings, ARRAY_SIZE(strings_fn)); if (IS_ERR(us)) -- 2.30.2