From a6b19e380cb3a0e77626565ae1391bbf2c184038 Mon Sep 17 00:00:00 2001 From: dlg Date: Fri, 4 Sep 2015 02:58:14 +0000 Subject: fix a race when fetching files from the kernel. the number of files might have increased between when we got the number of files and when we requested that number of files. now we allocate another 10ish percent on top of what the kernel says we need, and retry if that still isnt enough. while here use realloc instead of constantly going through free/malloc sequences. with input from claudio@ deraadt@ ok millert@ guenther@ --- lib/libkvm/kvm_file2.c | 77 +++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 36 deletions(-) (limited to 'lib/libkvm/kvm_file2.c') diff --git a/lib/libkvm/kvm_file2.c b/lib/libkvm/kvm_file2.c index 04912bf2562..669a7d4ae77 100644 --- a/lib/libkvm/kvm_file2.c +++ b/lib/libkvm/kvm_file2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kvm_file2.c,v 1.46 2015/08/28 04:38:47 guenther Exp $ */ +/* $OpenBSD: kvm_file2.c,v 1.47 2015/09/04 02:58:14 dlg Exp $ */ /* * Copyright (c) 2009 Todd C. Miller @@ -114,6 +114,7 @@ #include #include #include +#include #include "kvm_private.h" #include "kvm_file.h" @@ -132,44 +133,46 @@ struct kinfo_file * kvm_getfiles(kvm_t *kd, int op, int arg, size_t esize, int *cnt) { int mib[6], rv; + void *filebase; size_t size; - if (kd->filebase != NULL) { - free(kd->filebase); - /* - * Clear this pointer in case this call fails. Otherwise, - * kvm_close() will free it again. - */ - kd->filebase = 0; - } - if (ISALIVE(kd)) { mib[0] = CTL_KERN; mib[1] = KERN_FILE; mib[2] = op; mib[3] = arg; mib[4] = esize; - mib[5] = 0; - - /* find size and alloc buffer */ - rv = sysctl(mib, 6, NULL, &size, NULL, 0); - if (rv == -1) { - if (kd->vmfd != -1) - goto deadway; - _kvm_syserr(kd, kd->program, "kvm_getfiles"); - return (NULL); - } - kd->filebase = _kvm_malloc(kd, size); - if (kd->filebase == NULL) - return (NULL); - /* get actual data */ - mib[5] = size / esize; - rv = sysctl(mib, 6, kd->filebase, &size, NULL, 0); - if (rv == -1) { - _kvm_syserr(kd, kd->program, "kvm_getfiles"); - return (NULL); - } + do { + mib[5] = 0; + + /* find size and alloc buffer */ + rv = sysctl(mib, 6, NULL, &size, NULL, 0); + if (rv == -1) { + if (kd->vmfd != -1) + goto deadway; + _kvm_syserr(kd, kd->program, "kvm_getfiles"); + return (NULL); + } + + size += size / 8; /* add ~10% */ + + filebase = _kvm_realloc(kd, kd->filebase, size); + if (filebase == NULL) + return (NULL); + + kd->filebase = filebase; + + /* get actual data */ + mib[5] = size / esize; + rv = sysctl(mib, 6, kd->filebase, &size, NULL, 0); + if (rv == -1 && errno != ENOMEM) { + _kvm_syserr(kd, kd->program, + "kvm_getfiles"); + return (NULL); + } + } while (rv == -1); + *cnt = size / esize; return (kd->filebase); } else { @@ -224,10 +227,11 @@ kvm_deadfile_byfile(kvm_t *kd, int op, int arg, size_t esize, int *cnt) _kvm_err(kd, kd->program, "can't read nfiles"); return (NULL); } - where = _kvm_reallocarray(kd, NULL, nfiles, esize); - kd->filebase = (void *)where; - if (kd->filebase == NULL) + where = _kvm_reallocarray(kd, kd->filebase, nfiles, esize); + if (where == NULL) return (NULL); + + kd->filebase = (void *)where; buflen = nfiles * esize; for (fp = LIST_FIRST(&filehead); @@ -301,10 +305,11 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt) return (NULL); } /* this may be more room than we need but counting is expensive */ - where = _kvm_reallocarray(kd, NULL, nfiles + 10, esize); - kd->filebase = (void *)where; - if (kd->filebase == NULL) + where = _kvm_reallocarray(kd, kd->filebase, nfiles + 10, esize); + if (where == NULL) return (NULL); + + kd->filebase = (void *)where; buflen = (nfiles + 10) * esize; for (pr = LIST_FIRST(&allprocess); -- cgit v1.2.3-59-g8ed1b