From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 35375C433E0 for ; Sat, 9 Jan 2021 17:51:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E81D0239FE for ; Sat, 9 Jan 2021 17:51:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726073AbhAIRvh (ORCPT ); Sat, 9 Jan 2021 12:51:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725926AbhAIRvg (ORCPT ); Sat, 9 Jan 2021 12:51:36 -0500 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38A3EC061786 for ; Sat, 9 Jan 2021 09:50:56 -0800 (PST) Received: by mail-pf1-x42f.google.com with SMTP id q20so5062756pfu.8 for ; Sat, 09 Jan 2021 09:50:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Rz+U26z6jIIN1uPsEdI8MtvxyqTX3edy55x6DrScyKg=; b=c4bK3l0yv3gmB2uDggyRYvZ392JRXxNpt6ffWyyp/6G1wJPeDMGpurfYs7It0uM9uX 0rbU6k8DigmpdRHD2ncyKGQ0y4QPAoW6EZ/ZBwLJKVdODkTf0cM/ifGELos2JR28VEPs ZbpGlwQQzLfB0UKEUQoeYKrXrLKgKUGQS+7Ftnh0Cw6WReD+962gWI4p50Sli1lShulk zDXEekr3MqZBaGgiBbxZ2pmiq33azvRRFcMJe3KhGfONRUVPrZRBaX/FjYzoQBDXXQHZ yAocCh0ukK+nqdnGi+i/NwddPD3WhvVJRi8EB5puWwgflLnc0YnBtYJGSkpAFXVwzRbE 8ehA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Rz+U26z6jIIN1uPsEdI8MtvxyqTX3edy55x6DrScyKg=; b=Yi+MaK8quvmPI8Al5kI7ugS+Zh6vFJ89cSRlUrd+IotpBu3fiWL3Yd+ojCk1DCihOi VqgS8xRRPvpkSeSANGTRKQn20ABr27IDH9MCct4L7yS/7Bjg5tv2VO5F/PwQedLveSlW U1zBxa3r3e30q1zK951Bu5tYmZFISHphgjQxfiURoCRKe70USLT2gh//eYEqrk66oFkm XvzfFllpVJizLfOQ+kgJx/MA8ivx5ucEP+4QpwsDOZFb0ewkorhty2LCNh0Ipbzc/f7g OyTQ6vzYfG7bbVufdefp010YNCBnY/zNA9bxZNgumIEva7ITKykzRAwPaVvplJUCsWnD a/WA== X-Gm-Message-State: AOAM53160BHiWCGeIPET1QG3+UvJuEgsieFKf7rW78//C4w0kSjzG2+b /LGniQiuluQyF9p92jehh9SUn/vPYPpNXEHmIRry9A== X-Google-Smtp-Source: ABdhPJyJP3tazaWOmwaeqbiNSqIF4LtHAbHFNqsY7t9yn3KE2WlrRzEN5UBeo4m5E5d/MzfFWhSdDcnkddpynrl5QIs= X-Received: by 2002:a63:1142:: with SMTP id 2mr12540451pgr.263.1610214655571; Sat, 09 Jan 2021 09:50:55 -0800 (PST) MIME-Version: 1.0 References: <20210109171058.497636-1-alobakin@pm.me> In-Reply-To: <20210109171058.497636-1-alobakin@pm.me> From: Nick Desaulniers Date: Sat, 9 Jan 2021 09:50:44 -0800 Message-ID: Subject: Re: [BUG mips llvm] MIPS: malformed R_MIPS_{HI16,LO16} with LLVM To: Alexander Lobakin Cc: clang-built-linux , linux-mips@vger.kernel.org, Thomas Bogendoerfer , Kees Cook , Nathan Chancellor , Fangrui Song , Sami Tolvanen , Ralf Baechle , LKML , linux-arch Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jan 9, 2021 at 9:11 AM 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. > > = 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 == c06b1444, ra == 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 for the report. Sounds like we may indeed be producing an incorrect relocation. This is only seen for big endian triples? Getting a way for us to deterministically reproduce would be a good first step. Which config or configs beyond defconfig, and which relocations specifically are you observing this with? -- Thanks, ~Nick Desaulniers