From patchwork Mon Jan 9 15:01:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jaroslav Kysela X-Patchwork-Id: 640928 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C17BDC678D5 for ; Mon, 9 Jan 2023 15:02:56 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 09E25AADB; Mon, 9 Jan 2023 16:02:04 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 09E25AADB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1673276574; bh=OEA+wMG7cmDPnnJ9Jp33RYPPJawIbwW3pZYATC1WWiA=; h=From:To:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:Cc:From; b=dnGTta5eI+RxXaCGfU/6skBMkS+QzNBKBNkE5CeJmF7Zba88Tws04FdcnEjuRe4aE swKYohDstteFDr9J7SXIEuNHnqjY80owEzwiuUK68DfqbcsLgcARitur/F+1ns9e5e UsuHbRJh0mLKptpz49SpKW87/hZORPLRd8CZxYTs= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id B23B8F802BE; Mon, 9 Jan 2023 16:02:03 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id C20EFF8030F; Mon, 9 Jan 2023 16:02:02 +0100 (CET) Received: from mail1.perex.cz (mail1.perex.cz [77.48.224.245]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 16041F800C7 for ; Mon, 9 Jan 2023 16:01:55 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 16041F800C7 Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key, secure) header.d=perex.cz header.i=@perex.cz header.a=rsa-sha256 header.s=default header.b=PT/wX+QH Received: from mail1.perex.cz (localhost [127.0.0.1]) by smtp1.perex.cz (Perex's E-mail Delivery System) with ESMTP id 94B40A0058; Mon, 9 Jan 2023 16:01:54 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.perex.cz 94B40A0058 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=perex.cz; s=default; t=1673276514; bh=T3sdsyIaTFFXNPa/h3w8asbkqmac5HDKD0k0F0F8LIM=; h=From:To:Cc:Subject:Date:From; b=PT/wX+QHkZ/Dt/6pFZZDwGBAuvVPmr3wy26a3J6v29aKaOm8kKpSQvFdD6/ObUDbq AxE4gC6+48vgwcool/3zfma68KFgYyLposR5iSrY2bu/17Si2Z5lWANL+u3Q9qRrVP BCENQsm/sjxV1zVPIMIXN+9ZG0QbNe0D16Sp3rXw= Received: from p1gen2.perex-int.cz (unknown [192.168.100.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: perex) by mail1.perex.cz (Perex's E-mail Delivery System) with ESMTPSA; Mon, 9 Jan 2023 16:01:49 +0100 (CET) From: Jaroslav Kysela To: ALSA development Subject: [PATCH v2] ALSA: control-led: use strscpy in set_led_id() Date: Mon, 9 Jan 2023 16:01:18 +0100 Message-Id: <20230109150119.342771-1-perex@perex.cz> X-Mailer: git-send-email 2.39.0 MIME-Version: 1.0 X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Takashi Iwai , yang.yang29@zte.com.cn, stable@vger.kernel.org Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" The use of strncpy() in the set_led_id() was incorrect. The len variable should use 'min(sizeof(buf2) - 1, count)' expression. Use strscpy() function to simplify things and handle the error gracefully. Reported-by: yang.yang29@zte.com.cn BugLink: https://lore.kernel.org/alsa-devel/202301091945513559977@zte.com.cn/ Cc: Signed-off-by: Jaroslav Kysela v1..v2: - remove min() expression which strips the last char --- sound/core/control_led.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sound/core/control_led.c b/sound/core/control_led.c index f975cc85772b..3cadd40100f3 100644 --- a/sound/core/control_led.c +++ b/sound/core/control_led.c @@ -530,12 +530,11 @@ static ssize_t set_led_id(struct snd_ctl_led_card *led_card, const char *buf, si bool attach) { char buf2[256], *s, *os; - size_t len = max(sizeof(s) - 1, count); struct snd_ctl_elem_id id; int err; - strncpy(buf2, buf, len); - buf2[len] = '\0'; + if (strscpy(buf2, buf, sizeof(buf2)) < 0) + return -E2BIG; memset(&id, 0, sizeof(id)); id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; s = buf2;