Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966070AbdGUAjM (ORCPT ); Thu, 20 Jul 2017 20:39:12 -0400 Received: from m15-16.126.com ([220.181.15.16]:1398 "EHLO m15-16.126.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935901AbdGUAjL (ORCPT ); Thu, 20 Jul 2017 20:39:11 -0400 X-Originating-IP: [113.200.156.99] Date: Fri, 21 Jul 2017 08:39:04 +0800 (CST) From: "Tiezhu Yang" To: gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH] driver core: restrict buffer length in online_store() X-Priority: 3 X-Mailer: Coremail Webmail Server Version SP_ntes V3.5 build 20160729(86883.8884) Copyright (c) 2002-2017 www.mailtech.cn 126com Content-Type: text/plain; charset=GBK MIME-Version: 1.0 Message-ID: <665c0831.b66.15d629566c7.Coremail.kernelpatch@126.com> X-Coremail-Locale: zh_CN X-CM-TRANSID: EMqowAA3bQgpTXFZpSljAQ--.863W X-CM-SenderInfo: xnhu0vxosd3ubk6rjloofrz/1tbi6BYb9VUJZvDwswABsY X-Coremail-Antispam: 1U5529EdanIXcx71UUUUU7vcSsGvfC2KfnxnUU== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by nfs id v6L0dKbH030787 Content-Length: 2477 Lines: 63 According to Documentation/ABI/testing/sysfs-devices-online, in order to control CPU N's hotplug state, we should write one of 'Yy1Nn0' to the file /sys/devices/system/cpu/cpuN/online, other values should be invalid. so the buffer length should be 2, buf[0] is one of 'Yy1Nn0' and buf[1] is '\n'. without patch: [root@localhost home]# echo 0test > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 0 [root@localhost home]# echo 1test > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 1 [root@localhost home]# echo ntest > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 0 [root@localhost home]# echo ytest > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 1 [root@localhost home]# echo Ntest > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 0 [root@localhost home]# echo Ytest > /sys/devices/system/cpu/cpu1/online [root@localhost home]# cat /sys/devices/system/cpu/cpu1/online 1 with patch: [root@localhost home]# echo 0test > /sys/devices/system/cpu/cpu1/online bash: echo: write error: Invalid argument [root@localhost home]# echo 1test > /sys/devices/system/cpu/cpu1/online bash: echo: write error: Invalid argument [root@localhost home]# echo ntest > /sys/devices/system/cpu/cpu1/online bash: echo: write error: Invalid argument [root@localhost home]# echo ytest > /sys/devices/system/cpu/cpu1/online bash: echo: write error: Invalid argument [root@localhost home]# echo Ntest > /sys/devices/system/cpu/cpu1/online bash: echo: write error: Invalid argument [root@localhost home]# echo Ytest > /sys/devices/system/cpu/cpu1/online bash: echo: write error: Invalid argument Signed-off-by: Tiezhu Yang --- drivers/base/core.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/base/core.c b/drivers/base/core.c index 755451f..6588ed5 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -1005,6 +1005,12 @@ static ssize_t online_store(struct device *dev, struct device_attribute *attr, bool val; int ret; + /* According to Documentation/ABI/testing/sysfs-devices-online, + * the buf length should be 2, buf[0]: one of 'Yy1Nn0', buf[1]: '\n'. + */ + if (strlen(buf) != 2) + return -EINVAL; + ret = strtobool(buf, &val); if (ret < 0) return ret; -- 1.8.3.1