Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp2510179ybe; Thu, 12 Sep 2019 10:27:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqyIamG6/n0hurrcg5xovR+687N/Zbr87AglasXk00/46wgvhpFMK1msMYQU4DVtimSLnQvQ X-Received: by 2002:aa7:dccb:: with SMTP id w11mr42605812edu.235.1568309230287; Thu, 12 Sep 2019 10:27:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568309230; cv=none; d=google.com; s=arc-20160816; b=vr1QYEmdWIbD/mUAuw5Hlp/SDxmO+V4XeR9mZw0+aB4mA5r9fZmXj/xZpD15GJtKFN lavUHC3w4sBpFkjDP4epc2ZLKRZD0JW7KPmfqkQOpQWAQqNSMl6aShrK0y+jL6xmBEy9 fUGrxUdhX9OIr9+uLcTddiP2V1GYbKZpQj245dKC6MZyVk3npW7jWxf0YAb3MfpseAQi GPoH4DnFAHMO0xY2NSCXBVeH2/2O58Wd1yfT1y8RqotCIb7GDpzvoylaXdllWMZOnDCW ndVYvPQNiKOgEvDE7si2VDdWa66Uih7Eao+RjmERoRKKqOz9YUzjdXDv2Nwq2Po5sZrj +Lfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=0cPxuROKfunnc3LwkXNBAPlZE+hAhdYjtbSuTN8kvi8=; b=YjMyYEcgi6QiBEKWGDPw/s36MkHA20K9/Lp4HC5Hhm6NUQ3GiNGJg4NXxLr/wRKa4U MfKpBu+iNfZDrUCTPbc+ZPE43aw+VwbTuGd4BOaN1AYl2n3BGYOhRYW0XdH8OQDKGwr9 4/X20quBDrxaYWxSFhTCm6c/TGDgflHXfW2MmlDUIocFsV8tFEWTWl8DoWTcfHT2C/Zf TWmWNpGIfmU70ezSsZLxUfeauaF3aq98N2NcNJxNiixJx8NC5dfGid7MGax2wc3nDs3p qa/1rr6qMHY8emN08OAOcRi51AfafFL9rXtzjIKrwzQfxxbhUAAZMf3H91pPdNfI9x40 xYTA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q10si14760773eda.293.2019.09.12.10.26.46; Thu, 12 Sep 2019 10:27:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731027AbfILKfE (ORCPT + 99 others); Thu, 12 Sep 2019 06:35:04 -0400 Received: from mailgw01.mediatek.com ([210.61.82.183]:47479 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1730470AbfILKfE (ORCPT ); Thu, 12 Sep 2019 06:35:04 -0400 X-UUID: db49a7ec7fa84b9c9ce2069bd83947ed-20190912 X-UUID: db49a7ec7fa84b9c9ce2069bd83947ed-20190912 Received: from mtkcas06.mediatek.inc [(172.21.101.30)] by mailgw01.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.10 Build 0809 with TLS) with ESMTP id 469677759; Thu, 12 Sep 2019 18:34:54 +0800 Received: from mtkcas07.mediatek.inc (172.21.101.84) by mtkmbs06n1.mediatek.inc (172.21.101.129) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Thu, 12 Sep 2019 18:34:51 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkcas07.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1395.4 via Frontend Transport; Thu, 12 Sep 2019 18:34:52 +0800 From: Miles Chen To: Ingo Molnar , Peter Zijlstra CC: , , , Miles Chen Subject: [PATCH] psi: put NULL char correctly in psi_write() Date: Thu, 12 Sep 2019 18:34:52 +0800 Message-ID: <20190912103452.13281-1-miles.chen@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When passing a equal or more then 32 bytes long string to psi_write(), psi_write() copies 31 bytes to its buf and overwrites buf[30] with '\0'. Which makes the input string 1 byte shorter than it should be. Fix it by copying sizeof(buf) bytes when nbytes >= sizeof(buf). This does not cause problems in normal use case like: "some 500000 10000000" or "full 500000 10000000" because they are less than 32 bytes in length. /* assuming nbytes == 35 */ char buf[32]; buf_size = min(nbytes, (sizeof(buf) - 1)); /* buf_size = 31 */ if (copy_from_user(buf, user_buf, buf_size)) return -EFAULT; buf[buf_size - 1] = '\0'; /* buf[30] = '\0' */ Before: %cd /proc/pressure/ %echo "123456789|123456789|123456789|1234" > memory [ 22.473497] nbytes=35,buf_size=31 [ 22.473775] 123456789|123456789|123456789| (print 30 chars) %sh: write error: Invalid argument %echo "123456789|123456789|123456789|1" > memory [ 64.916162] nbytes=32,buf_size=31 [ 64.916331] 123456789|123456789|123456789| (print 30 chars) %sh: write error: Invalid argument After: %cd /proc/pressure/ %echo "123456789|123456789|123456789|1234" > memory [ 254.837863] nbytes=35,buf_size=32 [ 254.838541] 123456789|123456789|123456789|1 (print 31 chars) %sh: write error: Invalid argument %echo "123456789|123456789|123456789|1" > memory [ 9965.714935] nbytes=32,buf_size=32 [ 9965.715096] 123456789|123456789|123456789|1 (print 31 chars) %sh: write error: Invalid argument Signed-off-by: Miles Chen --- kernel/sched/psi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c index 6e52b67b420e..517e3719027e 100644 --- a/kernel/sched/psi.c +++ b/kernel/sched/psi.c @@ -1198,7 +1198,7 @@ static ssize_t psi_write(struct file *file, const char __user *user_buf, if (static_branch_likely(&psi_disabled)) return -EOPNOTSUPP; - buf_size = min(nbytes, (sizeof(buf) - 1)); + buf_size = min(nbytes, sizeof(buf)); if (copy_from_user(buf, user_buf, buf_size)) return -EFAULT; -- 2.18.0