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