Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp1171764pxb; Sat, 9 Jan 2021 09:52:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJzDMsimFFJnNsAgJSzs1oj36O6GNtrxVtZlZIdJoqcFyXL9RY/DZYmaxRzcvHz1QoBI9Q6f X-Received: by 2002:a17:906:ceca:: with SMTP id si10mr5962693ejb.547.1610214770571; Sat, 09 Jan 2021 09:52:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610214770; cv=none; d=google.com; s=arc-20160816; b=zs398XlnAjA1e0QzelRUKYD6kubz+MWdhvkoy+Isv/Hgq4WuwjzSXFEk1zpUjzrh+Q WFTBrRIGCVGdp5TolPErWnaKSZHo5V/Z7oTig0sjTq1teGjI6L2f/bCjXJi1RcMYZV6k M7uEJfs/De54uQdXiNeVGXuiyZ+s3mUoQYtXmLqrSNdkqyL2LQ0M12/Kl2vFEdGFD9q4 Aoh2WZRpLea2rOpGnCMHIWrzp1twrZOEpO9sLqAjnTrSmpFcz2lE5rt+BlF5YIy1R7/C gBF8dQJ/Ab2fbFePowCrhnGztvgBbtkmhCoHaEptm56I5F6GUDhliSk/aG+kOOGutC4j 0IQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Rz+U26z6jIIN1uPsEdI8MtvxyqTX3edy55x6DrScyKg=; b=U+ZOT+4pnhtQTWUtv6oJNvzVqTQyVA11HtnQ4nw1O23h+3tztbkgLh7Ui4yMbyc9dP ZwLazrq/7LTvdqmZLuGyrg/HNdrxpmf8vNe8nXq75hIxZfLl2nqsT93qpK7ODCenPnbc n0iB6qCniBQn8Oo+Qr6INn3tqkNeyb0TnvyySlC5ZyOiq+foKLR6KkKg1sXOwhm4aNYN ZeQxHPDQ6tshHvoqNflHPw77oQ5/TPAUHjMcwjsg2qrdEDgAAtvAqk6IqE27hdFRtnb6 NytTotwqVoJD0g7aP9GcjfoEOA9jPDOWmFU/03302ZLXmjySOlKPOeaROhLmxM4S3Byb 0seQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=c4bK3l0y; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q4si5170066edn.338.2021.01.09.09.52.26; Sat, 09 Jan 2021 09:52:50 -0800 (PST) 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=@google.com header.s=20161025 header.b=c4bK3l0y; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726013AbhAIRvh (ORCPT + 99 others); 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-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