1/*	$NetBSD: misc.c,v 1.27 2023/07/29 08:46:47 riastradh Exp $	*/
2
3/*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__RCSID("$NetBSD: misc.c,v 1.27 2023/07/29 08:46:47 riastradh Exp $");
34
35#include <stdbool.h>
36#include <sys/param.h>
37#include <sys/types.h>
38#include <sys/time.h>
39#include <sys/stat.h>
40#include <sys/condvar.h>
41#include <sys/selinfo.h>
42#include <sys/filedesc.h>
43#define _KERNEL
44#include <sys/mqueue.h>
45#include <sys/eventvar.h>
46#undef _KERNEL
47#include <sys/proc.h>
48#define _KERNEL
49#include <sys/file.h>
50#define copyin_t int
51#define copyout_t int
52#include <sys/ksem.h>
53#define _LIB_LIBKERN_LIBKERN_H_
54#define mutex_enter(a)
55#define mutex_exit(a)
56#undef _KERNEL
57#include <sys/cprng.h>
58#include <sys/vnode.h>
59#include <sys/memfd.h>
60#include <sys/mount.h>
61
62#include <net/bpfdesc.h>
63
64#include <dev/audio/audiodef.h>
65#include <dev/audio/audio_if.h>
66
67#include <err.h>
68#include <util.h>
69#include <string.h>
70#include <kvm.h>
71#include "fstat.h"
72
73static struct nlist nl[] = {
74#define NL_BPF		0
75    { .n_name = "bpf_fileops", },
76#define NL_CRYPTO	1
77    { .n_name = "cryptofops" },
78#define NL_DMIO		2
79    { .n_name = "dmio_fileops", },
80#define NL_DRVCTL	3
81    { .n_name = "drvctl_fileops", },
82#define NL_DTV_DEMUX	4
83    { .n_name = "dtv_demux_fileops", },
84#define NL_FILEMON	5
85    { .n_name = "filemon_fileops", },
86#define NL_KQUEUE	6
87    { .n_name = "kqueueops" },
88#define NL_MQUEUE	7
89    { .n_name = "mqops" },
90#define NL_PIPE		8
91    { .n_name = "pipeops" },
92#define NL_PUTTER	9
93    { .n_name = "putter_fileops", },
94#define NL_RND		10
95    { .n_name = "rnd_fileops", },
96#define NL_SEM		11
97    { .n_name = "semops", },
98#define NL_SOCKET	12
99    { .n_name = "socketops" },
100#define NL_SVR4_NET	13
101    { .n_name = "svr4_netops" },
102#define NL_SVR4_32_NET	14
103    { .n_name = "svr4_32_netops" },
104#define NL_TAP		15
105    { .n_name = "tap_fileops", },
106#define NL_VNOPS	16
107    { .n_name = "vnops" },
108#define NL_XENEVT	17
109    { .n_name = "xenevt_fileops" },
110#define NL_AUDIO	18
111    { .n_name = "audio_fileops" },
112#define NL_PAD		19
113    { .n_name = "pad_fileops" },
114#define NL_MEMFD	20
115    { .n_name = "memfd_fileops" },
116#define NL_MAX		21
117    { .n_name = NULL }
118};
119
120extern int vflg;
121
122
123static int
124p_bpf(struct file *f)
125{
126	struct bpf_d bpf;
127	struct bpf_if bi;
128	struct ifnet ifn;
129
130	strlcpy(ifn.if_xname, "???", sizeof(ifn.if_xname));
131
132	if (!KVM_READ(f->f_data, &bpf, sizeof(bpf))) {
133		dprintf("can't read bpf at %p for pid %d", f->f_data, Pid);
134		return 0;
135	}
136	if (bpf.bd_bif != NULL) {
137		if (!KVM_READ(bpf.bd_bif, &bi, sizeof(bi)))
138			dprintf("can't read bpf interface at %p for pid %d",
139			    bpf.bd_bif, Pid);
140		if (bi.bif_ifp != NULL)
141			if (!KVM_READ(bi.bif_ifp, &ifn, sizeof(ifn)))
142				dprintf("can't read net interfsace"
143				    " at %p for pid %d", bi.bif_ifp, Pid);
144	}
145	(void)printf("* bpf@%s rec=%lu, dr=%lu, cap=%lu, pid=%lu", ifn.if_xname,
146	    bpf.bd_rcount, bpf.bd_dcount, bpf.bd_ccount,
147	    (unsigned long)bpf.bd_pid);
148	if (bpf.bd_promisc)
149		(void)printf(", promisc");
150	if (bpf.bd_immediate)
151		(void)printf(", immed");
152	if (bpf.bd_direction == BPF_D_IN)
153		(void)printf(", in");
154	else if (bpf.bd_direction == BPF_D_INOUT)
155		(void)printf(", inout");
156	else if (bpf.bd_direction == BPF_D_OUT)
157		(void)printf(", out");
158	if (bpf.bd_jitcode != NULL)
159		(void)printf(", jit");
160	if (bpf.bd_async)
161		(void)printf(", asyncgrp=%lu", (unsigned long)bpf.bd_pgid);
162	if (bpf.bd_state == BPF_IDLE)
163		(void)printf(", idle");
164	else if (bpf.bd_state == BPF_WAITING)
165		(void)printf(", waiting");
166	else if (bpf.bd_state == BPF_TIMED_OUT)
167		(void)printf(", timeout");
168	oprint(f, "\n");
169	return 0;
170}
171
172static int
173p_sem(struct file *f)
174{
175	ksem_t ks;
176	if (!KVM_READ(f->f_data, &ks, sizeof(ks))) {
177		dprintf("can't read sem at %p for pid %d", f->f_data, Pid);
178		return 0;
179	}
180	(void)printf("* ksem ref=%u, value=%u, waiters=%u, flags=0x%x, "
181	    "mode=%o, uid=%u, gid=%u", ks.ks_ref, ks.ks_value, ks.ks_waiters,
182	    ks.ks_flags, ks.ks_mode, ks.ks_uid, ks.ks_gid);
183	if (ks.ks_name && ks.ks_namelen) {
184		char buf[64];
185		if (ks.ks_namelen >= sizeof(buf))
186			ks.ks_namelen = sizeof(buf) - 1;
187		if (!KVM_READ(ks.ks_name, buf, ks.ks_namelen)) {
188			dprintf("can't read sem name at %p for pid %d",
189			    ks.ks_name, Pid);
190		} else {
191			buf[ks.ks_namelen] = '\0';
192			(void)printf(", name=%s", buf);
193			oprint(f, "\n");
194			return 0;
195		}
196	}
197	oprint(f, "\n");
198	return 0;
199}
200
201static int
202p_mqueue(struct file *f)
203{
204	struct mqueue mq;
205
206	if (!KVM_READ(f->f_data, &mq, sizeof(mq))) {
207		dprintf("can't read mqueue at %p for pid %d", f->f_data, Pid);
208		return 0;
209	}
210	(void)printf("* mqueue \"%s\"", mq.mq_name);
211	oprint(f, "\n");
212	return 0;
213}
214
215static int
216p_kqueue(struct file *f)
217{
218	struct kqueue kq;
219
220	if (!KVM_READ(f->f_data, &kq, sizeof(kq))) {
221		dprintf("can't read kqueue at %p for pid %d", f->f_data, Pid);
222		return 0;
223	}
224	(void)printf("* kqueue pending %d", kq.kq_count);
225	oprint(f, "\n");
226	return 0;
227}
228
229static int
230p_audio(struct file *f)
231{
232	struct audio_file af;
233	const char *devname;
234	const char *modename;
235
236	if (!KVM_READ(f->f_data, &af, sizeof(af))) {
237		dprintf("can't read audio_file at %p for pid %d",
238		    f->f_data, Pid);
239		return 0;
240	}
241
242	if (ISDEVAUDIO(af.dev)) {
243		devname = "audio";
244	} else if (ISDEVSOUND(af.dev)) {
245		devname = "sound";
246	} else if (ISDEVAUDIOCTL(af.dev)) {
247		devname = "audioctl";
248	} else if (ISDEVMIXER(af.dev)) {
249		devname = "mixer";
250	} else {
251		devname = "???";
252	}
253
254	if (af.ptrack && af.rtrack) {
255		modename = "playback, record";
256	} else if (af.ptrack) {
257		modename = "playback";
258	} else if (af.rtrack) {
259		modename = "record";
260	} else {
261		modename = "-";
262	}
263
264	(void)printf("* audio@%s%d %s", devname, AUDIOUNIT(af.dev), modename);
265	oprint(f, "\n");
266	return 0;
267}
268
269static int
270p_memfd_seal(int seen, int all, int target, const char *name)
271{
272	if (all & target)
273		(void)printf("%s%s", (seen ? "|" : ""), name);
274
275	return seen || (all & target);
276}
277
278static int
279p_memfd(struct file *f)
280{
281	int seal_yet = 0;
282	struct memfd mfd;
283
284	if (!KVM_READ(f->f_data, &mfd, sizeof(mfd))) {
285		dprintf("can't read memfd at %p for pid %d", f->f_data, Pid);
286		return 0;
287	}
288	(void)printf("* %s, seals=", mfd.mfd_name);
289	if (mfd.mfd_seals == 0)
290		(void)printf("0");
291	else {
292		seal_yet = p_memfd_seal(seal_yet, mfd.mfd_seals, F_SEAL_SEAL, "F_SEAL_SEAL");
293		seal_yet = p_memfd_seal(seal_yet, mfd.mfd_seals, F_SEAL_SHRINK, "F_SEAL_SHRINK");
294		seal_yet = p_memfd_seal(seal_yet, mfd.mfd_seals, F_SEAL_GROW, "F_SEAL_GROW");
295		seal_yet = p_memfd_seal(seal_yet, mfd.mfd_seals, F_SEAL_WRITE, "F_SEAL_WRITE");
296		seal_yet = p_memfd_seal(seal_yet, mfd.mfd_seals, F_SEAL_FUTURE_WRITE, "F_SEAL_FUTURE_WRITE");
297	}
298
299	oprint(f, "\n");
300	return 0;
301}
302
303int
304pmisc(struct file *f, const char *name)
305{
306	size_t i;
307	if (nl[0].n_value == 0) {
308		int n;
309		if ((n = KVM_NLIST(nl)) == -1)
310			errx(1, "Cannot list kernel symbols (%s)",
311			    KVM_GETERR());
312		else if (n != 0 && vflg) {
313			char buf[1024];
314			buf[0] = '\0';
315			for (struct nlist *l = nl; l->n_name != NULL; l++) {
316				if (l->n_value != 0)
317					continue;
318				strlcat(buf, ", ", sizeof(buf));
319				strlcat(buf, l->n_name, sizeof(buf));
320			}
321			warnx("Could not find %d symbols: %s", n, buf + 2);
322		}
323	}
324	for (i = 0; i < NL_MAX; i++)
325		if ((uintptr_t)f->f_ops == nl[i].n_value)
326			break;
327	switch (i) {
328	case NL_BPF:
329		return p_bpf(f);
330	case NL_MQUEUE:
331		return p_mqueue(f);
332	case NL_KQUEUE:
333		return p_kqueue(f);
334	case NL_RND:
335		printf("* random %p", f->f_data);
336		break;
337	case NL_SEM:
338		return p_sem(f);
339	case NL_TAP:
340		printf("* tap %lu", (unsigned long)(intptr_t)f->f_data);
341		break;
342	case NL_CRYPTO:
343		printf("* crypto %p", f->f_data);
344		break;
345	case NL_AUDIO:
346		return p_audio(f);
347	case NL_PAD:
348		printf("* pad %p", f->f_data);
349		break;
350	case NL_MEMFD:
351		return p_memfd(f);
352	case NL_MAX:
353		printf("* %s ops=%p %p", name, f->f_ops, f->f_data);
354		break;
355	default:
356		printf("* %s %p", nl[i].n_name, f->f_data);
357		break;
358	}
359	oprint(f, "\n");
360	return 0;
361}
362