Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3563609pxb; Mon, 24 Jan 2022 12:18:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJzgQ8apeAq6ria4YAFQY/F8213lYt8xNGhqVGAMcSLiyGVj47szB7wJUB7GucBY+Lc3jAEA X-Received: by 2002:a05:6a00:240b:b0:4bc:ff85:4e0b with SMTP id z11-20020a056a00240b00b004bcff854e0bmr15319214pfh.85.1643055496222; Mon, 24 Jan 2022 12:18:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643055496; cv=none; d=google.com; s=arc-20160816; b=0OJOvwHAnyfeC4vcyT/IR8UJgranOQTWepIccyGxwfFaaG/So/BMjzswb7zVk6Rtzd /3YenVskzpSzIgkuUMmOO/z6rA8N8ujDxoVXAudXqieTLFxD4m34s8GTF001amnUBG+r bukHwsxQvkRtyf37+VKPKGnNatES2C1+92Om3BejYmjrC3HDGon54jHWvaZH3774aI/W 76sfSKSLnRtoUCMIijSwARwvdF1DJmrbH32pN+3PoWSqZXCmrZMRqGLbvyyL9KKUkkG6 nEb6cCzycDhDe7EaZSQru11//8fmoiLMolPt+X6gV+CpIl19zzXDDbPz8LP8peSJtLoS kylg== 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=uPRNTOHjrsg5bzqFpt+yfnUXBKw33z12mdcnLGBxS6M=; b=QXc/ePGXMr/2etq9QA5xWuDrPp7byZJaWG19GbqYdQPJ22c3VunwG8BuK3h0wWM2RP bdlb25l1oXIZbYVoXfg8OKPMNxrP5XXOjIPWnwwnZ7Ei/SZ7I5M8eTdLtNCSTsNINaxM FW1nwmE/+cFBOg2Vl80fOCsUKIJdQvI/9tQm8Aje8yLPOtN6htKNT+dNJzLKXTwZCfsJ VRgjAVBOgc1hM+j7g0qnJYaw2fyZIvEeZgvAtlrKWBMh9Q4/mSQcGo4rlNnIf0pSnw8S 4FB5BSZNNCkxzGD0K/ag6k2qBdkU7E0Ky+xg9kkNVEh+flhDfX3Wb4lPUYTBISTtsJ/7 0Izw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WKhK2YGa; 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 d2si16203918plg.51.2022.01.24.12.18.03; Mon, 24 Jan 2022 12:18:16 -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=WKhK2YGa; 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 S1347962AbiAXTXO (ORCPT + 99 others); Mon, 24 Jan 2022 14:23:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347840AbiAXTNQ (ORCPT ); Mon, 24 Jan 2022 14:13:16 -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 CE8F2C061748; Mon, 24 Jan 2022 11:05:10 -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 6CC1C60E8D; Mon, 24 Jan 2022 19:05:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 41733C340E5; Mon, 24 Jan 2022 19:05:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643051109; bh=TSAP/ppNRb5T4pMvCke62Iec0PJ8ZtsT/XTjWgEu9FI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WKhK2YGavDt7M42Xvtjgtd8J/zdFWB0pUVRVbtF+iMDUQJFp4/Z+SVDeqmJahtuxh ji1nsUdk1si/ew1T98kXWEVOkRkQDI0n3WMcZ9gdCB7lMBhATOFLTh3NLe6YvX1YJx /5sJcaqlZFJ4GHt7AytFtY73iK+NXzgQAppFwFqs= 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 4.14 057/186] x86/mce/inject: Avoid out-of-bounds write when setting flags Date: Mon, 24 Jan 2022 19:42:12 +0100 Message-Id: <20220124183938.960121531@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183937.101330125@linuxfoundation.org> References: <20220124183937.101330125@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/mcheck/mce-inject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/mcheck/mce-inject.c b/arch/x86/kernel/cpu/mcheck/mce-inject.c index 94aa91b09c288..2778b66aee8fc 100644 --- a/arch/x86/kernel/cpu/mcheck/mce-inject.c +++ b/arch/x86/kernel/cpu/mcheck/mce-inject.c @@ -354,7 +354,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