1 /* SPDX-License-Identifier: GPL-2.0-or-later
2 * Copyright (c) 2010-2018 Linux Test Project
3 * Copyright (c) 2011-2015 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6 #ifndef TST_SAFE_MACROS_H__
7 #define TST_SAFE_MACROS_H__
8
9 #include <sys/mman.h>
10 #include <sys/types.h>
11 #include <sys/time.h>
12 #include <sys/resource.h>
13 #include <sys/stat.h>
14 #include <sys/vfs.h>
15 #include <fcntl.h>
16 #include <libgen.h>
17 #include <signal.h>
18 #include <stdarg.h>
19 #include <unistd.h>
20 #include <dirent.h>
21 #include <grp.h>
22
23 #include "safe_macros_fn.h"
24 #include "tst_cmd.h"
25
26 #define SAFE_BASENAME(path) \
27 safe_basename(__FILE__, __LINE__, NULL, (path))
28
29 #define SAFE_CHDIR(path) \
30 safe_chdir(__FILE__, __LINE__, NULL, (path))
31
32 #define SAFE_CLOSE(fd) do { \
33 safe_close(__FILE__, __LINE__, NULL, (fd)); \
34 fd = -1; \
35 } while (0)
36
37 #define SAFE_CREAT(pathname, mode) \
38 safe_creat(__FILE__, __LINE__, NULL, (pathname), (mode))
39
40 #define SAFE_CHROOT(path) \
41 safe_chroot(__FILE__, __LINE__, (path))
42 int safe_chroot(const char *file, const int lineno, const char *path);
43
44 #define SAFE_DIRNAME(path) \
45 safe_dirname(__FILE__, __LINE__, NULL, (path))
46
safe_dup(const char * file,const int lineno,int oldfd)47 static inline int safe_dup(const char *file, const int lineno,
48 int oldfd)
49 {
50 int rval;
51
52 rval = dup(oldfd);
53 if (rval == -1) {
54 tst_brk_(file, lineno, TBROK | TERRNO,
55 "dup(%i) failed", oldfd);
56 }
57
58 return rval;
59 }
60 #define SAFE_DUP(oldfd) \
61 safe_dup(__FILE__, __LINE__, (oldfd))
62
63 #define SAFE_GETCWD(buf, size) \
64 safe_getcwd(__FILE__, __LINE__, NULL, (buf), (size))
65
66 #define SAFE_GETPWNAM(name) \
67 safe_getpwnam(__FILE__, __LINE__, NULL, (name))
68
69 #define SAFE_GETRUSAGE(who, usage) \
70 safe_getrusage(__FILE__, __LINE__, NULL, (who), (usage))
71
72 #define SAFE_MALLOC(size) \
73 safe_malloc(__FILE__, __LINE__, NULL, (size))
74
75 #define SAFE_MKDIR(pathname, mode) \
76 safe_mkdir(__FILE__, __LINE__, NULL, (pathname), (mode))
77
78 #define SAFE_RMDIR(pathname) \
79 safe_rmdir(__FILE__, __LINE__, NULL, (pathname))
80
81 #define SAFE_MUNMAP(addr, length) \
82 safe_munmap(__FILE__, __LINE__, NULL, (addr), (length))
83
84 #define SAFE_OPEN(pathname, oflags, ...) \
85 safe_open(__FILE__, __LINE__, NULL, (pathname), (oflags), \
86 ##__VA_ARGS__)
87
88 #define SAFE_PIPE(fildes) \
89 safe_pipe(__FILE__, __LINE__, NULL, (fildes))
90
91 int safe_pipe2(const char *file, const int lineno, int fildes[2], int flags);
92
93 #define SAFE_PIPE2(fildes, flags) \
94 safe_pipe2(__FILE__, __LINE__, (fildes), (flags))
95
96 #define SAFE_READ(len_strict, fildes, buf, nbyte) \
97 safe_read(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
98
99 #define SAFE_SETEGID(egid) \
100 safe_setegid(__FILE__, __LINE__, NULL, (egid))
101
102 #define SAFE_SETEUID(euid) \
103 safe_seteuid(__FILE__, __LINE__, NULL, (euid))
104
105 #define SAFE_SETGID(gid) \
106 safe_setgid(__FILE__, __LINE__, NULL, (gid))
107
108 #define SAFE_SETUID(uid) \
109 safe_setuid(__FILE__, __LINE__, NULL, (uid))
110
111 int safe_setregid(const char *file, const int lineno,
112 gid_t rgid, gid_t egid);
113
114 #define SAFE_SETREGID(rgid, egid) \
115 safe_setregid(__FILE__, __LINE__, (rgid), (egid))
116
117 int safe_setreuid(const char *file, const int lineno,
118 uid_t ruid, uid_t euid);
119
120 #define SAFE_SETREUID(ruid, euid) \
121 safe_setreuid(__FILE__, __LINE__, (ruid), (euid))
122
123 #define SAFE_GETRESUID(ruid, euid, suid) \
124 safe_getresuid(__FILE__, __LINE__, NULL, (ruid), (euid), (suid))
125
126 #define SAFE_GETRESGID(rgid, egid, sgid) \
127 safe_getresgid(__FILE__, __LINE__, NULL, (rgid), (egid), (sgid))
128
129 int safe_setpgid(const char *file, const int lineno, pid_t pid, pid_t pgid);
130
131 #define SAFE_SETPGID(pid, pgid) \
132 safe_setpgid(__FILE__, __LINE__, (pid), (pgid));
133
134 pid_t safe_getpgid(const char *file, const int lineno, pid_t pid);
135
136 #define SAFE_GETPGID(pid) \
137 safe_getpgid(__FILE__, __LINE__, (pid))
138
139 #define SAFE_UNLINK(pathname) \
140 safe_unlink(__FILE__, __LINE__, NULL, (pathname))
141
142 #define SAFE_LINK(oldpath, newpath) \
143 safe_link(__FILE__, __LINE__, NULL, (oldpath), (newpath))
144
145 #define SAFE_LINKAT(olddirfd, oldpath, newdirfd, newpath, flags) \
146 safe_linkat(__FILE__, __LINE__, NULL, (olddirfd), (oldpath), \
147 (newdirfd), (newpath), (flags))
148
149 #define SAFE_READLINK(path, buf, bufsize) \
150 safe_readlink(__FILE__, __LINE__, NULL, (path), (buf), (bufsize))
151
152 #define SAFE_SYMLINK(oldpath, newpath) \
153 safe_symlink(__FILE__, __LINE__, NULL, (oldpath), (newpath))
154
155 #define SAFE_WRITE(len_strict, fildes, buf, nbyte) \
156 safe_write(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
157
158 #define SAFE_STRTOL(str, min, max) \
159 safe_strtol(__FILE__, __LINE__, NULL, (str), (min), (max))
160
161 #define SAFE_STRTOUL(str, min, max) \
162 safe_strtoul(__FILE__, __LINE__, NULL, (str), (min), (max))
163
164 #define SAFE_SYSCONF(name) \
165 safe_sysconf(__FILE__, __LINE__, NULL, name)
166
167 #define SAFE_CHMOD(path, mode) \
168 safe_chmod(__FILE__, __LINE__, NULL, (path), (mode))
169
170 #define SAFE_FCHMOD(fd, mode) \
171 safe_fchmod(__FILE__, __LINE__, NULL, (fd), (mode))
172
173 #define SAFE_CHOWN(path, owner, group) \
174 safe_chown(__FILE__, __LINE__, NULL, (path), (owner), (group))
175
176 #define SAFE_FCHOWN(fd, owner, group) \
177 safe_fchown(__FILE__, __LINE__, NULL, (fd), (owner), (group))
178
179 #define SAFE_WAIT(status) \
180 safe_wait(__FILE__, __LINE__, NULL, (status))
181
182 #define SAFE_WAITPID(pid, status, opts) \
183 safe_waitpid(__FILE__, __LINE__, NULL, (pid), (status), (opts))
184
185 #define SAFE_KILL(pid, sig) \
186 safe_kill(__FILE__, __LINE__, NULL, (pid), (sig))
187
188 #define SAFE_MEMALIGN(alignment, size) \
189 safe_memalign(__FILE__, __LINE__, NULL, (alignment), (size))
190
191 #define SAFE_MKFIFO(pathname, mode) \
192 safe_mkfifo(__FILE__, __LINE__, NULL, (pathname), (mode))
193
194 #define SAFE_RENAME(oldpath, newpath) \
195 safe_rename(__FILE__, __LINE__, NULL, (oldpath), (newpath))
196
197 #define SAFE_MOUNT(source, target, filesystemtype, \
198 mountflags, data) \
199 safe_mount(__FILE__, __LINE__, NULL, (source), (target), \
200 (filesystemtype), (mountflags), (data))
201
202 #define SAFE_UMOUNT(target) \
203 safe_umount(__FILE__, __LINE__, NULL, (target))
204
205 #define SAFE_OPENDIR(name) \
206 safe_opendir(__FILE__, __LINE__, NULL, (name))
207
208 #define SAFE_CLOSEDIR(dirp) \
209 safe_closedir(__FILE__, __LINE__, NULL, (dirp))
210
211 #define SAFE_READDIR(dirp) \
212 safe_readdir(__FILE__, __LINE__, NULL, (dirp))
213
214 #define SAFE_IOCTL(fd, request, ...) \
215 ({int tst_ret_ = ioctl(fd, request, ##__VA_ARGS__); \
216 tst_ret_ < 0 ? \
217 tst_brk(TBROK | TERRNO, \
218 "ioctl(%i,%s,...) failed", fd, #request), 0 \
219 : tst_ret_;})
220
221 #define SAFE_FCNTL(fd, cmd, ...) \
222 ({int tst_ret_ = fcntl(fd, cmd, ##__VA_ARGS__); \
223 tst_ret_ == -1 ? \
224 tst_brk(TBROK | TERRNO, \
225 "fcntl(%i,%s,...) failed", fd, #cmd), 0 \
226 : tst_ret_;})
227
228 /*
229 * following functions are inline because the behaviour may depend on
230 * -D_FILE_OFFSET_BITS=64 -DOFF_T=off64_t compile flags
231 */
232
safe_mmap(const char * file,const int lineno,void * addr,size_t length,int prot,int flags,int fd,off_t offset)233 static inline void *safe_mmap(const char *file, const int lineno,
234 void *addr, size_t length,
235 int prot, int flags, int fd, off_t offset)
236 {
237 void *rval;
238
239 rval = mmap(addr, length, prot, flags, fd, offset);
240 if (rval == MAP_FAILED) {
241 tst_brk_(file, lineno, TBROK | TERRNO,
242 "mmap(%p,%zu,%d,%d,%d,%ld) failed",
243 addr, length, prot, flags, fd, (long) offset);
244 }
245
246 return rval;
247 }
248 #define SAFE_MMAP(addr, length, prot, flags, fd, offset) \
249 safe_mmap(__FILE__, __LINE__, (addr), (length), (prot), \
250 (flags), (fd), (offset))
251
safe_ftruncate(const char * file,const int lineno,int fd,off_t length)252 static inline int safe_ftruncate(const char *file, const int lineno,
253 int fd, off_t length)
254 {
255 int rval;
256
257 rval = ftruncate(fd, length);
258 if (rval == -1) {
259 tst_brk_(file, lineno, TBROK | TERRNO,
260 "ftruncate(%d,%ld) failed",
261 fd, (long)length);
262 }
263
264 return rval;
265 }
266 #define SAFE_FTRUNCATE(fd, length) \
267 safe_ftruncate(__FILE__, __LINE__, (fd), (length))
268
safe_truncate(const char * file,const int lineno,const char * path,off_t length)269 static inline int safe_truncate(const char *file, const int lineno,
270 const char *path, off_t length)
271 {
272 int rval;
273
274 rval = truncate(path, length);
275 if (rval == -1) {
276 tst_brk_(file, lineno, TBROK | TERRNO,
277 "truncate(%s,%ld) failed",
278 path, (long)length);
279 }
280
281 return rval;
282 }
283 #define SAFE_TRUNCATE(path, length) \
284 safe_truncate(__FILE__, __LINE__, (path), (length))
285
safe_stat(const char * file,const int lineno,const char * path,struct stat * buf)286 static inline int safe_stat(const char *file, const int lineno,
287 const char *path, struct stat *buf)
288 {
289 int rval;
290
291 rval = stat(path, buf);
292
293 if (rval == -1) {
294 tst_brk_(file, lineno, TBROK | TERRNO,
295 "stat(%s,%p) failed", path, buf);
296 }
297
298 return rval;
299 }
300 #define SAFE_STAT(path, buf) \
301 safe_stat(__FILE__, __LINE__, (path), (buf))
302
safe_fstat(const char * file,const int lineno,int fd,struct stat * buf)303 static inline int safe_fstat(const char *file, const int lineno,
304 int fd, struct stat *buf)
305 {
306 int rval;
307
308 rval = fstat(fd, buf);
309
310 if (rval == -1) {
311 tst_brk_(file, lineno, TBROK | TERRNO,
312 "fstat(%d,%p) failed", fd, buf);
313 }
314
315 return rval;
316 }
317 #define SAFE_FSTAT(fd, buf) \
318 safe_fstat(__FILE__, __LINE__, (fd), (buf))
319
safe_lstat(const char * file,const int lineno,const char * path,struct stat * buf)320 static inline int safe_lstat(const char *file, const int lineno,
321 const char *path, struct stat *buf)
322 {
323 int rval;
324
325 rval = lstat(path, buf);
326
327 if (rval == -1) {
328 tst_brk_(file, lineno, TBROK | TERRNO,
329 "lstat(%s,%p) failed", path, buf);
330 }
331
332 return rval;
333 }
334 #define SAFE_LSTAT(path, buf) \
335 safe_lstat(__FILE__, __LINE__, (path), (buf))
336
safe_statfs(const char * file,const int lineno,const char * path,struct statfs * buf)337 static inline int safe_statfs(const char *file, const int lineno,
338 const char *path, struct statfs *buf)
339 {
340 int rval;
341
342 rval = statfs(path, buf);
343
344 if (rval == -1) {
345 tst_brk_(file, lineno, TBROK | TERRNO,
346 "statfs(%s,%p) failed", path, buf);
347 }
348
349 return rval;
350 }
351 #define SAFE_STATFS(path, buf) \
352 safe_statfs(__FILE__, __LINE__, (path), (buf))
353
safe_lseek(const char * file,const int lineno,int fd,off_t offset,int whence)354 static inline off_t safe_lseek(const char *file, const int lineno,
355 int fd, off_t offset, int whence)
356 {
357 off_t rval;
358
359 rval = lseek(fd, offset, whence);
360
361 if (rval == (off_t) -1) {
362 tst_brk_(file, lineno, TBROK | TERRNO,
363 "lseek(%d,%ld,%d) failed",
364 fd, (long)offset, whence);
365 }
366
367 return rval;
368 }
369 #define SAFE_LSEEK(fd, offset, whence) \
370 safe_lseek(__FILE__, __LINE__, (fd), (offset), (whence))
371
safe_getrlimit(const char * file,const int lineno,int resource,struct rlimit * rlim)372 static inline int safe_getrlimit(const char *file, const int lineno,
373 int resource, struct rlimit *rlim)
374 {
375 int rval;
376
377 rval = getrlimit(resource, rlim);
378
379 if (rval == -1) {
380 tst_brk_(file, lineno, TBROK | TERRNO,
381 "getrlimit(%d,%p) failed",
382 resource, rlim);
383 }
384
385 return rval;
386 }
387 #define SAFE_GETRLIMIT(resource, rlim) \
388 safe_getrlimit(__FILE__, __LINE__, (resource), (rlim))
389
safe_setrlimit(const char * file,const int lineno,int resource,const struct rlimit * rlim)390 static inline int safe_setrlimit(const char *file, const int lineno,
391 int resource, const struct rlimit *rlim)
392 {
393 int rval;
394
395 rval = setrlimit(resource, rlim);
396
397 if (rval == -1) {
398 tst_brk_(file, lineno, TBROK | TERRNO,
399 "setrlimit(%d,%p) failed",
400 resource, rlim);
401 }
402
403 return rval;
404 }
405 #define SAFE_SETRLIMIT(resource, rlim) \
406 safe_setrlimit(__FILE__, __LINE__, (resource), (rlim))
407
408 typedef void (*sighandler_t)(int);
safe_signal(const char * file,const int lineno,int signum,sighandler_t handler)409 static inline sighandler_t safe_signal(const char *file, const int lineno,
410 int signum, sighandler_t handler)
411 {
412 sighandler_t rval;
413
414 rval = signal(signum, handler);
415
416 if (rval == SIG_ERR) {
417 tst_brk_(file, lineno, TBROK | TERRNO,
418 "signal(%d,%p) failed",
419 signum, handler);
420 }
421
422 return rval;
423 }
424
425 #define SAFE_SIGNAL(signum, handler) \
426 safe_signal(__FILE__, __LINE__, (signum), (handler))
427
428 int safe_sigaction(const char *file, const int lineno,
429 int signum, const struct sigaction *act,
430 struct sigaction *oldact);
431 #define SAFE_SIGACTION(signum, act, oldact) \
432 safe_sigaction(__FILE__, __LINE__, (signum), (act), (oldact))
433
434 #define SAFE_EXECLP(file, arg, ...) do { \
435 execlp((file), (arg), ##__VA_ARGS__); \
436 tst_brk_(__FILE__, __LINE__, TBROK | TERRNO, \
437 "execlp(%s, %s, ...) failed", file, arg); \
438 } while (0)
439
440 #define SAFE_EXECL(file, arg, ...) do { \
441 execl((file), (arg), ##__VA_ARGS__); \
442 tst_brk_(__FILE__, __LINE__, TBROK | TERRNO, \
443 "execl(%s, %s, ...) failed", file, arg); \
444 } while (0)
445
446 int safe_getpriority(const char *file, const int lineno, int which, id_t who);
447 #define SAFE_GETPRIORITY(which, who) \
448 safe_getpriority(__FILE__, __LINE__, (which), (who))
449
450 struct group *safe_getgrnam(const char *file, const int lineno,
451 const char *name);
452 #define SAFE_GETGRNAM(name) \
453 safe_getgrnam(__FILE__, __LINE__, (name))
454
455 struct group *safe_getgrnam_fallback(const char *file, const int lineno,
456 const char *name, const char *fallback);
457 #define SAFE_GETGRNAM_FALLBACK(name, fallback) \
458 safe_getgrnam_fallback(__FILE__, __LINE__, (name), (fallback))
459
460 struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid);
461 #define SAFE_GETGRGID(gid) \
462 safe_getgrgid(__FILE__, __LINE__, (gid))
463
464 ssize_t safe_getxattr(const char *file, const int lineno, const char *path,
465 const char *name, void *value, size_t size);
466 #define SAFE_GETXATTR(path, name, value, size) \
467 safe_getxattr(__FILE__, __LINE__, (path), (name), (value), (size))
468
469 int safe_setxattr(const char *file, const int lineno, const char *path,
470 const char *name, const void *value, size_t size, int flags);
471 #define SAFE_SETXATTR(path, name, value, size, flags) \
472 safe_setxattr(__FILE__, __LINE__, (path), (name), (value), (size), (flags))
473
474 int safe_lsetxattr(const char *file, const int lineno, const char *path,
475 const char *name, const void *value, size_t size, int flags);
476 #define SAFE_LSETXATTR(path, name, value, size, flags) \
477 safe_lsetxattr(__FILE__, __LINE__, (path), (name), (value), (size), (flags))
478
479 int safe_fsetxattr(const char *file, const int lineno, int fd, const char *name,
480 const void *value, size_t size, int flags);
481 #define SAFE_FSETXATTR(fd, name, value, size, flags) \
482 safe_fsetxattr(__FILE__, __LINE__, (fd), (name), (value), (size), (flags))
483
484 int safe_removexattr(const char *file, const int lineno, const char *path,
485 const char *name);
486 #define SAFE_REMOVEXATTR(path, name) \
487 safe_removexattr(__FILE__, __LINE__, (path), (name))
488
489 int safe_lremovexattr(const char *file, const int lineno, const char *path,
490 const char *name);
491 #define SAFE_LREMOVEXATTR(path, name) \
492 safe_lremovexattr(__FILE__, __LINE__, (path), (name))
493
494 int safe_fremovexattr(const char *file, const int lineno, int fd,
495 const char *name);
496 #define SAFE_FREMOVEXATTR(fd, name) \
497 safe_fremovexattr(__FILE__, __LINE__, (fd), (name))
498
499 int safe_fsync(const char *file, const int lineno, int fd);
500 #define SAFE_FSYNC(fd) safe_fsync(__FILE__, __LINE__, (fd))
501
502 int safe_setsid(const char *file, const int lineno);
503 #define SAFE_SETSID() safe_setsid(__FILE__, __LINE__)
504
505 int safe_mknod(const char *file, const int lineno, const char *pathname,
506 mode_t mode, dev_t dev);
507 #define SAFE_MKNOD(pathname, mode, dev) \
508 safe_mknod(__FILE__, __LINE__, (pathname), (mode), (dev))
509
510 int safe_mlock(const char *file, const int lineno, const char *addr,
511 size_t len);
512 #define SAFE_MLOCK(addr, len) safe_mlock(__FILE__, __LINE__, (addr), (len))
513
514 int safe_munlock(const char *file, const int lineno, const char *addr,
515 size_t len);
516 #define SAFE_MUNLOCK(addr, len) safe_munlock(__FILE__, __LINE__, (addr), (len))
517
518 int safe_mincore(const char *file, const int lineno, void *start,
519 size_t length, unsigned char *vec);
520 #define SAFE_MINCORE(start, length, vec) \
521 safe_mincore(__FILE__, __LINE__, (start), (length), (vec))
522
523 int safe_fanotify_init(const char *file, const int lineno,
524 unsigned int flags, unsigned int event_f_flags);
525 #define SAFE_FANOTIFY_INIT(fan, mode) \
526 safe_fanotify_init(__FILE__, __LINE__, (fan), (mode))
527
528 int safe_personality(const char *filename, unsigned int lineno,
529 unsigned long persona);
530 #define SAFE_PERSONALITY(persona) safe_personality(__FILE__, __LINE__, persona)
531
532 #define SAFE_SETENV(name, value, overwrite) do { \
533 if (setenv(name, value, overwrite)) { \
534 tst_brk_(__FILE__, __LINE__, TBROK | TERRNO, \
535 "setenv(%s, %s, %d) failed", \
536 name, value, overwrite); \
537 } \
538 } while (0)
539
540 void safe_unshare(const char *file, const int lineno, int flags);
541 #define SAFE_UNSHARE(flags) safe_unshare(__FILE__, __LINE__, (flags))
542
543 void safe_setns(const char *file, const int lineno, int fd, int nstype);
544 #define SAFE_SETNS(fd, nstype) safe_setns(__FILE__, __LINE__, (fd), (nstype));
545
safe_cmd(const char * file,const int lineno,const char * const argv[],const char * stdout_path,const char * stderr_path)546 static inline void safe_cmd(const char *file, const int lineno, const char *const argv[],
547 const char *stdout_path, const char *stderr_path)
548 {
549 int rval;
550
551 switch ((rval = tst_cmd(argv, stdout_path, stderr_path,
552 TST_CMD_PASS_RETVAL | TST_CMD_TCONF_ON_MISSING))) {
553 case 0:
554 break;
555 default:
556 tst_brk(TBROK, "%s:%d: %s failed (%d)", file, lineno, argv[0], rval);
557 }
558 }
559 #define SAFE_CMD(argv, stdout_path, stderr_path) \
560 safe_cmd(__FILE__, __LINE__, (argv), (stdout_path), (stderr_path))
561 /*
562 * SAFE_PTRACE() treats any non-zero return value as error. Don't use it
563 * for requests like PTRACE_PEEK* or PTRACE_SECCOMP_GET_FILTER which use
564 * the return value to pass arbitrary data.
565 */
566 long tst_safe_ptrace(const char *file, const int lineno, int req, pid_t pid,
567 void *addr, void *data);
568 #define SAFE_PTRACE(req, pid, addr, data) \
569 tst_safe_ptrace(__FILE__, __LINE__, req, pid, addr, data)
570
571 #endif /* SAFE_MACROS_H__ */
572