From owner-svn-src-head@freebsd.org Wed Jul 15 10:13:24 2020 Return-Path: Delivered-To: svn-src-head@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 9EC80361D8D; Wed, 15 Jul 2020 10:13:24 +0000 (UTC) (envelope-from mjg@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) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 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 4B6Crh33c4z42Xp; Wed, 15 Jul 2020 10:13:24 +0000 (UTC) (envelope-from mjg@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 30635158FB; Wed, 15 Jul 2020 10:13:24 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 06FADNLU001486; Wed, 15 Jul 2020 10:13:23 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 06FADNUk001485; Wed, 15 Jul 2020 10:13:23 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202007151013.06FADNUk001485@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Wed, 15 Jul 2020 10:13:23 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r363212 - head/lib/libprocstat X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: head/lib/libprocstat X-SVN-Commit-Revision: 363212 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Jul 2020 10:13:24 -0000 Author: mjg Date: Wed Jul 15 10:13:23 2020 New Revision: 363212 URL: https://svnweb.freebsd.org/changeset/base/363212 Log: libprocstat: fix kvm filedesc access after introduction of fdescenttbl Modified: head/lib/libprocstat/libprocstat.c Modified: head/lib/libprocstat/libprocstat.c ============================================================================== --- head/lib/libprocstat/libprocstat.c Wed Jul 15 09:48:36 2020 (r363211) +++ head/lib/libprocstat/libprocstat.c Wed Jul 15 10:13:23 2020 (r363212) @@ -459,6 +459,7 @@ procstat_getfiles_kvm(struct procstat *procstat, struc { struct file file; struct filedesc filed; + struct fdescenttbl *fdt; struct pwd pwd; unsigned long pwd_addr; struct vm_map_entry vmentry; @@ -467,13 +468,14 @@ procstat_getfiles_kvm(struct procstat *procstat, struc vm_map_entry_t entryp; vm_object_t objp; struct vnode *vp; - struct filedescent *ofiles; struct filestat *entry; struct filestat_list *head; kvm_t *kd; void *data; - int i, fflags; + int fflags; + unsigned int i; int prot, type; + size_t fdt_size; unsigned int nfiles; bool haspwd; @@ -553,26 +555,31 @@ procstat_getfiles_kvm(struct procstat *procstat, struc STAILQ_INSERT_TAIL(head, entry, next); } - nfiles = filed.fd_lastfile + 1; - ofiles = malloc(nfiles * sizeof(struct filedescent)); - if (ofiles == NULL) { - warn("malloc(%zu)", nfiles * sizeof(struct filedescent)); + if (!kvm_read_all(kd, (unsigned long)filed.fd_files, &nfiles, + sizeof(nfiles))) { + warnx("can't read fd_files at %p", (void *)filed.fd_files); + return (NULL); + } + + fdt_size = sizeof(*fdt) + nfiles * sizeof(struct filedescent); + fdt = malloc(fdt_size); + if (fdt == NULL) { + warn("malloc(%zu)", fdt_size); goto do_mmapped; } - if (!kvm_read_all(kd, (unsigned long)filed.fd_ofiles, ofiles, - nfiles * sizeof(struct filedescent))) { - warnx("cannot read file structures at %p", - (void *)filed.fd_ofiles); - free(ofiles); + if (!kvm_read_all(kd, (unsigned long)filed.fd_files, fdt, fdt_size)) { + warnx("cannot read file structures at %p", (void *)filed.fd_files); + free(fdt); goto do_mmapped; } - for (i = 0; i <= filed.fd_lastfile; i++) { - if (ofiles[i].fde_file == NULL) + for (i = 0; i < nfiles; i++) { + if (fdt->fdt_ofiles[i].fde_file == NULL) { continue; - if (!kvm_read_all(kd, (unsigned long)ofiles[i].fde_file, &file, + } + if (!kvm_read_all(kd, (unsigned long)fdt->fdt_ofiles[i].fde_file, &file, sizeof(struct file))) { warnx("can't read file %d at %p", i, - (void *)ofiles[i].fde_file); + (void *)fdt->fdt_ofiles[i].fde_file); continue; } switch (file.f_type) { @@ -623,7 +630,7 @@ procstat_getfiles_kvm(struct procstat *procstat, struc if (entry != NULL) STAILQ_INSERT_TAIL(head, entry, next); } - free(ofiles); + free(fdt); do_mmapped: