From nobody Thu Jan 20 16:17:31 2022 X-Original-To: dev-commits-src-main@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 BCD4F1970A05; Thu, 20 Jan 2022 16:17:35 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: from mail-qk1-x72a.google.com (mail-qk1-x72a.google.com [IPv6:2607:f8b0:4864:20::72a]) (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 4JfnjC3YRVz4mxd; Thu, 20 Jan 2022 16:17:35 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: by mail-qk1-x72a.google.com with SMTP id z10so6277336qkf.7; Thu, 20 Jan 2022 08:17:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=KqEhV9GMM+OiU1T1ygN8wnx/ErDUYw1fpZUn2iVJ3f4=; b=or1duLvPNQ7zV5QzYCRqKT9xX8ApzmO+6mw1BMFpNbyIpGHT2q0sFwK2JOTXEZWjY9 AnaFMA2P8R0NMZIHWDRjiuqRCpcgWfAaMxL4UYhDjL7pEnMTH8akJTrJU4rgrKH+0dAs +gUH1OjQEe2YKb4dEVDdE55TXmCwSMRjbOtbq/I7j8uPU3nM7GZ+Z+UZzEIROjSmUlVR 6AkrvbslPhA0InZ/qGzC3DtpxL4KS/siGqF6bZrkdNNUbSNgAdESp93fR0b5O6Eq0umY UzjAPkOhZH1e0Kbom/SoWvcyWDDqHwBWyeSoNmJlFb28PBbgJHRE0iTSxNdB9MqCVg/g 4inQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=KqEhV9GMM+OiU1T1ygN8wnx/ErDUYw1fpZUn2iVJ3f4=; b=YmklAGv1TWOUVZ9gSUi58TNRVR6+jSV07BP2I2B95AqWJlPwepNCNmby6KmO5UBYrB U3NxdfGaEcrhidx+KgtCoqQ4tkxt3e7XrfkFAyhrvE/j5EgWyg9ud8hyyn0Kdqc58Oof cbqKOTSsuDDHOj8r4+87eQJe0bCpQ2sjyY1TIOKK5EAy5ybYb/5kgp2PYgdW90R0a3GR q09zxeABpkf5yql5WdaBRKgrZ7tXq1Advy71+MYx04Oa+QeJt3XJbkYR4/zNpsLCQgbX PcX1thwq/IRSywiVsTdaN7aXeVbszAAX8LopxYb09dpsYc+IYfXAraoDl3yOCCGEjExZ 3Rhw== X-Gm-Message-State: AOAM530j+sw8SJs//gE5yQgQPennReRtH2PdLYCkwrYakKR3/lAaifcr Ve5MhsSRm9D52cNe7IwNNNkE+QYeszM= X-Google-Smtp-Source: ABdhPJxNof2Amh4/ELPXucgtnEkipBBpsORykRvx1HnfwLDnrj0nX4M6zU5KaGsHJqLMiA1ORXJ4Kw== X-Received: by 2002:a37:9a13:: with SMTP id c19mr19834060qke.527.1642695454494; Thu, 20 Jan 2022 08:17:34 -0800 (PST) Received: from nuc (198-84-189-58.cpe.teksavvy.com. [198.84.189.58]) by smtp.gmail.com with ESMTPSA id v6sm1603268qtk.88.2022.01.20.08.17.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jan 2022 08:17:33 -0800 (PST) Sender: Mark Johnston Date: Thu, 20 Jan 2022 11:17:31 -0500 From: Mark Johnston To: Brooks Davis 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: References: <202201172113.20HLDAGF007006@gitrepo.freebsd.org> <20220119211846.GD79998@spindle.one-eyed-alien.net> List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220119211846.GD79998@spindle.one-eyed-alien.net> X-Rspamd-Queue-Id: 4JfnjC3YRVz4mxd X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] X-ThisMailContainsUnwantedMimeParts: N On Wed, Jan 19, 2022 at 09:18:46PM +0000, Brooks Davis wrote: > On Mon, Jan 17, 2022 at 09:13:10PM +0000, Mark Johnston wrote: > > The branch main has been updated by markj: > > > > URL: https://cgit.FreeBSD.org/src/commit/?id=3ce04aca49e9228c3c6ab24ffbee709f5b464765 > > > > commit 3ce04aca49e9228c3c6ab24ffbee709f5b464765 > > Author: Mark Johnston > > AuthorDate: 2022-01-17 16:43:03 +0000 > > Commit: Mark Johnston > > CommitDate: 2022-01-17 21:12:43 +0000 > > > > proc: Add a sysctl to fetch virtual address space layout info > > > > This provides information about fixed regions of the target process' > > user memory map. > > > > 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(+) > > > > diff --git a/sys/compat/freebsd32/freebsd32.h b/sys/compat/freebsd32/freebsd32.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]; > > }; > > > > +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); > > } > > > > +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 = (int *)arg1; > > + if ((u_int)arg2 != 1) > > + return (EINVAL); > > + > > + error = pget((pid_t)name[0], PGET_CANDEBUG, &p); > > + if (error != 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 = vmspace_acquire_ref(p); > > + PROC_UNLOCK(p); > > + > > + memset(&kvm, 0, sizeof(kvm)); > > + kvm.kvm_min_user_addr = vm_map_min(&vmspace->vm_map); > > + kvm.kvm_max_user_addr = vm_map_max(&vmspace->vm_map); > > + kvm.kvm_text_addr = (uintptr_t)vmspace->vm_taddr; > > + kvm.kvm_text_size = vmspace->vm_tsize; > > + kvm.kvm_data_addr = (uintptr_t)vmspace->vm_daddr; > > + kvm.kvm_data_size = vmspace->vm_dsize; > > + kvm.kvm_stack_addr = (uintptr_t)vmspace->vm_maxsaddr; > > + kvm.kvm_stack_size = vmspace->vm_ssize; > > + if ((vmspace->vm_map.flags & MAP_WIREFUTURE) != 0) > > + kvm.kvm_map_flags |= KMAP_FLAG_WIREFUTURE; > > + if ((vmspace->vm_map.flags & MAP_ASLR) != 0) > > + kvm.kvm_map_flags |= KMAP_FLAG_ASLR; > > + if ((vmspace->vm_map.flags & MAP_ASLR_IGNSTART) != 0) > > + kvm.kvm_map_flags |= KMAP_FLAG_ASLR_IGNSTART; > > + if ((vmspace->vm_map.flags & MAP_WXORX) != 0) > > + kvm.kvm_map_flags |= KMAP_FLAG_WXORX; > > + if ((vmspace->vm_map.flags & MAP_ASLR_STACK) != 0) > > + kvm.kvm_map_flags |= 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 = (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 = (uint32_t)kvm.kvm_max_user_addr; > > + kvm32.kvm_text_addr = (uint32_t)kvm.kvm_text_addr; > > + kvm32.kvm_text_size = (uint32_t)kvm.kvm_text_size; > > + kvm32.kvm_data_addr = (uint32_t)kvm.kvm_data_addr; > > + kvm32.kvm_data_size = (uint32_t)kvm.kvm_data_size; > > + kvm32.kvm_stack_addr = (uint32_t)kvm.kvm_stack_addr; > > + kvm32.kvm_stack_size = (uint32_t)kvm.kvm_stack_size; > > + kvm32.kvm_map_flags = kvm.kvm_map_flags; > > + vmspace_free(vmspace); > > + error = SYSCTL_OUT(req, &kvm32, sizeof(kvm32)); > > + goto out; > > + } > > +#endif > > + > > + error = 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"); > > > > @@ -3318,6 +3392,10 @@ static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD | > > CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk, > > "Thread sigfastblock address"); > > > > +static SYSCTL_NODE(_kern_proc, KERN_PROC_VM_LAYOUT, vm_layout, CTLFLAG_RD | > > + CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_vm_layout, > > + "Process virtual address space layout info"); > > + > > int allproc_gen; > > > > /* > > 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 */ > > > > /* > > * 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]; > > }; > > > > +#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 region */ > > +#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. Thanks, see https://reviews.freebsd.org/D33964 .