From nobody Sat Nov 13 17:57:56 2021 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 2F4DF183DAEC for ; Sat, 13 Nov 2021 17:58:05 +0000 (UTC) (envelope-from jrtc27@jrtc27.com) Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Hs38W5fVPz3qYb for ; Sat, 13 Nov 2021 17:58:03 +0000 (UTC) (envelope-from jrtc27@jrtc27.com) Received: by mail-wr1-f44.google.com with SMTP id b12so21622798wrh.4 for ; Sat, 13 Nov 2021 09:58:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=ViBWgMWmt8IVKc/yP0Of7cCSkV1eq9vgyYUZJ5VlTns=; b=JTSyBxOEOpTxkhsR9BYX91/i75VQpQ59syvNYT8Y7vO0QPL592BipexmwEYg6IMGK3 eLmxm9Rj3DT1KmsTx1EgcJMzi0IhOWLMi0ydQwaSIRBl8w9bJG5vgONhdC+ZJaVa0OrO WVjsawrV8XSpKKK9f1nfF8n0I0L3zio1fV44PYTinoi6fal6PNz8Bfo17rCoCMU1Xe1m QoDadpxPkth5yt2Rte4ogVyAXVuNeBiEu+bHoTPaH735Zlw7quWqUeRbSgIkrgDQJ5XQ Z4aoH9NUfh56D9BBTa+QMf6Wkn+9q7m7dPMR8Vjj1NHu8I8VQnLL0T7NzbOCfa96Dg4A a7vg== X-Gm-Message-State: AOAM530lr4nzjQbDc0hVGP1Gn/LkPm0BK7A6eSoa8VAbZEpfOSVE8Yzf igKS5KI5XwAgZEgK1icjPMxG0WcfgrCMSw== X-Google-Smtp-Source: ABdhPJywXN0F8lv8rZjdeFc1ul6RKgp/PK5ErVlCZaQuE2OwBBy9SiXS3UnNRrFBds+QHDMi0H8BOQ== X-Received: by 2002:a5d:648e:: with SMTP id o14mr31105155wri.69.1636826277017; Sat, 13 Nov 2021 09:57:57 -0800 (PST) Received: from smtpclient.apple (global-5-141.nat-2.net.cam.ac.uk. [131.111.5.141]) by smtp.gmail.com with ESMTPSA id h13sm9127338wrx.82.2021.11.13.09.57.56 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 13 Nov 2021 09:57:56 -0800 (PST) Content-Type: text/plain; charset=utf-8 List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: git: 64ba1f4cf3a6 - main - rtld: Implement LD_SHOW_AUXV From: Jessica Clarke In-Reply-To: <37FC39AA-925D-4D75-8E0A-EA14E846E3A6@freebsd.org> Date: Sat, 13 Nov 2021 17:57:56 +0000 Cc: "src-committers@freebsd.org" , "dev-commits-src-all@freebsd.org" , "dev-commits-src-main@freebsd.org" Content-Transfer-Encoding: quoted-printable Message-Id: <110784F6-3A7A-4F27-AAEB-E9B5A8F7CF0E@freebsd.org> References: <202111131733.1ADHXekX049248@gitrepo.freebsd.org> <37FC39AA-925D-4D75-8E0A-EA14E846E3A6@freebsd.org> To: Konstantin Belousov X-Mailer: Apple Mail (2.3654.120.0.1.13) X-Rspamd-Queue-Id: 4Hs38W5fVPz3qYb X-Spamd-Bar: / Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of jrtc27@jrtc27.com designates 209.85.221.44 as permitted sender) smtp.mailfrom=jrtc27@jrtc27.com X-Spamd-Result: default: False [-0.50 / 15.00]; TO_DN_EQ_ADDR_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; TO_DN_SOME(0.00)[]; MV_CASE(0.50)[]; R_SPF_ALLOW(-0.20)[+ip4:209.85.128.0/17]; RCVD_COUNT_THREE(0.00)[3]; FORGED_SENDER(0.30)[jrtc27@freebsd.org,jrtc27@jrtc27.com]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:15169, ipnet:209.85.128.0/17, country:US]; MID_RHS_MATCH_FROM(0.00)[]; FROM_NEQ_ENVFROM(0.00)[jrtc27@freebsd.org,jrtc27@jrtc27.com]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; FREEFALL_USER(0.00)[jrtc27]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[4]; NEURAL_SPAM_SHORT(1.00)[1.000]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[dev-commits-src-all@freebsd.org]; DMARC_NA(0.00)[freebsd.org]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[209.85.221.44:from]; RWL_MAILSPIKE_POSSIBLE(0.00)[209.85.221.44:from]; RCVD_TLS_ALL(0.00)[] X-ThisMailContainsUnwantedMimeParts: N Jess > On 13 Nov 2021, at 17:54, Jessica Clarke wrote: >=20 > On 13 Nov 2021, at 17:33, Konstantin Belousov wrote: >>=20 >> The branch main has been updated by kib: >>=20 >> URL: = https://cgit.FreeBSD.org/src/commit/?id=3D64ba1f4cf3a6847a1dacf4bab0409d94= 898fa168 >>=20 >> commit 64ba1f4cf3a6847a1dacf4bab0409d94898fa168 >> Author: Konstantin Belousov >> AuthorDate: 2021-11-13 01:18:13 +0000 >> Commit: Konstantin Belousov >> CommitDate: 2021-11-13 17:33:13 +0000 >>=20 >> rtld: Implement LD_SHOW_AUXV >>=20 >> It dumps auxv as seen by interpreter, right before starting any = user >> code. >>=20 >> Copied from: glibc >> Sponsored by: The FreeBSD Foundation >> MFC after: 1 week >> --- >> libexec/rtld-elf/rtld.1 | 7 +++++- >> libexec/rtld-elf/rtld.c | 67 = +++++++++++++++++++++++++++++++++++++++++++++++++ >> 2 files changed, 73 insertions(+), 1 deletion(-) >>=20 >> diff --git a/libexec/rtld-elf/rtld.1 b/libexec/rtld-elf/rtld.1 >> index 187dc105667a..66aa2bdabd17 100644 >> --- a/libexec/rtld-elf/rtld.1 >> +++ b/libexec/rtld-elf/rtld.1 >> @@ -28,7 +28,7 @@ >> .\" >> .\" $FreeBSD$ >> .\" >> -.Dd August 15, 2021 >> +.Dd November 13, 2021 >> .Dt RTLD 1 >> .Os >> .Sh NAME >> @@ -309,6 +309,11 @@ will process the filtee dependencies of the = loaded objects immediately, >> instead of postponing it until required. >> Normally, the filtees are opened at the time of the first symbol = resolution >> from the filter object. >> +.It Ev LD_SHOW_AUXV >> +If set, causes >> +.Nm >> +to dump content of the aux vector to standard output, before passing >> +control to any user code. >> .El >> .Sh DIRECT EXECUTION MODE >> .Nm >> diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c >> index c173c5a6e22e..0475134b0d96 100644 >> --- a/libexec/rtld-elf/rtld.c >> +++ b/libexec/rtld-elf/rtld.c >> @@ -104,6 +104,7 @@ static Obj_Entry *dlopen_object(const char *name, = int fd, Obj_Entry *refobj, >> static Obj_Entry *do_load_object(int, const char *, char *, struct = stat *, int); >> static int do_search_info(const Obj_Entry *obj, int, struct = dl_serinfo *); >> static bool donelist_check(DoneList *, const Obj_Entry *); >> +static void dump_auxv(Elf_Auxinfo **aux_info); >> static void errmsg_restore(struct dlerror_save *); >> static struct dlerror_save *errmsg_save(void); >> static void *fill_search_info(const char *, size_t, void *); >> @@ -364,6 +365,7 @@ enum { >> LD_TRACE_LOADED_OBJECTS_FMT1, >> LD_TRACE_LOADED_OBJECTS_FMT2, >> LD_TRACE_LOADED_OBJECTS_ALL, >> + LD_SHOW_AUXV, >> }; >>=20 >> struct ld_env_var_desc { >> @@ -396,6 +398,7 @@ static struct ld_env_var_desc ld_env_vars[] =3D { >> LD_ENV_DESC(TRACE_LOADED_OBJECTS_FMT1, false), >> LD_ENV_DESC(TRACE_LOADED_OBJECTS_FMT2, false), >> LD_ENV_DESC(TRACE_LOADED_OBJECTS_ALL, false), >> + LD_ENV_DESC(SHOW_AUXV, false), >> }; >>=20 >> static const char * >> @@ -857,6 +860,9 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, = Obj_Entry **objp) >> if (rtld_verify_versions(&list_main) =3D=3D -1 && !ld_tracing) >> rtld_die(); >>=20 >> + if (ld_get_env_var(LD_SHOW_AUXV) !=3D NULL) >> + dump_auxv(aux_info); >> + >> if (ld_tracing) { /* We're done */ >> trace_loaded_objects(obj_main); >> exit(0); >> @@ -6058,6 +6064,67 @@ print_usage(const char *argv0) >> " Arguments to the executed process\n", argv0); >> } >>=20 >> +#define AUXFMT(at, xfmt) [at] =3D { .name =3D #at, .fmt =3D xfmt = } >> +static const struct auxfmt { >> + const char *name; >> + const char *fmt; >> +} auxfmts[] =3D { >> + AUXFMT(AT_NULL, NULL), >> + AUXFMT(AT_IGNORE, NULL), >> + AUXFMT(AT_EXECFD, "%d"), >> + AUXFMT(AT_PHDR, "%p"), >> + AUXFMT(AT_PHENT, "%u"), >> + AUXFMT(AT_PHNUM, "%u"), >> + AUXFMT(AT_PAGESZ, "%u"), >> + AUXFMT(AT_BASE, "%#lx"), >> + AUXFMT(AT_FLAGS, "%#lx"), >> + AUXFMT(AT_ENTRY, "%p"), >> + AUXFMT(AT_NOTELF, NULL), >> + AUXFMT(AT_UID, "%d"), >> + AUXFMT(AT_EUID, "%d"), >> + AUXFMT(AT_GID, "%d"), >> + AUXFMT(AT_EGID, "%d"), >> + AUXFMT(AT_EXECPATH, "%s"), >> + AUXFMT(AT_CANARY, "%p"), >> + AUXFMT(AT_CANARYLEN, "%u"), >> + AUXFMT(AT_OSRELDATE, "%u"), >> + AUXFMT(AT_NCPUS, "%u"), >> + AUXFMT(AT_PAGESIZES, "%p"), >> + AUXFMT(AT_PAGESIZESLEN, "%u"), >> + AUXFMT(AT_TIMEKEEP, "%p"), >> + AUXFMT(AT_STACKPROT, "%#x"), >> + AUXFMT(AT_EHDRFLAGS, "%#lx"), >> + AUXFMT(AT_HWCAP, "%#lx"), >> + AUXFMT(AT_HWCAP2, "%#lx"), >> + AUXFMT(AT_BSDFLAGS, "%#lx"), >> + AUXFMT(AT_ARGC, "%u"), >> + AUXFMT(AT_ARGV, "%p"), >> + AUXFMT(AT_ENVC, "%p"), >> + AUXFMT(AT_ENVV, "%p"), >> + AUXFMT(AT_PS_STRINGS, "%p"), >> + AUXFMT(AT_FXRNG, "%p"), >> +}; >> + >> +static void >> +dump_auxv(Elf_Auxinfo **aux_info) >> +{ >> + Elf_Auxinfo *auxp; >> + const struct auxfmt *fmt; >> + int i; >> + >> + for (i =3D 0; i < AT_COUNT; i++) { >> + auxp =3D aux_info[i]; >> + if (auxp =3D=3D NULL) >> + continue; >> + fmt =3D &auxfmts[i]; >> + if (fmt->fmt =3D=3D NULL) >> + continue; >> + rtld_fdprintf(STDOUT_FILENO, "%s:\t", fmt->name); >> + rtld_fdprintfx(STDOUT_FILENO, fmt->fmt, = auxp->a_un.a_ptr); >> + rtld_fdprintf(STDOUT_FILENO, "\n"); >=20 > This is undefined behaviour, breaks CHERI, and totally unnecessary. = You > have a handful of cases here, just make an enum and have separate > rtld_fdprintf calls. >=20 > Also the table itself is brittle, there=E2=80=99s nothing checking = that the > order perfectly matches up with the defines in the header. Why not use > designated initialisers to ensure that the right values are in the > right entries (and handle the possibility that name might be NULL)? Scratch that second part, I missed the [at] =3D in the macro. Jess