1 /* 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice(s), this list of conditions and the following disclaimer as 12 * the first lines of this file unmodified other than the possible 13 * addition of one or more copyright notices. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice(s), this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE 23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 26 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 28 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/types.h> 36 #include <sys/mman.h> 37 #include <sys/syscall.h> 38 39 #include <errno.h> 40 #include <fcntl.h> 41 #include <limits.h> 42 #include <unistd.h> 43 #include <stdio.h> 44 #include <string.h> 45 46 #include "libc_private.h" 47 48 __weak_reference(shm_open, _shm_open); 49 __weak_reference(shm_open, __sys_shm_open); 50 51 #define SHM_OPEN2_OSREL 1300048 52 53 #define MEMFD_NAME_PREFIX "memfd:" 54 55 int 56 shm_open(const char *path, int flags, mode_t mode) 57 { 58 59 if (__getosreldate() >= SHM_OPEN2_OSREL) 60 return (__sys_shm_open2(path, flags | O_CLOEXEC, mode, 0, 61 NULL)); 62 63 /* 64 * Fallback to shm_open(2) on older kernels. The kernel will enforce 65 * O_CLOEXEC in this interface, unlike the newer shm_open2 which does 66 * not enforce it. The newer interface allows memfd_create(), for 67 * instance, to not have CLOEXEC on the returned fd. 68 */ 69 return (syscall(SYS_freebsd12_shm_open, path, flags, mode)); 70 } 71 72 /* 73 * The path argument is passed to the kernel, but the kernel doesn't currently 74 * do anything with it. Linux exposes it in linprocfs for debugging purposes 75 * only, but our kernel currently will not do the same. 76 */ 77 int 78 memfd_create(const char *name, unsigned int flags) 79 { 80 char memfd_name[NAME_MAX + 1]; 81 size_t namelen; 82 int oflags, shmflags; 83 84 if (name == NULL) 85 return (EBADF); 86 namelen = strlen(name); 87 if (namelen + sizeof(MEMFD_NAME_PREFIX) - 1 > NAME_MAX) 88 return (EINVAL); 89 if ((flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB | 90 MFD_HUGE_MASK)) != 0) 91 return (EINVAL); 92 /* HUGETLB set with no size specified. */ 93 if ((flags & MFD_HUGETLB) != 0 && (flags & MFD_HUGE_MASK) == 0) 94 return (EINVAL); 95 /* Size specified but no HUGETLB. */ 96 if ((flags & MFD_HUGE_MASK) != 0 && (flags & MFD_HUGETLB) == 0) 97 return (EINVAL); 98 /* We don't actually support HUGETLB. */ 99 if ((flags & MFD_HUGETLB) != 0) 100 return (ENOSYS); 101 102 /* We've already validated that we're sufficiently sized. */ 103 snprintf(memfd_name, NAME_MAX + 1, "%s%s", MEMFD_NAME_PREFIX, name); 104 oflags = O_RDWR; 105 shmflags = 0; 106 if ((flags & MFD_CLOEXEC) != 0) 107 oflags |= O_CLOEXEC; 108 if ((flags & MFD_ALLOW_SEALING) != 0) 109 shmflags |= SHM_ALLOW_SEALING; 110 return (__sys_shm_open2(SHM_ANON, oflags, 0, shmflags, memfd_name)); 111 } 112