Received: by 2002:a05:7412:8d1c:b0:fa:4c10:6cad with SMTP id bj28csp537700rdb; Wed, 17 Jan 2024 09:21:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IE1AWcHZ2gQ+qPtu2eLsOQYSmPCnafL561hF8DvbPVv0wBfN76K+taPcIYQQJjYNBlD1t7e X-Received: by 2002:a05:6870:2d2:b0:206:9934:72d6 with SMTP id r18-20020a05687002d200b00206993472d6mr11359628oaf.101.1705512115305; Wed, 17 Jan 2024 09:21:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705512115; cv=pass; d=google.com; s=arc-20160816; b=qnZvbOuLpCS26rNirTHJQqk4K8+H95vlp9kAYcPerzBUSSRJsXViWXI86LXfbwulJA /FU1g0i+t/EiZVKS0aVHdhwP8F3BWYP4Vap8zNUmHSQZYwiVH/Ovi0VK9jWKM/Uuuwit V6Oa7NwY/JuThneYtqyDd9OpVJtbD8ozlbEc6z9WKG0zW2ioynSXSsjgoOB7aOdUBflm 8N2jRZnNd2Zdo9QpauJGMPerY+0Qd9RGi9ZZ+2thFaExjNDjvFy2RmjdX0DO58/scUC4 cYcT+uhi3VPLWZdo/Wyh1cLal1CHaW9NOaS4jyptOYdjFp2qzNuBq8QfZO3qiSxOjQTh Qb3w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=UYBsFknY3ImOfYB9kfT3cJP7oPzr41NJL3MolYvO8iw=; fh=aHyJqBMjOguKB58BjPlggoJu4ScradDqWNiS7Z/r1/w=; b=gVZBPOsrJ/GibR3gphzX04V5k3hT+T0iZHKxUdwFQOamsXakuDbbKNJT8tga0Ax8eh X40pD8tbXOLRc3pSghVewf7q4D/Z6i0DHo10aQJbSrwUQTVkfDxftZiq1iBVrFgxx59X RAHSDUMcxZuANC0hyxUzIrNoTbDCw0bgwH/LfuokfIOrjx12Nm8Bl9PzoX4IxuLYHK4Y 0Q0Y057LHD6d2DVyaZtcw+/aQ5m92PtMnfs6NX93YwtpynP9/5/NHaCUIc6x32Fi0GnU CoN6AlkfJ7pTTBjwj+4IVWWOyN78lC11hXC6ZBSr5scK/dHXywoSu6nRlqZkm1F2aFZa MIRg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=D7oJnCpW; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-29268-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-29268-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id bw16-20020a056a02049000b005ce26bd4812si15666963pgb.200.2024.01.17.09.21.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jan 2024 09:21:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-29268-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=D7oJnCpW; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-29268-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-29268-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id F0092B21E6B for ; Wed, 17 Jan 2024 17:21:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6893C22616; Wed, 17 Jan 2024 17:21:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="D7oJnCpW" Received: from mail-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F318121A0E for ; Wed, 17 Jan 2024 17:21:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705512107; cv=none; b=iWbI7CzZyNOlNibn4gMSC0LoTlscO0yXLgCr8KoDC3Hz+VKIQGOtRBqqgtMgPgV+Kux1IXeaUZF5AET2gXB8nkYDUL8DzC3DhLFsSdtHDKmldDbYrF9IsayPuHLhicgrSeSszfYhlnWp35XP3cNyMmvUmZORbdEND7u5RFCvUTY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705512107; c=relaxed/simple; bh=vwJ72PoSiCDBjaA+okTI+Kg6BS/K0JeEgtI4ljotfO8=; h=Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Google-Smtp-Source:X-Received:MIME-Version: References:In-Reply-To:From:Date:Message-ID:Subject:To:Cc: Content-Type:Content-Transfer-Encoding; b=S3LHxyyC3+QFcYHVugX0pCrxvzYDCZL4/vh1DYl1F7PSjzyL5oGM0RQEdOxwyLA5w2rtb77a33jSeYX6YC0UAS44BzkWka4ndsbomovQvotV/zI37fkum5nGiEMvj5UTwiNaw1NciimC2j5/q4y9CldNICMHpaM5XyyuMmtHlLE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=D7oJnCpW; arc=none smtp.client-ip=209.85.219.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-yb1-f177.google.com with SMTP id 3f1490d57ef6-dc21ad8266fso2162329276.1 for ; Wed, 17 Jan 2024 09:21:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705512105; x=1706116905; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=UYBsFknY3ImOfYB9kfT3cJP7oPzr41NJL3MolYvO8iw=; b=D7oJnCpWLLSScdHKsCX1VyinMzCXFtN7H/X9PflUT8LNPpjaI+PGqExWzh6bXyC/H6 6WmmEPjcmN3LmZ6cTW6iPJK27Y3Nc9JiWMbDZpfAHLsZTEGcSQMXmEqmr0R0FXltQJd9 HI4hNAaGZ+l8bg0UFonZa9kQytxrtgE5la2Gm/WHOFkbd7a4TRGXR2KOAVWeUJMYjoyB pJkh/0lAHa2t6KbkMELM8+DMWefo4HKMwR1ifn3ir20cDdPjzkylPs9vaHGOPAouORZZ mz8X9la/zvxDyE2kVAyA5vcDO2kk8wVnuh8GOhc6KwTSs8YtTS/mB4L9vyUbTJP9S7B4 bPpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705512105; x=1706116905; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UYBsFknY3ImOfYB9kfT3cJP7oPzr41NJL3MolYvO8iw=; b=W9fsrFyiOjX6Qj7Ihvf0HIN72Snes5H2RGwGvtXQtyjlCpaprY/YVOb3UqTP4vCl2E SDPEYMLyoiZEugezTTQ7L9obMVTjwV2vKOMTZQI8atLod4siv/sQgjPV4FjoJDpSXVF1 Q+nAAfyumagwxnSxE6b3F3to79K/ZAJ4JxZla2aMxvU/usLX1bAoceWFR58TSPwbGlXM 5ZT4TfRvXlI96DrvPO/ivMDc9quoFmGa7in8syKNJkHuVyyXpDFOoygV4MgF35DSsqrm HHCMQE6ARSyXeiNej6I/0d/tCwDmBUjy38mxUKyccvLb8CMEDm/erQi+swFFXTlWHarx R3SA== X-Gm-Message-State: AOJu0YzeEr1e3b5FgDjMgXG1g9Ysa35IfBZzNdknt313lA182HI/yF4j LdQRWLgvyr7+0L1jnzMqiifzL2lm56VXnuGcC1RSTCmqPyeDG3NUXcyIZQmHAw== X-Received: by 2002:a25:9703:0:b0:dc2:36d7:1a79 with SMTP id d3-20020a259703000000b00dc236d71a79mr1436789ybo.20.1705512104633; Wed, 17 Jan 2024 09:21:44 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240117170743.GD939255@cmpxchg.org> In-Reply-To: <20240117170743.GD939255@cmpxchg.org> From: Suren Baghdasaryan Date: Wed, 17 Jan 2024 09:21:30 -0800 Message-ID: Subject: Re: [PATCH] sched/psi: Fix the bug where the last character is overwritten To: Johannes Weiner Cc: Yi Tao , peterz@infradead.org, linux-kernel@vger.kernel.org, Miles Chen Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jan 17, 2024 at 9:07=E2=80=AFAM Johannes Weiner wrote: > > On Wed, Jan 17, 2024 at 07:26:01PM +0800, Yi Tao wrote: > > The buffer buf in psi_write has only 32 bytes, and to ensure the correc= t > > 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. > > Good catch. There is actually a history of this code changing back and > forth. However, it's always assumed the last byte to be \n and cut > that off. The original version was this: > > char buf[32]; > buf_size =3D min(nbytes, (sizeof(buf) - 1) /* 31 */); > if (copy_from_user(buf, user_buf, buf_size)) > return -EFAULT; > buf[buf_size - 1 /* 30 */] =3D '\0'; > > Which expected \n and actually cut off the last copied character. So > without a newline, the window would have been truncated then already. > > It also didn't use the full buffer, which Miles fixed in 4adcdcea717c > ("sched/psi: Correct overly pessimistic size calculation"): > > char buf[32]; > buf_size =3D min(nbytes, (sizeof(buf)) /* 32 */); > if (copy_from_user(buf, user_buf, buf_size)) > return -EFAULT; > buf[buf_size - 1 /* 31 */] =3D '\0'; > > but it still cut off that last character, which is either newline or, > welp, the last character of the window spec. Bad, bad. Indeed, nice catch! Thanks! > > It also copies the last input byte just to overwrite it again, which > is a bit odd. > > > Limiting buf_size to no more than 31 and writing '\0' at the position o= f > > 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, con= st char __user *user_buf, > > if (!nbytes) > > return -EINVAL; > > > > - buf_size =3D min(nbytes, sizeof(buf)); > > + buf_size =3D min(nbytes, sizeof(buf) - 1); > > if (copy_from_user(buf, user_buf, buf_size)) > > return -EFAULT; > > > > - buf[buf_size - 1] =3D '\0'; > > + buf[buf_size] =3D '\0'; > > This looks right. It'll leave a newline in the buffer if present, but > the sscanf() that follows is happy to ignore that. > > Acked-by: Johannes Weiner > > I'm thinking we should also CC stable. A truncated window is pretty > difficult to debug, and may not even result in the (ominous) -EINVAL > if the resulting value is still a valid size. It'll quietly poll for > very different parameters than requested. Agree. Acked-by: Suren Baghdasaryan > > Cc: stable@vger.kernel.org