]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/utils.h
github: Update for main branch
[mirror_lxc.git] / src / lxc / utils.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXC_UTILS_H
4 #define __LXC_UTILS_H
5
6 #include <errno.h>
7 #include <linux/loop.h>
8 #include <linux/types.h>
9 #include <stdarg.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <sys/syscall.h>
14 #include <sys/types.h>
15 #include <sys/vfs.h>
16 #include <unistd.h>
17
18 #include "compiler.h"
19 #include "file_utils.h"
20 #include "initutils.h"
21 #include "macro.h"
22 #include "memory_utils.h"
23 #include "process_utils.h"
24 #include "string_utils.h"
25
26 /* returns 1 on success, 0 if there were any failures */
27 __hidden extern int lxc_rmdir_onedev(const char *path, const char *exclude);
28 __hidden extern int get_u16(unsigned short *val, const char *arg, int base);
29 __hidden extern int lxc_mkdir_p(const char *dir, mode_t mode);
30 __hidden extern char *get_rundir(void);
31
32 /* Define getline() if missing from the C library */
33 #if !HAVE_GETLINE
34 #if !HAVE_FGETLN
35 #include "getline.h"
36 #endif
37 #endif
38
39 static inline int lxc_set_cloexec(int fd)
40 {
41 return fcntl(fd, F_SETFD, FD_CLOEXEC);
42 }
43
44 /*
45 * Struct to carry child pid from lxc_popen() to lxc_pclose(). Not an opaque
46 * struct to allow direct access to the underlying FILE without additional
47 * wrappers.
48 */
49 struct lxc_popen_FILE {
50 int pipe;
51 FILE *f;
52 pid_t child_pid;
53 };
54
55 /* popen(command, "re") replacement that restores default signal mask
56 * via sigprocmask(2) (unblocks all signals) after fork(2) but prior to calling exec(3).
57 * In short, popen(command, "re") does pipe() + fork() + exec()
58 * while lxc_popen(command) does pipe() + fork() + sigprocmask() + exec().
59 * Returns pointer to struct lxc_popen_FILE, that should be freed with lxc_pclose().
60 * On error returns NULL.
61 */
62 __hidden extern struct lxc_popen_FILE *lxc_popen(const char *command);
63
64 /* pclose() replacement to be used on struct lxc_popen_FILE *,
65 * returned by lxc_popen().
66 * Waits for associated process to terminate, returns its exit status and
67 * frees resources, pointed to by struct lxc_popen_FILE *.
68 */
69 __hidden extern int lxc_pclose(struct lxc_popen_FILE *fp);
70
71 static inline void __auto_lxc_pclose__(struct lxc_popen_FILE **f)
72 {
73 if (*f)
74 lxc_pclose(*f);
75 }
76 #define __do_lxc_pclose __attribute__((__cleanup__(__auto_lxc_pclose__)))
77
78 /*
79 * wait on a child we forked
80 */
81 __hidden extern int wait_for_pid(pid_t pid);
82 __hidden extern int lxc_wait_for_pid_status(pid_t pid);
83 __hidden extern int wait_for_pidfd(int pidfd);
84 __hidden extern bool wait_exited(pid_t pid);
85
86 #if HAVE_OPENSSL
87 __hidden extern int sha1sum_file(char *fnam, unsigned char *md_value, unsigned int *md_len);
88 #endif
89
90 /* initialize rand with urandom */
91 __hidden extern int randseed(bool);
92
93 /* are we unprivileged with respect to our namespaces */
94 inline static bool am_guest_unpriv(void) {
95 return geteuid() != 0;
96 }
97
98 /* are we unprivileged with respect to init_user_ns */
99 inline static bool am_host_unpriv(void)
100 {
101 __do_fclose FILE *f = NULL;
102 uid_t user, host, count;
103 int ret;
104
105 if (geteuid() != 0)
106 return true;
107
108 /* Now: are we in a user namespace? Because then we're also
109 * unprivileged.
110 */
111 f = fopen("/proc/self/uid_map", "re");
112 if (!f)
113 return false;
114
115 ret = fscanf(f, "%u %u %u", &user, &host, &count);
116 if (ret != 3)
117 return false;
118
119 return user != 0 || host != 0 || count != UINT32_MAX;
120 }
121
122 /*
123 * parse /proc/self/uid_map to find what @orig maps to
124 */
125 __hidden extern uid_t get_ns_uid(uid_t orig);
126 /*
127 * parse /proc/self/gid_map to find what @orig maps to
128 */
129 __hidden extern gid_t get_ns_gid(gid_t orig);
130
131 __hidden extern bool dir_exists(const char *path);
132
133 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
134 __hidden extern uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval);
135
136 __hidden extern bool is_shared_mountpoint(const char *path);
137 __hidden extern int detect_shared_rootfs(void);
138 __hidden extern bool detect_ramfs_rootfs(void);
139 __hidden extern char *on_path(const char *cmd, const char *rootfs);
140 __hidden extern char *choose_init(const char *rootfs);
141 __hidden extern bool switch_to_ns(pid_t pid, const char *ns);
142 __hidden extern char *get_template_path(const char *t);
143 __hidden extern int safe_mount(const char *src, const char *dest, const char *fstype,
144 unsigned long flags, const void *data, const char *rootfs);
145 __hidden extern int open_devnull(void);
146 __hidden extern int set_stdfds(int fd);
147 __hidden extern int null_stdfds(void);
148 __hidden extern int lxc_preserve_ns(const int pid, const char *ns);
149
150 /* Check whether a signal is blocked by a process. */
151 __hidden extern bool task_blocks_signal(pid_t pid, int signal);
152
153 /* Switch to a new uid and gid.
154 * If LXC_INVALID_{G,U}ID is passed then the set{g,u}id() will not be called.
155 */
156 __hidden extern bool lxc_switch_uid_gid(uid_t uid, gid_t gid);
157 __hidden extern bool lxc_setgroups(gid_t list[], size_t size);
158 __hidden extern bool lxc_drop_groups(void);
159
160 /* Find an unused loop device and associate it with source. */
161 __hidden extern int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags);
162
163 /* Clear all mounts on a given node.
164 * >= 0 successfully cleared. The number returned is the number of umounts
165 * performed.
166 * < 0 error umounting. Return -errno.
167 */
168 __hidden extern int lxc_unstack_mountpoint(const char *path, bool lazy);
169
170 /*
171 * run_command runs a command and collect it's std{err,out} output in buf.
172 *
173 * @param[out] buf The buffer where the commands std{err,out] output will be
174 * read into. If no output was produced, buf will be memset
175 * to 0.
176 * @param[in] buf_size The size of buf. This function will reserve one byte for
177 * \0-termination.
178 * @param[in] child_fn The function to be run in the child process. This
179 * function must exec.
180 * @param[in] args Arguments to be passed to child_fn.
181 */
182 __hidden extern int run_command(char *buf, size_t buf_size, int (*child_fn)(void *), void *args);
183
184 /*
185 * run_command runs a command and collect it's std{err,out} output in buf, returns exit status.
186 *
187 * @param[out] buf The buffer where the commands std{err,out] output will be
188 * read into. If no output was produced, buf will be memset
189 * to 0.
190 * @param[in] buf_size The size of buf. This function will reserve one byte for
191 * \0-termination.
192 * @param[in] child_fn The function to be run in the child process. This
193 * function must exec.
194 * @param[in] args Arguments to be passed to child_fn.
195 */
196 __hidden extern int run_command_status(char *buf, size_t buf_size, int (*child_fn)(void *),
197 void *args);
198
199 __hidden extern bool lxc_nic_exists(char *nic);
200
201 static inline uint64_t lxc_getpagesize(void)
202 {
203 int64_t pgsz;
204
205 pgsz = sysconf(_SC_PAGESIZE);
206 if (pgsz <= 0)
207 pgsz = 1 << 12;
208
209 return pgsz;
210 }
211
212 /* If n is not a power of 2 this function will return the next power of 2
213 * greater than that number. Note that this function always returns the *next*
214 * power of 2 *greater* that number not the *nearest*. For example, passing 1025
215 * as argument this function will return 2048 although the closest power of 2
216 * would be 1024.
217 * If the caller passes in 0 they will receive 0 in return since this is invalid
218 * input and 0 is not a power of 2.
219 */
220 __hidden extern uint64_t lxc_find_next_power2(uint64_t n);
221
222 /* Set a signal the child process will receive after the parent has died. */
223 __hidden extern int lxc_set_death_signal(int signal, pid_t parent, int parent_status_fd);
224 __hidden extern int lxc_rm_rf(const char *dirname);
225 __hidden extern bool lxc_can_use_pidfd(int pidfd);
226
227 __hidden extern int fix_stdio_permissions(uid_t uid);
228
229 static inline bool uid_valid(uid_t uid)
230 {
231 return uid != LXC_INVALID_UID;
232 }
233
234 static inline bool gid_valid(gid_t gid)
235 {
236 return gid != LXC_INVALID_GID;
237 }
238
239 __hidden extern bool multiply_overflow(int64_t base, uint64_t mult, int64_t *res);
240
241 __hidden extern int safe_mount_beneath(const char *beneath, const char *src, const char *dst,
242 const char *fstype, unsigned int flags, const void *data);
243 __hidden extern int safe_mount_beneath_at(int beneat_fd, const char *src, const char *dst,
244 const char *fstype, unsigned int flags, const void *data);
245 __hidden __lxc_unused int print_r(int fd, const char *path);
246
247 static inline int copy_struct_from_client(__u32 server_size, void *dst,
248 __u32 client_size, const void *src)
249 {
250 __u32 size = min(server_size, client_size);
251 __u32 rest = min(server_size, client_size) - size;
252
253 /* Deal with trailing bytes. */
254 if (client_size < server_size) {
255 memset(dst + size, 0, rest);
256 } else if (client_size > server_size) {
257 /* TODO: Actually come up with a nice way to test for 0. */
258 return 0;
259 }
260
261 memcpy(dst, src, size);
262 return 0;
263 }
264
265 static inline __u32 copy_struct_to_client(__u32 client_size, void *dst,
266 __u32 server_size, const void *src)
267 {
268 __u32 size = min(server_size, client_size);
269 memcpy(dst, src, size);
270 return size;
271 }
272
273 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
274 static inline int is_in_comm(const char *s)
275 {
276 __do_free char *buf = NULL;
277 __do_free char *comm = NULL;
278 size_t buf_size;
279
280 buf = file_to_buf("/proc/self/comm", &buf_size);
281 if (!buf)
282 return -1;
283
284 if (buf_size == 0)
285 return -1;
286
287 comm = malloc(buf_size + 1);
288 if (!comm)
289 return -1;
290 memcpy(comm, buf, buf_size);
291 comm[buf_size] = '\0';
292
293 return strstr(comm, s) != NULL;
294 }
295 #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
296
297 #endif /* __LXC_UTILS_H */