Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp412580pxj; Thu, 20 May 2021 12:13:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxSUWb0k8bpMP9Talqee34JzeMTtnDDkCEzQb62TSrcrmHDotyGSvoNUZIGe6ruer5EtF6O X-Received: by 2002:a17:906:1997:: with SMTP id g23mr6295539ejd.168.1621537999065; Thu, 20 May 2021 12:13:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621537999; cv=none; d=google.com; s=arc-20160816; b=rnt0WaD1yHhpmWEdw7xxeL6nWFDNBuaLVu0hGixCetcUyLuP2nY0mWWZswNSHPQV6D wEKK2MWgfuVjBbCnJfZD7KsXfpLAQ9xYdU6MjXFOSp1+PDFVN3Jcvw6w+aE8mgPPUGPg FjIyiZcqaH6TzpPPXmgpluoNMsnn5N97tdnASFb+7+gggZMmScFMXuT8COnyxX9Qy25P gPDaVxuvLh2zjZJ2u06Qhy8WjWPv7L2DY+KQrbpbSSW55fRpu30oVYF3quJBdO//GZke OQCjlql7mZeayMthYTm/o4Y3SN0y1qlTeEo1FTPxkcdi17zdVaDCi2vsBsycPu6lCgr1 TXBw== 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=MrLtaEaShj83Yb2jHxZ9gIJD8uKktvSlfPxgDvdSCmU=; b=W9e4CtztIQJvHroQbkNYabg8xhlKyFrP/FcCBWDjB7wTr6lQm4g96ikCpWbv/+jzXF uxlMMGVI7VmRIgf8kTph5CKwejXi+CRYo3K8cy0q0rM12rAi9XTwsES1i547nNoeUBK4 0W/YOCmI/vTAjmHrb319ENYJnYxDif6DBbrMdZdDNb1Tcidk4JHz+H88Mry8YPEn2vKS fnvmlM5DH4XbieNSTojgCYD2r6DAzNfE6f6YiSlu/e6v+FAbCmTqKtILxndK3/8cjnAX FJylYBxM3VYY74DhOvUgb/Y/ROceIctvtusAuMgL+Vg6/kCPY2+cBGRhIlP8kc5fwxNC agFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GDav2c2K; 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 r10si3386502ejy.100.2021.05.20.12.12.54; Thu, 20 May 2021 12:13:19 -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=GDav2c2K; 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 S237133AbhETK3j (ORCPT + 99 others); Thu, 20 May 2021 06:29:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:47660 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235800AbhETKRQ (ORCPT ); Thu, 20 May 2021 06:17:16 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 077BD61454; Thu, 20 May 2021 09:46:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621503963; bh=nr8udd1qg14NizUVqi69DoKYfc9SNyTvp8JR4yw2N9U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GDav2c2KtQn/Tb3bbTrFWEj/TRSrQLtC/PV0g0R3tLagru+WqE9FFDr7bMIQoxm3p 7GvmxmVujyIeJfbi+p1rAi2guNhRqwsd2uTzxsR7ItdwtpYcaeVknSaGT4OpwlUK86 B89C3/s4WqLQ7zZvc6XA+XEtr/Ok2r99dhnA4CkY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ruslan Bilovol , Sasha Levin Subject: [PATCH 4.14 033/323] usb: gadget: f_uac1: validate input parameters Date: Thu, 20 May 2021 11:18:45 +0200 Message-Id: <20210520092121.248458105@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092120.115153432@linuxfoundation.org> References: <20210520092120.115153432@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 a59c68a6a3d1b18e2494f526eb19893a34fa6ec6 ] Currently user can configure UAC1 function with parameters that violate UAC1 spec or are not supported by UAC1 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 UAC1 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-5-git-send-email-ruslan.bilovol@gmail.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/gadget/function/f_uac1.c | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/drivers/usb/gadget/function/f_uac1.c b/drivers/usb/gadget/function/f_uac1.c index 3f4ee28e7896..edbb3b9a9709 100644 --- a/drivers/usb/gadget/function/f_uac1.c +++ b/drivers/usb/gadget/function/f_uac1.c @@ -23,6 +23,9 @@ #include "u_audio.h" #include "u_uac1.h" +/* UAC1 spec: 3.7.2.3 Audio Channel Cluster Format */ +#define UAC1_CHANNEL_MASK 0x0FFF + struct f_uac1 { struct g_audio g_audio; u8 ac_intf, as_in_intf, as_out_intf; @@ -34,6 +37,11 @@ static inline struct f_uac1 *func_to_uac1(struct usb_function *f) return container_of(f, struct f_uac1, g_audio.func); } +static inline struct f_uac1_opts *g_audio_to_uac1_opts(struct g_audio *audio) +{ + return container_of(audio->func.fi, struct f_uac1_opts, func_inst); +} + /* * DESCRIPTORS ... most are static, but strings and full * configuration descriptors are built on demand. @@ -509,11 +517,42 @@ static void f_audio_disable(struct usb_function *f) /*-------------------------------------------------------------------------*/ +static int f_audio_validate_opts(struct g_audio *audio, struct device *dev) +{ + struct f_uac1_opts *opts = g_audio_to_uac1_opts(audio); + + if (!opts->p_chmask && !opts->c_chmask) { + dev_err(dev, "Error: no playback and capture channels\n"); + return -EINVAL; + } else if (opts->p_chmask & ~UAC1_CHANNEL_MASK) { + dev_err(dev, "Error: unsupported playback channels mask\n"); + return -EINVAL; + } else if (opts->c_chmask & ~UAC1_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; +} + /* audio function driver setup/binding */ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f) { struct usb_composite_dev *cdev = c->cdev; struct usb_gadget *gadget = cdev->gadget; + struct device *dev = &gadget->dev; struct f_uac1 *uac1 = func_to_uac1(f); struct g_audio *audio = func_to_g_audio(f); struct f_uac1_opts *audio_opts; @@ -523,6 +562,10 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f) int rate; int status; + status = f_audio_validate_opts(audio, dev); + if (status) + return status; + audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst); us = usb_gstrings_attach(cdev, uac1_strings, ARRAY_SIZE(strings_uac1)); -- 2.30.2