Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 15 Jul 2020 10:13:23 +0000 (UTC)
From:      Mateusz Guzik <mjg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r363212 - head/lib/libprocstat
Message-ID:  <202007151013.06FADNUk001485@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
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:
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202007151013.06FADNUk001485>