]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/uipc_shm.c
hv_kbd: Fix build with EVDEV_SUPPORT kernel option disabled.
[FreeBSD/FreeBSD.git] / sys / kern / uipc_shm.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006, 2011, 2016-2017 Robert N. M. Watson
5  * Copyright 2020 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by BAE Systems, the University of
9  * Cambridge Computer Laboratory, and Memorial University under DARPA/AFRL
10  * contract FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent
11  * Computing (TC) research program.
12  *
13  * Portions of this software were developed by Konstantin Belousov
14  * under sponsorship from the FreeBSD Foundation.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37
38 /*
39  * Support for shared swap-backed anonymous memory objects via
40  * shm_open(2), shm_rename(2), and shm_unlink(2).
41  * While most of the implementation is here, vm_mmap.c contains
42  * mapping logic changes.
43  *
44  * posixshmcontrol(1) allows users to inspect the state of the memory
45  * objects.  Per-uid swap resource limit controls total amount of
46  * memory that user can consume for anonymous objects, including
47  * shared.
48  */
49
50 #include <sys/cdefs.h>
51 #include "opt_capsicum.h"
52 #include "opt_ktrace.h"
53
54 #include <sys/param.h>
55 #include <sys/capsicum.h>
56 #include <sys/conf.h>
57 #include <sys/fcntl.h>
58 #include <sys/file.h>
59 #include <sys/filedesc.h>
60 #include <sys/filio.h>
61 #include <sys/fnv_hash.h>
62 #include <sys/kernel.h>
63 #include <sys/limits.h>
64 #include <sys/uio.h>
65 #include <sys/signal.h>
66 #include <sys/jail.h>
67 #include <sys/ktrace.h>
68 #include <sys/lock.h>
69 #include <sys/malloc.h>
70 #include <sys/mman.h>
71 #include <sys/mutex.h>
72 #include <sys/priv.h>
73 #include <sys/proc.h>
74 #include <sys/refcount.h>
75 #include <sys/resourcevar.h>
76 #include <sys/rwlock.h>
77 #include <sys/sbuf.h>
78 #include <sys/stat.h>
79 #include <sys/syscallsubr.h>
80 #include <sys/sysctl.h>
81 #include <sys/sysproto.h>
82 #include <sys/systm.h>
83 #include <sys/sx.h>
84 #include <sys/time.h>
85 #include <sys/vmmeter.h>
86 #include <sys/vnode.h>
87 #include <sys/unistd.h>
88 #include <sys/user.h>
89
90 #include <security/audit/audit.h>
91 #include <security/mac/mac_framework.h>
92
93 #include <vm/vm.h>
94 #include <vm/vm_param.h>
95 #include <vm/pmap.h>
96 #include <vm/vm_extern.h>
97 #include <vm/vm_map.h>
98 #include <vm/vm_kern.h>
99 #include <vm/vm_object.h>
100 #include <vm/vm_page.h>
101 #include <vm/vm_pageout.h>
102 #include <vm/vm_pager.h>
103 #include <vm/swap_pager.h>
104
105 struct shm_mapping {
106         char            *sm_path;
107         Fnv32_t         sm_fnv;
108         struct shmfd    *sm_shmfd;
109         LIST_ENTRY(shm_mapping) sm_link;
110 };
111
112 static MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor");
113 static LIST_HEAD(, shm_mapping) *shm_dictionary;
114 static struct sx shm_dict_lock;
115 static struct mtx shm_timestamp_lock;
116 static u_long shm_hash;
117 static struct unrhdr64 shm_ino_unr;
118 static dev_t shm_dev_ino;
119
120 #define SHM_HASH(fnv)   (&shm_dictionary[(fnv) & shm_hash])
121
122 static void     shm_init(void *arg);
123 static void     shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd);
124 static struct shmfd *shm_lookup(char *path, Fnv32_t fnv);
125 static int      shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
126 static void     shm_doremove(struct shm_mapping *map);
127 static int      shm_dotruncate_cookie(struct shmfd *shmfd, off_t length,
128     void *rl_cookie);
129 static int      shm_dotruncate_locked(struct shmfd *shmfd, off_t length,
130     void *rl_cookie);
131 static int      shm_copyin_path(struct thread *td, const char *userpath_in,
132     char **path_out);
133
134 static fo_rdwr_t        shm_read;
135 static fo_rdwr_t        shm_write;
136 static fo_truncate_t    shm_truncate;
137 static fo_ioctl_t       shm_ioctl;
138 static fo_stat_t        shm_stat;
139 static fo_close_t       shm_close;
140 static fo_chmod_t       shm_chmod;
141 static fo_chown_t       shm_chown;
142 static fo_seek_t        shm_seek;
143 static fo_fill_kinfo_t  shm_fill_kinfo;
144 static fo_mmap_t        shm_mmap;
145 static fo_get_seals_t   shm_get_seals;
146 static fo_add_seals_t   shm_add_seals;
147 static fo_fallocate_t   shm_fallocate;
148
149 /* File descriptor operations. */
150 struct fileops shm_ops = {
151         .fo_read = shm_read,
152         .fo_write = shm_write,
153         .fo_truncate = shm_truncate,
154         .fo_ioctl = shm_ioctl,
155         .fo_poll = invfo_poll,
156         .fo_kqfilter = invfo_kqfilter,
157         .fo_stat = shm_stat,
158         .fo_close = shm_close,
159         .fo_chmod = shm_chmod,
160         .fo_chown = shm_chown,
161         .fo_sendfile = vn_sendfile,
162         .fo_seek = shm_seek,
163         .fo_fill_kinfo = shm_fill_kinfo,
164         .fo_mmap = shm_mmap,
165         .fo_get_seals = shm_get_seals,
166         .fo_add_seals = shm_add_seals,
167         .fo_fallocate = shm_fallocate,
168         .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE,
169 };
170
171 FEATURE(posix_shm, "POSIX shared memory");
172
173 static SYSCTL_NODE(_vm, OID_AUTO, largepages, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
174     "");
175
176 static int largepage_reclaim_tries = 1;
177 SYSCTL_INT(_vm_largepages, OID_AUTO, reclaim_tries,
178     CTLFLAG_RWTUN, &largepage_reclaim_tries, 0,
179     "Number of contig reclaims before giving up for default alloc policy");
180
181 #define shm_rangelock_unlock(shmfd, cookie)                             \
182         rangelock_unlock(&(shmfd)->shm_rl, (cookie), &(shmfd)->shm_mtx)
183 #define shm_rangelock_rlock(shmfd, start, end)                          \
184         rangelock_rlock(&(shmfd)->shm_rl, (start), (end), &(shmfd)->shm_mtx)
185 #define shm_rangelock_tryrlock(shmfd, start, end)                       \
186         rangelock_tryrlock(&(shmfd)->shm_rl, (start), (end), &(shmfd)->shm_mtx)
187 #define shm_rangelock_wlock(shmfd, start, end)                          \
188         rangelock_wlock(&(shmfd)->shm_rl, (start), (end), &(shmfd)->shm_mtx)
189
190 static int
191 uiomove_object_page(vm_object_t obj, size_t len, struct uio *uio)
192 {
193         vm_page_t m;
194         vm_pindex_t idx;
195         size_t tlen;
196         int error, offset, rv;
197
198         idx = OFF_TO_IDX(uio->uio_offset);
199         offset = uio->uio_offset & PAGE_MASK;
200         tlen = MIN(PAGE_SIZE - offset, len);
201
202         rv = vm_page_grab_valid_unlocked(&m, obj, idx,
203             VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY | VM_ALLOC_NOCREAT);
204         if (rv == VM_PAGER_OK)
205                 goto found;
206
207         /*
208          * Read I/O without either a corresponding resident page or swap
209          * page: use zero_region.  This is intended to avoid instantiating
210          * pages on read from a sparse region.
211          */
212         VM_OBJECT_WLOCK(obj);
213         m = vm_page_lookup(obj, idx);
214         if (uio->uio_rw == UIO_READ && m == NULL &&
215             !vm_pager_has_page(obj, idx, NULL, NULL)) {
216                 VM_OBJECT_WUNLOCK(obj);
217                 return (uiomove(__DECONST(void *, zero_region), tlen, uio));
218         }
219
220         /*
221          * Although the tmpfs vnode lock is held here, it is
222          * nonetheless safe to sleep waiting for a free page.  The
223          * pageout daemon does not need to acquire the tmpfs vnode
224          * lock to page out tobj's pages because tobj is a OBJT_SWAP
225          * type object.
226          */
227         rv = vm_page_grab_valid(&m, obj, idx,
228             VM_ALLOC_NORMAL | VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY);
229         if (rv != VM_PAGER_OK) {
230                 VM_OBJECT_WUNLOCK(obj);
231                 if (bootverbose) {
232                         printf("uiomove_object: vm_obj %p idx %jd "
233                             "pager error %d\n", obj, idx, rv);
234                 }
235                 return (rv == VM_PAGER_AGAIN ? ENOSPC : EIO);
236         }
237         VM_OBJECT_WUNLOCK(obj);
238
239 found:
240         error = uiomove_fromphys(&m, offset, tlen, uio);
241         if (uio->uio_rw == UIO_WRITE && error == 0)
242                 vm_page_set_dirty(m);
243         vm_page_activate(m);
244         vm_page_sunbusy(m);
245
246         return (error);
247 }
248
249 int
250 uiomove_object(vm_object_t obj, off_t obj_size, struct uio *uio)
251 {
252         ssize_t resid;
253         size_t len;
254         int error;
255
256         error = 0;
257         while ((resid = uio->uio_resid) > 0) {
258                 if (obj_size <= uio->uio_offset)
259                         break;
260                 len = MIN(obj_size - uio->uio_offset, resid);
261                 if (len == 0)
262                         break;
263                 error = uiomove_object_page(obj, len, uio);
264                 if (error != 0 || resid == uio->uio_resid)
265                         break;
266         }
267         return (error);
268 }
269
270 static u_long count_largepages[MAXPAGESIZES];
271
272 static int
273 shm_largepage_phys_populate(vm_object_t object, vm_pindex_t pidx,
274     int fault_type, vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
275 {
276         vm_page_t m __diagused;
277         int psind;
278
279         psind = object->un_pager.phys.data_val;
280         if (psind == 0 || pidx >= object->size)
281                 return (VM_PAGER_FAIL);
282         *first = rounddown2(pidx, pagesizes[psind] / PAGE_SIZE);
283
284         /*
285          * We only busy the first page in the superpage run.  It is
286          * useless to busy whole run since we only remove full
287          * superpage, and it takes too long to busy e.g. 512 * 512 ==
288          * 262144 pages constituing 1G amd64 superage.
289          */
290         m = vm_page_grab(object, *first, VM_ALLOC_NORMAL | VM_ALLOC_NOCREAT);
291         MPASS(m != NULL);
292
293         *last = *first + atop(pagesizes[psind]) - 1;
294         return (VM_PAGER_OK);
295 }
296
297 static boolean_t
298 shm_largepage_phys_haspage(vm_object_t object, vm_pindex_t pindex,
299     int *before, int *after)
300 {
301         int psind;
302
303         psind = object->un_pager.phys.data_val;
304         if (psind == 0 || pindex >= object->size)
305                 return (FALSE);
306         if (before != NULL) {
307                 *before = pindex - rounddown2(pindex, pagesizes[psind] /
308                     PAGE_SIZE);
309         }
310         if (after != NULL) {
311                 *after = roundup2(pindex, pagesizes[psind] / PAGE_SIZE) -
312                     pindex;
313         }
314         return (TRUE);
315 }
316
317 static void
318 shm_largepage_phys_ctor(vm_object_t object, vm_prot_t prot,
319     vm_ooffset_t foff, struct ucred *cred)
320 {
321 }
322
323 static void
324 shm_largepage_phys_dtor(vm_object_t object)
325 {
326         int psind;
327
328         psind = object->un_pager.phys.data_val;
329         if (psind != 0) {
330                 atomic_subtract_long(&count_largepages[psind],
331                     object->size / (pagesizes[psind] / PAGE_SIZE));
332                 vm_wire_sub(object->size);
333         } else {
334                 KASSERT(object->size == 0,
335                     ("largepage phys obj %p not initialized bit size %#jx > 0",
336                     object, (uintmax_t)object->size));
337         }
338 }
339
340 static const struct phys_pager_ops shm_largepage_phys_ops = {
341         .phys_pg_populate =     shm_largepage_phys_populate,
342         .phys_pg_haspage =      shm_largepage_phys_haspage,
343         .phys_pg_ctor =         shm_largepage_phys_ctor,
344         .phys_pg_dtor =         shm_largepage_phys_dtor,
345 };
346
347 bool
348 shm_largepage(struct shmfd *shmfd)
349 {
350         return (shmfd->shm_object->type == OBJT_PHYS);
351 }
352
353 static void
354 shm_pager_freespace(vm_object_t obj, vm_pindex_t start, vm_size_t size)
355 {
356         struct shmfd *shm;
357         vm_size_t c;
358
359         swap_pager_freespace(obj, start, size, &c);
360         if (c == 0)
361                 return;
362
363         shm = obj->un_pager.swp.swp_priv;
364         if (shm == NULL)
365                 return;
366         KASSERT(shm->shm_pages >= c,
367             ("shm %p pages %jd free %jd", shm,
368             (uintmax_t)shm->shm_pages, (uintmax_t)c));
369         shm->shm_pages -= c;
370 }
371
372 static void
373 shm_page_inserted(vm_object_t obj, vm_page_t m)
374 {
375         struct shmfd *shm;
376
377         shm = obj->un_pager.swp.swp_priv;
378         if (shm == NULL)
379                 return;
380         if (!vm_pager_has_page(obj, m->pindex, NULL, NULL))
381                 shm->shm_pages += 1;
382 }
383
384 static void
385 shm_page_removed(vm_object_t obj, vm_page_t m)
386 {
387         struct shmfd *shm;
388
389         shm = obj->un_pager.swp.swp_priv;
390         if (shm == NULL)
391                 return;
392         if (!vm_pager_has_page(obj, m->pindex, NULL, NULL)) {
393                 KASSERT(shm->shm_pages >= 1,
394                     ("shm %p pages %jd free 1", shm,
395                     (uintmax_t)shm->shm_pages));
396                 shm->shm_pages -= 1;
397         }
398 }
399
400 static struct pagerops shm_swap_pager_ops = {
401         .pgo_kvme_type = KVME_TYPE_SWAP,
402         .pgo_freespace = shm_pager_freespace,
403         .pgo_page_inserted = shm_page_inserted,
404         .pgo_page_removed = shm_page_removed,
405 };
406 static int shmfd_pager_type = -1;
407
408 static int
409 shm_seek(struct file *fp, off_t offset, int whence, struct thread *td)
410 {
411         struct shmfd *shmfd;
412         off_t foffset;
413         int error;
414
415         shmfd = fp->f_data;
416         foffset = foffset_lock(fp, 0);
417         error = 0;
418         switch (whence) {
419         case L_INCR:
420                 if (foffset < 0 ||
421                     (offset > 0 && foffset > OFF_MAX - offset)) {
422                         error = EOVERFLOW;
423                         break;
424                 }
425                 offset += foffset;
426                 break;
427         case L_XTND:
428                 if (offset > 0 && shmfd->shm_size > OFF_MAX - offset) {
429                         error = EOVERFLOW;
430                         break;
431                 }
432                 offset += shmfd->shm_size;
433                 break;
434         case L_SET:
435                 break;
436         default:
437                 error = EINVAL;
438         }
439         if (error == 0) {
440                 if (offset < 0 || offset > shmfd->shm_size)
441                         error = EINVAL;
442                 else
443                         td->td_uretoff.tdu_off = offset;
444         }
445         foffset_unlock(fp, offset, error != 0 ? FOF_NOUPDATE : 0);
446         return (error);
447 }
448
449 static int
450 shm_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
451     int flags, struct thread *td)
452 {
453         struct shmfd *shmfd;
454         void *rl_cookie;
455         int error;
456
457         shmfd = fp->f_data;
458 #ifdef MAC
459         error = mac_posixshm_check_read(active_cred, fp->f_cred, shmfd);
460         if (error)
461                 return (error);
462 #endif
463         foffset_lock_uio(fp, uio, flags);
464         rl_cookie = shm_rangelock_rlock(shmfd, uio->uio_offset,
465             uio->uio_offset + uio->uio_resid);
466         error = uiomove_object(shmfd->shm_object, shmfd->shm_size, uio);
467         shm_rangelock_unlock(shmfd, rl_cookie);
468         foffset_unlock_uio(fp, uio, flags);
469         return (error);
470 }
471
472 static int
473 shm_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
474     int flags, struct thread *td)
475 {
476         struct shmfd *shmfd;
477         void *rl_cookie;
478         int error;
479         off_t size;
480
481         shmfd = fp->f_data;
482 #ifdef MAC
483         error = mac_posixshm_check_write(active_cred, fp->f_cred, shmfd);
484         if (error)
485                 return (error);
486 #endif
487         if (shm_largepage(shmfd) && shmfd->shm_lp_psind == 0)
488                 return (EINVAL);
489         foffset_lock_uio(fp, uio, flags);
490         if (uio->uio_resid > OFF_MAX - uio->uio_offset) {
491                 /*
492                  * Overflow is only an error if we're supposed to expand on
493                  * write.  Otherwise, we'll just truncate the write to the
494                  * size of the file, which can only grow up to OFF_MAX.
495                  */
496                 if ((shmfd->shm_flags & SHM_GROW_ON_WRITE) != 0) {
497                         foffset_unlock_uio(fp, uio, flags);
498                         return (EFBIG);
499                 }
500
501                 size = shmfd->shm_size;
502         } else {
503                 size = uio->uio_offset + uio->uio_resid;
504         }
505         if ((flags & FOF_OFFSET) == 0)
506                 rl_cookie = shm_rangelock_wlock(shmfd, 0, OFF_MAX);
507         else
508                 rl_cookie = shm_rangelock_wlock(shmfd, uio->uio_offset, size);
509         if ((shmfd->shm_seals & F_SEAL_WRITE) != 0) {
510                 error = EPERM;
511         } else {
512                 error = 0;
513                 if ((shmfd->shm_flags & SHM_GROW_ON_WRITE) != 0 &&
514                     size > shmfd->shm_size) {
515                         error = shm_dotruncate_cookie(shmfd, size, rl_cookie);
516                 }
517                 if (error == 0)
518                         error = uiomove_object(shmfd->shm_object,
519                             shmfd->shm_size, uio);
520         }
521         shm_rangelock_unlock(shmfd, rl_cookie);
522         foffset_unlock_uio(fp, uio, flags);
523         return (error);
524 }
525
526 static int
527 shm_truncate(struct file *fp, off_t length, struct ucred *active_cred,
528     struct thread *td)
529 {
530         struct shmfd *shmfd;
531 #ifdef MAC
532         int error;
533 #endif
534
535         shmfd = fp->f_data;
536 #ifdef MAC
537         error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd);
538         if (error)
539                 return (error);
540 #endif
541         return (shm_dotruncate(shmfd, length));
542 }
543
544 int
545 shm_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
546     struct thread *td)
547 {
548         struct shmfd *shmfd;
549         struct shm_largepage_conf *conf;
550         void *rl_cookie;
551
552         shmfd = fp->f_data;
553         switch (com) {
554         case FIONBIO:
555         case FIOASYNC:
556                 /*
557                  * Allow fcntl(fd, F_SETFL, O_NONBLOCK) to work,
558                  * just like it would on an unlinked regular file
559                  */
560                 return (0);
561         case FIOSSHMLPGCNF:
562                 if (!shm_largepage(shmfd))
563                         return (ENOTTY);
564                 conf = data;
565                 if (shmfd->shm_lp_psind != 0 &&
566                     conf->psind != shmfd->shm_lp_psind)
567                         return (EINVAL);
568                 if (conf->psind <= 0 || conf->psind >= MAXPAGESIZES ||
569                     pagesizes[conf->psind] == 0)
570                         return (EINVAL);
571                 if (conf->alloc_policy != SHM_LARGEPAGE_ALLOC_DEFAULT &&
572                     conf->alloc_policy != SHM_LARGEPAGE_ALLOC_NOWAIT &&
573                     conf->alloc_policy != SHM_LARGEPAGE_ALLOC_HARD)
574                         return (EINVAL);
575
576                 rl_cookie = shm_rangelock_wlock(shmfd, 0, OFF_MAX);
577                 shmfd->shm_lp_psind = conf->psind;
578                 shmfd->shm_lp_alloc_policy = conf->alloc_policy;
579                 shmfd->shm_object->un_pager.phys.data_val = conf->psind;
580                 shm_rangelock_unlock(shmfd, rl_cookie);
581                 return (0);
582         case FIOGSHMLPGCNF:
583                 if (!shm_largepage(shmfd))
584                         return (ENOTTY);
585                 conf = data;
586                 rl_cookie = shm_rangelock_rlock(shmfd, 0, OFF_MAX);
587                 conf->psind = shmfd->shm_lp_psind;
588                 conf->alloc_policy = shmfd->shm_lp_alloc_policy;
589                 shm_rangelock_unlock(shmfd, rl_cookie);
590                 return (0);
591         default:
592                 return (ENOTTY);
593         }
594 }
595
596 static int
597 shm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
598     struct thread *td)
599 {
600         struct shmfd *shmfd;
601 #ifdef MAC
602         int error;
603 #endif
604
605         shmfd = fp->f_data;
606
607 #ifdef MAC
608         error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd);
609         if (error)
610                 return (error);
611 #endif
612
613         /*
614          * Attempt to return sanish values for fstat() on a memory file
615          * descriptor.
616          */
617         bzero(sb, sizeof(*sb));
618         sb->st_blksize = PAGE_SIZE;
619         sb->st_size = shmfd->shm_size;
620         mtx_lock(&shm_timestamp_lock);
621         sb->st_atim = shmfd->shm_atime;
622         sb->st_ctim = shmfd->shm_ctime;
623         sb->st_mtim = shmfd->shm_mtime;
624         sb->st_birthtim = shmfd->shm_birthtime;
625         sb->st_mode = S_IFREG | shmfd->shm_mode;                /* XXX */
626         sb->st_uid = shmfd->shm_uid;
627         sb->st_gid = shmfd->shm_gid;
628         mtx_unlock(&shm_timestamp_lock);
629         sb->st_dev = shm_dev_ino;
630         sb->st_ino = shmfd->shm_ino;
631         sb->st_nlink = shmfd->shm_object->ref_count;
632         if (shm_largepage(shmfd)) {
633                 sb->st_blocks = shmfd->shm_object->size /
634                     (pagesizes[shmfd->shm_lp_psind] >> PAGE_SHIFT);
635         } else {
636                 sb->st_blocks = shmfd->shm_pages;
637         }
638
639         return (0);
640 }
641
642 static int
643 shm_close(struct file *fp, struct thread *td)
644 {
645         struct shmfd *shmfd;
646
647         shmfd = fp->f_data;
648         fp->f_data = NULL;
649         shm_drop(shmfd);
650
651         return (0);
652 }
653
654 static int
655 shm_copyin_path(struct thread *td, const char *userpath_in, char **path_out) {
656         int error;
657         char *path;
658         const char *pr_path;
659         size_t pr_pathlen;
660
661         path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK);
662         pr_path = td->td_ucred->cr_prison->pr_path;
663
664         /* Construct a full pathname for jailed callers. */
665         pr_pathlen = strcmp(pr_path, "/") ==
666             0 ? 0 : strlcpy(path, pr_path, MAXPATHLEN);
667         error = copyinstr(userpath_in, path + pr_pathlen,
668             MAXPATHLEN - pr_pathlen, NULL);
669         if (error != 0)
670                 goto out;
671
672 #ifdef KTRACE
673         if (KTRPOINT(curthread, KTR_NAMEI))
674                 ktrnamei(path);
675 #endif
676
677         /* Require paths to start with a '/' character. */
678         if (path[pr_pathlen] != '/') {
679                 error = EINVAL;
680                 goto out;
681         }
682
683         *path_out = path;
684
685 out:
686         if (error != 0)
687                 free(path, M_SHMFD);
688
689         return (error);
690 }
691
692 static int
693 shm_dotruncate_locked(struct shmfd *shmfd, off_t length, void *rl_cookie)
694 {
695         vm_object_t object;
696         vm_page_t m;
697         vm_pindex_t idx, nobjsize;
698         vm_ooffset_t delta;
699         int base, rv;
700
701         KASSERT(length >= 0, ("shm_dotruncate: length < 0"));
702         object = shmfd->shm_object;
703         VM_OBJECT_ASSERT_WLOCKED(object);
704         rangelock_cookie_assert(rl_cookie, RA_WLOCKED);
705         if (length == shmfd->shm_size)
706                 return (0);
707         nobjsize = OFF_TO_IDX(length + PAGE_MASK);
708
709         /* Are we shrinking?  If so, trim the end. */
710         if (length < shmfd->shm_size) {
711                 if ((shmfd->shm_seals & F_SEAL_SHRINK) != 0)
712                         return (EPERM);
713
714                 /*
715                  * Disallow any requests to shrink the size if this
716                  * object is mapped into the kernel.
717                  */
718                 if (shmfd->shm_kmappings > 0)
719                         return (EBUSY);
720
721                 /*
722                  * Zero the truncated part of the last page.
723                  */
724                 base = length & PAGE_MASK;
725                 if (base != 0) {
726                         idx = OFF_TO_IDX(length);
727 retry:
728                         m = vm_page_grab(object, idx, VM_ALLOC_NOCREAT);
729                         if (m != NULL) {
730                                 MPASS(vm_page_all_valid(m));
731                         } else if (vm_pager_has_page(object, idx, NULL, NULL)) {
732                                 m = vm_page_alloc(object, idx,
733                                     VM_ALLOC_NORMAL | VM_ALLOC_WAITFAIL);
734                                 if (m == NULL)
735                                         goto retry;
736                                 vm_object_pip_add(object, 1);
737                                 VM_OBJECT_WUNLOCK(object);
738                                 rv = vm_pager_get_pages(object, &m, 1, NULL,
739                                     NULL);
740                                 VM_OBJECT_WLOCK(object);
741                                 vm_object_pip_wakeup(object);
742                                 if (rv == VM_PAGER_OK) {
743                                         /*
744                                          * Since the page was not resident,
745                                          * and therefore not recently
746                                          * accessed, immediately enqueue it
747                                          * for asynchronous laundering.  The
748                                          * current operation is not regarded
749                                          * as an access.
750                                          */
751                                         vm_page_launder(m);
752                                 } else {
753                                         vm_page_free(m);
754                                         VM_OBJECT_WUNLOCK(object);
755                                         return (EIO);
756                                 }
757                         }
758                         if (m != NULL) {
759                                 pmap_zero_page_area(m, base, PAGE_SIZE - base);
760                                 KASSERT(vm_page_all_valid(m),
761                                     ("shm_dotruncate: page %p is invalid", m));
762                                 vm_page_set_dirty(m);
763                                 vm_page_xunbusy(m);
764                         }
765                 }
766                 delta = IDX_TO_OFF(object->size - nobjsize);
767
768                 if (nobjsize < object->size)
769                         vm_object_page_remove(object, nobjsize, object->size,
770                             0);
771
772                 /* Free the swap accounted for shm */
773                 swap_release_by_cred(delta, object->cred);
774                 object->charge -= delta;
775         } else {
776                 if ((shmfd->shm_seals & F_SEAL_GROW) != 0)
777                         return (EPERM);
778
779                 /* Try to reserve additional swap space. */
780                 delta = IDX_TO_OFF(nobjsize - object->size);
781                 if (!swap_reserve_by_cred(delta, object->cred))
782                         return (ENOMEM);
783                 object->charge += delta;
784         }
785         shmfd->shm_size = length;
786         mtx_lock(&shm_timestamp_lock);
787         vfs_timestamp(&shmfd->shm_ctime);
788         shmfd->shm_mtime = shmfd->shm_ctime;
789         mtx_unlock(&shm_timestamp_lock);
790         object->size = nobjsize;
791         return (0);
792 }
793
794 static int
795 shm_dotruncate_largepage(struct shmfd *shmfd, off_t length, void *rl_cookie)
796 {
797         vm_object_t object;
798         vm_page_t m;
799         vm_pindex_t newobjsz;
800         vm_pindex_t oldobjsz __unused;
801         int aflags, error, i, psind, try;
802
803         KASSERT(length >= 0, ("shm_dotruncate: length < 0"));
804         object = shmfd->shm_object;
805         VM_OBJECT_ASSERT_WLOCKED(object);
806         rangelock_cookie_assert(rl_cookie, RA_WLOCKED);
807
808         oldobjsz = object->size;
809         newobjsz = OFF_TO_IDX(length);
810         if (length == shmfd->shm_size)
811                 return (0);
812         psind = shmfd->shm_lp_psind;
813         if (psind == 0 && length != 0)
814                 return (EINVAL);
815         if ((length & (pagesizes[psind] - 1)) != 0)
816                 return (EINVAL);
817
818         if (length < shmfd->shm_size) {
819                 if ((shmfd->shm_seals & F_SEAL_SHRINK) != 0)
820                         return (EPERM);
821                 if (shmfd->shm_kmappings > 0)
822                         return (EBUSY);
823                 return (ENOTSUP);       /* Pages are unmanaged. */
824 #if 0
825                 vm_object_page_remove(object, newobjsz, oldobjsz, 0);
826                 object->size = newobjsz;
827                 shmfd->shm_size = length;
828                 return (0);
829 #endif
830         }
831
832         if ((shmfd->shm_seals & F_SEAL_GROW) != 0)
833                 return (EPERM);
834
835         aflags = VM_ALLOC_NORMAL | VM_ALLOC_ZERO;
836         if (shmfd->shm_lp_alloc_policy == SHM_LARGEPAGE_ALLOC_NOWAIT)
837                 aflags |= VM_ALLOC_WAITFAIL;
838         try = 0;
839
840         /*
841          * Extend shmfd and object, keeping all already fully
842          * allocated large pages intact even on error, because dropped
843          * object lock might allowed mapping of them.
844          */
845         while (object->size < newobjsz) {
846                 m = vm_page_alloc_contig(object, object->size, aflags,
847                     pagesizes[psind] / PAGE_SIZE, 0, ~0,
848                     pagesizes[psind], 0,
849                     VM_MEMATTR_DEFAULT);
850                 if (m == NULL) {
851                         VM_OBJECT_WUNLOCK(object);
852                         if (shmfd->shm_lp_alloc_policy ==
853                             SHM_LARGEPAGE_ALLOC_NOWAIT ||
854                             (shmfd->shm_lp_alloc_policy ==
855                             SHM_LARGEPAGE_ALLOC_DEFAULT &&
856                             try >= largepage_reclaim_tries)) {
857                                 VM_OBJECT_WLOCK(object);
858                                 return (ENOMEM);
859                         }
860                         error = vm_page_reclaim_contig(aflags,
861                             pagesizes[psind] / PAGE_SIZE, 0, ~0,
862                             pagesizes[psind], 0) ? 0 :
863                             vm_wait_intr(object);
864                         if (error != 0) {
865                                 VM_OBJECT_WLOCK(object);
866                                 return (error);
867                         }
868                         try++;
869                         VM_OBJECT_WLOCK(object);
870                         continue;
871                 }
872                 try = 0;
873                 for (i = 0; i < pagesizes[psind] / PAGE_SIZE; i++) {
874                         if ((m[i].flags & PG_ZERO) == 0)
875                                 pmap_zero_page(&m[i]);
876                         vm_page_valid(&m[i]);
877                         vm_page_xunbusy(&m[i]);
878                 }
879                 object->size += OFF_TO_IDX(pagesizes[psind]);
880                 shmfd->shm_size += pagesizes[psind];
881                 atomic_add_long(&count_largepages[psind], 1);
882                 vm_wire_add(atop(pagesizes[psind]));
883         }
884         return (0);
885 }
886
887 static int
888 shm_dotruncate_cookie(struct shmfd *shmfd, off_t length, void *rl_cookie)
889 {
890         int error;
891
892         VM_OBJECT_WLOCK(shmfd->shm_object);
893         error = shm_largepage(shmfd) ? shm_dotruncate_largepage(shmfd,
894             length, rl_cookie) : shm_dotruncate_locked(shmfd, length,
895             rl_cookie);
896         VM_OBJECT_WUNLOCK(shmfd->shm_object);
897         return (error);
898 }
899
900 int
901 shm_dotruncate(struct shmfd *shmfd, off_t length)
902 {
903         void *rl_cookie;
904         int error;
905
906         rl_cookie = shm_rangelock_wlock(shmfd, 0, OFF_MAX);
907         error = shm_dotruncate_cookie(shmfd, length, rl_cookie);
908         shm_rangelock_unlock(shmfd, rl_cookie);
909         return (error);
910 }
911
912 /*
913  * shmfd object management including creation and reference counting
914  * routines.
915  */
916 struct shmfd *
917 shm_alloc(struct ucred *ucred, mode_t mode, bool largepage)
918 {
919         struct shmfd *shmfd;
920         vm_object_t obj;
921
922         shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO);
923         shmfd->shm_size = 0;
924         shmfd->shm_uid = ucred->cr_uid;
925         shmfd->shm_gid = ucred->cr_gid;
926         shmfd->shm_mode = mode;
927         if (largepage) {
928                 shmfd->shm_object = phys_pager_allocate(NULL,
929                     &shm_largepage_phys_ops, NULL, shmfd->shm_size,
930                     VM_PROT_DEFAULT, 0, ucred);
931                 shmfd->shm_lp_alloc_policy = SHM_LARGEPAGE_ALLOC_DEFAULT;
932         } else {
933                 obj = vm_pager_allocate(shmfd_pager_type, NULL,
934                     shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred);
935                 VM_OBJECT_WLOCK(obj);
936                 obj->un_pager.swp.swp_priv = shmfd;
937                 VM_OBJECT_WUNLOCK(obj);
938                 shmfd->shm_object = obj;
939         }
940         KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate"));
941         vfs_timestamp(&shmfd->shm_birthtime);
942         shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime =
943             shmfd->shm_birthtime;
944         shmfd->shm_ino = alloc_unr64(&shm_ino_unr);
945         refcount_init(&shmfd->shm_refs, 1);
946         mtx_init(&shmfd->shm_mtx, "shmrl", NULL, MTX_DEF);
947         rangelock_init(&shmfd->shm_rl);
948 #ifdef MAC
949         mac_posixshm_init(shmfd);
950         mac_posixshm_create(ucred, shmfd);
951 #endif
952
953         return (shmfd);
954 }
955
956 struct shmfd *
957 shm_hold(struct shmfd *shmfd)
958 {
959
960         refcount_acquire(&shmfd->shm_refs);
961         return (shmfd);
962 }
963
964 void
965 shm_drop(struct shmfd *shmfd)
966 {
967         vm_object_t obj;
968
969         if (refcount_release(&shmfd->shm_refs)) {
970 #ifdef MAC
971                 mac_posixshm_destroy(shmfd);
972 #endif
973                 rangelock_destroy(&shmfd->shm_rl);
974                 mtx_destroy(&shmfd->shm_mtx);
975                 obj = shmfd->shm_object;
976                 if (!shm_largepage(shmfd)) {
977                         VM_OBJECT_WLOCK(obj);
978                         obj->un_pager.swp.swp_priv = NULL;
979                         VM_OBJECT_WUNLOCK(obj);
980                 }
981                 vm_object_deallocate(obj);
982                 free(shmfd, M_SHMFD);
983         }
984 }
985
986 /*
987  * Determine if the credentials have sufficient permissions for a
988  * specified combination of FREAD and FWRITE.
989  */
990 int
991 shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags)
992 {
993         accmode_t accmode;
994         int error;
995
996         accmode = 0;
997         if (flags & FREAD)
998                 accmode |= VREAD;
999         if (flags & FWRITE)
1000                 accmode |= VWRITE;
1001         mtx_lock(&shm_timestamp_lock);
1002         error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
1003             accmode, ucred);
1004         mtx_unlock(&shm_timestamp_lock);
1005         return (error);
1006 }
1007
1008 static void
1009 shm_init(void *arg)
1010 {
1011         char name[32];
1012         int i;
1013
1014         mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF);
1015         sx_init(&shm_dict_lock, "shm dictionary");
1016         shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash);
1017         new_unrhdr64(&shm_ino_unr, 1);
1018         shm_dev_ino = devfs_alloc_cdp_inode();
1019         KASSERT(shm_dev_ino > 0, ("shm dev inode not initialized"));
1020         shmfd_pager_type = vm_pager_alloc_dyn_type(&shm_swap_pager_ops,
1021             OBJT_SWAP);
1022         MPASS(shmfd_pager_type != -1);
1023
1024         for (i = 1; i < MAXPAGESIZES; i++) {
1025                 if (pagesizes[i] == 0)
1026                         break;
1027 #define M       (1024 * 1024)
1028 #define G       (1024 * M)
1029                 if (pagesizes[i] >= G)
1030                         snprintf(name, sizeof(name), "%luG", pagesizes[i] / G);
1031                 else if (pagesizes[i] >= M)
1032                         snprintf(name, sizeof(name), "%luM", pagesizes[i] / M);
1033                 else
1034                         snprintf(name, sizeof(name), "%lu", pagesizes[i]);
1035 #undef G
1036 #undef M
1037                 SYSCTL_ADD_ULONG(NULL, SYSCTL_STATIC_CHILDREN(_vm_largepages),
1038                     OID_AUTO, name, CTLFLAG_RD, &count_largepages[i],
1039                     "number of non-transient largepages allocated");
1040         }
1041 }
1042 SYSINIT(shm_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_init, NULL);
1043
1044 /*
1045  * Remove all shared memory objects that belong to a prison.
1046  */
1047 void
1048 shm_remove_prison(struct prison *pr)
1049 {
1050         struct shm_mapping *shmm, *tshmm;
1051         u_long i;
1052
1053         sx_xlock(&shm_dict_lock);
1054         for (i = 0; i < shm_hash + 1; i++) {
1055                 LIST_FOREACH_SAFE(shmm, &shm_dictionary[i], sm_link, tshmm) {
1056                         if (shmm->sm_shmfd->shm_object->cred &&
1057                             shmm->sm_shmfd->shm_object->cred->cr_prison == pr)
1058                                 shm_doremove(shmm);
1059                 }
1060         }
1061         sx_xunlock(&shm_dict_lock);
1062 }
1063
1064 /*
1065  * Dictionary management.  We maintain an in-kernel dictionary to map
1066  * paths to shmfd objects.  We use the FNV hash on the path to store
1067  * the mappings in a hash table.
1068  */
1069 static struct shmfd *
1070 shm_lookup(char *path, Fnv32_t fnv)
1071 {
1072         struct shm_mapping *map;
1073
1074         LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
1075                 if (map->sm_fnv != fnv)
1076                         continue;
1077                 if (strcmp(map->sm_path, path) == 0)
1078                         return (map->sm_shmfd);
1079         }
1080
1081         return (NULL);
1082 }
1083
1084 static void
1085 shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd)
1086 {
1087         struct shm_mapping *map;
1088
1089         map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK);
1090         map->sm_path = path;
1091         map->sm_fnv = fnv;
1092         map->sm_shmfd = shm_hold(shmfd);
1093         shmfd->shm_path = path;
1094         LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link);
1095 }
1096
1097 static int
1098 shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
1099 {
1100         struct shm_mapping *map;
1101         int error;
1102
1103         LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
1104                 if (map->sm_fnv != fnv)
1105                         continue;
1106                 if (strcmp(map->sm_path, path) == 0) {
1107 #ifdef MAC
1108                         error = mac_posixshm_check_unlink(ucred, map->sm_shmfd);
1109                         if (error)
1110                                 return (error);
1111 #endif
1112                         error = shm_access(map->sm_shmfd, ucred,
1113                             FREAD | FWRITE);
1114                         if (error)
1115                                 return (error);
1116                         shm_doremove(map);
1117                         return (0);
1118                 }
1119         }
1120
1121         return (ENOENT);
1122 }
1123
1124 static void
1125 shm_doremove(struct shm_mapping *map)
1126 {
1127         map->sm_shmfd->shm_path = NULL;
1128         LIST_REMOVE(map, sm_link);
1129         shm_drop(map->sm_shmfd);
1130         free(map->sm_path, M_SHMFD);
1131         free(map, M_SHMFD);
1132 }
1133
1134 int
1135 kern_shm_open2(struct thread *td, const char *userpath, int flags, mode_t mode,
1136     int shmflags, struct filecaps *fcaps, const char *name __unused)
1137 {
1138         struct pwddesc *pdp;
1139         struct shmfd *shmfd;
1140         struct file *fp;
1141         char *path;
1142         void *rl_cookie;
1143         Fnv32_t fnv;
1144         mode_t cmode;
1145         int error, fd, initial_seals;
1146         bool largepage;
1147
1148         if ((shmflags & ~(SHM_ALLOW_SEALING | SHM_GROW_ON_WRITE |
1149             SHM_LARGEPAGE)) != 0)
1150                 return (EINVAL);
1151
1152         initial_seals = F_SEAL_SEAL;
1153         if ((shmflags & SHM_ALLOW_SEALING) != 0)
1154                 initial_seals &= ~F_SEAL_SEAL;
1155
1156 #ifdef CAPABILITY_MODE
1157         /*
1158          * shm_open(2) is only allowed for anonymous objects.
1159          */
1160         if (IN_CAPABILITY_MODE(td) && (userpath != SHM_ANON))
1161                 return (ECAPMODE);
1162 #endif
1163
1164         AUDIT_ARG_FFLAGS(flags);
1165         AUDIT_ARG_MODE(mode);
1166
1167         if ((flags & O_ACCMODE) != O_RDONLY && (flags & O_ACCMODE) != O_RDWR)
1168                 return (EINVAL);
1169
1170         if ((flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC)) != 0)
1171                 return (EINVAL);
1172
1173         largepage = (shmflags & SHM_LARGEPAGE) != 0;
1174         if (largepage && !PMAP_HAS_LARGEPAGES)
1175                 return (ENOTTY);
1176
1177         /*
1178          * Currently only F_SEAL_SEAL may be set when creating or opening shmfd.
1179          * If the decision is made later to allow additional seals, care must be
1180          * taken below to ensure that the seals are properly set if the shmfd
1181          * already existed -- this currently assumes that only F_SEAL_SEAL can
1182          * be set and doesn't take further precautions to ensure the validity of
1183          * the seals being added with respect to current mappings.
1184          */
1185         if ((initial_seals & ~F_SEAL_SEAL) != 0)
1186                 return (EINVAL);
1187
1188         pdp = td->td_proc->p_pd;
1189         cmode = (mode & ~pdp->pd_cmask) & ACCESSPERMS;
1190
1191         /*
1192          * shm_open(2) created shm should always have O_CLOEXEC set, as mandated
1193          * by POSIX.  We allow it to be unset here so that an in-kernel
1194          * interface may be written as a thin layer around shm, optionally not
1195          * setting CLOEXEC.  For shm_open(2), O_CLOEXEC is set unconditionally
1196          * in sys_shm_open() to keep this implementation compliant.
1197          */
1198         error = falloc_caps(td, &fp, &fd, flags & O_CLOEXEC, fcaps);
1199         if (error)
1200                 return (error);
1201
1202         /* A SHM_ANON path pointer creates an anonymous object. */
1203         if (userpath == SHM_ANON) {
1204                 /* A read-only anonymous object is pointless. */
1205                 if ((flags & O_ACCMODE) == O_RDONLY) {
1206                         fdclose(td, fp, fd);
1207                         fdrop(fp, td);
1208                         return (EINVAL);
1209                 }
1210                 shmfd = shm_alloc(td->td_ucred, cmode, largepage);
1211                 shmfd->shm_seals = initial_seals;
1212                 shmfd->shm_flags = shmflags;
1213         } else {
1214                 error = shm_copyin_path(td, userpath, &path);
1215                 if (error != 0) {
1216                         fdclose(td, fp, fd);
1217                         fdrop(fp, td);
1218                         return (error);
1219                 }
1220
1221                 AUDIT_ARG_UPATH1_CANON(path);
1222                 fnv = fnv_32_str(path, FNV1_32_INIT);
1223                 sx_xlock(&shm_dict_lock);
1224                 shmfd = shm_lookup(path, fnv);
1225                 if (shmfd == NULL) {
1226                         /* Object does not yet exist, create it if requested. */
1227                         if (flags & O_CREAT) {
1228 #ifdef MAC
1229                                 error = mac_posixshm_check_create(td->td_ucred,
1230                                     path);
1231                                 if (error == 0) {
1232 #endif
1233                                         shmfd = shm_alloc(td->td_ucred, cmode,
1234                                             largepage);
1235                                         shmfd->shm_seals = initial_seals;
1236                                         shmfd->shm_flags = shmflags;
1237                                         shm_insert(path, fnv, shmfd);
1238 #ifdef MAC
1239                                 }
1240 #endif
1241                         } else {
1242                                 free(path, M_SHMFD);
1243                                 error = ENOENT;
1244                         }
1245                 } else {
1246                         rl_cookie = shm_rangelock_wlock(shmfd, 0, OFF_MAX);
1247
1248                         /*
1249                          * kern_shm_open() likely shouldn't ever error out on
1250                          * trying to set a seal that already exists, unlike
1251                          * F_ADD_SEALS.  This would break terribly as
1252                          * shm_open(2) actually sets F_SEAL_SEAL to maintain
1253                          * historical behavior where the underlying file could
1254                          * not be sealed.
1255                          */
1256                         initial_seals &= ~shmfd->shm_seals;
1257
1258                         /*
1259                          * Object already exists, obtain a new
1260                          * reference if requested and permitted.
1261                          */
1262                         free(path, M_SHMFD);
1263
1264                         /*
1265                          * initial_seals can't set additional seals if we've
1266                          * already been set F_SEAL_SEAL.  If F_SEAL_SEAL is set,
1267                          * then we've already removed that one from
1268                          * initial_seals.  This is currently redundant as we
1269                          * only allow setting F_SEAL_SEAL at creation time, but
1270                          * it's cheap to check and decreases the effort required
1271                          * to allow additional seals.
1272                          */
1273                         if ((shmfd->shm_seals & F_SEAL_SEAL) != 0 &&
1274                             initial_seals != 0)
1275                                 error = EPERM;
1276                         else if ((flags & (O_CREAT | O_EXCL)) ==
1277                             (O_CREAT | O_EXCL))
1278                                 error = EEXIST;
1279                         else if (shmflags != 0 && shmflags != shmfd->shm_flags)
1280                                 error = EINVAL;
1281                         else {
1282 #ifdef MAC
1283                                 error = mac_posixshm_check_open(td->td_ucred,
1284                                     shmfd, FFLAGS(flags & O_ACCMODE));
1285                                 if (error == 0)
1286 #endif
1287                                 error = shm_access(shmfd, td->td_ucred,
1288                                     FFLAGS(flags & O_ACCMODE));
1289                         }
1290
1291                         /*
1292                          * Truncate the file back to zero length if
1293                          * O_TRUNC was specified and the object was
1294                          * opened with read/write.
1295                          */
1296                         if (error == 0 &&
1297                             (flags & (O_ACCMODE | O_TRUNC)) ==
1298                             (O_RDWR | O_TRUNC)) {
1299                                 VM_OBJECT_WLOCK(shmfd->shm_object);
1300 #ifdef MAC
1301                                 error = mac_posixshm_check_truncate(
1302                                         td->td_ucred, fp->f_cred, shmfd);
1303                                 if (error == 0)
1304 #endif
1305                                         error = shm_dotruncate_locked(shmfd, 0,
1306                                             rl_cookie);
1307                                 VM_OBJECT_WUNLOCK(shmfd->shm_object);
1308                         }
1309                         if (error == 0) {
1310                                 /*
1311                                  * Currently we only allow F_SEAL_SEAL to be
1312                                  * set initially.  As noted above, this would
1313                                  * need to be reworked should that change.
1314                                  */
1315                                 shmfd->shm_seals |= initial_seals;
1316                                 shm_hold(shmfd);
1317                         }
1318                         shm_rangelock_unlock(shmfd, rl_cookie);
1319                 }
1320                 sx_xunlock(&shm_dict_lock);
1321
1322                 if (error) {
1323                         fdclose(td, fp, fd);
1324                         fdrop(fp, td);
1325                         return (error);
1326                 }
1327         }
1328
1329         finit(fp, FFLAGS(flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops);
1330
1331         td->td_retval[0] = fd;
1332         fdrop(fp, td);
1333
1334         return (0);
1335 }
1336
1337 /* System calls. */
1338 #ifdef COMPAT_FREEBSD12
1339 int
1340 freebsd12_shm_open(struct thread *td, struct freebsd12_shm_open_args *uap)
1341 {
1342
1343         return (kern_shm_open(td, uap->path, uap->flags | O_CLOEXEC,
1344             uap->mode, NULL));
1345 }
1346 #endif
1347
1348 int
1349 sys_shm_unlink(struct thread *td, struct shm_unlink_args *uap)
1350 {
1351         char *path;
1352         Fnv32_t fnv;
1353         int error;
1354
1355         error = shm_copyin_path(td, uap->path, &path);
1356         if (error != 0)
1357                 return (error);
1358
1359         AUDIT_ARG_UPATH1_CANON(path);
1360         fnv = fnv_32_str(path, FNV1_32_INIT);
1361         sx_xlock(&shm_dict_lock);
1362         error = shm_remove(path, fnv, td->td_ucred);
1363         sx_xunlock(&shm_dict_lock);
1364         free(path, M_SHMFD);
1365
1366         return (error);
1367 }
1368
1369 int
1370 sys_shm_rename(struct thread *td, struct shm_rename_args *uap)
1371 {
1372         char *path_from = NULL, *path_to = NULL;
1373         Fnv32_t fnv_from, fnv_to;
1374         struct shmfd *fd_from;
1375         struct shmfd *fd_to;
1376         int error;
1377         int flags;
1378
1379         flags = uap->flags;
1380         AUDIT_ARG_FFLAGS(flags);
1381
1382         /*
1383          * Make sure the user passed only valid flags.
1384          * If you add a new flag, please add a new term here.
1385          */
1386         if ((flags & ~(
1387             SHM_RENAME_NOREPLACE |
1388             SHM_RENAME_EXCHANGE
1389             )) != 0) {
1390                 error = EINVAL;
1391                 goto out;
1392         }
1393
1394         /*
1395          * EXCHANGE and NOREPLACE don't quite make sense together. Let's
1396          * force the user to choose one or the other.
1397          */
1398         if ((flags & SHM_RENAME_NOREPLACE) != 0 &&
1399             (flags & SHM_RENAME_EXCHANGE) != 0) {
1400                 error = EINVAL;
1401                 goto out;
1402         }
1403
1404         /* Renaming to or from anonymous makes no sense */
1405         if (uap->path_from == SHM_ANON || uap->path_to == SHM_ANON) {
1406                 error = EINVAL;
1407                 goto out;
1408         }
1409
1410         error = shm_copyin_path(td, uap->path_from, &path_from);
1411         if (error != 0)
1412                 goto out;
1413
1414         error = shm_copyin_path(td, uap->path_to, &path_to);
1415         if (error != 0)
1416                 goto out;
1417
1418         AUDIT_ARG_UPATH1_CANON(path_from);
1419         AUDIT_ARG_UPATH2_CANON(path_to);
1420
1421         /* Rename with from/to equal is a no-op */
1422         if (strcmp(path_from, path_to) == 0)
1423                 goto out;
1424
1425         fnv_from = fnv_32_str(path_from, FNV1_32_INIT);
1426         fnv_to = fnv_32_str(path_to, FNV1_32_INIT);
1427
1428         sx_xlock(&shm_dict_lock);
1429
1430         fd_from = shm_lookup(path_from, fnv_from);
1431         if (fd_from == NULL) {
1432                 error = ENOENT;
1433                 goto out_locked;
1434         }
1435
1436         fd_to = shm_lookup(path_to, fnv_to);
1437         if ((flags & SHM_RENAME_NOREPLACE) != 0 && fd_to != NULL) {
1438                 error = EEXIST;
1439                 goto out_locked;
1440         }
1441
1442         /*
1443          * Unconditionally prevents shm_remove from invalidating the 'from'
1444          * shm's state.
1445          */
1446         shm_hold(fd_from);
1447         error = shm_remove(path_from, fnv_from, td->td_ucred);
1448
1449         /*
1450          * One of my assumptions failed if ENOENT (e.g. locking didn't
1451          * protect us)
1452          */
1453         KASSERT(error != ENOENT, ("Our shm disappeared during shm_rename: %s",
1454             path_from));
1455         if (error != 0) {
1456                 shm_drop(fd_from);
1457                 goto out_locked;
1458         }
1459
1460         /*
1461          * If we are exchanging, we need to ensure the shm_remove below
1462          * doesn't invalidate the dest shm's state.
1463          */
1464         if ((flags & SHM_RENAME_EXCHANGE) != 0 && fd_to != NULL)
1465                 shm_hold(fd_to);
1466
1467         /*
1468          * NOTE: if path_to is not already in the hash, c'est la vie;
1469          * it simply means we have nothing already at path_to to unlink.
1470          * That is the ENOENT case.
1471          *
1472          * If we somehow don't have access to unlink this guy, but
1473          * did for the shm at path_from, then relink the shm to path_from
1474          * and abort with EACCES.
1475          *
1476          * All other errors: that is weird; let's relink and abort the
1477          * operation.
1478          */
1479         error = shm_remove(path_to, fnv_to, td->td_ucred);
1480         if (error != 0 && error != ENOENT) {
1481                 shm_insert(path_from, fnv_from, fd_from);
1482                 shm_drop(fd_from);
1483                 /* Don't free path_from now, since the hash references it */
1484                 path_from = NULL;
1485                 goto out_locked;
1486         }
1487
1488         error = 0;
1489
1490         shm_insert(path_to, fnv_to, fd_from);
1491
1492         /* Don't free path_to now, since the hash references it */
1493         path_to = NULL;
1494
1495         /* We kept a ref when we removed, and incremented again in insert */
1496         shm_drop(fd_from);
1497         KASSERT(fd_from->shm_refs > 0, ("Expected >0 refs; got: %d\n",
1498             fd_from->shm_refs));
1499
1500         if ((flags & SHM_RENAME_EXCHANGE) != 0 && fd_to != NULL) {
1501                 shm_insert(path_from, fnv_from, fd_to);
1502                 path_from = NULL;
1503                 shm_drop(fd_to);
1504                 KASSERT(fd_to->shm_refs > 0, ("Expected >0 refs; got: %d\n",
1505                     fd_to->shm_refs));
1506         }
1507
1508 out_locked:
1509         sx_xunlock(&shm_dict_lock);
1510
1511 out:
1512         free(path_from, M_SHMFD);
1513         free(path_to, M_SHMFD);
1514         return (error);
1515 }
1516
1517 static int
1518 shm_mmap_large(struct shmfd *shmfd, vm_map_t map, vm_offset_t *addr,
1519     vm_size_t size, vm_prot_t prot, vm_prot_t max_prot, int flags,
1520     vm_ooffset_t foff, struct thread *td)
1521 {
1522         struct vmspace *vms;
1523         vm_map_entry_t next_entry, prev_entry;
1524         vm_offset_t align, mask, maxaddr;
1525         int docow, error, rv, try;
1526         bool curmap;
1527
1528         if (shmfd->shm_lp_psind == 0)
1529                 return (EINVAL);
1530
1531         /* MAP_PRIVATE is disabled */
1532         if ((flags & ~(MAP_SHARED | MAP_FIXED | MAP_EXCL |
1533             MAP_NOCORE |
1534 #ifdef MAP_32BIT
1535             MAP_32BIT |
1536 #endif
1537             MAP_ALIGNMENT_MASK)) != 0)
1538                 return (EINVAL);
1539
1540         vms = td->td_proc->p_vmspace;
1541         curmap = map == &vms->vm_map;
1542         if (curmap) {
1543                 error = kern_mmap_racct_check(td, map, size);
1544                 if (error != 0)
1545                         return (error);
1546         }
1547
1548         docow = shmfd->shm_lp_psind << MAP_SPLIT_BOUNDARY_SHIFT;
1549         docow |= MAP_INHERIT_SHARE;
1550         if ((flags & MAP_NOCORE) != 0)
1551                 docow |= MAP_DISABLE_COREDUMP;
1552
1553         mask = pagesizes[shmfd->shm_lp_psind] - 1;
1554         if ((foff & mask) != 0)
1555                 return (EINVAL);
1556         maxaddr = vm_map_max(map);
1557 #ifdef MAP_32BIT
1558         if ((flags & MAP_32BIT) != 0 && maxaddr > MAP_32BIT_MAX_ADDR)
1559                 maxaddr = MAP_32BIT_MAX_ADDR;
1560 #endif
1561         if (size == 0 || (size & mask) != 0 ||
1562             (*addr != 0 && ((*addr & mask) != 0 ||
1563             *addr + size < *addr || *addr + size > maxaddr)))
1564                 return (EINVAL);
1565
1566         align = flags & MAP_ALIGNMENT_MASK;
1567         if (align == 0) {
1568                 align = pagesizes[shmfd->shm_lp_psind];
1569         } else if (align == MAP_ALIGNED_SUPER) {
1570                 if (shmfd->shm_lp_psind != 1)
1571                         return (EINVAL);
1572                 align = pagesizes[1];
1573         } else {
1574                 align >>= MAP_ALIGNMENT_SHIFT;
1575                 align = 1ULL << align;
1576                 /* Also handles overflow. */
1577                 if (align < pagesizes[shmfd->shm_lp_psind])
1578                         return (EINVAL);
1579         }
1580
1581         vm_map_lock(map);
1582         if ((flags & MAP_FIXED) == 0) {
1583                 try = 1;
1584                 if (curmap && (*addr == 0 ||
1585                     (*addr >= round_page((vm_offset_t)vms->vm_taddr) &&
1586                     *addr < round_page((vm_offset_t)vms->vm_daddr +
1587                     lim_max(td, RLIMIT_DATA))))) {
1588                         *addr = roundup2((vm_offset_t)vms->vm_daddr +
1589                             lim_max(td, RLIMIT_DATA),
1590                             pagesizes[shmfd->shm_lp_psind]);
1591                 }
1592 again:
1593                 rv = vm_map_find_aligned(map, addr, size, maxaddr, align);
1594                 if (rv != KERN_SUCCESS) {
1595                         if (try == 1) {
1596                                 try = 2;
1597                                 *addr = vm_map_min(map);
1598                                 if ((*addr & mask) != 0)
1599                                         *addr = (*addr + mask) & mask;
1600                                 goto again;
1601                         }
1602                         goto fail1;
1603                 }
1604         } else if ((flags & MAP_EXCL) == 0) {
1605                 rv = vm_map_delete(map, *addr, *addr + size);
1606                 if (rv != KERN_SUCCESS)
1607                         goto fail1;
1608         } else {
1609                 error = ENOSPC;
1610                 if (vm_map_lookup_entry(map, *addr, &prev_entry))
1611                         goto fail;
1612                 next_entry = vm_map_entry_succ(prev_entry);
1613                 if (next_entry->start < *addr + size)
1614                         goto fail;
1615         }
1616
1617         rv = vm_map_insert(map, shmfd->shm_object, foff, *addr, *addr + size,
1618             prot, max_prot, docow);
1619 fail1:
1620         error = vm_mmap_to_errno(rv);
1621 fail:
1622         vm_map_unlock(map);
1623         return (error);
1624 }
1625
1626 static int
1627 shm_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t objsize,
1628     vm_prot_t prot, vm_prot_t cap_maxprot, int flags,
1629     vm_ooffset_t foff, struct thread *td)
1630 {
1631         struct shmfd *shmfd;
1632         vm_prot_t maxprot;
1633         int error;
1634         bool writecnt;
1635         void *rl_cookie;
1636
1637         shmfd = fp->f_data;
1638         maxprot = VM_PROT_NONE;
1639
1640         rl_cookie = shm_rangelock_rlock(shmfd, 0, objsize);
1641         /* FREAD should always be set. */
1642         if ((fp->f_flag & FREAD) != 0)
1643                 maxprot |= VM_PROT_EXECUTE | VM_PROT_READ;
1644
1645         /*
1646          * If FWRITE's set, we can allow VM_PROT_WRITE unless it's a shared
1647          * mapping with a write seal applied.  Private mappings are always
1648          * writeable.
1649          */
1650         if ((flags & MAP_SHARED) == 0) {
1651                 cap_maxprot |= VM_PROT_WRITE;
1652                 maxprot |= VM_PROT_WRITE;
1653                 writecnt = false;
1654         } else {
1655                 if ((fp->f_flag & FWRITE) != 0 &&
1656                     (shmfd->shm_seals & F_SEAL_WRITE) == 0)
1657                         maxprot |= VM_PROT_WRITE;
1658
1659                 /*
1660                  * Any mappings from a writable descriptor may be upgraded to
1661                  * VM_PROT_WRITE with mprotect(2), unless a write-seal was
1662                  * applied between the open and subsequent mmap(2).  We want to
1663                  * reject application of a write seal as long as any such
1664                  * mapping exists so that the seal cannot be trivially bypassed.
1665                  */
1666                 writecnt = (maxprot & VM_PROT_WRITE) != 0;
1667                 if (!writecnt && (prot & VM_PROT_WRITE) != 0) {
1668                         error = EACCES;
1669                         goto out;
1670                 }
1671         }
1672         maxprot &= cap_maxprot;
1673
1674         /* See comment in vn_mmap(). */
1675         if (
1676 #ifdef _LP64
1677             objsize > OFF_MAX ||
1678 #endif
1679             foff > OFF_MAX - objsize) {
1680                 error = EINVAL;
1681                 goto out;
1682         }
1683
1684 #ifdef MAC
1685         error = mac_posixshm_check_mmap(td->td_ucred, shmfd, prot, flags);
1686         if (error != 0)
1687                 goto out;
1688 #endif
1689
1690         mtx_lock(&shm_timestamp_lock);
1691         vfs_timestamp(&shmfd->shm_atime);
1692         mtx_unlock(&shm_timestamp_lock);
1693         vm_object_reference(shmfd->shm_object);
1694
1695         if (shm_largepage(shmfd)) {
1696                 writecnt = false;
1697                 error = shm_mmap_large(shmfd, map, addr, objsize, prot,
1698                     maxprot, flags, foff, td);
1699         } else {
1700                 if (writecnt) {
1701                         vm_pager_update_writecount(shmfd->shm_object, 0,
1702                             objsize);
1703                 }
1704                 error = vm_mmap_object(map, addr, objsize, prot, maxprot, flags,
1705                     shmfd->shm_object, foff, writecnt, td);
1706         }
1707         if (error != 0) {
1708                 if (writecnt)
1709                         vm_pager_release_writecount(shmfd->shm_object, 0,
1710                             objsize);
1711                 vm_object_deallocate(shmfd->shm_object);
1712         }
1713 out:
1714         shm_rangelock_unlock(shmfd, rl_cookie);
1715         return (error);
1716 }
1717
1718 static int
1719 shm_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
1720     struct thread *td)
1721 {
1722         struct shmfd *shmfd;
1723         int error;
1724
1725         error = 0;
1726         shmfd = fp->f_data;
1727         mtx_lock(&shm_timestamp_lock);
1728         /*
1729          * SUSv4 says that x bits of permission need not be affected.
1730          * Be consistent with our shm_open there.
1731          */
1732 #ifdef MAC
1733         error = mac_posixshm_check_setmode(active_cred, shmfd, mode);
1734         if (error != 0)
1735                 goto out;
1736 #endif
1737         error = vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
1738             VADMIN, active_cred);
1739         if (error != 0)
1740                 goto out;
1741         shmfd->shm_mode = mode & ACCESSPERMS;
1742 out:
1743         mtx_unlock(&shm_timestamp_lock);
1744         return (error);
1745 }
1746
1747 static int
1748 shm_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
1749     struct thread *td)
1750 {
1751         struct shmfd *shmfd;
1752         int error;
1753
1754         error = 0;
1755         shmfd = fp->f_data;
1756         mtx_lock(&shm_timestamp_lock);
1757 #ifdef MAC
1758         error = mac_posixshm_check_setowner(active_cred, shmfd, uid, gid);
1759         if (error != 0)
1760                 goto out;
1761 #endif
1762         if (uid == (uid_t)-1)
1763                 uid = shmfd->shm_uid;
1764         if (gid == (gid_t)-1)
1765                  gid = shmfd->shm_gid;
1766         if (((uid != shmfd->shm_uid && uid != active_cred->cr_uid) ||
1767             (gid != shmfd->shm_gid && !groupmember(gid, active_cred))) &&
1768             (error = priv_check_cred(active_cred, PRIV_VFS_CHOWN)))
1769                 goto out;
1770         shmfd->shm_uid = uid;
1771         shmfd->shm_gid = gid;
1772 out:
1773         mtx_unlock(&shm_timestamp_lock);
1774         return (error);
1775 }
1776
1777 /*
1778  * Helper routines to allow the backing object of a shared memory file
1779  * descriptor to be mapped in the kernel.
1780  */
1781 int
1782 shm_map(struct file *fp, size_t size, off_t offset, void **memp)
1783 {
1784         struct shmfd *shmfd;
1785         vm_offset_t kva, ofs;
1786         vm_object_t obj;
1787         int rv;
1788
1789         if (fp->f_type != DTYPE_SHM)
1790                 return (EINVAL);
1791         shmfd = fp->f_data;
1792         obj = shmfd->shm_object;
1793         VM_OBJECT_WLOCK(obj);
1794         /*
1795          * XXXRW: This validation is probably insufficient, and subject to
1796          * sign errors.  It should be fixed.
1797          */
1798         if (offset >= shmfd->shm_size ||
1799             offset + size > round_page(shmfd->shm_size)) {
1800                 VM_OBJECT_WUNLOCK(obj);
1801                 return (EINVAL);
1802         }
1803
1804         shmfd->shm_kmappings++;
1805         vm_object_reference_locked(obj);
1806         VM_OBJECT_WUNLOCK(obj);
1807
1808         /* Map the object into the kernel_map and wire it. */
1809         kva = vm_map_min(kernel_map);
1810         ofs = offset & PAGE_MASK;
1811         offset = trunc_page(offset);
1812         size = round_page(size + ofs);
1813         rv = vm_map_find(kernel_map, obj, offset, &kva, size, 0,
1814             VMFS_OPTIMAL_SPACE, VM_PROT_READ | VM_PROT_WRITE,
1815             VM_PROT_READ | VM_PROT_WRITE, 0);
1816         if (rv == KERN_SUCCESS) {
1817                 rv = vm_map_wire(kernel_map, kva, kva + size,
1818                     VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
1819                 if (rv == KERN_SUCCESS) {
1820                         *memp = (void *)(kva + ofs);
1821                         return (0);
1822                 }
1823                 vm_map_remove(kernel_map, kva, kva + size);
1824         } else
1825                 vm_object_deallocate(obj);
1826
1827         /* On failure, drop our mapping reference. */
1828         VM_OBJECT_WLOCK(obj);
1829         shmfd->shm_kmappings--;
1830         VM_OBJECT_WUNLOCK(obj);
1831
1832         return (vm_mmap_to_errno(rv));
1833 }
1834
1835 /*
1836  * We require the caller to unmap the entire entry.  This allows us to
1837  * safely decrement shm_kmappings when a mapping is removed.
1838  */
1839 int
1840 shm_unmap(struct file *fp, void *mem, size_t size)
1841 {
1842         struct shmfd *shmfd;
1843         vm_map_entry_t entry;
1844         vm_offset_t kva, ofs;
1845         vm_object_t obj;
1846         vm_pindex_t pindex;
1847         vm_prot_t prot;
1848         boolean_t wired;
1849         vm_map_t map;
1850         int rv;
1851
1852         if (fp->f_type != DTYPE_SHM)
1853                 return (EINVAL);
1854         shmfd = fp->f_data;
1855         kva = (vm_offset_t)mem;
1856         ofs = kva & PAGE_MASK;
1857         kva = trunc_page(kva);
1858         size = round_page(size + ofs);
1859         map = kernel_map;
1860         rv = vm_map_lookup(&map, kva, VM_PROT_READ | VM_PROT_WRITE, &entry,
1861             &obj, &pindex, &prot, &wired);
1862         if (rv != KERN_SUCCESS)
1863                 return (EINVAL);
1864         if (entry->start != kva || entry->end != kva + size) {
1865                 vm_map_lookup_done(map, entry);
1866                 return (EINVAL);
1867         }
1868         vm_map_lookup_done(map, entry);
1869         if (obj != shmfd->shm_object)
1870                 return (EINVAL);
1871         vm_map_remove(map, kva, kva + size);
1872         VM_OBJECT_WLOCK(obj);
1873         KASSERT(shmfd->shm_kmappings > 0, ("shm_unmap: object not mapped"));
1874         shmfd->shm_kmappings--;
1875         VM_OBJECT_WUNLOCK(obj);
1876         return (0);
1877 }
1878
1879 static int
1880 shm_fill_kinfo_locked(struct shmfd *shmfd, struct kinfo_file *kif, bool list)
1881 {
1882         const char *path, *pr_path;
1883         size_t pr_pathlen;
1884         bool visible;
1885
1886         sx_assert(&shm_dict_lock, SA_LOCKED);
1887         kif->kf_type = KF_TYPE_SHM;
1888         kif->kf_un.kf_file.kf_file_mode = S_IFREG | shmfd->shm_mode;
1889         kif->kf_un.kf_file.kf_file_size = shmfd->shm_size;
1890         if (shmfd->shm_path != NULL) {
1891                 if (shmfd->shm_path != NULL) {
1892                         path = shmfd->shm_path;
1893                         pr_path = curthread->td_ucred->cr_prison->pr_path;
1894                         if (strcmp(pr_path, "/") != 0) {
1895                                 /* Return the jail-rooted pathname. */
1896                                 pr_pathlen = strlen(pr_path);
1897                                 visible = strncmp(path, pr_path, pr_pathlen)
1898                                     == 0 && path[pr_pathlen] == '/';
1899                                 if (list && !visible)
1900                                         return (EPERM);
1901                                 if (visible)
1902                                         path += pr_pathlen;
1903                         }
1904                         strlcpy(kif->kf_path, path, sizeof(kif->kf_path));
1905                 }
1906         }
1907         return (0);
1908 }
1909
1910 static int
1911 shm_fill_kinfo(struct file *fp, struct kinfo_file *kif,
1912     struct filedesc *fdp __unused)
1913 {
1914         int res;
1915
1916         sx_slock(&shm_dict_lock);
1917         res = shm_fill_kinfo_locked(fp->f_data, kif, false);
1918         sx_sunlock(&shm_dict_lock);
1919         return (res);
1920 }
1921
1922 static int
1923 shm_add_seals(struct file *fp, int seals)
1924 {
1925         struct shmfd *shmfd;
1926         void *rl_cookie;
1927         vm_ooffset_t writemappings;
1928         int error, nseals;
1929
1930         error = 0;
1931         shmfd = fp->f_data;
1932         rl_cookie = shm_rangelock_wlock(shmfd, 0, OFF_MAX);
1933
1934         /* Even already-set seals should result in EPERM. */
1935         if ((shmfd->shm_seals & F_SEAL_SEAL) != 0) {
1936                 error = EPERM;
1937                 goto out;
1938         }
1939         nseals = seals & ~shmfd->shm_seals;
1940         if ((nseals & F_SEAL_WRITE) != 0) {
1941                 if (shm_largepage(shmfd)) {
1942                         error = ENOTSUP;
1943                         goto out;
1944                 }
1945
1946                 /*
1947                  * The rangelock above prevents writable mappings from being
1948                  * added after we've started applying seals.  The RLOCK here
1949                  * is to avoid torn reads on ILP32 arches as unmapping/reducing
1950                  * writemappings will be done without a rangelock.
1951                  */
1952                 VM_OBJECT_RLOCK(shmfd->shm_object);
1953                 writemappings = shmfd->shm_object->un_pager.swp.writemappings;
1954                 VM_OBJECT_RUNLOCK(shmfd->shm_object);
1955                 /* kmappings are also writable */
1956                 if (writemappings > 0) {
1957                         error = EBUSY;
1958                         goto out;
1959                 }
1960         }
1961         shmfd->shm_seals |= nseals;
1962 out:
1963         shm_rangelock_unlock(shmfd, rl_cookie);
1964         return (error);
1965 }
1966
1967 static int
1968 shm_get_seals(struct file *fp, int *seals)
1969 {
1970         struct shmfd *shmfd;
1971
1972         shmfd = fp->f_data;
1973         *seals = shmfd->shm_seals;
1974         return (0);
1975 }
1976
1977 static int
1978 shm_fallocate(struct file *fp, off_t offset, off_t len, struct thread *td)
1979 {
1980         void *rl_cookie;
1981         struct shmfd *shmfd;
1982         size_t size;
1983         int error;
1984
1985         /* This assumes that the caller already checked for overflow. */
1986         error = 0;
1987         shmfd = fp->f_data;
1988         size = offset + len;
1989
1990         /*
1991          * Just grab the rangelock for the range that we may be attempting to
1992          * grow, rather than blocking read/write for regions we won't be
1993          * touching while this (potential) resize is in progress.  Other
1994          * attempts to resize the shmfd will have to take a write lock from 0 to
1995          * OFF_MAX, so this being potentially beyond the current usable range of
1996          * the shmfd is not necessarily a concern.  If other mechanisms are
1997          * added to grow a shmfd, this may need to be re-evaluated.
1998          */
1999         rl_cookie = shm_rangelock_wlock(shmfd, offset, size);
2000         if (size > shmfd->shm_size)
2001                 error = shm_dotruncate_cookie(shmfd, size, rl_cookie);
2002         shm_rangelock_unlock(shmfd, rl_cookie);
2003         /* Translate to posix_fallocate(2) return value as needed. */
2004         if (error == ENOMEM)
2005                 error = ENOSPC;
2006         return (error);
2007 }
2008
2009 static int
2010 sysctl_posix_shm_list(SYSCTL_HANDLER_ARGS)
2011 {
2012         struct shm_mapping *shmm;
2013         struct sbuf sb;
2014         struct kinfo_file kif;
2015         u_long i;
2016         ssize_t curlen;
2017         int error, error2;
2018
2019         sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_file) * 5, req);
2020         sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2021         curlen = 0;
2022         error = 0;
2023         sx_slock(&shm_dict_lock);
2024         for (i = 0; i < shm_hash + 1; i++) {
2025                 LIST_FOREACH(shmm, &shm_dictionary[i], sm_link) {
2026                         error = shm_fill_kinfo_locked(shmm->sm_shmfd,
2027                             &kif, true);
2028                         if (error == EPERM) {
2029                                 error = 0;
2030                                 continue;
2031                         }
2032                         if (error != 0)
2033                                 break;
2034                         pack_kinfo(&kif);
2035                         error = sbuf_bcat(&sb, &kif, kif.kf_structsize) == 0 ?
2036                             0 : ENOMEM;
2037                         if (error != 0)
2038                                 break;
2039                         curlen += kif.kf_structsize;
2040                 }
2041         }
2042         sx_sunlock(&shm_dict_lock);
2043         error2 = sbuf_finish(&sb);
2044         sbuf_delete(&sb);
2045         return (error != 0 ? error : error2);
2046 }
2047
2048 SYSCTL_PROC(_kern_ipc, OID_AUTO, posix_shm_list,
2049     CTLFLAG_RD | CTLFLAG_PRISON | CTLFLAG_MPSAFE | CTLTYPE_OPAQUE,
2050     NULL, 0, sysctl_posix_shm_list, "",
2051     "POSIX SHM list");
2052
2053 int
2054 kern_shm_open(struct thread *td, const char *path, int flags, mode_t mode,
2055     struct filecaps *caps)
2056 {
2057
2058         return (kern_shm_open2(td, path, flags, mode, 0, caps, NULL));
2059 }
2060
2061 /*
2062  * This version of the shm_open() interface leaves CLOEXEC behavior up to the
2063  * caller, and libc will enforce it for the traditional shm_open() call.  This
2064  * allows other consumers, like memfd_create(), to opt-in for CLOEXEC.  This
2065  * interface also includes a 'name' argument that is currently unused, but could
2066  * potentially be exported later via some interface for debugging purposes.
2067  * From the kernel's perspective, it is optional.  Individual consumers like
2068  * memfd_create() may require it in order to be compatible with other systems
2069  * implementing the same function.
2070  */
2071 int
2072 sys_shm_open2(struct thread *td, struct shm_open2_args *uap)
2073 {
2074
2075         return (kern_shm_open2(td, uap->path, uap->flags, uap->mode,
2076             uap->shmflags, NULL, uap->name));
2077 }