1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6 #define _GNU_SOURCE
7 #include <unistd.h>
8 #include <errno.h>
9 #include <sched.h>
10 #include "config.h"
11 #ifdef HAVE_SYS_FANOTIFY_H
12 # include <sys/fanotify.h>
13 #endif
14 #define TST_NO_DEFAULT_MAIN
15 #include "tst_test.h"
16 #include "tst_safe_macros.h"
17 #include "lapi/personality.h"
18
safe_setpgid(const char * file,const int lineno,pid_t pid,pid_t pgid)19 int safe_setpgid(const char *file, const int lineno, pid_t pid, pid_t pgid)
20 {
21 int rval;
22
23 rval = setpgid(pid, pgid);
24 if (rval) {
25 tst_brk(TBROK | TERRNO,
26 "%s:%d: setpgid(%i, %i) failed",
27 file, lineno, pid, pgid);
28 }
29
30 return rval;
31 }
32
safe_getpgid(const char * file,const int lineno,pid_t pid)33 pid_t safe_getpgid(const char *file, const int lineno, pid_t pid)
34 {
35 pid_t pgid;
36
37 pgid = getpgid(pid);
38 if (pgid == -1) {
39 tst_brk(TBROK | TERRNO,
40 "%s:%d: getpgid(%i) failed", file, lineno, pid);
41 }
42
43 return pgid;
44 }
45
safe_fanotify_init(const char * file,const int lineno,unsigned int flags,unsigned int event_f_flags)46 int safe_fanotify_init(const char *file, const int lineno,
47 unsigned int flags, unsigned int event_f_flags)
48 {
49 int rval;
50
51 #ifdef HAVE_SYS_FANOTIFY_H
52 rval = fanotify_init(flags, event_f_flags);
53
54 if (rval == -1) {
55 if (errno == ENOSYS) {
56 tst_brk(TCONF,
57 "fanotify is not configured in this kernel.");
58 }
59 tst_brk(TBROK | TERRNO,
60 "%s:%d: fanotify_init() failed", file, lineno);
61 }
62 #else
63 tst_brk(TCONF, "Header <sys/fanotify.h> is not present");
64 #endif /* HAVE_SYS_FANOTIFY_H */
65
66 return rval;
67 }
68
safe_personality(const char * filename,unsigned int lineno,unsigned long persona)69 int safe_personality(const char *filename, unsigned int lineno,
70 unsigned long persona)
71 {
72 int prev_persona = personality(persona);
73
74 if (prev_persona < 0) {
75 tst_brk_(filename, lineno, TBROK | TERRNO,
76 "persona(%ld) failed", persona);
77 }
78
79 return prev_persona;
80 }
81
safe_setregid(const char * file,const int lineno,gid_t rgid,gid_t egid)82 int safe_setregid(const char *file, const int lineno,
83 gid_t rgid, gid_t egid)
84 {
85 int rval;
86
87 rval = setregid(rgid, egid);
88 if (rval == -1) {
89 tst_brk_(file, lineno, TBROK | TERRNO,
90 "setregid(%li, %li) failed",
91 (long)rgid, (long)egid);
92 }
93
94 return rval;
95 }
96
97
safe_setreuid(const char * file,const int lineno,uid_t ruid,uid_t euid)98 int safe_setreuid(const char *file, const int lineno,
99 uid_t ruid, uid_t euid)
100 {
101 int rval;
102
103 rval = setreuid(ruid, euid);
104 if (rval == -1) {
105 tst_brk_(file, lineno, TBROK | TERRNO,
106 "setreuid(%li, %li) failed",
107 (long)ruid, (long)euid);
108 }
109
110 return rval;
111 }
112
113
safe_sigaction(const char * file,const int lineno,int signum,const struct sigaction * act,struct sigaction * oldact)114 int safe_sigaction(const char *file, const int lineno,
115 int signum, const struct sigaction *act,
116 struct sigaction *oldact)
117 {
118 int rval;
119
120 rval = sigaction(signum, act, oldact);
121
122 if (rval == -1) {
123 tst_brk_(file, lineno, TBROK | TERRNO,
124 "sigaction(%s (%d), %p, %p) failed",
125 tst_strsig(signum), signum, act, oldact);
126 }
127
128 return rval;
129 }
130
safe_getgrnam(const char * file,const int lineno,const char * name)131 struct group *safe_getgrnam(const char *file, const int lineno,
132 const char *name)
133 {
134 struct group *rval;
135
136 errno = 0;
137 rval = getgrnam(name);
138 if (rval == NULL) {
139 tst_brk_(file, lineno, TBROK | TERRNO,
140 "getgrnam(%s) failed", name);
141 }
142
143 return rval;
144 }
145
safe_getgrnam_fallback(const char * file,const int lineno,const char * name,const char * fallback)146 struct group *safe_getgrnam_fallback(const char *file, const int lineno,
147 const char *name, const char *fallback)
148 {
149 struct group *rval;
150
151 errno = 0;
152 rval = getgrnam(name);
153 if (rval == NULL) {
154 tst_res_(file, lineno, TINFO,
155 "getgrnam(%s) failed - try fallback %s",
156 name, fallback);
157 rval = safe_getgrnam(file, lineno, fallback);
158 }
159
160 return rval;
161 }
162
safe_getgrgid(const char * file,const int lineno,gid_t gid)163 struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid)
164 {
165 struct group *rval;
166
167 errno = 0;
168 rval = getgrgid(gid);
169 if (rval == NULL) {
170 tst_brk_(file, lineno, TBROK | TERRNO,
171 "getgrgid(%li) failed", (long)gid);
172 }
173
174 return rval;
175 }
176
safe_chroot(const char * file,const int lineno,const char * path)177 int safe_chroot(const char *file, const int lineno, const char *path)
178 {
179 int rval;
180
181 rval = chroot(path);
182 if (rval == -1) {
183 tst_brk_(file, lineno, TBROK | TERRNO,
184 "chroot(%s) failed", path);
185 }
186
187 return rval;
188 }
189
safe_unshare(const char * file,const int lineno,int flags)190 void safe_unshare(const char *file, const int lineno, int flags)
191 {
192 int res;
193
194 res = unshare(flags);
195 if (res == -1) {
196 if (errno == EINVAL) {
197 tst_brk_(file, lineno, TCONF | TERRNO,
198 "unshare(%d) unsupported", flags);
199 } else {
200 tst_brk_(file, lineno, TBROK | TERRNO,
201 "unshare(%d) failed", flags);
202 }
203 }
204 }
205