Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935225AbcIQKUQ (ORCPT ); Sat, 17 Sep 2016 06:20:16 -0400 Received: from youngberry.canonical.com ([91.189.89.112]:42655 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757077AbcIQKUI (ORCPT ); Sat, 17 Sep 2016 06:20:08 -0400 From: Colin King To: Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" , x86@kernel.org, Borislav Petkov , Aravind Gopalakrishnan , Yazen Ghannam , Peter Zijlstra Cc: linux-kernel@vger.kernel.org Subject: [PATCH][V3] x86/RAS/mce_amd_inj: fix signed wrap around when decrementing index i Date: Sat, 17 Sep 2016 11:17:50 +0100 Message-Id: <20160917101750.6436-1-colin.king@canonical.com> X-Mailer: git-send-email 2.9.3 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Colin Ian King Change predecrement compare to post decrement compare to avoid an unsigned integer wrap-around comparisomn when decrementing in the while loop. For example, if the debugfs_create_file fails when i is zero, the current situation will predecrement i in the while loop, wrapping i to the maximum signed integer and cause multiple out of bounds reads on dfs_fls[i].d as the loop interates to zero. Also, as Borislav Petkov suggested, return -ENODEV rather than -ENOMEM on the error condition. Signed-off-by: Colin Ian King --- arch/x86/ras/mce_amd_inj.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/ras/mce_amd_inj.c b/arch/x86/ras/mce_amd_inj.c index cd318d9..20b227f 100644 --- a/arch/x86/ras/mce_amd_inj.c +++ b/arch/x86/ras/mce_amd_inj.c @@ -464,13 +464,13 @@ static int __init init_mce_inject(void) return 0; err_dfs_add: - while (--i >= 0) + while (i-- > 0) debugfs_remove(dfs_fls[i].d); debugfs_remove(dfs_inj); dfs_inj = NULL; - return -ENOMEM; + return -ENODEV; } static void __exit exit_mce_inject(void) -- 2.9.3