Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3765559pxv; Mon, 19 Jul 2021 08:14:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy4RzJLv1nxtRWCaFTQ19FPKBxZS1OPuN82/mxWUmHSbRGtsZaq0Z82zd4RHUEYUVDx/sah X-Received: by 2002:a92:c10c:: with SMTP id p12mr16794082ile.92.1626707675999; Mon, 19 Jul 2021 08:14:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626707675; cv=none; d=google.com; s=arc-20160816; b=ckqlOqk2lupT++c4tooUUFoiNvFXSJ2fBPaO6Pzk/lCYEc87xRVghHaE39u5Wao+wu lfWHeA89nK43MxvpDZSM7a+3iNy47cF7rZzLhdrAc3xhub8liQVljopI2W3K82BxDGPN zyVa4HTg21XNzrjnyzAb6oC6mSgJzcOooIjpydrHS4khVQ6MXJOmXmg0J6MJHaQqZWtz ZW8M/ywSk17fKKTaEWyAyzBvwPqVrgFPH0wQtns1YIGt/X8JzdpCmwtnas67MRojbeaO 36WulNgEVpBEijuGKZxdBhMXJ1mbuFYiWgNvfZ8STQo2GNcWlCSSOpD2wSPntKb+DMBj sWyw== 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=CqlQUrnRf9nQw5IC+9iiNGKVkz0ZkVqowv/+OsdLey4=; b=jjRRpfWrtr/jkx6PJs2uNEOZZnI5yCWhqlpw6En5GzNVN4hGh1/a6cQr8GNN9vJmLF GCyoVt92cQ7II5amlj6GMgIVZfpo4d5oMEv+llTX71t4Zvc+EG0xleNDyfR/eVOee8GX w/2DZZeTQ5L14QSsebnhikSFIT5EpoCB3wnBcMFS9fD+pe3T5aRvsWE+Kon/MNLVHpsV 51+hnUMqGwqGujLbmDMb96v/f2cuDqk6zN+YYxs0No7hKEAm2I1OtMCQs7unxgcsU2XK 9W6WdgbojQBRRJVl1YPZPR1naPKi8VjO80UX5mEuwyjd8zMHTwqtPy/xulbNS5Pr9Yxo cTCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EB1vZGDM; 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 h5si21174898iob.96.2021.07.19.08.14.23; Mon, 19 Jul 2021 08:14:35 -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=EB1vZGDM; 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 S244934AbhGSOcR (ORCPT + 99 others); Mon, 19 Jul 2021 10:32:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:39958 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243281AbhGSO0P (ORCPT ); Mon, 19 Jul 2021 10:26:15 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2C50761242; Mon, 19 Jul 2021 15:06:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626707214; bh=HRsLykcVAM6qb97IVwJWQm1m7XYOy2U68fbD+4dniOk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EB1vZGDM/Jk2Pp+akPhTPx0HEb0p+7K0OUIG9/MbZeR+0Y3D06PL83/CseTttr1rh xO/j+QM9Ia27vA8dN8TTO1ywrOWM/0NqosQyn5ACqLrTaUUWjSxh4AfApKNxwfopub kSO8WLDlaKKUUgkiO+ygpuVEpKYrFus1MXCoU/aU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Bjorn Helgaas , "Rafael J. Wysocki" , Sasha Levin Subject: [PATCH 4.9 073/245] ACPI: sysfs: Fix a buffer overrun problem with description_show() Date: Mon, 19 Jul 2021 16:50:15 +0200 Message-Id: <20210719144942.772942786@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210719144940.288257948@linuxfoundation.org> References: <20210719144940.288257948@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: Krzysztof Wilczyński [ Upstream commit 888be6067b97132c3992866bbcf647572253ab3f ] Currently, a device description can be obtained using ACPI, if the _STR method exists for a particular device, and then exposed to the userspace via a sysfs object as a string value. If the _STR method is available for a given device then the data (usually a Unicode string) is read and stored in a buffer (of the ACPI_TYPE_BUFFER type) with a pointer to said buffer cached in the struct acpi_device_pnp for later access. The description_show() function is responsible for exposing the device description to the userspace via a corresponding sysfs object and internally calls the utf16s_to_utf8s() function with a pointer to the buffer that contains the Unicode string so that it can be converted from UTF16 encoding to UTF8 and thus allowing for the value to be safely stored and later displayed. When invoking the utf16s_to_utf8s() function, the description_show() function also sets a limit of the data that can be saved into a provided buffer as a result of the character conversion to be a total of PAGE_SIZE, and upon completion, the utf16s_to_utf8s() function returns an integer value denoting the number of bytes that have been written into the provided buffer. Following the execution of the utf16s_to_utf8s() a newline character will be added at the end of the resulting buffer so that when the value is read in the userspace through the sysfs object then it would include newline making it more accessible when working with the sysfs file system in the shell, etc. Normally, this wouldn't be a problem, but if the function utf16s_to_utf8s() happens to return the number of bytes written to be precisely PAGE_SIZE, then we would overrun the buffer and write the newline character outside the allotted space which can have undefined consequences or result in a failure. To fix this buffer overrun, ensure that there always is enough space left for the newline character to be safely appended. Fixes: d1efe3c324ea ("ACPI: Add new sysfs interface to export device description") Signed-off-by: Krzysztof Wilczyński Reviewed-by: Bjorn Helgaas Signed-off-by: Rafael J. Wysocki Signed-off-by: Sasha Levin --- drivers/acpi/device_sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/acpi/device_sysfs.c b/drivers/acpi/device_sysfs.c index fb610ad495f1..152ba55fd908 100644 --- a/drivers/acpi/device_sysfs.c +++ b/drivers/acpi/device_sysfs.c @@ -452,7 +452,7 @@ static ssize_t description_show(struct device *dev, (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer, acpi_dev->pnp.str_obj->buffer.length, UTF16_LITTLE_ENDIAN, buf, - PAGE_SIZE); + PAGE_SIZE - 1); buf[result++] = '\n'; -- 2.30.2