procfs_vfsops.c revision 1.90 1 /* $NetBSD: procfs_vfsops.c,v 1.90 2014/03/23 15:21:16 hannken Exp $ */
2
3 /*
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Jan-Simon Pendry.
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 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)procfs_vfsops.c 8.7 (Berkeley) 5/10/95
35 */
36
37 /*
38 * Copyright (c) 1993 Jan-Simon Pendry
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Jan-Simon Pendry.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)procfs_vfsops.c 8.7 (Berkeley) 5/10/95
72 */
73
74 /*
75 * procfs VFS interface
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: procfs_vfsops.c,v 1.90 2014/03/23 15:21:16 hannken Exp $");
80
81 #if defined(_KERNEL_OPT)
82 #include "opt_compat_netbsd.h"
83 #endif
84
85 #include <sys/param.h>
86 #include <sys/time.h>
87 #include <sys/kernel.h>
88 #include <sys/systm.h>
89 #include <sys/sysctl.h>
90 #include <sys/proc.h>
91 #include <sys/buf.h>
92 #include <sys/syslog.h>
93 #include <sys/mount.h>
94 #include <sys/dirent.h>
95 #include <sys/signalvar.h>
96 #include <sys/vnode.h>
97 #include <sys/malloc.h>
98 #include <sys/kauth.h>
99 #include <sys/module.h>
100
101 #include <miscfs/genfs/genfs.h>
102
103 #include <miscfs/procfs/procfs.h>
104
105 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
106
107 MODULE(MODULE_CLASS_VFS, procfs, NULL);
108
109 VFS_PROTOS(procfs);
110
111 static struct sysctllog *procfs_sysctl_log;
112
113 static kauth_listener_t procfs_listener;
114
115 /*
116 * VFS Operations.
117 *
118 * mount system call
119 */
120 /* ARGSUSED */
121 int
122 procfs_mount(
123 struct mount *mp,
124 const char *path,
125 void *data,
126 size_t *data_len)
127 {
128 struct lwp *l = curlwp;
129 struct procfsmount *pmnt;
130 struct procfs_args *args = data;
131 int error;
132
133 if (UIO_MX & (UIO_MX-1)) {
134 log(LOG_ERR, "procfs: invalid directory entry size");
135 return (EINVAL);
136 }
137
138 if (mp->mnt_flag & MNT_GETARGS) {
139 if (*data_len < sizeof *args)
140 return EINVAL;
141
142 pmnt = VFSTOPROC(mp);
143 if (pmnt == NULL)
144 return EIO;
145 args->version = PROCFS_ARGSVERSION;
146 args->flags = pmnt->pmnt_flags;
147 *data_len = sizeof *args;
148 return 0;
149 }
150
151 if (mp->mnt_flag & MNT_UPDATE)
152 return (EOPNOTSUPP);
153
154 if (*data_len >= sizeof *args && args->version != PROCFS_ARGSVERSION)
155 return EINVAL;
156
157 pmnt = kmem_zalloc(sizeof(struct procfsmount), KM_SLEEP);
158
159 mp->mnt_stat.f_namemax = PROCFS_MAXNAMLEN;
160 mp->mnt_flag |= MNT_LOCAL;
161 mp->mnt_data = pmnt;
162 vfs_getnewfsid(mp);
163
164 error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
165 mp->mnt_op->vfs_name, mp, l);
166 pmnt->pmnt_exechook = exechook_establish(procfs_revoke_vnodes, mp);
167 if (*data_len >= sizeof *args)
168 pmnt->pmnt_flags = args->flags;
169 else
170 pmnt->pmnt_flags = 0;
171
172 mp->mnt_iflag |= IMNT_MPSAFE;
173 return error;
174 }
175
176 /*
177 * unmount system call
178 */
179 int
180 procfs_unmount(struct mount *mp, int mntflags)
181 {
182 int error;
183 int flags = 0;
184
185 if (mntflags & MNT_FORCE)
186 flags |= FORCECLOSE;
187
188 if ((error = vflush(mp, 0, flags)) != 0)
189 return (error);
190
191 exechook_disestablish(VFSTOPROC(mp)->pmnt_exechook);
192
193 kmem_free(mp->mnt_data, sizeof(struct procfsmount));
194 mp->mnt_data = NULL;
195
196 return 0;
197 }
198
199 int
200 procfs_root(struct mount *mp, struct vnode **vpp)
201 {
202 int error;
203
204 error = procfs_allocvp(mp, vpp, 0, PFSroot, -1, NULL);
205 if (error == 0) {
206 error = vn_lock(*vpp, LK_EXCLUSIVE);
207 if (error != 0) {
208 vrele(*vpp);
209 *vpp = NULL;
210 }
211 }
212
213 return error;
214 }
215
216 /* ARGSUSED */
217 int
218 procfs_start(struct mount *mp, int flags)
219 {
220
221 return (0);
222 }
223
224 /*
225 * Get file system statistics.
226 */
227 int
228 procfs_statvfs(struct mount *mp, struct statvfs *sbp)
229 {
230
231 genfs_statvfs(mp, sbp);
232
233 sbp->f_bsize = PAGE_SIZE;
234 sbp->f_frsize = PAGE_SIZE;
235 sbp->f_iosize = PAGE_SIZE;
236 sbp->f_blocks = 1;
237 sbp->f_files = maxproc; /* approx */
238 sbp->f_ffree = maxproc - nprocs; /* approx */
239 sbp->f_favail = maxproc - nprocs; /* approx */
240
241 return (0);
242 }
243
244 /*ARGSUSED*/
245 int
246 procfs_sync(
247 struct mount *mp,
248 int waitfor,
249 kauth_cred_t uc)
250 {
251
252 return (0);
253 }
254
255 /*ARGSUSED*/
256 int
257 procfs_vget(struct mount *mp, ino_t ino,
258 struct vnode **vpp)
259 {
260 return (EOPNOTSUPP);
261 }
262
263 void
264 procfs_init(void)
265 {
266 procfs_hashinit();
267 }
268
269 void
270 procfs_reinit(void)
271 {
272 procfs_hashreinit();
273 }
274
275 void
276 procfs_done(void)
277 {
278 procfs_hashdone();
279 }
280
281 extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
282
283 const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
284 &procfs_vnodeop_opv_desc,
285 NULL,
286 };
287
288 struct vfsops procfs_vfsops = {
289 .vfs_name = MOUNT_PROCFS,
290 .vfs_min_mount_data = sizeof (struct procfs_args),
291 .vfs_mount = procfs_mount,
292 .vfs_start = procfs_start,
293 .vfs_unmount = procfs_unmount,
294 .vfs_root = procfs_root,
295 .vfs_quotactl = (void *)eopnotsupp,
296 .vfs_statvfs = procfs_statvfs,
297 .vfs_sync = procfs_sync,
298 .vfs_vget = procfs_vget,
299 .vfs_fhtovp = (void *)eopnotsupp,
300 .vfs_vptofh = (void *)eopnotsupp,
301 .vfs_init = procfs_init,
302 .vfs_reinit = procfs_reinit,
303 .vfs_done = procfs_done,
304 .vfs_snapshot = (void *)eopnotsupp,
305 .vfs_extattrctl = vfs_stdextattrctl,
306 .vfs_suspendctl = (void *)eopnotsupp,
307 .vfs_renamelock_enter = genfs_renamelock_enter,
308 .vfs_renamelock_exit = genfs_renamelock_exit,
309 .vfs_fsync = (void *)eopnotsupp,
310 .vfs_opv_descs = procfs_vnodeopv_descs
311 };
312
313 static int
314 procfs_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
315 void *arg0, void *arg1, void *arg2, void *arg3)
316 {
317 struct proc *p;
318 struct pfsnode *pfs;
319 enum kauth_process_req req;
320 int result;
321
322 result = KAUTH_RESULT_DEFER;
323 p = arg0;
324 pfs = arg1;
325 req = (enum kauth_process_req)(unsigned long)arg2;
326
327 if (action != KAUTH_PROCESS_PROCFS)
328 return result;
329
330 /* Privileged; let secmodel handle that. */
331 if (req == KAUTH_REQ_PROCESS_PROCFS_CTL)
332 return result;
333
334 switch (pfs->pfs_type) {
335 case PFSregs:
336 case PFSfpregs:
337 case PFSmem:
338 if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
339 ISSET(p->p_flag, PK_SUGID))
340 break;
341
342 /*FALLTHROUGH*/
343 default:
344 result = KAUTH_RESULT_ALLOW;
345 break;
346 }
347
348 return result;
349 }
350
351
352 static int
353 procfs_modcmd(modcmd_t cmd, void *arg)
354 {
355 int error;
356
357 switch (cmd) {
358 case MODULE_CMD_INIT:
359 error = vfs_attach(&procfs_vfsops);
360 if (error != 0)
361 break;
362 sysctl_createv(&procfs_sysctl_log, 0, NULL, NULL,
363 CTLFLAG_PERMANENT,
364 CTLTYPE_NODE, "procfs",
365 SYSCTL_DESCR("Process file system"),
366 NULL, 0, NULL, 0,
367 CTL_VFS, 12, CTL_EOL);
368 /*
369 * XXX the "12" above could be dynamic, thereby eliminating
370 * one more instance of the "number to vfs" mapping problem,
371 * but "12" is the order as taken from sys/mount.h
372 */
373
374 procfs_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
375 procfs_listener_cb, NULL);
376
377 break;
378 case MODULE_CMD_FINI:
379 error = vfs_detach(&procfs_vfsops);
380 if (error != 0)
381 break;
382 sysctl_teardown(&procfs_sysctl_log);
383 kauth_unlisten_scope(procfs_listener);
384 break;
385 default:
386 error = ENOTTY;
387 break;
388 }
389
390 return (error);
391 }
392