Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1169981pxj; Sat, 8 May 2021 09:33:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy0n8EJ4IKvp430ebU81cAWEbMaXQdtO3ZioiX52MeIiqEPMJ1GtEUYUqO+ko5ZwVRueAoF X-Received: by 2002:a05:6402:1a:: with SMTP id d26mr18903135edu.99.1620491591133; Sat, 08 May 2021 09:33:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620491591; cv=none; d=google.com; s=arc-20160816; b=nMWC2KsPbc4o5Nq5ziq/rTy5Wt9qOhCM6JJaTpsweiO1vxrHPV2mfmBmprD7NDcvt3 GFsDOuDtsd6aQo/7pTXywu4nt4WBUEuaiEtNUVU9juXVXXCwGySk/YurAWPFf7a46Og+ 2K1v4GpA2TJ8Ob/3Hiw5qCzwnTAHmH6ejbumzB80iaQRY+tiOUqug4PV27ZRqVZn9ESO 0zsvZZ+CGIIDfFuyPkGqx74NgTI6atelyn7e99dzrPnW/MDSQcfxA+uPyFmkYmbGOhfw +QNgIGYc1r6yihn/tTTO8XCL+Au7L3SF0xbsoQdKBQt0XAE9yLVWFtsy+eIgU4xYM+Uz wQSQ== 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 :references:in-reply-to:message-id:subject:reply-to:cc:from:to :dkim-signature:date; bh=5rNl+kAanqy5Ow6PHG0Nk/iTcHfaHO7mTi0wPOmmCc0=; b=zfeQKBvplxpAd1e2kJrdStf2i0ITDph5fW5YtvDaGfrLTzIc8FOP3EbFa3GsrYPWGB dWQR9t8BqH+py/fGEJ32TkYME9cTS7IDLd9FopbCOGyTEHYT7BJUlQYQeaPOqjMvL/cG AOWqQa4rs8jSsK1jQE7UVJJS36xuOCqWV9BO0HD6BhDRrmeLPhOsTVlUzeSkMkUzVA71 K8UPFO4FL5l1th73BnozuZ3Pfl7Kwe7p28NvovGsJQi8WSBxKKucYJmDOZPHTTQte8Vq KEYQ3TFLfcpkKclCkk0g/pD1/kTKf6ndjFp9fquxu1BEAkGR7PQT1ZwPCZUJEIPeAG8e 3ZJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@pm.me header.s=protonmail header.b="WyD5ez/5"; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=pm.me Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y13si7334772edc.322.2021.05.08.09.32.47; Sat, 08 May 2021 09:33:11 -0700 (PDT) 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=@pm.me header.s=protonmail header.b="WyD5ez/5"; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=pm.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229751AbhEHQa4 (ORCPT + 99 others); Sat, 8 May 2021 12:30:56 -0400 Received: from mail-40136.protonmail.ch ([185.70.40.136]:13578 "EHLO mail-40136.protonmail.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229749AbhEHQay (ORCPT ); Sat, 8 May 2021 12:30:54 -0400 Date: Sat, 08 May 2021 16:29:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail; t=1620491388; bh=5rNl+kAanqy5Ow6PHG0Nk/iTcHfaHO7mTi0wPOmmCc0=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=WyD5ez/5my30NXYcWHdga/2UTQhj3s1Mai4b6T2yCM7pXPFqSIyVDLZr44wTS1Sz6 9JnHA0T8xPqHDnBBEngpRZ9z/HlmGq5WdFMsxgIQyE6HZQi/WcWkpStY4jdMmBFZ03 QmWx1tyQKZv31lXLsZOPWM/yc3JUTdeIzzNZZ0YuWUrQPIe41RMeeFT1ZOQu5EPlQX eeNtYz6tMydBuvU/l1YDzPcaWpKbnohxfwvDNkRMAKXeILIBtFlCwdW0UFvOHYfEB5 TLfd8iAn6ysn8OTtXLzEE5aHuj0aWwGlEnLwHk+Lfn9UG2BhGfJRD4BAXwsPqHGwlQ liOPHHS4GgHVQ== To: Nathan Chancellor From: Alexander Lobakin Cc: clang-built-linux@googlegroups.com, linux-mips@vger.kernel.org, Thomas Bogendoerfer , Kees Cook , Nathan Chancellor , Nick Desaulniers , Fangrui Song , Sami Tolvanen , Ralf Baechle , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Reply-To: Alexander Lobakin Subject: Re: [BUG mips llvm] MIPS: malformed R_MIPS_{HI16,LO16} with LLVM Message-ID: In-Reply-To: References: <20210109171058.497636-1-alobakin@pm.me> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.2 required=10.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on mailout.protonmail.ch Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nathan Chancellor Date: Fri, 7 May 2021 00:47:14 -0700 > On Sat, Jan 09, 2021 at 05:11:18PM +0000, Alexander Lobakin wrote: > > Machine: MIPS32 R2 Big Endian (interAptiv (multi)) > > > > While testing MIPS with LLVM, I found a weird and very rare bug with > > MIPS relocs that LLVM emits into kernel modules. It happens on both > > 11.0.0 and latest git snapshot and applies, as I can see, only to > > references to static symbols. > > > > When the kernel loads the module, it allocates a space for every > > section and then manually apply the relocations relative to the > > new address. > > > > Let's say we have a function phy_probe() in drivers/net/phy/libphy.ko. > > It's static and referenced only in phy_register_driver(), where it's > > used to fill callback pointer in a structure. > > > > The real function address after module loading is 0xc06c1444, that > > is observed in its ELF st_value field. > > There are two relocs related to this usage in phy_register_driver(): > > > > R_MIPS_HI16 refers to 0x3c010000 > > R_MIPS_LO16 refers to 0x24339444 > > > > The address of .text is 0xc06b8000. So the destination is calculated > > as follows: > > > > 0x00000000 from hi16; > > 0xffff9444 from lo16 (sign extend as it's always treated as signed); > > 0xc06b8000 from base. > > > > =3D3D 0xc06b1444. The value is lower than the real phy_probe() address > > (0xc06c1444) by 0x10000 and is lower than the base address of > > module's .text, so it's 100% incorrect. > > > > This results in: > > > > [ 2.204022] CPU 3 Unable to handle kernel paging request at virtual > > address c06b1444, epc =3D3D=3D3D c06b1444, ra =3D3D=3D3D 803f1090 > > > > The correct instructions should be: > > > > R_MIPS_HI16 0x3c010001 > > R_MIPS_LO16 0x24339444 > > > > so there'll be 0x00010000 from hi16. > > > > I tried to catch those bugs in arch/mips/kernel/module.c (by checking > > if the destination is lower than the base address, which should never > > happen), and seems like I have only 3 such places in libphy.ko (and > > one in nf_tables.ko). > > I don't think it should be handled somehow in mentioned source code > > as it would look rather ugly and may break kernels build with GNU > > stack, which seems to not produce such bad codes. > > > > If I should report this to any other resources, please let me know. > > I chose clang-built-linux and LKML as it may not happen with userland > > (didn't tried to catch). > > > > Thanks, > > Al > > > > Hi Alexander, Hi! > Doubling back around to this as I was browsing through the LLVM 12.0.1 > blockers on LLVM's bug tracker and I noticed a commit that could resolve > this? It refers to the same relocations that you reference here. > > https://bugs.llvm.org/show_bug.cgi?id=3D3D49821 > > http://github.com/llvm/llvm-project/commit/7e83a7f1fdfcc2edde61f0a535f9d7= a=3D > 56f531db9 This really seems very related to the bug I encountered. Currently I don't have a MIPS setup to try since I've moved to another country, but I should "deploy" it again soon. So I'll definitely take a look a bit later, thanks for pointing on this commit! > I think that Debian's apt.llvm.org repository should have a build > available with that commit in it. Otherwise, building it from source is > not too complicated with my script: > > https://github.com/ClangBuiltLinux/tc-build > > $ ./build-llvm.py --build-stage1-only --install-stage1-only --projects "c= l=3D > ang;lld" --targets "Mips;X86" > > would get you a working toolchain relatively quickly. I could just build llvm-git from Arch Linux User Repository :) I did that last time when was checking if the latest snapshot also suffers from the bug, and I think it didn't take much time to build. > Cheers, > Nathan Thanks, Al