]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/fdescfs/fdesc_vnops.c
fdesc_allocvp(): fix potential use after free
[FreeBSD/FreeBSD.git] / sys / fs / fdescfs / fdesc_vnops.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated 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  *      @(#)fdesc_vnops.c       8.9 (Berkeley) 1/21/94
35  *
36  * $FreeBSD$
37  */
38
39 /*
40  * /dev/fd Filesystem
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/capsicum.h>
46 #include <sys/conf.h>
47 #include <sys/dirent.h>
48 #include <sys/filedesc.h>
49 #include <sys/kernel.h> /* boottime */
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/malloc.h>
53 #include <sys/file.h>   /* Must come after sys/malloc.h */
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/proc.h>
57 #include <sys/stat.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/unistd.h>
60 #include <sys/vnode.h>
61
62 #include <fs/fdescfs/fdesc.h>
63
64 #define NFDCACHE 4
65 #define FD_NHASH(ix) \
66         (&fdhashtbl[(ix) & fdhash])
67 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
68 static u_long fdhash;
69
70 struct mtx fdesc_hashmtx;
71
72 static vop_getattr_t    fdesc_getattr;
73 static vop_lookup_t     fdesc_lookup;
74 static vop_open_t       fdesc_open;
75 static vop_pathconf_t   fdesc_pathconf;
76 static vop_readdir_t    fdesc_readdir;
77 static vop_readlink_t   fdesc_readlink;
78 static vop_reclaim_t    fdesc_reclaim;
79 static vop_setattr_t    fdesc_setattr;
80
81 static struct vop_vector fdesc_vnodeops = {
82         .vop_default =          &default_vnodeops,
83
84         .vop_access =           VOP_NULL,
85         .vop_getattr =          fdesc_getattr,
86         .vop_lookup =           fdesc_lookup,
87         .vop_open =             fdesc_open,
88         .vop_pathconf =         fdesc_pathconf,
89         .vop_readdir =          fdesc_readdir,
90         .vop_readlink =         fdesc_readlink,
91         .vop_reclaim =          fdesc_reclaim,
92         .vop_setattr =          fdesc_setattr,
93 };
94 VFS_VOP_VECTOR_REGISTER(fdesc_vnodeops);
95
96 static void fdesc_remove_entry(struct fdescnode *);
97
98 /*
99  * Initialise cache headers
100  */
101 int
102 fdesc_init(struct vfsconf *vfsp)
103 {
104
105         mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
106         fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
107         return (0);
108 }
109
110 /*
111  * Uninit ready for unload.
112  */
113 int
114 fdesc_uninit(struct vfsconf *vfsp)
115 {
116
117         hashdestroy(fdhashtbl, M_CACHE, fdhash);
118         mtx_destroy(&fdesc_hashmtx);
119         return (0);
120 }
121
122 /*
123  * Remove an entry from the hash if it exists.
124  */
125 static void
126 fdesc_remove_entry(struct fdescnode *fd)
127 {
128         struct fdhashhead *fc;
129         struct fdescnode *fd2;
130
131         fc = FD_NHASH(fd->fd_ix);
132         mtx_lock(&fdesc_hashmtx);
133         LIST_FOREACH(fd2, fc, fd_hash) {
134                 if (fd == fd2) {
135                         LIST_REMOVE(fd, fd_hash);
136                         break;
137                 }
138         }
139         mtx_unlock(&fdesc_hashmtx);
140 }
141
142 int
143 fdesc_allocvp(fdntype ftype, unsigned fd_fd, int ix, struct mount *mp,
144     struct vnode **vpp)
145 {
146         struct fdescmount *fmp;
147         struct fdhashhead *fc;
148         struct fdescnode *fd, *fd2;
149         struct vnode *vp, *vp2;
150         enum vgetstate vgs;
151         int error;
152
153         fc = FD_NHASH(ix);
154 loop:
155         mtx_lock(&fdesc_hashmtx);
156         /*
157          * If a forced unmount is progressing, we need to drop it. The flags are
158          * protected by the hashmtx.
159          */
160         fmp = mp->mnt_data;
161         if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
162                 mtx_unlock(&fdesc_hashmtx);
163                 return (-1);
164         }
165
166         LIST_FOREACH(fd, fc, fd_hash) {
167                 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
168                         /* Get reference to vnode in case it's being free'd */
169                         vp = fd->fd_vnode;
170                         vgs = vget_prep(vp);
171                         mtx_unlock(&fdesc_hashmtx);
172                         if (vget_finish(vp, LK_EXCLUSIVE, vgs) != 0)
173                                 goto loop;
174                         *vpp = vp;
175                         return (0);
176                 }
177         }
178         mtx_unlock(&fdesc_hashmtx);
179
180         fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
181
182         error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
183         if (error) {
184                 free(fd, M_TEMP);
185                 return (error);
186         }
187         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
188         vp->v_data = fd;
189         fd->fd_vnode = vp;
190         fd->fd_type = ftype;
191         fd->fd_fd = fd_fd;
192         fd->fd_ix = ix;
193         if (ftype == Fdesc && fmp->flags & FMNT_LINRDLNKF)
194                 vp->v_vflag |= VV_READLINK;
195         error = insmntque1(vp, mp);
196         if (error != 0) {
197                 vgone(vp);
198                 vput(vp);
199                 *vpp = NULLVP;
200                 return (error);
201         }
202
203         /* Make sure that someone didn't beat us when inserting the vnode. */
204         mtx_lock(&fdesc_hashmtx);
205         /*
206          * If a forced unmount is progressing, we need to drop it. The flags are
207          * protected by the hashmtx.
208          */
209         fmp = mp->mnt_data;
210         if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
211                 mtx_unlock(&fdesc_hashmtx);
212                 vgone(vp);
213                 vput(vp);
214                 *vpp = NULLVP;
215                 return (-1);
216         }
217
218         LIST_FOREACH(fd2, fc, fd_hash) {
219                 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
220                         /* Get reference to vnode in case it's being free'd */
221                         vp2 = fd2->fd_vnode;
222                         vgs = vget_prep(vp2);
223                         mtx_unlock(&fdesc_hashmtx);
224                         error = vget_finish(vp2, LK_EXCLUSIVE, vgs);
225                         /* Someone beat us, dec use count and wait for reclaim */
226                         vgone(vp);
227                         vput(vp);
228                         /* If we didn't get it, return no vnode. */
229                         if (error)
230                                 vp2 = NULLVP;
231                         *vpp = vp2;
232                         return (error);
233                 }
234         }
235
236         /* If we came here, we can insert it safely. */
237         LIST_INSERT_HEAD(fc, fd, fd_hash);
238         mtx_unlock(&fdesc_hashmtx);
239         vn_set_state(vp, VSTATE_CONSTRUCTED);
240         *vpp = vp;
241         return (0);
242 }
243
244 struct fdesc_get_ino_args {
245         fdntype ftype;
246         unsigned fd_fd;
247         int ix;
248         struct file *fp;
249         struct thread *td;
250         bool fdropped;
251 };
252
253 static int
254 fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
255     struct vnode **rvp)
256 {
257         struct fdesc_get_ino_args *a;
258         struct fdescmount *fdm;
259         struct vnode *vp;
260         int error;
261
262         a = arg;
263         fdm = VFSTOFDESC(mp);
264         if ((fdm->flags & FMNT_NODUP) != 0 && a->fp->f_type == DTYPE_VNODE) {
265                 vp = a->fp->f_vnode;
266                 vget(vp, lkflags | LK_RETRY);
267                 *rvp = vp;
268                 error = 0;
269         } else {
270                 error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp);
271         }
272         fdrop(a->fp, a->td);
273         a->fdropped = true;
274         return (error);
275 }
276
277 /*
278  * vp is the current namei directory
279  * ndp is the name to locate in that directory...
280  */
281 static int
282 fdesc_lookup(struct vop_lookup_args *ap)
283 {
284         struct vnode **vpp = ap->a_vpp;
285         struct vnode *dvp = ap->a_dvp;
286         struct componentname *cnp = ap->a_cnp;
287         char *pname = cnp->cn_nameptr;
288         struct thread *td = curthread;
289         struct file *fp;
290         struct fdesc_get_ino_args arg;
291         int nlen = cnp->cn_namelen;
292         u_int fd, fd1;
293         int error;
294         bool fdropped;
295         struct vnode *fvp;
296
297         if ((cnp->cn_flags & ISLASTCN) &&
298             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
299                 error = EROFS;
300                 goto bad;
301         }
302
303         if (cnp->cn_namelen == 1 && *pname == '.') {
304                 *vpp = dvp;
305                 VREF(dvp);
306                 return (0);
307         }
308
309         if (VTOFDESC(dvp)->fd_type != Froot) {
310                 error = ENOTDIR;
311                 goto bad;
312         }
313
314         fd = 0;
315         /* the only time a leading 0 is acceptable is if it's "0" */
316         if (*pname == '0' && nlen != 1) {
317                 error = ENOENT;
318                 goto bad;
319         }
320         while (nlen--) {
321                 if (*pname < '0' || *pname > '9') {
322                         error = ENOENT;
323                         goto bad;
324                 }
325                 fd1 = 10 * fd + *pname++ - '0';
326                 if (fd1 < fd) {
327                         error = ENOENT;
328                         goto bad;
329                 }
330                 fd = fd1;
331         }
332
333         /*
334          * No rights to check since 'fp' isn't actually used.
335          */
336         if ((error = fget(td, fd, &cap_no_rights, &fp)) != 0)
337                 goto bad;
338         fdropped = false;
339
340         /* Make sure we're not looking up the dvp itself. */
341         if (VTOFDESC(dvp)->fd_ix != FD_DESC + fd) {
342                 /*
343                  * Unlock our root node (dvp) when doing this, since we might
344                  * deadlock since the vnode might be locked by another thread
345                  * and the root vnode lock will be obtained afterwards (in case
346                  * we're looking up the fd of the root vnode), which will be the
347                  * opposite lock order. Vhold the root vnode first so we don't
348                  * lose it.
349                  */
350                 arg.ftype = Fdesc;
351                 arg.fd_fd = fd;
352                 arg.ix = FD_DESC + fd;
353                 arg.fp = fp;
354                 arg.td = td;
355                 arg.fdropped = fdropped;
356                 error = vn_vget_ino_gen(dvp, fdesc_get_ino_alloc, &arg,
357                     LK_EXCLUSIVE, &fvp);
358                 fdropped = arg.fdropped;
359         }
360
361         if (!fdropped) {
362                 /*
363                  * In case we're holding the last reference to the file, the dvp
364                  * will be re-acquired.
365                  */
366                 vhold(dvp);
367                 VOP_UNLOCK(dvp);
368                 fdrop(fp, td);
369                 fdropped = true;
370
371                 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
372                 vdrop(dvp);
373                 fvp = dvp;
374                 if (error == 0 && VN_IS_DOOMED(dvp))
375                         error = ENOENT;
376         }
377
378         if (error)
379                 goto bad;
380         *vpp = fvp;
381         return (0);
382
383 bad:
384         *vpp = NULL;
385         return (error);
386 }
387
388 static int
389 fdesc_open(struct vop_open_args *ap)
390 {
391         struct vnode *vp = ap->a_vp;
392
393         if (VTOFDESC(vp)->fd_type == Froot)
394                 return (0);
395
396         /*
397          * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file
398          * descriptor being sought for duplication. The error return ensures
399          * that the vnode for this device will be released by vn_open. Open
400          * will detect this special error and take the actions in dupfdopen.
401          * Other callers of vn_open or VOP_OPEN will simply report the
402          * error.
403          */
404         ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd;       /* XXX */
405         return (ENODEV);
406 }
407
408 static int
409 fdesc_pathconf(struct vop_pathconf_args *ap)
410 {
411         struct vnode *vp = ap->a_vp;
412         int error;
413
414         switch (ap->a_name) {
415         case _PC_NAME_MAX:
416                 *ap->a_retval = NAME_MAX;
417                 return (0);
418         case _PC_LINK_MAX:
419                 if (VTOFDESC(vp)->fd_type == Froot)
420                         *ap->a_retval = 2;
421                 else
422                         *ap->a_retval = 1;
423                 return (0);
424         default:
425                 if (VTOFDESC(vp)->fd_type == Froot)
426                         return (vop_stdpathconf(ap));
427                 vref(vp);
428                 VOP_UNLOCK(vp);
429                 error = kern_fpathconf(curthread, VTOFDESC(vp)->fd_fd,
430                     ap->a_name, ap->a_retval);
431                 vn_lock(vp, LK_SHARED | LK_RETRY);
432                 vunref(vp);
433                 return (error);
434         }
435 }
436
437 static int
438 fdesc_getattr(struct vop_getattr_args *ap)
439 {
440         struct vnode *vp = ap->a_vp;
441         struct vattr *vap = ap->a_vap;
442         struct timeval boottime;
443
444         getboottime(&boottime);
445         vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
446         vap->va_fileid = VTOFDESC(vp)->fd_ix;
447         vap->va_uid = 0;
448         vap->va_gid = 0;
449         vap->va_blocksize = DEV_BSIZE;
450         vap->va_atime.tv_sec = boottime.tv_sec;
451         vap->va_atime.tv_nsec = 0;
452         vap->va_mtime = vap->va_atime;
453         vap->va_ctime = vap->va_mtime;
454         vap->va_gen = 0;
455         vap->va_flags = 0;
456         vap->va_bytes = 0;
457         vap->va_filerev = 0;
458
459         switch (VTOFDESC(vp)->fd_type) {
460         case Froot:
461                 vap->va_type = VDIR;
462                 vap->va_nlink = 2;
463                 vap->va_size = DEV_BSIZE;
464                 vap->va_rdev = NODEV;
465                 break;
466
467         case Fdesc:
468                 vap->va_type = (vp->v_vflag & VV_READLINK) == 0 ? VCHR : VLNK;
469                 vap->va_nlink = 1;
470                 vap->va_size = 0;
471                 vap->va_rdev = makedev(0, vap->va_fileid);
472                 break;
473
474         default:
475                 panic("fdesc_getattr");
476                 break;
477         }
478
479         vp->v_type = vap->va_type;
480         return (0);
481 }
482
483 static int
484 fdesc_setattr(struct vop_setattr_args *ap)
485 {
486         struct vattr *vap = ap->a_vap;
487         struct vnode *vp;
488         struct mount *mp;
489         struct file *fp;
490         struct thread *td = curthread;
491         cap_rights_t rights;
492         unsigned fd;
493         int error;
494
495         /*
496          * Can't mess with the root vnode
497          */
498         if (VTOFDESC(ap->a_vp)->fd_type == Froot)
499                 return (EACCES);
500
501         fd = VTOFDESC(ap->a_vp)->fd_fd;
502
503         /*
504          * Allow setattr where there is an underlying vnode.
505          * For O_PATH descriptors, disallow truncate.
506          */
507         if (vap->va_size != VNOVAL) {
508                 error = getvnode(td, fd,
509                     cap_rights_init_one(&rights, CAP_EXTATTR_SET), &fp);
510         } else {
511                 error = getvnode_path(td, fd,
512                     cap_rights_init_one(&rights, CAP_EXTATTR_SET), &fp);
513         }
514         if (error) {
515                 /*
516                  * getvnode() returns EINVAL if the file descriptor is not
517                  * backed by a vnode.  Silently drop all changes except
518                  * chflags(2) in this case.
519                  */
520                 if (error == EINVAL) {
521                         if (vap->va_flags != VNOVAL)
522                                 error = EOPNOTSUPP;
523                         else
524                                 error = 0;
525                 }
526                 return (error);
527         }
528         vp = fp->f_vnode;
529         if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) == 0) {
530                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
531                 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred);
532                 VOP_UNLOCK(vp);
533                 vn_finished_write(mp);
534         }
535         fdrop(fp, td);
536         return (error);
537 }
538
539 #define UIO_MX _GENERIC_DIRLEN(10) /* number of symbols in INT_MAX printout */
540
541 static int
542 fdesc_readdir(struct vop_readdir_args *ap)
543 {
544         struct fdescmount *fmp;
545         struct uio *uio = ap->a_uio;
546         struct filedesc *fdp;
547         struct dirent d;
548         struct dirent *dp = &d;
549         int error, i, off, fcnt;
550
551         if (VTOFDESC(ap->a_vp)->fd_type != Froot)
552                 panic("fdesc_readdir: not dir");
553
554         fmp = VFSTOFDESC(ap->a_vp->v_mount);
555         if (ap->a_ncookies != NULL)
556                 *ap->a_ncookies = 0;
557
558         off = (int)uio->uio_offset;
559         if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
560             uio->uio_resid < UIO_MX)
561                 return (EINVAL);
562         i = (u_int)off / UIO_MX;
563         fdp = uio->uio_td->td_proc->p_fd;
564         error = 0;
565
566         fcnt = i - 2;           /* The first two nodes are `.' and `..' */
567
568         FILEDESC_SLOCK(fdp);
569         while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
570                 bzero((caddr_t)dp, UIO_MX);
571                 switch (i) {
572                 case 0: /* `.' */
573                 case 1: /* `..' */
574                         dp->d_fileno = i + FD_ROOT;
575                         dp->d_namlen = i + 1;
576                         dp->d_reclen = UIO_MX;
577                         bcopy("..", dp->d_name, dp->d_namlen);
578                         dp->d_type = DT_DIR;
579                         dirent_terminate(dp);
580                         break;
581                 default:
582                         if (fdp->fd_ofiles[fcnt].fde_file == NULL)
583                                 break;
584                         dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
585                         dp->d_reclen = UIO_MX;
586                         dp->d_type = (fmp->flags & FMNT_LINRDLNKF) == 0 ?
587                             DT_CHR : DT_LNK;
588                         dp->d_fileno = i + FD_DESC;
589                         dirent_terminate(dp);
590                         break;
591                 }
592                 /* NOTE: d_off is the offset of the *next* entry. */
593                 dp->d_off = UIO_MX * (i + 1);
594                 if (dp->d_namlen != 0) {
595                         /*
596                          * And ship to userland
597                          */
598                         FILEDESC_SUNLOCK(fdp);
599                         error = uiomove(dp, UIO_MX, uio);
600                         if (error)
601                                 goto done;
602                         FILEDESC_SLOCK(fdp);
603                 }
604                 i++;
605                 fcnt++;
606         }
607         FILEDESC_SUNLOCK(fdp);
608
609 done:
610         uio->uio_offset = i * UIO_MX;
611         return (error);
612 }
613
614 static int
615 fdesc_reclaim(struct vop_reclaim_args *ap)
616 {
617         struct vnode *vp;
618         struct fdescnode *fd;
619
620         vp = ap->a_vp;
621         fd = VTOFDESC(vp);
622         fdesc_remove_entry(fd);
623         free(vp->v_data, M_TEMP);
624         vp->v_data = NULL;
625         return (0);
626 }
627
628 static int
629 fdesc_readlink(struct vop_readlink_args *va)
630 {
631         struct vnode *vp, *vn;
632         struct thread *td;
633         struct uio *uio;
634         struct file *fp;
635         char *freepath, *fullpath;
636         size_t pathlen;
637         int lockflags, fd_fd;
638         int error;
639
640         freepath = NULL;
641         vn = va->a_vp;
642         if (VTOFDESC(vn)->fd_type != Fdesc)
643                 panic("fdesc_readlink: not fdescfs link");
644         fd_fd = ((struct fdescnode *)vn->v_data)->fd_fd;
645         lockflags = VOP_ISLOCKED(vn);
646         VOP_UNLOCK(vn);
647
648         td = curthread;
649         error = fget_cap(td, fd_fd, &cap_no_rights, &fp, NULL);
650         if (error != 0)
651                 goto out;
652
653         switch (fp->f_type) {
654         case DTYPE_VNODE:
655                 vp = fp->f_vnode;
656                 error = vn_fullpath(vp, &fullpath, &freepath);
657                 break;
658         default:
659                 fullpath = "anon_inode:[unknown]";
660                 break;
661         }
662         if (error == 0) {
663                 uio = va->a_uio;
664                 pathlen = strlen(fullpath);
665                 error = uiomove(fullpath, pathlen, uio);
666         }
667         if (freepath != NULL)
668                 free(freepath, M_TEMP);
669         fdrop(fp, td);
670
671 out:
672         vn_lock(vn, lockflags | LK_RETRY);
673         return (error);
674 }