From patchwork Fri Jan 2 00:27:12 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 5557821 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id B1E309F2ED for ; Fri, 2 Jan 2015 00:27:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DC17E20256 for ; Fri, 2 Jan 2015 00:27:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EDF002022D for ; Fri, 2 Jan 2015 00:27:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751631AbbABA1d (ORCPT ); Thu, 1 Jan 2015 19:27:33 -0500 Received: from userp1040.oracle.com ([156.151.31.81]:45719 "EHLO userp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751520AbbABA1c (ORCPT ); Thu, 1 Jan 2015 19:27:32 -0500 Received: from ucsinet22.oracle.com (ucsinet22.oracle.com [156.151.31.94]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id t020RPQg015367 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 2 Jan 2015 00:27:26 GMT Received: from userz7021.oracle.com (userz7021.oracle.com [156.151.31.85]) by ucsinet22.oracle.com (8.14.5+Sun/8.14.5) with ESMTP id t020RP5h000089 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Fri, 2 Jan 2015 00:27:25 GMT Received: from abhmp0002.oracle.com (abhmp0002.oracle.com [141.146.116.8]) by userz7021.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id t020ROqe024019; Fri, 2 Jan 2015 00:27:25 GMT Received: from lappy.hsd1.nh.comcast.net (/10.154.189.253) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 01 Jan 2015 16:27:24 -0800 From: Sasha Levin To: linux-kernel@vger.kernel.org Cc: gregkh@linuxfoundation.org, Sasha Levin , Bjorn Helgaas , linux-pci@vger.kernel.org (open list:PCI SUBSYSTEM) Subject: [PATCH] PCI/sysfs: off by two and memory corruption in driver_override parameter Date: Thu, 1 Jan 2015 19:27:12 -0500 Message-Id: <1420158432-645-1-git-send-email-sasha.levin@oracle.com> X-Mailer: git-send-email 2.1.0 X-Source-IP: ucsinet22.oracle.com [156.151.31.94] Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP There are two different issues fixed here: - On nun-NULL terminated input a random byte of memory would get flipped from '\n' to '\0'. - When printing the driver_override parameter when it is 4095 and 4094 bytes long the printing code would access invalid memory. Signed-off-by: Sasha Levin --- drivers/pci/pci-sysfs.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index aa012fb..be6b2b1 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -521,17 +521,17 @@ static ssize_t driver_override_store(struct device *dev, struct pci_dev *pdev = to_pci_dev(dev); char *driver_override, *old = pdev->driver_override, *cp; - if (count > PATH_MAX) + /* We need to keep extra room for a newline */ + if (count >= (PATH_MAX - 1)) return -EINVAL; driver_override = kstrndup(buf, count, GFP_KERNEL); if (!driver_override) return -ENOMEM; - cp = strchr(driver_override, '\n'); - if (cp) - *cp = '\0'; - + driver_override[count] = '\0'; + if (driver_override[count - 1] == '\n') + driver_override[count - 1] = 0; if (strlen(driver_override)) { pdev->driver_override = driver_override; } else {