From nobody Wed Jan 19 21:18:46 2022 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 901CF193A8AC; Wed, 19 Jan 2022 21:18:47 +0000 (UTC) (envelope-from brooks@spindle.one-eyed-alien.net) Received: from spindle.one-eyed-alien.net (spindle.one-eyed-alien.net [199.48.129.229]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4JfJRB2qBwz4cBN; Wed, 19 Jan 2022 21:18:46 +0000 (UTC) (envelope-from brooks@spindle.one-eyed-alien.net) Received: by spindle.one-eyed-alien.net (Postfix, from userid 3001) id 12BD23C0199; Wed, 19 Jan 2022 21:18:46 +0000 (UTC) Date: Wed, 19 Jan 2022 21:18:46 +0000 From: Brooks Davis To: Mark Johnston Cc: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Subject: Re: git: 3ce04aca49e9 - main - proc: Add a sysctl to fetch virtual address space layout info Message-ID: <20220119211846.GD79998@spindle.one-eyed-alien.net> References: <202201172113.20HLDAGF007006@gitrepo.freebsd.org> 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 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="l76fUT7nc3MelDdI" Content-Disposition: inline In-Reply-To: <202201172113.20HLDAGF007006@gitrepo.freebsd.org> User-Agent: Mutt/1.9.4 (2018-02-28) X-Rspamd-Queue-Id: 4JfJRB2qBwz4cBN X-Spamd-Bar: / Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=none (mx1.freebsd.org: domain of brooks@spindle.one-eyed-alien.net has no SPF policy when checking 199.48.129.229) smtp.mailfrom=brooks@spindle.one-eyed-alien.net X-Spamd-Result: default: False [0.10 / 15.00]; R_SPF_NA(0.00)[no SPF record]; ARC_NA(0.00)[]; FREEFALL_USER(0.00)[brooks]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[4]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-0.998]; MIME_GOOD(-0.20)[multipart/signed,text/plain]; DMARC_NA(0.00)[freebsd.org]; AUTH_NA(1.00)[]; TO_DN_SOME(0.00)[]; NEURAL_SPAM_MEDIUM(1.00)[1.000]; NEURAL_SPAM_SHORT(1.00)[1.000]; MLMMJ_DEST(0.00)[dev-commits-src-all,dev-commits-src-main]; FORGED_SENDER(0.30)[brooks@freebsd.org,brooks@spindle.one-eyed-alien.net]; RCVD_COUNT_ZERO(0.00)[0]; SIGNED_PGP(-2.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:~]; ASN(0.00)[asn:36236, ipnet:199.48.128.0/22, country:US]; FROM_NEQ_ENVFROM(0.00)[brooks@freebsd.org,brooks@spindle.one-eyed-alien.net] X-ThisMailContainsUnwantedMimeParts: N --l76fUT7nc3MelDdI Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Jan 17, 2022 at 09:13:10PM +0000, Mark Johnston wrote: > The branch main has been updated by markj: >=20 > URL: https://cgit.FreeBSD.org/src/commit/?id=3D3ce04aca49e9228c3c6ab24ffb= ee709f5b464765 >=20 > commit 3ce04aca49e9228c3c6ab24ffbee709f5b464765 > Author: Mark Johnston > AuthorDate: 2022-01-17 16:43:03 +0000 > Commit: Mark Johnston > CommitDate: 2022-01-17 21:12:43 +0000 >=20 > proc: Add a sysctl to fetch virtual address space layout info > =20 > This provides information about fixed regions of the target process' > user memory map. > =20 > Reviewed by: kib > MFC after: 1 month > Sponsored by: The FreeBSD Foundation > Differential Revision: https://reviews.freebsd.org/D33708 > --- > sys/compat/freebsd32/freebsd32.h | 13 +++++++ > sys/kern/kern_proc.c | 78 ++++++++++++++++++++++++++++++++++= ++++++ > sys/sys/sysctl.h | 1 + > sys/sys/user.h | 19 ++++++++++ > 4 files changed, 111 insertions(+) >=20 > diff --git a/sys/compat/freebsd32/freebsd32.h b/sys/compat/freebsd32/free= bsd32.h > index 1f6270d684ee..96bf79d28c02 100644 > --- a/sys/compat/freebsd32/freebsd32.h > +++ b/sys/compat/freebsd32/freebsd32.h > @@ -432,6 +432,19 @@ struct kinfo_sigtramp32 { > uint32_t ksigtramp_spare[4]; > }; > =20 > +struct kinfo_vm_layout32 { > + uint32_t kvm_min_user_addr; > + uint32_t kvm_max_user_addr; > + uint32_t kvm_text_addr; > + uint32_t kvm_text_size; > + uint32_t kvm_data_addr; > + uint32_t kvm_data_size; > + uint32_t kvm_stack_addr; > + uint32_t kvm_stack_size; > + int kvm_map_flags; > + uint32_t kvm_spare[14]; > +}; > + > struct kld_file_stat_1_32 { > int version; /* set to sizeof(struct kld_file_stat_1) */ > char name[MAXPATHLEN]; > diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c > index 1ef8d86295b3..b5896cedf3b9 100644 > --- a/sys/kern/kern_proc.c > +++ b/sys/kern/kern_proc.c > @@ -3200,6 +3200,80 @@ errlocked: > return (error); > } > =20 > +static int > +sysctl_kern_proc_vm_layout(SYSCTL_HANDLER_ARGS) > +{ > + struct kinfo_vm_layout kvm; > + struct proc *p; > + struct vmspace *vmspace; > + int error, *name; > + > + name =3D (int *)arg1; > + if ((u_int)arg2 !=3D 1) > + return (EINVAL); > + > + error =3D pget((pid_t)name[0], PGET_CANDEBUG, &p); > + if (error !=3D 0) > + return (error); > +#ifdef COMPAT_FREEBSD32 > + if (SV_CURPROC_FLAG(SV_ILP32)) { > + if (!SV_PROC_FLAG(p, SV_ILP32)) { > + PROC_UNLOCK(p); > + return (EINVAL); > + } > + } > +#endif > + vmspace =3D vmspace_acquire_ref(p); > + PROC_UNLOCK(p); > + > + memset(&kvm, 0, sizeof(kvm)); > + kvm.kvm_min_user_addr =3D vm_map_min(&vmspace->vm_map); > + kvm.kvm_max_user_addr =3D vm_map_max(&vmspace->vm_map); > + kvm.kvm_text_addr =3D (uintptr_t)vmspace->vm_taddr; > + kvm.kvm_text_size =3D vmspace->vm_tsize; > + kvm.kvm_data_addr =3D (uintptr_t)vmspace->vm_daddr; > + kvm.kvm_data_size =3D vmspace->vm_dsize; > + kvm.kvm_stack_addr =3D (uintptr_t)vmspace->vm_maxsaddr; > + kvm.kvm_stack_size =3D vmspace->vm_ssize; > + if ((vmspace->vm_map.flags & MAP_WIREFUTURE) !=3D 0) > + kvm.kvm_map_flags |=3D KMAP_FLAG_WIREFUTURE; > + if ((vmspace->vm_map.flags & MAP_ASLR) !=3D 0) > + kvm.kvm_map_flags |=3D KMAP_FLAG_ASLR; > + if ((vmspace->vm_map.flags & MAP_ASLR_IGNSTART) !=3D 0) > + kvm.kvm_map_flags |=3D KMAP_FLAG_ASLR_IGNSTART; > + if ((vmspace->vm_map.flags & MAP_WXORX) !=3D 0) > + kvm.kvm_map_flags |=3D KMAP_FLAG_WXORX; > + if ((vmspace->vm_map.flags & MAP_ASLR_STACK) !=3D 0) > + kvm.kvm_map_flags |=3D KMAP_FLAG_ASLR_STACK; > + > +#ifdef COMPAT_FREEBSD32 > + if (SV_CURPROC_FLAG(SV_ILP32)) { > + struct kinfo_vm_layout32 kvm32; > + > + memset(&kvm32, 0, sizeof(kvm32)); > + kvm32.kvm_min_user_addr =3D (uint32_t)kvm.kvm_min_user_addr; If this is kept (see below), could the CP macros be used from sys/abi_compat.h be used? > + kvm32.kvm_max_user_addr =3D (uint32_t)kvm.kvm_max_user_addr; > + kvm32.kvm_text_addr =3D (uint32_t)kvm.kvm_text_addr; > + kvm32.kvm_text_size =3D (uint32_t)kvm.kvm_text_size; > + kvm32.kvm_data_addr =3D (uint32_t)kvm.kvm_data_addr; > + kvm32.kvm_data_size =3D (uint32_t)kvm.kvm_data_size; > + kvm32.kvm_stack_addr =3D (uint32_t)kvm.kvm_stack_addr; > + kvm32.kvm_stack_size =3D (uint32_t)kvm.kvm_stack_size; > + kvm32.kvm_map_flags =3D kvm.kvm_map_flags; > + vmspace_free(vmspace); > + error =3D SYSCTL_OUT(req, &kvm32, sizeof(kvm32)); > + goto out; > + } > +#endif > + > + error =3D SYSCTL_OUT(req, &kvm, sizeof(kvm)); > +#ifdef COMPAT_FREEBSD32 > +out: > +#endif > + vmspace_free(vmspace); > + return (error); > +} > + > SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, > "Process table"); > =20 > @@ -3318,6 +3392,10 @@ static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBL= K, sigfastblk, CTLFLAG_RD | > CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk, > "Thread sigfastblock address"); > =20 > +static SYSCTL_NODE(_kern_proc, KERN_PROC_VM_LAYOUT, vm_layout, CTLFLAG_R= D | > + CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_vm_layout, > + "Process virtual address space layout info"); > + > int allproc_gen; > =20 > /* > diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h > index 71a34652ff44..f25152db8215 100644 > --- a/sys/sys/sysctl.h > +++ b/sys/sys/sysctl.h > @@ -1013,6 +1013,7 @@ TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry); > #define KERN_PROC_CWD 42 /* process current working directory */ > #define KERN_PROC_NFDS 43 /* number of open file descriptors */ > #define KERN_PROC_SIGFASTBLK 44 /* address of fastsigblk magic word */ > +#define KERN_PROC_VM_LAYOUT 45 /* virtual address space layout info */ > =20 > /* > * KERN_IPC identifiers > diff --git a/sys/sys/user.h b/sys/sys/user.h > index e8bfba981e83..f57a69aed13e 100644 > --- a/sys/sys/user.h > +++ b/sys/sys/user.h > @@ -598,6 +598,25 @@ struct kinfo_sigtramp { > void *ksigtramp_spare[4]; > }; > =20 > +#define KMAP_FLAG_WIREFUTURE 0x01 /* all future mappings wil be wired */ > +#define KMAP_FLAG_ASLR 0x02 /* ASLR is applied to mappings */ > +#define KMAP_FLAG_ASLR_IGNSTART 0x04 /* ASLR may map into sbrk grow regi= on */ > +#define KMAP_FLAG_WXORX 0x08 /* W^X mapping policy is enforced */ > +#define KMAP_FLAG_ASLR_STACK 0x10 /* the stack location is randomized */ > + > +struct kinfo_vm_layout { > + uintptr_t kvm_min_user_addr; > + uintptr_t kvm_max_user_addr; > + uintptr_t kvm_text_addr; > + size_t kvm_text_size; > + uintptr_t kvm_data_addr; > + size_t kvm_data_size; > + uintptr_t kvm_stack_addr; > + size_t kvm_stack_size; > + int kvm_map_flags; Should there be an explicit pad here? > + uintptr_t kvm_spare[14]; > +}; I'd prefer these were kvaddr_t's (uint64_t) as that would eliminate the need for a 32-bit translation (and avoid future complications on for CheriABI). All the _addrs really are addresses rather than pointers. > + > #ifdef _KERNEL > /* Flags for kern_proc_out function. */ > #define KERN_PROC_NOTHREADS 0x1 >=20 -- Brooks --l76fUT7nc3MelDdI Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEcBAEBAgAGBQJh6IA1AAoJEKzQXbSebgfAqWQH/3Qv8ddkE8bwy1hUGGJXJwoU AjivLEntfb7Az98gBVBhP073vfnQqSLDgZsiFhRWqmI/bHJrQWgkDMahYwP16Jes 9CTAnoGO8cQOK1ay9S7rIOBR+kGffns4OO/PAcHjmffw+iUoJU/p2GXNmb2MEnHD lMShs0SX9KCjQDbWAELXk7/6qe7C9hDpCgOBGL72wVqAiqde8PstApzHGo7LQa5E h6Mr0MgV/YcmjRE4SQ0i15XsRIksttJ7za+lrZt0odtOZfojPXgMI/qTMOIDYXBl PAs6EIH3K3aKeLX7UgLwqd115R/6uGES39YbjKcXPeSAhlWd1+IlGfzTE+uqkbo= =Lu+m -----END PGP SIGNATURE----- --l76fUT7nc3MelDdI--