summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzhuk <zhuk@openbsd.org>2016-05-04 01:28:42 +0000
committerzhuk <zhuk@openbsd.org>2016-05-04 01:28:42 +0000
commit8493a6f22f4639f33b42c241d7f9bae545c5c5ec (patch)
tree9823ca03eba27592d438869bb9215de4e87cc305
parentPrint missing newline if we bail out because of a uid mismatch. (diff)
downloadwireguard-openbsd-8493a6f22f4639f33b42c241d7f9bae545c5c5ec.tar.xz
wireguard-openbsd-8493a6f22f4639f33b42c241d7f9bae545c5c5ec.zip
Make KERN_FILE_BYPID return ESRCH when PID not found, both in sysctl and
offline paths. More polishing to come. Input and okay bluhm@ & kettenis@.
-rw-r--r--lib/libkvm/kvm_file2.c20
-rw-r--r--sys/kern/kern_sysctl.c8
2 files changed, 20 insertions, 8 deletions
diff --git a/lib/libkvm/kvm_file2.c b/lib/libkvm/kvm_file2.c
index db267196c68..087b9abf296 100644
--- a/lib/libkvm/kvm_file2.c
+++ b/lib/libkvm/kvm_file2.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kvm_file2.c,v 1.48 2016/04/25 20:42:55 tedu Exp $ */
+/* $OpenBSD: kvm_file2.c,v 1.49 2016/05/04 01:28:42 zhuk Exp $ */
/*
* Copyright (c) 2009 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -148,7 +148,7 @@ kvm_getfiles(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
/* find size and alloc buffer */
rv = sysctl(mib, 6, NULL, &size, NULL, 0);
if (rv == -1) {
- if (kd->vmfd != -1)
+ if (errno != ESRCH && kd->vmfd != -1)
goto deadway;
_kvm_syserr(kd, kd->program, "kvm_getfiles");
return (NULL);
@@ -265,7 +265,7 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
{
size_t buflen;
struct nlist nl[4], *np;
- int n = 0;
+ int n = 0, matched = 0;
char *where;
struct kinfo_file kf;
struct file *fp, file;
@@ -311,6 +311,9 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
kd->filebase = (void *)where;
buflen = (nfiles + 10) * esize;
+ if (op != KERN_FILE_BYPID || arg <= 0)
+ matched = 1;
+
for (pr = LIST_FIRST(&allprocess);
pr != NULL;
pr = LIST_NEXT(&process, ps_list)) {
@@ -332,10 +335,11 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
goto cleanup;
}
- if (op == KERN_FILE_BYPID && arg > 0 &&
- proc.p_pid != (pid_t)arg) {
- /* not the pid we are looking for */
+ if (op == KERN_FILE_BYPID) {
+ /* check if this is the pid we are looking for */
+ if (arg > 0 && proc.p_pid != (pid_t)arg)
continue;
+ matched = 1;
}
if (KREAD(kd, (u_long)process.ps_ucred, &ucred)) {
@@ -457,6 +461,10 @@ kvm_deadfile_byid(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
n++;
}
}
+ if (!matched) {
+ errno = ESRCH;
+ goto cleanup;
+ }
done:
*cnt = n;
free(filebuf);
diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index f27c4d26d56..e5cab67d9b4 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_sysctl.c,v 1.301 2016/04/25 20:00:33 tedu Exp $ */
+/* $OpenBSD: kern_sysctl.c,v 1.302 2016/05/04 01:28:42 zhuk Exp $ */
/* $NetBSD: kern_sysctl.c,v 1.17 1996/05/20 17:49:05 mrg Exp $ */
/*-
@@ -1218,7 +1218,7 @@ sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
struct process *pr;
size_t buflen, elem_size, elem_count, outsize;
char *dp = where;
- int arg, i, error = 0, needed = 0;
+ int arg, i, error = 0, needed = 0, matched;
u_int op;
int show_pointers;
@@ -1318,6 +1318,7 @@ sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
error = EINVAL;
break;
}
+ matched = 0;
LIST_FOREACH(pr, &allprocess, ps_list) {
/*
* skip system, exiting, embryonic and undead
@@ -1329,6 +1330,7 @@ sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
/* not the pid we are looking for */
continue;
}
+ matched = 1;
fdp = pr->ps_fd;
if (pr->ps_textvp)
FILLIT(NULL, NULL, KERN_FILE_TEXT, pr->ps_textvp, pr);
@@ -1346,6 +1348,8 @@ sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
FILLIT(fp, fdp, i, NULL, pr);
}
}
+ if (!matched)
+ error = ESRCH;
break;
case KERN_FILE_BYUID:
LIST_FOREACH(pr, &allprocess, ps_list) {