From owner-svn-src-all@freebsd.org Fri May 8 14:48:48 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id E91652D933C; Fri, 8 May 2020 14:48:48 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 49JY9r5r3nz3NvM; Fri, 8 May 2020 14:48:48 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id BEE824035; Fri, 8 May 2020 14:48:48 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 048EmmrU062627; Fri, 8 May 2020 14:48:48 GMT (envelope-from jhb@FreeBSD.org) Received: (from jhb@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 048Emjdc062610; Fri, 8 May 2020 14:48:45 GMT (envelope-from jhb@FreeBSD.org) Message-Id: <202005081448.048Emjdc062610@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jhb set sender to jhb@FreeBSD.org using -f From: John Baldwin Date: Fri, 8 May 2020 14:48:45 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r360814 - in stable/12/sys: conf kern X-SVN-Group: stable-12 X-SVN-Commit-Author: jhb X-SVN-Commit-Paths: in stable/12/sys: conf kern X-SVN-Commit-Revision: 360814 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.32 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 May 2020 14:48:49 -0000 Author: jhb Date: Fri May 8 14:48:45 2020 New Revision: 360814 URL: https://svnweb.freebsd.org/changeset/base/360814 Log: MFC 348611: Expose the kernel's build-ID through sysctl After our migration (of certain architectures) to lld the kernel is built with a unique build-ID. Make it available via a sysctl and uname(1) to allow the user to identify their running kernel. Modified: stable/12/sys/conf/ldscript.amd64 stable/12/sys/conf/ldscript.arm stable/12/sys/conf/ldscript.arm64 stable/12/sys/conf/ldscript.i386 stable/12/sys/conf/ldscript.mips stable/12/sys/conf/ldscript.mips.cfe stable/12/sys/conf/ldscript.mips.mips64 stable/12/sys/conf/ldscript.mips.octeon1 stable/12/sys/conf/ldscript.powerpc stable/12/sys/conf/ldscript.powerpc64 stable/12/sys/conf/ldscript.powerpcspe stable/12/sys/conf/ldscript.riscv stable/12/sys/conf/ldscript.sparc64 stable/12/sys/kern/kern_mib.c Directory Properties: stable/12/ (props changed) Modified: stable/12/sys/conf/ldscript.amd64 ============================================================================== --- stable/12/sys/conf/ldscript.amd64 Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.amd64 Fri May 8 14:48:45 2020 (r360814) @@ -74,6 +74,11 @@ SECTIONS PROVIDE (etext = .); .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } .rodata1 : { *(.rodata1) } + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .eh_frame_hdr : { *(.eh_frame_hdr) } .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) } .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } Modified: stable/12/sys/conf/ldscript.arm ============================================================================== --- stable/12/sys/conf/ldscript.arm Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.arm Fri May 8 14:48:45 2020 (r360814) @@ -27,6 +27,11 @@ SECTIONS .gnu.version : { *(.gnu.version) } .gnu.version_d : { *(.gnu.version_d) } .gnu.version_r : { *(.gnu.version_r) } + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .rel.text : { *(.rel.text) *(.rel.gnu.linkonce.t*) } .rela.text : Modified: stable/12/sys/conf/ldscript.arm64 ============================================================================== --- stable/12/sys/conf/ldscript.arm64 Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.arm64 Fri May 8 14:48:45 2020 (r360814) @@ -27,6 +27,11 @@ SECTIONS .gnu.version : { *(.gnu.version) } .gnu.version_d : { *(.gnu.version_d) } .gnu.version_r : { *(.gnu.version_r) } + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .rel.text : { *(.rel.text) *(.rel.gnu.linkonce.t*) } .rela.text : Modified: stable/12/sys/conf/ldscript.i386 ============================================================================== --- stable/12/sys/conf/ldscript.i386 Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.i386 Fri May 8 14:48:45 2020 (r360814) @@ -62,6 +62,11 @@ SECTIONS PROVIDE (etext = .); .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } .rodata1 : { *(.rodata1) } + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .eh_frame_hdr : { *(.eh_frame_hdr) } .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) } .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } Modified: stable/12/sys/conf/ldscript.mips ============================================================================== --- stable/12/sys/conf/ldscript.mips Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.mips Fri May 8 14:48:45 2020 (r360814) @@ -63,6 +63,11 @@ SECTIONS .gnu.version : { *(.gnu.version) } .gnu.version_d : { *(.gnu.version_d) } .gnu.version_r : { *(.gnu.version_r) } + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .rel.init : { *(.rel.init) } .rela.init : { *(.rela.init) } .rel.text : Modified: stable/12/sys/conf/ldscript.mips.cfe ============================================================================== --- stable/12/sys/conf/ldscript.mips.cfe Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.mips.cfe Fri May 8 14:48:45 2020 (r360814) @@ -192,6 +192,11 @@ SECTIONS PROVIDE (etext = .); .rodata : { *(.rodata) *(.rodata.*) *(.gnu.linkonce.r.*) } .rodata1 : { *(.rodata1) } + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .reginfo : { *(.reginfo) } .sdata2 : { *(.sdata2) *(.sdata2.*) *(.gnu.linkonce.s2.*) } .sbss2 : { *(.sbss2) *(.sbss2.*) *(.gnu.linkonce.sb2.*) } Modified: stable/12/sys/conf/ldscript.mips.mips64 ============================================================================== --- stable/12/sys/conf/ldscript.mips.mips64 Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.mips.mips64 Fri May 8 14:48:45 2020 (r360814) @@ -176,6 +176,11 @@ SECTIONS PROVIDE (etext = .); .rodata : { *(.rodata) *(.rodata.*) *(.gnu.linkonce.r.*) } .rodata1 : { *(.rodata1) } + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .reginfo : { *(.reginfo) } .sdata2 : { *(.sdata2) *(.sdata2.*) *(.gnu.linkonce.s2.*) } .sbss2 : { *(.sbss2) *(.sbss2.*) *(.gnu.linkonce.sb2.*) } Modified: stable/12/sys/conf/ldscript.mips.octeon1 ============================================================================== --- stable/12/sys/conf/ldscript.mips.octeon1 Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.mips.octeon1 Fri May 8 14:48:45 2020 (r360814) @@ -23,6 +23,12 @@ SECTIONS { *(.rodata) . = ALIGN(32); } + + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .data : { _rwdata = .; Modified: stable/12/sys/conf/ldscript.powerpc ============================================================================== --- stable/12/sys/conf/ldscript.powerpc Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.powerpc Fri May 8 14:48:45 2020 (r360814) @@ -55,6 +55,11 @@ SECTIONS .fini : { *(.fini) } =0 .rodata : { *(.rodata) *(.gnu.linkonce.r*) } .rodata1 : { *(.rodata1) } + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .sdata2 : { *(.sdata2) } .sbss2 : { *(.sbss2) } /* Adjust the address for the data segment to the next page up. */ Modified: stable/12/sys/conf/ldscript.powerpc64 ============================================================================== --- stable/12/sys/conf/ldscript.powerpc64 Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.powerpc64 Fri May 8 14:48:45 2020 (r360814) @@ -41,6 +41,11 @@ SECTIONS .gnu.version : { *(.gnu.version) } .gnu.version_d : { *(.gnu.version_d) } .gnu.version_r : { *(.gnu.version_r) } + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .rela.text : { *(.rela.text) *(.rela.gnu.linkonce.t*) } .rela.data : Modified: stable/12/sys/conf/ldscript.powerpcspe ============================================================================== --- stable/12/sys/conf/ldscript.powerpcspe Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.powerpcspe Fri May 8 14:48:45 2020 (r360814) @@ -55,6 +55,11 @@ SECTIONS .fini : { *(.fini) } =0 .rodata : { *(.rodata) *(.gnu.linkonce.r*) } .rodata1 : { *(.rodata1) } + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .sdata2 : { *(.sdata2) } .sbss2 : { *(.sbss2) } /* Adjust the address for the data segment to the next page up. */ Modified: stable/12/sys/conf/ldscript.riscv ============================================================================== --- stable/12/sys/conf/ldscript.riscv Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.riscv Fri May 8 14:48:45 2020 (r360814) @@ -28,6 +28,11 @@ SECTIONS .gnu.version : { *(.gnu.version) } .gnu.version_d : { *(.gnu.version_d) } .gnu.version_r : { *(.gnu.version_r) } + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .rel.text : { *(.rel.text) *(.rel.gnu.linkonce.t*) } .rela.text : Modified: stable/12/sys/conf/ldscript.sparc64 ============================================================================== --- stable/12/sys/conf/ldscript.sparc64 Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/conf/ldscript.sparc64 Fri May 8 14:48:45 2020 (r360814) @@ -146,6 +146,11 @@ SECTIONS PROVIDE (etext = .); .rodata : { *(.rodata) *(.rodata.*) *(.gnu.linkonce.r.*) } .rodata1 : { *(.rodata1) } + .note.gnu.build-id : { + PROVIDE (__build_id_start = .); + *(.note.gnu.build-id) + PROVIDE (__build_id_end = .); + } .sdata2 : { *(.sdata2) *(.sdata2.*) *(.gnu.linkonce.s2.*) } .sbss2 : { *(.sbss2) *(.sbss2.*) *(.gnu.linkonce.sb2.*) } . = ALIGN(0x2000) + (. & (0x2000 - 1)); Modified: stable/12/sys/kern/kern_mib.c ============================================================================== --- stable/12/sys/kern/kern_mib.c Fri May 8 14:39:38 2020 (r360813) +++ stable/12/sys/kern/kern_mib.c Fri May 8 14:48:45 2020 (r360814) @@ -491,6 +491,54 @@ SYSCTL_PROC(_kern, KERN_OSRELDATE, osreldate, CTLTYPE_INT | CTLFLAG_CAPRD | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, sysctl_osreldate, "I", "Kernel release date"); +/* + * The build-id is copied from the ELF section .note.gnu.build-id. The linker + * script defines two variables to expose the beginning and end. LLVM + * currently uses a SHA-1 hash, but other formats can be supported by checking + * the length of the section. + */ + +extern char __build_id_start[]; +extern char __build_id_end[]; + +#define BUILD_ID_HEADER_LEN 0x10 +#define BUILD_ID_HASH_MAXLEN 0x14 + +static int +sysctl_build_id(SYSCTL_HANDLER_ARGS) +{ + uintptr_t sectionlen = (uintptr_t)(__build_id_end - __build_id_start); + int hashlen; + char buf[2*BUILD_ID_HASH_MAXLEN+1]; + + /* + * The ELF note section has a four byte length for the vendor name, + * four byte length for the value, and a four byte vendor specific + * type. The name for the build id is "GNU\0". We skip the first 16 + * bytes to read the build hash. We will return the remaining bytes up + * to 20 (SHA-1) hash size. If the hash happens to be a custom number + * of bytes we will pad the value with zeros, as the section should be + * four byte aligned. + */ + if (sectionlen <= BUILD_ID_HEADER_LEN || + sectionlen > (BUILD_ID_HEADER_LEN + BUILD_ID_HASH_MAXLEN)) { + return (ENOENT); + } + + + hashlen = sectionlen - BUILD_ID_HEADER_LEN; + for (int i = 0; i < hashlen; i++) { + uint8_t c = __build_id_start[i+BUILD_ID_HEADER_LEN]; + snprintf(&buf[2*i], 3, "%02x", c); + } + + return (SYSCTL_OUT(req, buf, strlen(buf) + 1)); +} + +SYSCTL_PROC(_kern, OID_AUTO, build_id, + CTLTYPE_STRING | CTLFLAG_CAPRD | CTLFLAG_RD | CTLFLAG_MPSAFE, + NULL, 0, sysctl_build_id, "A", "Operating system build-id"); + SYSCTL_NODE(_kern, OID_AUTO, features, CTLFLAG_RD, 0, "Kernel Features"); #ifdef COMPAT_FREEBSD4