Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3572522pxb; Mon, 24 Jan 2022 12:31:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJyL/RND//JXC4yS5rvAi89HFYXbzGO4L/Z/KKZYAjYNxIwVZ1V04vo+m2vATD7tE16o+GP0 X-Received: by 2002:a17:90a:7024:: with SMTP id f33mr15181pjk.15.1643056313242; Mon, 24 Jan 2022 12:31:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643056313; cv=none; d=google.com; s=arc-20160816; b=xE1x6tTyYeRcwzx/TG7EwsZZZlpwNGl4aRUHaobGA7uTJ0Ry7R2lI8P70spXersG7S VraFuZj2iUFhHgP7fWv5bDdW3I42zt950AlC/qHIyMkL+fLXENftVAQSa/uUtxRvNvwo TyZcTZ8j8HSI/1Nt+iH+D5qqpILe0ahDQurkuduYFGgVYOBLiB3/1VknYh+pKCvOOrux nsGy6JXQdyUOumygoymJnHyJH9Bqz/148VsrKUkStPR4n/6SUpuogxOiASEOf6pGM+qi aXyC+Q0C6RkLvJM95NhpiGPWJR1JzTz2j2sIozuC7FPY+vIp/HBztzJVNCfVU84TICj9 4idQ== 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=QYBxJU1/+GoKNoO+/sMsvooR6ViUin9vrgtRdTFZC4M=; b=BE+ZDnKsyVJL0SFpRrXw24H0zdM996V27J16XADChrVjZB9HnCqAb5UmvS+ZEOcSA4 51w7BJDXidxq+PibyKBo70gZXSSXqdOGXn2aIoosXUtt+lJ996faqFrNesKDK7ae5gb2 a+j2KRIlE6Egdqm/i75VmIKFyMGRt8sO4hrgy7lGRiXaNAh07VcDcMckDDVkNY5iMXLQ 59poNb3kFo00UNptnE+A8v1YSBlBpwPf5ySjaByxwrkC86GO6LTiOoKcT9aGj+Bfj6tN nkp7zr2m1SYS28jbewABzsghC13UgKIymYqk1bvWeMHH8fJGpIi0UxAckby81GlhtfuB OpVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0f5jruIC; 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 iw4si296370pjb.71.2022.01.24.12.31.35; Mon, 24 Jan 2022 12:31:53 -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=0f5jruIC; 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 S1354262AbiAXTgV (ORCPT + 99 others); Mon, 24 Jan 2022 14:36:21 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:57640 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352504AbiAXTaf (ORCPT ); Mon, 24 Jan 2022 14:30:35 -0500 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 326B3612A5; Mon, 24 Jan 2022 19:30:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0ABE3C340E7; Mon, 24 Jan 2022 19:30:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643052634; bh=CRtmMYSLfsM5EdcONKC8ytJivi6XgTG8sj7QVf4Qt/Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0f5jruICbqhZPQYCBCyxh2YfP41HbSVy5zsf64+GFoINaFj1mo9xltXbXz1Y2RB6i w8cpP8whKXcZlvbZfon1DQe2BPlsz8XytzIfWxJeCvgdxlo7frfnFFp+39TViBXD+g D1Aqp7Ps0ZGfoRwqwQx9DszsrRqxpVs31YC6+h7o= 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.4 097/320] x86/mce/inject: Avoid out-of-bounds write when setting flags Date: Mon, 24 Jan 2022 19:41:21 +0100 Message-Id: <20220124183957.027398739@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183953.750177707@linuxfoundation.org> References: <20220124183953.750177707@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 eb2d41c1816d6..e1fda5b19b6f6 100644 --- a/arch/x86/kernel/cpu/mce/inject.c +++ b/arch/x86/kernel/cpu/mce/inject.c @@ -347,7 +347,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