Received: by 10.213.65.68 with SMTP id h4csp513286imn; Fri, 16 Mar 2018 10:02:44 -0700 (PDT) X-Google-Smtp-Source: AG47ELuAojDc+1pFSicEyzn2nvcwwrPRx8fn2QEe5KkRdnK8MHWHoUnQF51JdKUmGHIil32oLGfH X-Received: by 10.99.142.201 with SMTP id k192mr1993092pge.278.1521219764372; Fri, 16 Mar 2018 10:02:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521219764; cv=none; d=google.com; s=arc-20160816; b=oVFWaXaybF4nZmZx9AUZzFBFdl56nBE6xIN85sc1uZfTyUgrs+/qb3CRAi8YSeBfqe 6mkjaCYYKTz3nyifkv0WRjFdE7kSs1Ny8wyRtwx4phVKCgsEvswGxxmmm62lqjS569/m fYXM4JfGLZ/gCJONYDYiZoyfYxuypxujcR3+v5C4wNtDPXNTLLrtSQJaauryw1IpKhUU LQVUOdKvOVGVJpIfOOnTZRnqMLoiYICTSfH1ZVy9TqLIF8IGIQCa+UWXkH6CwVxe2mHA 0pZq3/ZOg2EWYWW+srLhy8he1jp2tVml7B1Ab1cerAx2l6qVlcZgmYQe3XXCrwwJe9pG pZYQ== 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=UdpBa96UFw5E81eJ8tuxvYai6KGkacfNt1V4Oozds3E=; b=ft+ihAvYqwxl3oEM2BvCJ+iY32gnRgn/+M4B3oWUj6Bm/1/aEpU6LtfDkeSisEnEra KlkRf0UgQt3/b0LfwBUJGoPZR9zpI9soVBfbKi/qA7pVNj/aSydcdjD8s/DJyVaGVsS8 mKJsk9XBitVR/GcooJZX0jd/OXMfoNU/IJ9altSHrcbUvIqUq32EE8FIW1Yrynzkr2ZG Lm79y843sS0/7rkPLYhhptTYK1AM7A0gRnUq/foFdkNqPaDwS8wQgbQ1wAPMP6yj4zci 1/6nhgt43ba+Y6m+aZ9V+qomnjgg16Gt8s9hnXhygC7aDKJnSt+9uVmdoQRpeUS98672 E2jQ== 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 f5-v6si6622251plf.223.2018.03.16.10.02.29; Fri, 16 Mar 2018 10:02:44 -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 S932919AbeCPRAf (ORCPT + 99 others); Fri, 16 Mar 2018 13:00:35 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:38142 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933621AbeCPPct (ORCPT ); Fri, 16 Mar 2018 11:32:49 -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 25DA111EE; Fri, 16 Mar 2018 15:32:48 +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.9 69/86] x86/module: Detect and skip invalid relocations Date: Fri, 16 Mar 2018 16:23:32 +0100 Message-Id: <20180316152322.014506339@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180316152317.167709497@linuxfoundation.org> References: <20180316152317.167709497@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.9-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 @@ -171,19 +171,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 @@ -199,6 +207,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);