Page MenuHomeFreeBSD

Expose the buildid hash through sysctl
ClosedPublic

Authored by ali_mashtizadeh.com on May 20 2019, 8:57 PM.

Details

Summary

Exposes the buildid hash through sysctl and adds uname -b to display the buildid

Test Plan

check output using uname and sysctl

Diff Detail

Repository
rS FreeBSD src repository
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

emaste added a subscriber: markj.May 20 2019, 9:00 PM
ali_mashtizadeh.com edited the summary of this revision. (Show Details)May 20 2019, 9:02 PM
imp accepted this revision.May 20 2019, 9:04 PM
This revision is now accepted and ready to land.May 20 2019, 9:04 PM
seanc added a subscriber: seanc.May 20 2019, 9:05 PM
rpokala added inline comments.
sys/kern/kern_mib.c
501 ↗(On Diff #57605)

Please explain the magic number. Or, better still, #define it and put the explanation there.

jhb added a subscriber: jhb.May 20 2019, 9:07 PM

Just to be clear, this doesn't compile yet on !amd64? Also, does uname -a still work for a kernel built without build-id?

sys/kern/kern_mib.c
491 ↗(On Diff #57605)

Does this link if you don't have build-id support in the linker?

will also want a uname.3 update

sys/kern/kern_mib.c
519 ↗(On Diff #57605)

let's make this kern.build_id, buildid parses as "buil did" for me

521 ↗(On Diff #57605)

build-id

ali_mashtizadeh.com marked 2 inline comments as done.May 20 2019, 9:19 PM
ali_mashtizadeh.com updated this revision to Diff 57608.
  • Updates and linker scripts for other archs
This revision now requires review to proceed.May 20 2019, 9:19 PM
emaste added inline comments.May 20 2019, 9:28 PM
sys/kern/kern_mib.c
501 ↗(On Diff #57605)

trying strange build-id sizes - % cc -Wl,--build-id=0x1234 hello.c

file does not report the build-id at all:

% file a.out
a.out: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), dynamically linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 12.0 (1200085), FreeBSD-style, with debug_info, not stripped

but readelf shows that it's there:

% readelf -n a.out

Notes at offset 0x000002c0 with length 0x00000048:
  Owner         Data size       Description
  FreeBSD       0x00000004      NT_FREEBSD_ABI_TAG
  FreeBSD       0x00000004      NT_FREEBSD_FEATURE_CTL
  FreeBSD       0x00000004      NT_FREEBSD_NOINIT_TAG

Notes at offset 0x00000310 with length 0x00000012:
  Owner         Data size       Description
  GNU           0x00000002      NT_GNU_BUILD_ID (Build id set by ld(1))

my lld defaults to 8-byte xxHash:

% cc -Wl,--build-id hello.c      
% file a.out               
a.out: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), dynamically linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 12.0 (1200085), FreeBSD-style, BuildID[xxHash]=7bda57f23f3db219, with debug_info, not stripped

I think we can assume it will be one of 8, 16, or 20 bytes long though.

ali_mashtizadeh.com marked 2 inline comments as done.May 20 2019, 9:32 PM
ali_mashtizadeh.com added inline comments.
sys/kern/kern_mib.c
491 ↗(On Diff #57605)

In the updated diff I fix all linker scripts. This will show up as an empty section.

  • Addresses Ed Maste's feedback. This change allows the sysctl to return any build-id between 1 and 20 bytes.

Sample output:
root@skylake8:~ # uname -a
FreeBSD skylake8.rcs.uwaterloo.ca 13.0-CURRENT FreeBSD 13.0-CURRENT #9 da147860ca7(master)-dirty: Mon May 20 17:32:20 EDT 2019 ali@strange.rcs.uwaterloo.ca:/build/ali/obj/working/freebsd-dev/amd64.amd64/sys/GENERIC amd64 42e907d740dd9560a2dfd7187eee20e41e9fbd9f
root@skylake8:~ # uname -b
42e907d740dd9560a2dfd7187eee20e41e9fbd9f

Sample output:
root@skylake8:~ # uname -a
FreeBSD skylake8.rcs.uwaterloo.ca 13.0-CURRENT FreeBSD 13.0-CURRENT #9 da147860ca7(master)-dirty: Mon May 20 17:32:20 EDT 2019 ali@strange.rcs.uwaterloo.ca:/build/ali/obj/working/freebsd-dev/amd64.amd64/sys/GENERIC amd64 42e907d740dd9560a2dfd7187eee20e41e9fbd9f

I worry that there are tools parsing uname -a and will be confused by the extra output. Maybe we should commit the kernel changes first and ask for an exp-run (and broader user testing) on the uname changes.

  • Update man page and remove from -a
  • Adding defines
rpokala added inline comments.May 21 2019, 4:45 PM
sys/kern/kern_mib.c
494 ↗(On Diff #57632)

May I suggest that you move the comment about the header here, where the value is defined? i.e.

	/* 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.
	 */
495 ↗(On Diff #57632)

And similarly, move the length comment here as well:

	/* 20 (SHA-1) hash size */
509 ↗(On Diff #57632)

If you move the comments to the definitions as I suggest above, this becomes

	/* Return the remaining bytes up to the 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.
	 */

(with whatever appropriate wrapping, of course)

I will pick up whitespace cleanup, .Dd bump etc. for commit

usr.bin/uname/uname.1
31 ↗(On Diff #57632)

will update .Dd upon commit

57 ↗(On Diff #57632)

maybe linker-generated kernel build-id? As is seems to suggest that this is some ID of the linker itself.

emaste added a comment.Jun 4 2019, 1:07 PM

I will commit the uname changes separately

This revision was not accepted when it landed; it landed in state Needs Review.Jun 4 2019, 1:07 PM
This revision was automatically updated to reflect the committed changes.