From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A7260848D for ; Fri, 10 Mar 2023 14:29:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C0AFC433EF; Fri, 10 Mar 2023 14:29:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1678458542; bh=W8UwYRrOuuAN07NjUgwA+ybcYCYJlMM2vAuC6bjrJrM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eFpr7b2dA07jsJY4XAye6GKT9N9qSFUhAEx2lMAm0bXtcB2lbZiJXQTXdZLpr0at4 qmjtfdeqtb+3UUbz5cS6DV0kFHGRsv1AotziMYk2FdrnlsDpwn+pEHeYqyYJNDKe8T RqKXykCBI5193R6ISL2emYBdBmKPVC+Z4z6Hc108= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Armin Wolf , "Rafael J. Wysocki" , Sasha Levin Subject: [PATCH 5.4 062/357] ACPI: battery: Fix missing NUL-termination with large strings Date: Fri, 10 Mar 2023 14:35:51 +0100 Message-Id: <20230310133736.715941431@linuxfoundation.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230310133733.973883071@linuxfoundation.org> References: <20230310133733.973883071@linuxfoundation.org> User-Agent: quilt/0.67 Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Armin Wolf [ Upstream commit f2ac14b5f197e4a2dec51e5ceaa56682ff1592bc ] When encountering a string bigger than the destination buffer (32 bytes), the string is not properly NUL-terminated, causing buffer overreads later. This for example happens on the Inspiron 3505, where the battery model name is larger than 32 bytes, which leads to sysfs showing the model name together with the serial number string (which is NUL-terminated and thus prevents worse). Fix this by using strscpy() which ensures that the result is always NUL-terminated. Fixes: 106449e870b3 ("ACPI: Battery: Allow extract string from integer") Signed-off-by: Armin Wolf Signed-off-by: Rafael J. Wysocki Signed-off-by: Sasha Levin --- drivers/acpi/battery.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/acpi/battery.c b/drivers/acpi/battery.c index 974c2df13da1d..a49a09e3de1b3 100644 --- a/drivers/acpi/battery.c +++ b/drivers/acpi/battery.c @@ -465,7 +465,7 @@ static int extract_package(struct acpi_battery *battery, u8 *ptr = (u8 *)battery + offsets[i].offset; if (element->type == ACPI_TYPE_STRING || element->type == ACPI_TYPE_BUFFER) - strncpy(ptr, element->string.pointer, 32); + strscpy(ptr, element->string.pointer, 32); else if (element->type == ACPI_TYPE_INTEGER) { strncpy(ptr, (u8 *)&element->integer.value, sizeof(u64)); -- 2.39.2