Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1372899ybe; Fri, 13 Sep 2019 15:45:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqyaIJi2+cGunFLPAsDvu+1+SYtgGaPCXUgM6P5d/+C3xtiiSgvTjx8VwkflE5sGYvIDtCAS X-Received: by 2002:a17:906:5299:: with SMTP id c25mr25081297ejm.64.1568414755118; Fri, 13 Sep 2019 15:45:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568414755; cv=none; d=google.com; s=arc-20160816; b=hZTVJFEQKcnXWs3VNi5nJ2GxnPnUAow9QlBdem2G7nlvcbHeOFXZVc0jipQOSev7qM t5yJDY+sD2IyBKm8mZK8NORO7tdforanVOf5ElD//EOwXUW1+NFJdaSYJtW9NWjx1E79 yD/RKJzn3J6vY2pU9F92rAatnRzMCwkSvPR3JxUzyijZVjK/JiCEZLb+W9zwzbEE4I6N BK2x96i0cP3sWXysvFW+h78iq/I5C/mh6Pyt1//9px/yE4nKoNzakvNoxbGC/u436o36 TWOrtbXtfhsMkKmNXaUVkHCqyytbYm/rWs0VgFDsIMagFGynIOxqdpbKJlcXPum2tw/y Li4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :robot-unsubscribe:robot-id:message-id:mime-version:references :in-reply-to:cc:subject:to:reply-to:from:date; bh=WRFK6nSZ1qxk1QGPl+avH6Ol30o74WPXy+W5eOv9ERg=; b=Ln4phFYGxfmrQGk9OTMcuHcx3NuFGnIAPONwqTg5Sq+dxgddXQXMPc+NagfIybec+0 brx4x9ZnBGm/B0/ggNl9uW08E58EwdLh4DZqcQNUfiicr3xR8Gd/JjEEvCWYg8I5+0RY JrQ+6c+Y9rZhRJCQdS9CErgnkw9BEOcPGGH948dZyqkTD0viNNIUej47InCkG4e+4Wx9 IqS6vjZarwKlNWX0zL/SKILqZJ7jbgUand689LVrBY0M5JDX7OFcm6er6vMfdDs+Rg/g k0Ct/DsYkhbrQoYs6gMp1YPQhm1/TfZmt2FGJmF2nxxLu8XVtUu4qvpOFN7Vj6xv4BJy aKCQ== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a26si284202edv.35.2019.09.13.15.45.29; Fri, 13 Sep 2019 15:45:55 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404124AbfIMWnq (ORCPT + 99 others); Fri, 13 Sep 2019 18:43:46 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:35704 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404009AbfIMWnq (ORCPT ); Fri, 13 Sep 2019 18:43:46 -0400 Received: from [5.158.153.53] (helo=tip-bot2.lab.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1i8uHm-0005iJ-Hf; Sat, 14 Sep 2019 00:43:27 +0200 Received: from [127.0.1.1] (localhost [IPv6:::1]) by tip-bot2.lab.linutronix.de (Postfix) with ESMTP id A23011C04F3; Sat, 14 Sep 2019 00:43:25 +0200 (CEST) Date: Fri, 13 Sep 2019 22:43:25 -0000 From: "tip-bot2 for Miles Chen" Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/core] sched/psi: Correct overly pessimistic size calculation Cc: Miles Chen , , , Linus Torvalds , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , linux-kernel@vger.kernel.org In-Reply-To: <20190912103452.13281-1-miles.chen@mediatek.com> References: <20190912103452.13281-1-miles.chen@mediatek.com> MIME-Version: 1.0 Message-ID: <156841460535.24167.6273030361884540421.tip-bot2@tip-bot2> X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the sched/core branch of tip: Commit-ID: 4adcdcea717cb2d8436bef00dd689aa5bc76f11b Gitweb: https://git.kernel.org/tip/4adcdcea717cb2d8436bef00dd689aa5bc76f11b Author: Miles Chen AuthorDate: Thu, 12 Sep 2019 18:34:52 +08:00 Committer: Ingo Molnar CommitterDate: Fri, 13 Sep 2019 07:49:28 +02:00 sched/psi: Correct overly pessimistic size calculation 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 Also remove the superfluous parentheses. Signed-off-by: Miles Chen Cc: Cc: Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Link: https://lkml.kernel.org/r/20190912103452.13281-1-miles.chen@mediatek.com Signed-off-by: Ingo Molnar --- 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 7acc632..4b14a32 100644 --- a/kernel/sched/psi.c +++ b/kernel/sched/psi.c @@ -1190,7 +1190,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;