Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp631234ybi; Fri, 24 May 2019 08:57:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqxLdkMDjorgOhZ2BOwTW6A6BHzj8qqugHP2s3Ns5F5jBHVYt1va8dP6oMhW0ltA5XTsy6hQ X-Received: by 2002:a17:902:9305:: with SMTP id bc5mr7943317plb.193.1558713440464; Fri, 24 May 2019 08:57:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558713440; cv=none; d=google.com; s=arc-20160816; b=FMYP5HuJE0DTbOL6Q/sNjpN0nqjECGXzrmCMjSepb4fK24kC7OPbPqp9lfeAizMGx9 VI0WP/eurBxDw36omGKOcF5UFuO91wxPx7NltsEt7lB5x+McywijVjDPfH1brD/b0BGC 6jeEUnHsxOiiBjjDhtxq+IOJZfh1qhble+izLu26AXBnj1Z5y+Cc1snyGDMeuktPH/AN XaEob8Y2NvQ6etuc6QUR8PmIMIg3/NJvWqCLl//LJ/1vTFkw87IOHtyiQuT7zZtiwp5a Ik7pw54b1YP77aKN/r1vnXkWOlgNnKNJc4D60Vd8ZU4Psq31ruaQy+rmULzGNRA8HNt9 ZcNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=uPv8IlVUlBLwszar8g8FSyr6oh1PreAw0BWGgT17m24=; b=HB67VfWidfGy9GHeQzQuvvj9YfF40xwY36v77bDmfE9nv/CA8Rxlp+vn7ge4aXMmG0 LNg9gukEb8aRmXgJ3LCi4lWZLajVt7yp/Eu7Bj6/3G78BcPLcauBLz6OYcKyogHh9EfJ 0LSXm+qW5zKsKvTV1akGKzT0UvqUAY27fSZ5Vh1JsPBHR2NouQoUtJnh92MSZ0VXl8zI ginmevTToAfQqVYQlTEMY61EDHA/BAQdvDgS/6sq5qe+zTYV176cmyqiK34c1Oxc7XsX xE+cZ4oEDSERZ8TeMQyebVQdFKCd/8am7EkPLDhucl1Dj/IgbkScsNdoBJW+4u3OtPFH aSeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="zN/BTNZn"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v20si4688851pgi.563.2019.05.24.08.57.04; Fri, 24 May 2019 08:57:20 -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; dkim=pass header.i=@linaro.org header.s=google header.b="zN/BTNZn"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390088AbfEXPzu (ORCPT + 99 others); Fri, 24 May 2019 11:55:50 -0400 Received: from mail-io1-f66.google.com ([209.85.166.66]:38923 "EHLO mail-io1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389385AbfEXPzu (ORCPT ); Fri, 24 May 2019 11:55:50 -0400 Received: by mail-io1-f66.google.com with SMTP id r185so3248547iod.6 for ; Fri, 24 May 2019 08:55:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=uPv8IlVUlBLwszar8g8FSyr6oh1PreAw0BWGgT17m24=; b=zN/BTNZn0LZGxpBEPIZhk1wKtEpFPiCHlRTjgucAdR18Pt7sRBKyqapFTzQyhxavY8 UI9bigXT2uxU4dzlGdUqnTLLuRHADTEv4IoyQ7/Ek/WCrpae8idVHwyoMjHmlZjpV7Tr IPs4ocWr8NpRtBoH/MiVU6Unhc7niQvJLstnUwcT1L2llRU6lc/z+UY7XydKUmc8yfDU xkXzyzd4CSsL83TThPC29ntfrRnbR4P9DG2ugI8LxnBzULCzYMWMskIiP8AzjoITY8JD EDeG+3UWY6QoSErXUeqBr1tlUf1UTppFw8ei2Bp/rbx1c6DsAiAz5h8OASbGc0vpOqAZ V1cA== 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=uPv8IlVUlBLwszar8g8FSyr6oh1PreAw0BWGgT17m24=; b=oDazG1sYx408a+PH3YRgvKneivb7xbyrXzwCB2FMBobf1dMgmjo43hzWuQYZBCdI8a n/uFvEJz4//EjENiO77NFMRBh9bHawmbvw0d2wpSbAv38rGrY5IsY+WGPqKcaExVPyvH ixpmptuJZZMsdeaXGaHTYPkwWQBsxsYwQTAXqcWivuzERERxhuTHynbVhplaRyjh4ZGo hqF/C6vrAN0eERmVuFERvMWdQpR6hQvEm2PAiG/bLH6tdr7VQDANHRPPpNN1Ki89BybF MVQr78CYSdJaBHz7+9KY+NIjyfKS+bzXxhii6lsiYUbOJnI1VhjQWWPfQ4WzEj/wKFcc 9L/Q== X-Gm-Message-State: APjAAAXid2sw5zAAlNT5y97IyhIS6ou9E630oK1qdMwiDUHpHpygbxua 626vmGJ3ERzXQGr+HibZS7vE+GvadqQ4zba8+okJMg== X-Received: by 2002:a05:6602:2109:: with SMTP id x9mr17805156iox.128.1558713348970; Fri, 24 May 2019 08:55:48 -0700 (PDT) MIME-Version: 1.0 References: <20190522150239.19314-1-ard.biesheuvel@arm.com> <293c9d0f-dc14-1413-e4b4-4299f0acfb9e@arm.com> <20190523091811.GA26646@fuggles.cambridge.arm.com> <907a9681-cd1d-3326-e3dd-5f6965497720@arm.com> <20190524152045.w3syntzp4bb5jb7u@treble> In-Reply-To: <20190524152045.w3syntzp4bb5jb7u@treble> From: Ard Biesheuvel Date: Fri, 24 May 2019 17:55:37 +0200 Message-ID: Subject: Re: [PATCH] module/ksymtab: use 64-bit relative reference for target symbol To: Josh Poimboeuf Cc: Ard Biesheuvel , Mark Rutland , linux-arch , Arnd Bergmann , guillaume.gardet@arm.com, Marc Zyngier , "the arch/x86 maintainers" , Will Deacon , Linux Kernel Mailing List , James Morse , Jessica Yu , Ingo Molnar , linux-arm-kernel 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 On Fri, 24 May 2019 at 17:21, Josh Poimboeuf wrote: > > On Thu, May 23, 2019 at 10:29:39AM +0100, Ard Biesheuvel wrote: > > > > > > On 5/23/19 10:18 AM, Will Deacon wrote: > > > On Thu, May 23, 2019 at 09:41:40AM +0100, Ard Biesheuvel wrote: > > > > > > > > > > > > On 5/22/19 5:28 PM, Ard Biesheuvel wrote: > > > > > > > > > > > > > > > On 5/22/19 4:02 PM, Ard Biesheuvel wrote: > > > > > > The following commit > > > > > > > > > > > > 7290d5809571 ("module: use relative references for __ksymtab entries") > > > > > > > > > > > > updated the ksymtab handling of some KASLR capable architectures > > > > > > so that ksymtab entries are emitted as pairs of 32-bit relative > > > > > > references. This reduces the size of the entries, but more > > > > > > importantly, it gets rid of statically assigned absolute > > > > > > addresses, which require fixing up at boot time if the kernel > > > > > > is self relocating (which takes a 24 byte RELA entry for each > > > > > > member of the ksymtab struct). > > > > > > > > > > > > Since ksymtab entries are always part of the same module as the > > > > > > symbol they export (or of the core kernel), it was assumed at the > > > > > > time that a 32-bit relative reference is always sufficient to > > > > > > capture the offset between a ksymtab entry and its target symbol. > > > > > > > > > > > > Unfortunately, this is not always true: in the case of per-CPU > > > > > > variables, a per-CPU variable's base address (which usually differs > > > > > > from the actual address of any of its per-CPU copies) could be at > > > > > > an arbitrary offset from the ksymtab entry, and so it may be out > > > > > > of range for a 32-bit relative reference. > > > > > > > > > > > > > > (Apologies for the 3-act monologue) > > > > > > Exposition, development and recapitulation ;) > > > > > > > This turns out to be incorrect. The symbol address of per-CPU variables > > > > exported by modules is always in the vicinity of __per_cpu_start, and so it > > > > is simply a matter of making sure that the core kernel is in range for > > > > module ksymtab entries containing 32-bit relative references. > > > > > > > > When running the arm64 with kaslr enabled, we currently randomize the module > > > > space based on the range of ADRP/ADD instruction pairs, which have a -/+ 4 > > > > GB range rather than the -/+ 2 GB range of 32-bit place relative data > > > > relocations. So we can fix this by simply reducing the randomization window > > > > to 2 GB. > > > > > > Makes sense. Do you see the need for an option to disable PREL relocs > > > altogether in case somebody wants the additional randomization range? > > > > > > > No, not really. To be honest, I don't think > > CONFIG_RANDOMIZE_MODULE_REGION_FULL is that useful to begin with, and the > > only reason we enabled it by default at the time was to ensure that the PLT > > code got some coverage after we introduced it. > > In code, percpu variables are accessed with absolute relocations, right? No, they are accessed just like ordinary symbols, so PC32 references on x86 or ADRP/ADD references on arm64 are both quite common. > Before I read your 3rd act, I was wondering if it would make sense to do > the same with the ksymtab relocations. > > Like if we somehow [ insert much hand waving ] ensured that everybody > uses EXPORT_PER_CPU_SYMBOL() for percpu symbols instead of just > EXPORT_SYMBOL() then we could use a different macro to create the > ksymtab relocations for percpu variables, such that they use absolute > relocations. > > Just an idea. Maybe the point is moot now. > The problem is that we already have four different ksymtab sections: normal, GPL, future GPL and unused, and adding the orthogonal per-CPU property to that would double it to 8. Since the purpose of the place relative ksymtabs applies to the core kernel only, another thing I contemplated is using a different ksymtab format between modules and the core kernel, but that is another can of worms that I'd rather not open. But it is indeed moot now ...