]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/memfd-util.c
Merge pull request #1695 from evverx/fix-cap-bounding-merging
[thirdparty/systemd.git] / src / basic / memfd-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <fcntl.h>
23 #ifdef HAVE_LINUX_MEMFD_H
24 #include <linux/memfd.h>
25 #endif
26 #include <stdio.h>
27 #include <sys/mman.h>
28 #include <sys/prctl.h>
29
30 #include "alloc-util.h"
31 #include "fd-util.h"
32 #include "memfd-util.h"
33 #include "missing.h"
34 #include "string-util.h"
35 #include "utf8.h"
36 #include "util.h"
37
38 int memfd_new(const char *name) {
39 _cleanup_free_ char *g = NULL;
40 int fd;
41
42 if (!name) {
43 char pr[17] = {};
44
45 /* If no name is specified we generate one. We include
46 * a hint indicating our library implementation, and
47 * add the thread name to it */
48
49 assert_se(prctl(PR_GET_NAME, (unsigned long) pr) >= 0);
50
51 if (isempty(pr))
52 name = "sd";
53 else {
54 _cleanup_free_ char *e = NULL;
55
56 e = utf8_escape_invalid(pr);
57 if (!e)
58 return -ENOMEM;
59
60 g = strappend("sd-", e);
61 if (!g)
62 return -ENOMEM;
63
64 name = g;
65 }
66 }
67
68 fd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
69 if (fd < 0)
70 return -errno;
71
72 return fd;
73 }
74
75 int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
76 void *q;
77 int sealed;
78
79 assert(fd >= 0);
80 assert(size > 0);
81 assert(p);
82
83 sealed = memfd_get_sealed(fd);
84 if (sealed < 0)
85 return sealed;
86
87 if (sealed)
88 q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
89 else
90 q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
91
92 if (q == MAP_FAILED)
93 return -errno;
94
95 *p = q;
96 return 0;
97 }
98
99 int memfd_set_sealed(int fd) {
100 int r;
101
102 assert(fd >= 0);
103
104 r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
105 if (r < 0)
106 return -errno;
107
108 return 0;
109 }
110
111 int memfd_get_sealed(int fd) {
112 int r;
113
114 assert(fd >= 0);
115
116 r = fcntl(fd, F_GET_SEALS);
117 if (r < 0)
118 return -errno;
119
120 return r == (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
121 }
122
123 int memfd_get_size(int fd, uint64_t *sz) {
124 struct stat stat;
125 int r;
126
127 assert(fd >= 0);
128 assert(sz);
129
130 r = fstat(fd, &stat);
131 if (r < 0)
132 return -errno;
133
134 *sz = stat.st_size;
135 return 0;
136 }
137
138 int memfd_set_size(int fd, uint64_t sz) {
139 int r;
140
141 assert(fd >= 0);
142
143 r = ftruncate(fd, sz);
144 if (r < 0)
145 return -errno;
146
147 return 0;
148 }
149
150 int memfd_new_and_map(const char *name, size_t sz, void **p) {
151 _cleanup_close_ int fd = -1;
152 int r;
153
154 assert(sz > 0);
155 assert(p);
156
157 fd = memfd_new(name);
158 if (fd < 0)
159 return fd;
160
161 r = memfd_set_size(fd, sz);
162 if (r < 0)
163 return r;
164
165 r = memfd_map(fd, 0, sz, p);
166 if (r < 0)
167 return r;
168
169 r = fd;
170 fd = -1;
171
172 return r;
173 }