Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3701073pxb; Mon, 24 Jan 2022 15:46:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJyDhbh2ZiCcNW1jirG2HLNRi0qJ7I7ZHnB8yOW7Bgfvuo87WQXbK0x0chTC+tr2apx/MNDF X-Received: by 2002:a17:902:c201:b0:14b:2c5f:b278 with SMTP id 1-20020a170902c20100b0014b2c5fb278mr12469705pll.13.1643067966151; Mon, 24 Jan 2022 15:46:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643067966; cv=none; d=google.com; s=arc-20160816; b=WFQxP6w8ZhFXmrbAqWD0hcdciPvrgdY6MFjBCHmWZR+3wFeXAqOSrFchX+cNSWxx0S qR1t3Iz8ybnS3et08IGhluazL2iWRMZviW0UFnn/7EkSz7ZplaUVlxJ87y08i0w/WC9W UdBy5bU6wrn+Y9+h+kp9oRoucAbL6sjS43hE9f5uUsj3s2ENLx9daNzjD1cIAmIQcPxc KAwGux0wOm04UHHMC9GSzHQsItppHcJ37kWGBaQbh+aiKIaWE1N+2A/qhpo5MGBOMa4N 5PllidxfsaPTpzIRQXF+t6BP2PdjBdJNA46ojmzC2CpNaIk5ntUC9qV7fbfLmG2DlvCZ WibQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=hX7lw63nck8prE8LRKDZX8QhYMKqCtXQXASzUoPRlQE=; b=G3cRbt0Mo1UNSsdMZ2BKg6qYIfbc/6qei7RDohbMbom190IM8twKOENjdxKNU8IJre SkfPZoOALtQVXZzTQybMtLjZdgqbK440+2LNAutRtAnrsDzFGNY8FUedWFzQybM3LxnC M6QcF88DpSmK25u19nI+zzhoUmpVA4w9CL9CCvoKikfmoEavbC9+nBuQm2RD7zWUp8Yb 69yjemM0wmjOJf410MIeV4urtkYxySdNiooDmnY8vgR0U+8OEJXHnIABW9yLkuWtCc4q AzyyxUntTGFx54QydWdtaUAHmkIfembAEyy2GQSNxd0+YOIt030QnV+hKn7MBqModHoF MlKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XB44q84o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l71si11997170pge.724.2022.01.24.15.45.54; Mon, 24 Jan 2022 15:46:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XB44q84o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2360567AbiAXXhB (ORCPT + 99 others); Mon, 24 Jan 2022 18:37:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382645AbiAXW4E (ORCPT ); Mon, 24 Jan 2022 17:56:04 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A21B3C055AA4; Mon, 24 Jan 2022 13:10:55 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 35C5F6141C; Mon, 24 Jan 2022 21:10:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 140B8C340E5; Mon, 24 Jan 2022 21:10:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643058654; bh=LMcJzmhZ16w/aeOwg1sAK9yzJhMtOiwmj70gGTRHMjY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XB44q84o9gU3n3jXu1cyh7205M5nbcsfMxhVkilSFqp1sRjK33oUCwAxuHsmHwYbG /g4A7Zew7PyJTT8awptb2wLEf6Oz9deCQ32/uwaVy2+9A3yF5ynd+TE6PHNo+l+Zon 7q2+F8t5tuX5AECsFXrFTjd7S5ePxKrdhq+PerwY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zhang Zixun , Borislav Petkov , Sasha Levin Subject: [PATCH 5.16 0359/1039] x86/mce/inject: Avoid out-of-bounds write when setting flags Date: Mon, 24 Jan 2022 19:35:48 +0100 Message-Id: <20220124184137.348537290@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zhang Zixun [ Upstream commit de768416b203ac84e02a757b782a32efb388476f ] A contrived zero-length write, for example, by using write(2): ... ret = write(fd, str, 0); ... to the "flags" file causes: BUG: KASAN: stack-out-of-bounds in flags_write Write of size 1 at addr ffff888019be7ddf by task writefile/3787 CPU: 4 PID: 3787 Comm: writefile Not tainted 5.16.0-rc7+ #12 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.14.0-2 04/01/2014 due to accessing buf one char before its start. Prevent such out-of-bounds access. [ bp: Productize into a proper patch. Link below is the next best thing because the original mail didn't get archived on lore. ] Fixes: 0451d14d0561 ("EDAC, mce_amd_inj: Modify flags attribute to use string arguments") Signed-off-by: Zhang Zixun Signed-off-by: Borislav Petkov Link: https://lore.kernel.org/linux-edac/YcnePfF1OOqoQwrX@zn.tnic/ Signed-off-by: Sasha Levin --- arch/x86/kernel/cpu/mce/inject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/mce/inject.c b/arch/x86/kernel/cpu/mce/inject.c index 0bfc14041bbb4..b63b548497c14 100644 --- a/arch/x86/kernel/cpu/mce/inject.c +++ b/arch/x86/kernel/cpu/mce/inject.c @@ -350,7 +350,7 @@ static ssize_t flags_write(struct file *filp, const char __user *ubuf, char buf[MAX_FLAG_OPT_SIZE], *__buf; int err; - if (cnt > MAX_FLAG_OPT_SIZE) + if (!cnt || cnt > MAX_FLAG_OPT_SIZE) return -EINVAL; if (copy_from_user(&buf, ubuf, cnt)) -- 2.34.1