Received: by 10.213.65.68 with SMTP id h4csp527686imn; Fri, 16 Mar 2018 10:27:27 -0700 (PDT) X-Google-Smtp-Source: AG47ELsPG0HnRJuYPnXbHBKmq5rdFvGsNTdPjSzrNhG0j7NhuKXhTy773NAJdmGKB26AACemEVL7 X-Received: by 10.99.98.196 with SMTP id w187mr2105321pgb.307.1521221247758; Fri, 16 Mar 2018 10:27:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521221247; cv=none; d=google.com; s=arc-20160816; b=nQ0VnAmqalFNM5g27Xf1QGdThtZvGKNJci+GQzygtc5xiWvwd8Yke7140NfL7awIX7 hX0+UslhZMMZvkjwbPCctkIABI06S8j6ikJ8jhoINz2YBTRFOnqEY+l/W7AsIBojuPNR 1ZiA8gEbnFo9TVOLgkc9pYL+CxIOfjnuwWbL6j6BptqXX4Cwhw4eouADhRD+n0IwfynU 4QfB/HJWPJvBVYGQVeo1XS6ikpPS0vDIc25iyA4q8np9mH0hilp8EcmmAv++bwvh8KmY xbJUAYxF0h6lJaiB/NLfnEnVq1x1HUwkObE4bxnkdWJA68KbsvcGckMBGoHIkwUIDKGE prbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=VlliM+IobCHDCczrfU3T4JGKug1Z90NERPWR36LDd6I=; b=iL9m1TTnCwVAqjU3SONbbwf+FA5PBun0G8Ay1GSj5AHXKemHAGZlVYTPT/pNcMhOos lnXp2dU6eNJ7wkbEyWBHlOUNULVxZePNI9R9KhWO3yoraRQ7zCxdCzNP3fPQgwwAMabr inPwbNIF2g+t1Ozvs3H0mJW5VU+x8c/sqgg1GiFh+UZx2aKeTXcaLR+mg42Tg13sNEH9 pp6rGl89aYvZBR6SM7WVN3/G5XjzYPSjQGSPzba8lHmP6Xs9pqQZQyKofqt4C66I1y9p 6Zb23J0DlifYRKi88bgzw9gohcz9fCNcMTU9k2Bas+lq2c6g0AM4HEJwd8m8m7V55GxO y2Ig== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 7-v6si748139pll.132.2018.03.16.10.27.14; Fri, 16 Mar 2018 10:27:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753556AbeCPR0U (ORCPT + 99 others); Fri, 16 Mar 2018 13:26:20 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:35856 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754050AbeCPP2q (ORCPT ); Fri, 16 Mar 2018 11:28:46 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id A98BFE4B; Fri, 16 Mar 2018 15:28:45 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Josh Poimboeuf , Linus Torvalds , Peter Zijlstra , Thomas Gleixner , jeyu@kernel.org, live-patching@vger.kernel.org, Ingo Molnar , Matthias Kaehlcke Subject: [PATCH 4.4 50/63] x86/module: Detect and skip invalid relocations Date: Fri, 16 Mar 2018 16:23:22 +0100 Message-Id: <20180316152305.414292807@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180316152259.964532775@linuxfoundation.org> References: <20180316152259.964532775@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Josh Poimboeuf commit eda9cec4c9a12208a6f69fbe68f72a6311d50032 upstream. There have been some cases where external tooling (e.g., kpatch-build) creates a corrupt relocation which targets the wrong address. This is a silent failure which can corrupt memory in unexpected places. On x86, the bytes of data being overwritten by relocations are always initialized to zero beforehand. Use that knowledge to add sanity checks to detect such cases before they corrupt memory. Signed-off-by: Josh Poimboeuf Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: jeyu@kernel.org Cc: live-patching@vger.kernel.org Link: http://lkml.kernel.org/r/37450d6c6225e54db107fba447ce9e56e5f758e9.1509713553.git.jpoimboe@redhat.com [ Restructured the messages, as it's unclear whether the relocation or the target is corrupted. ] Signed-off-by: Ingo Molnar Cc: Matthias Kaehlcke Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/module.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) --- a/arch/x86/kernel/module.c +++ b/arch/x86/kernel/module.c @@ -170,19 +170,27 @@ int apply_relocate_add(Elf64_Shdr *sechd case R_X86_64_NONE: break; case R_X86_64_64: + if (*(u64 *)loc != 0) + goto invalid_relocation; *(u64 *)loc = val; break; case R_X86_64_32: + if (*(u32 *)loc != 0) + goto invalid_relocation; *(u32 *)loc = val; if (val != *(u32 *)loc) goto overflow; break; case R_X86_64_32S: + if (*(s32 *)loc != 0) + goto invalid_relocation; *(s32 *)loc = val; if ((s64)val != *(s32 *)loc) goto overflow; break; case R_X86_64_PC32: + if (*(u32 *)loc != 0) + goto invalid_relocation; val -= (u64)loc; *(u32 *)loc = val; #if 0 @@ -198,6 +206,11 @@ int apply_relocate_add(Elf64_Shdr *sechd } return 0; +invalid_relocation: + pr_err("x86/modules: Skipping invalid relocation target, existing value is nonzero for type %d, loc %p, val %Lx\n", + (int)ELF64_R_TYPE(rel[i].r_info), loc, val); + return -ENOEXEC; + overflow: pr_err("overflow in relocation type %d val %Lx\n", (int)ELF64_R_TYPE(rel[i].r_info), val);