^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) ===========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) Livepatch module Elf format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) ===========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) This document outlines the Elf format requirements that livepatch modules must follow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) .. Table of Contents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) 1. Background and motivation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) 2. Livepatch modinfo field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) 3. Livepatch relocation sections
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) 3.1 Livepatch relocation section format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) 4. Livepatch symbols
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) 4.1 A livepatch module's symbol table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) 4.2 Livepatch symbol format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) 5. Symbol table and Elf section access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) 1. Background and motivation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) ============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) Formerly, livepatch required separate architecture-specific code to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) relocations. However, arch-specific code to write relocations already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) exists in the module loader, so this former approach produced redundant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) code. So, instead of duplicating code and re-implementing what the module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) loader can already do, livepatch leverages existing code in the module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) loader to perform the all the arch-specific relocation work. Specifically,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) livepatch reuses the apply_relocate_add() function in the module loader to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) write relocations. The patch module Elf format described in this document
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) enables livepatch to be able to do this. The hope is that this will make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) livepatch more easily portable to other architectures and reduce the amount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) of arch-specific code required to port livepatch to a particular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) architecture.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) Since apply_relocate_add() requires access to a module's section header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) table, symbol table, and relocation section indices, Elf information is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) preserved for livepatch modules (see section 5). Livepatch manages its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) relocation sections and symbols, which are described in this document. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) Elf constants used to mark livepatch symbols and relocation sections were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) selected from OS-specific ranges according to the definitions from glibc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) Why does livepatch need to write its own relocations?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) -----------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) A typical livepatch module contains patched versions of functions that can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) reference non-exported global symbols and non-included local symbols.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) Relocations referencing these types of symbols cannot be left in as-is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) since the kernel module loader cannot resolve them and will therefore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) reject the livepatch module. Furthermore, we cannot apply relocations that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) affect modules not yet loaded at patch module load time (e.g. a patch to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) driver that is not loaded). Formerly, livepatch solved this problem by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) embedding special "dynrela" (dynamic rela) sections in the resulting patch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) module Elf output. Using these dynrela sections, livepatch could resolve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) symbols while taking into account its scope and what module the symbol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) belongs to, and then manually apply the dynamic relocations. However this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) approach required livepatch to supply arch-specific code in order to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) these relocations. In the new format, livepatch manages its own SHT_RELA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) relocation sections in place of dynrela sections, and the symbols that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) relas reference are special livepatch symbols (see section 2 and 3). The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) arch-specific livepatch relocation code is replaced by a call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) apply_relocate_add().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) 2. Livepatch modinfo field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ==========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) Livepatch modules are required to have the "livepatch" modinfo attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) See the sample livepatch module in samples/livepatch/ for how this is done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) Livepatch modules can be identified by users by using the 'modinfo' command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) and looking for the presence of the "livepatch" field. This field is also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) used by the kernel module loader to identify livepatch modules.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) Example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) --------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) **Modinfo output:**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) % modinfo livepatch-meminfo.ko
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) filename: livepatch-meminfo.ko
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) livepatch: Y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) license: GPL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) depends:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) vermagic: 4.3.0+ SMP mod_unload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) 3. Livepatch relocation sections
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) ================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) A livepatch module manages its own Elf relocation sections to apply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) relocations to modules as well as to the kernel (vmlinux) at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) appropriate time. For example, if a patch module patches a driver that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) not currently loaded, livepatch will apply the corresponding livepatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) relocation section(s) to the driver once it loads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) Each "object" (e.g. vmlinux, or a module) within a patch module may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) multiple livepatch relocation sections associated with it (e.g. patches to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) multiple functions within the same object). There is a 1-1 correspondence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) between a livepatch relocation section and the target section (usually the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) text section of a function) to which the relocation(s) apply. It is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) also possible for a livepatch module to have no livepatch relocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) sections, as in the case of the sample livepatch module (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) samples/livepatch).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) Since Elf information is preserved for livepatch modules (see Section 5), a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) livepatch relocation section can be applied simply by passing in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) appropriate section index to apply_relocate_add(), which then uses it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) access the relocation section and apply the relocations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) Every symbol referenced by a rela in a livepatch relocation section is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) livepatch symbol. These must be resolved before livepatch can call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) apply_relocate_add(). See Section 3 for more information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 3.1 Livepatch relocation section format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) =======================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) Livepatch relocation sections must be marked with the SHF_RELA_LIVEPATCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) section flag. See include/uapi/linux/elf.h for the definition. The module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) loader recognizes this flag and will avoid applying those relocation sections
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) at patch module load time. These sections must also be marked with SHF_ALLOC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) so that the module loader doesn't discard them on module load (i.e. they will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) be copied into memory along with the other SHF_ALLOC sections).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) The name of a livepatch relocation section must conform to the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) format::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) .klp.rela.objname.section_name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ^ ^^ ^ ^ ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) |________||_____| |__________|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) [A] [B] [C]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) [A]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) The relocation section name is prefixed with the string ".klp.rela."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) [B]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) The name of the object (i.e. "vmlinux" or name of module) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) which the relocation section belongs follows immediately after the prefix.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) [C]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) The actual name of the section to which this relocation section applies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) Examples:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ---------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) **Livepatch relocation section names:**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .klp.rela.ext4.text.ext4_attr_store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) .klp.rela.vmlinux.text.cmdline_proc_show
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) **`readelf --sections` output for a patch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) module that patches vmlinux and modules 9p, btrfs, ext4:**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) Section Headers:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) [Nr] Name Type Address Off Size ES Flg Lk Inf Al
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) [ snip ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) [29] .klp.rela.9p.text.caches.show RELA 0000000000000000 002d58 0000c0 18 AIo 64 9 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) [30] .klp.rela.btrfs.text.btrfs.feature.attr.show RELA 0000000000000000 002e18 000060 18 AIo 64 11 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) [ snip ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) [34] .klp.rela.ext4.text.ext4.attr.store RELA 0000000000000000 002fd8 0000d8 18 AIo 64 13 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) [35] .klp.rela.ext4.text.ext4.attr.show RELA 0000000000000000 0030b0 000150 18 AIo 64 15 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) [36] .klp.rela.vmlinux.text.cmdline.proc.show RELA 0000000000000000 003200 000018 18 AIo 64 17 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) [37] .klp.rela.vmlinux.text.meminfo.proc.show RELA 0000000000000000 003218 0000f0 18 AIo 64 19 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) [ snip ] ^ ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) [*] [*]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) [*]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) Livepatch relocation sections are SHT_RELA sections but with a few special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) characteristics. Notice that they are marked SHF_ALLOC ("A") so that they will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) not be discarded when the module is loaded into memory, as well as with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) SHF_RELA_LIVEPATCH flag ("o" - for OS-specific).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) **`readelf --relocs` output for a patch module:**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) Relocation section '.klp.rela.btrfs.text.btrfs_feature_attr_show' at offset 0x2ba0 contains 4 entries:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) Offset Info Type Symbol's Value Symbol's Name + Addend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 000000000000001f 0000005e00000002 R_X86_64_PC32 0000000000000000 .klp.sym.vmlinux.printk,0 - 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 0000000000000028 0000003d0000000b R_X86_64_32S 0000000000000000 .klp.sym.btrfs.btrfs_ktype,0 + 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 0000000000000036 0000003b00000002 R_X86_64_PC32 0000000000000000 .klp.sym.btrfs.can_modify_feature.isra.3,0 - 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 000000000000004c 0000004900000002 R_X86_64_PC32 0000000000000000 .klp.sym.vmlinux.snprintf,0 - 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) [ snip ] ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) [*]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) [*]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) Every symbol referenced by a relocation is a livepatch symbol.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 4. Livepatch symbols
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ====================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) Livepatch symbols are symbols referred to by livepatch relocation sections.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) These are symbols accessed from new versions of functions for patched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) objects, whose addresses cannot be resolved by the module loader (because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) they are local or unexported global syms). Since the module loader only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) resolves exported syms, and not every symbol referenced by the new patched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) functions is exported, livepatch symbols were introduced. They are used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) also in cases where we cannot immediately know the address of a symbol when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) a patch module loads. For example, this is the case when livepatch patches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) a module that is not loaded yet. In this case, the relevant livepatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) symbols are resolved simply when the target module loads. In any case, for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) any livepatch relocation section, all livepatch symbols referenced by that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) section must be resolved before livepatch can call apply_relocate_add() for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) that reloc section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) Livepatch symbols must be marked with SHN_LIVEPATCH so that the module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) loader can identify and ignore them. Livepatch modules keep these symbols
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) in their symbol tables, and the symbol table is made accessible through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) module->symtab.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 4.1 A livepatch module's symbol table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) =====================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) Normally, a stripped down copy of a module's symbol table (containing only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) "core" symbols) is made available through module->symtab (See layout_symtab()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) in kernel/module.c). For livepatch modules, the symbol table copied into memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) on module load must be exactly the same as the symbol table produced when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) patch module was compiled. This is because the relocations in each livepatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) relocation section refer to their respective symbols with their symbol indices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) and the original symbol indices (and thus the symtab ordering) must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) preserved in order for apply_relocate_add() to find the right symbol.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) For example, take this particular rela from a livepatch module:::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) Relocation section '.klp.rela.btrfs.text.btrfs_feature_attr_show' at offset 0x2ba0 contains 4 entries:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) Offset Info Type Symbol's Value Symbol's Name + Addend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 000000000000001f 0000005e00000002 R_X86_64_PC32 0000000000000000 .klp.sym.vmlinux.printk,0 - 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) This rela refers to the symbol '.klp.sym.vmlinux.printk,0', and the symbol index is encoded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) in 'Info'. Here its symbol index is 0x5e, which is 94 in decimal, which refers to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) symbol index 94.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) And in this patch module's corresponding symbol table, symbol index 94 refers to that very symbol:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) [ snip ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 94: 0000000000000000 0 NOTYPE GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.printk,0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) [ snip ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 4.2 Livepatch symbol format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) ===========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) Livepatch symbols must have their section index marked as SHN_LIVEPATCH, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) that the module loader can identify them and not attempt to resolve them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) See include/uapi/linux/elf.h for the actual definitions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) Livepatch symbol names must conform to the following format::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) .klp.sym.objname.symbol_name,sympos
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) ^ ^^ ^ ^ ^ ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) |_______||_____| |_________| |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) [A] [B] [C] [D]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) [A]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) The symbol name is prefixed with the string ".klp.sym."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) [B]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) The name of the object (i.e. "vmlinux" or name of module) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) which the symbol belongs follows immediately after the prefix.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) [C]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) The actual name of the symbol.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) [D]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) The position of the symbol in the object (as according to kallsyms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) This is used to differentiate duplicate symbols within the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) object. The symbol position is expressed numerically (0, 1, 2...).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) The symbol position of a unique symbol is 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) Examples:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ---------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) **Livepatch symbol names:**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) .klp.sym.vmlinux.snprintf,0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) .klp.sym.vmlinux.printk,0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) .klp.sym.btrfs.btrfs_ktype,0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) **`readelf --symbols` output for a patch module:**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) Symbol table '.symtab' contains 127 entries:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) Num: Value Size Type Bind Vis Ndx Name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) [ snip ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 73: 0000000000000000 0 NOTYPE GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.snprintf,0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 74: 0000000000000000 0 NOTYPE GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.capable,0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 75: 0000000000000000 0 NOTYPE GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.find_next_bit,0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 76: 0000000000000000 0 NOTYPE GLOBAL DEFAULT OS [0xff20] .klp.sym.vmlinux.si_swapinfo,0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) [ snip ] ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) [*]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) [*]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) Note that the 'Ndx' (Section index) for these symbols is SHN_LIVEPATCH (0xff20).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) "OS" means OS-specific.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 5. Symbol table and Elf section access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) ======================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) A livepatch module's symbol table is accessible through module->symtab.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) Since apply_relocate_add() requires access to a module's section headers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) symbol table, and relocation section indices, Elf information is preserved for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) livepatch modules and is made accessible by the module loader through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) module->klp_info, which is a klp_modinfo struct. When a livepatch module loads,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) this struct is filled in by the module loader. Its fields are documented below::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) struct klp_modinfo {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) Elf_Ehdr hdr; /* Elf header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) Elf_Shdr *sechdrs; /* Section header table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) char *secstrings; /* String table for the section headers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) unsigned int symndx; /* The symbol table section index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) };