Received: by 2002:a05:7412:8d1c:b0:fa:4c10:6cad with SMTP id bj28csp329282rdb; Wed, 17 Jan 2024 03:26:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IEJxeX7CQKLzRkZ1bFlJRWjqKXFsGimwtVOz3SDk8Zx8lZxsSbn+qSptIZ4mYvS3OBITVyL X-Received: by 2002:a05:6402:22c6:b0:557:186c:3218 with SMTP id dm6-20020a05640222c600b00557186c3218mr2963930edb.22.1705490798629; Wed, 17 Jan 2024 03:26:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705490798; cv=pass; d=google.com; s=arc-20160816; b=bazDYZSkL84qRfTwmMv5uWcpZz5cAV3tb0IWrk4sf+z02SZzJkCVboClOEF7AEzxyp iG9Yi+LH/eSrUeJnyihPcw8GXt2E10Mhmwa8xA7ZFAOZDNnREllfu7hZCDNqL+6U2xjn kQdlUWpC3BXe8Pbkx987j0G9JKK5/lTm9nOwr8HSsCesGphBR6DrLxQtYT7vDh7fM06g kaScJu6zJmzF06RcKuKXvS4df0/QC4F+MJpiV5dzEpU64sZmlPonoCZAtPK5PYTieP6L hmoSGFvzhTp3ESST9NHvdd8DzeAH8V2kFwcuoHnvYJ9qF9pIZNv97TgWcPTakoGZWajs jr1g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=BZLQAD6zLv5s7GfOU0kGphTKQynBzASFqhi8uf7R5bs=; fh=0GMRHN+hRDKEhW8wj3y4T8gNfPqr/P/UegmIbVNUgx4=; b=ZgsyLqIGQxPAGUaU7zhm2bUEQR6fS2F1b+46c5cFqRwbEC2SbxviKzjCiqWigT5cSC RR7Nbvu9iTE6bdnu+4BBhsitTf7yRuof41TwFW87DK3wsCFilxUnWBkqMYj9XGsvqZMI 10dkKQZ6JPa1hlHItYhjul5iqNtZML5wzZbsin3/3oZpCo0ClptL9KtglZ7cqf2C/u7Q TOFcT38OECsxkbtU+ez+I03gtCwV2QDDKTtUeR2l4Cpp0kFbhsARtSQdHQHNbwMdTWme nSr/Q2Y9RP799pZu3Epigy4LZokpOnn2xDniaBO83fuVXn4SccqOqlHeLM6PnEGA/Eh3 AWzg== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-28909-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-28909-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id y2-20020a50e602000000b00559454d10dfsi2906737edm.663.2024.01.17.03.26.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jan 2024 03:26:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-28909-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=linux.alibaba.com dmarc=pass fromdomain=linux.alibaba.com); spf=pass (google.com: domain of linux-kernel+bounces-28909-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-28909-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 5BFCA1F24C6B for ; Wed, 17 Jan 2024 11:26:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B202E1DDF5; Wed, 17 Jan 2024 11:26:16 +0000 (UTC) Received: from out30-133.freemail.mail.aliyun.com (out30-133.freemail.mail.aliyun.com [115.124.30.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 24E261DDE7 for ; Wed, 17 Jan 2024 11:26:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.133 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705490776; cv=none; b=fXP6vrON/1wtKHc83lQVkfLXebIYjjyjLIhHjrKQYovJsvQSaWV8Jm+1vsBttTBx/9DL1w8s598Y06EEz5puHtoStfGbblhX6Q6wjTCoQj3MZxNYsyJdjIMsUVnu1t3Ktj+YBvTCc2s5A+pHIvfi+/xV5zToTx8w8gqle3U1tFA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705490776; c=relaxed/simple; bh=3g850I79bJFIBqc3V9qzvsDFb9DcwF8ME2+QRi5ZdsM=; h=X-Alimail-AntiSpam:Received:From:To:Cc:Subject:Date:Message-Id: X-Mailer:MIME-Version:Content-Transfer-Encoding; b=H4B0qFryTxT9OTYaSO1vrwIruB0hGv1XVcQLSLkoGAPsAOXco2nC8DiWxBMeNknuHH/F3EvqotBRAuATMLXCOla65BIyvJajr0X0zHNU0qA20eZqkgd141eu2MrMGGTkGQJtd5FF9FvwhI5OmruKU3XsDcRHahWqZCbBPwFo35U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; arc=none smtp.client-ip=115.124.30.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R531e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046049;MF=escape@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0W-pLl.H_1705490764; Received: from localhost(mailfrom:escape@linux.alibaba.com fp:SMTPD_---0W-pLl.H_1705490764) by smtp.aliyun-inc.com; Wed, 17 Jan 2024 19:26:05 +0800 From: Yi Tao To: hannes@cmpxchg.org, surenb@google.com, peterz@infradead.org Cc: linux-kernel@vger.kernel.org Subject: [PATCH] sched/psi: Fix the bug where the last character is overwritten Date: Wed, 17 Jan 2024 19:26:01 +0800 Message-Id: X-Mailer: git-send-email 2.32.0.3.g01195cf9f Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The buffer buf in psi_write has only 32 bytes, and to ensure the correct parsing of the string, it needs to be terminated with '\0', which means users can input no more than 31 characters. When the user inputs fewer than 31 characters, buf_size equals nbytes, which causes the last character entered by the user to be overwritten by '\0', affecting the parsing results. Here is a specific example. $echo -n "some 500000 1000000" > /proc/pressure/cpu $bash: echo: write error: Invalid argument Because the last character is overwritten, the value obtained by sscanf parsing is 500000 and 100000; window_us is missing a zero, hence the return of -EINVAL. The reason 'echo' without the '-n' flag can be parsed correctly is because the last character that gets overwritten is '\n', so it won't return an error. Limiting buf_size to no more than 31 and writing '\0' at the position of buf_size can fix this bug. Signed-off-by: Yi Tao --- kernel/sched/psi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c index 7b4aa5809c0f..5ae336e1c2d8 100644 --- a/kernel/sched/psi.c +++ b/kernel/sched/psi.c @@ -1523,11 +1523,11 @@ static ssize_t psi_write(struct file *file, const char __user *user_buf, if (!nbytes) return -EINVAL; - buf_size = min(nbytes, sizeof(buf)); + buf_size = min(nbytes, sizeof(buf) - 1); if (copy_from_user(buf, user_buf, buf_size)) return -EFAULT; - buf[buf_size - 1] = '\0'; + buf[buf_size] = '\0'; seq = file->private_data; -- 2.32.0.3.g01195cf9f