Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests: capabilities: convert error output to TAP13 ksft framework

Convert errx() and err() usage to appropriate TAP13 ksft API.

Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>

+65 -49
+60 -45
tools/testing/selftests/capabilities/test_execve.c
··· 1 1 #define _GNU_SOURCE 2 2 3 3 #include <cap-ng.h> 4 - #include <err.h> 5 4 #include <linux/capability.h> 6 5 #include <stdbool.h> 7 6 #include <string.h> ··· 38 39 int buf_len; 39 40 40 41 buf_len = vsnprintf(buf, sizeof(buf), fmt, ap); 41 - if (buf_len < 0) { 42 - err(1, "vsnprintf failed"); 43 - } 44 - if (buf_len >= sizeof(buf)) { 45 - errx(1, "vsnprintf output truncated"); 46 - } 42 + if (buf_len < 0) 43 + ksft_exit_fail_msg("vsnprintf failed - %s\n", strerror(errno)); 44 + 45 + if (buf_len >= sizeof(buf)) 46 + ksft_exit_fail_msg("vsnprintf output truncated\n"); 47 + 47 48 48 49 fd = open(filename, O_WRONLY); 49 50 if (fd < 0) { 50 51 if ((errno == ENOENT) && enoent_ok) 51 52 return; 52 - err(1, "open of %s failed", filename); 53 + ksft_exit_fail_msg("open of %s failed - %s\n", 54 + filename, strerror(errno)); 53 55 } 54 56 written = write(fd, buf, buf_len); 55 57 if (written != buf_len) { 56 58 if (written >= 0) { 57 - errx(1, "short write to %s", filename); 59 + ksft_exit_fail_msg("short write to %s\n", filename); 58 60 } else { 59 - err(1, "write to %s failed", filename); 61 + ksft_exit_fail_msg("write to %s failed - %s\n", 62 + filename, strerror(errno)); 60 63 } 61 64 } 62 65 if (close(fd) != 0) { 63 - err(1, "close of %s failed", filename); 66 + ksft_exit_fail_msg("close of %s failed - %s\n", 67 + filename, strerror(errno)); 64 68 } 65 69 } 66 70 ··· 102 100 if (unshare(CLONE_NEWNS) == 0) { 103 101 ksft_print_msg("[NOTE]\tUsing global UIDs for tests\n"); 104 102 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0) 105 - err(1, "PR_SET_KEEPCAPS"); 103 + ksft_exit_fail_msg("PR_SET_KEEPCAPS - %s\n", 104 + strerror(errno)); 106 105 if (setresuid(inner_uid, inner_uid, -1) != 0) 107 - err(1, "setresuid"); 106 + ksft_exit_fail_msg("setresuid - %s\n", strerror(errno)); 108 107 109 108 // Re-enable effective caps 110 109 capng_get_caps_process(); ··· 113 110 if (capng_have_capability(CAPNG_PERMITTED, i)) 114 111 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, i); 115 112 if (capng_apply(CAPNG_SELECT_CAPS) != 0) 116 - err(1, "capng_apply"); 113 + ksft_exit_fail_msg( 114 + "capng_apply - %s\n", strerror(errno)); 117 115 118 116 have_outer_privilege = true; 119 117 } else if (unshare(CLONE_NEWUSER | CLONE_NEWNS) == 0) { ··· 125 121 126 122 have_outer_privilege = false; 127 123 } else { 128 - errx(1, "must be root or be able to create a userns"); 124 + ksft_exit_skip("must be root or be able to create a userns\n"); 129 125 } 130 126 131 127 if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL) != 0) 132 - err(1, "remount everything private"); 128 + ksft_exit_fail_msg("remount everything private - %s\n", 129 + strerror(errno)); 133 130 134 131 return have_outer_privilege; 135 132 } ··· 139 134 { 140 135 char cwd[PATH_MAX]; 141 136 if (getcwd(cwd, sizeof(cwd)) != cwd) 142 - err(1, "getcwd"); 137 + ksft_exit_fail_msg("getcwd - %s\n", strerror(errno)); 143 138 144 139 if (mount("private_tmp", ".", "tmpfs", 0, "mode=0777") != 0) 145 - err(1, "mount private tmpfs"); 140 + ksft_exit_fail_msg("mount private tmpfs - %s\n", 141 + strerror(errno)); 146 142 147 143 if (chdir(cwd) != 0) 148 - err(1, "chdir to private tmpfs"); 144 + ksft_exit_fail_msg("chdir to private tmpfs - %s\n", 145 + strerror(errno)); 149 146 } 150 147 151 148 static void copy_fromat_to(int fromfd, const char *fromname, const char *toname) 152 149 { 153 150 int from = openat(fromfd, fromname, O_RDONLY); 154 151 if (from == -1) 155 - err(1, "open copy source"); 152 + ksft_exit_fail_msg("open copy source - %s\n", strerror(errno)); 156 153 157 154 int to = open(toname, O_CREAT | O_WRONLY | O_EXCL, 0700); 158 155 ··· 164 157 if (sz == 0) 165 158 break; 166 159 if (sz < 0) 167 - err(1, "read"); 160 + ksft_exit_fail_msg("read - %s\n", strerror(errno)); 168 161 169 162 if (write(to, buf, sz) != sz) 170 - err(1, "write"); /* no short writes on tmpfs */ 163 + /* no short writes on tmpfs */ 164 + ksft_exit_fail_msg("write - %s\n", strerror(errno)); 171 165 } 172 166 173 167 close(from); ··· 197 189 } 198 190 return false; 199 191 } else { 200 - err(1, "fork"); 192 + ksft_exit_fail_msg("fork - %s\n", strerror(errno)); 193 + return false; 201 194 } 202 195 } 203 196 ··· 208 199 execl(name, name, (eff ? "1" : "0"), 209 200 (perm ? "1" : "0"), (inh ? "1" : "0"), (ambient ? "1" : "0"), 210 201 NULL); 211 - err(1, "execl"); 202 + ksft_exit_fail_msg("execl - %s\n", strerror(errno)); 212 203 } 213 204 214 205 static void exec_validate_cap(bool eff, bool perm, bool inh, bool ambient) ··· 222 213 223 214 int ourpath_fd = open(our_path, O_RDONLY | O_DIRECTORY); 224 215 if (ourpath_fd == -1) 225 - err(1, "open '%s'", our_path); 216 + ksft_exit_fail_msg("open '%s' - %s\n", 217 + our_path, strerror(errno)); 226 218 227 219 chdir_to_tmpfs(); 228 220 ··· 235 225 copy_fromat_to(ourpath_fd, "validate_cap", 236 226 "validate_cap_suidroot"); 237 227 if (chown("validate_cap_suidroot", 0, -1) != 0) 238 - err(1, "chown"); 228 + ksft_exit_fail_msg("chown - %s\n", strerror(errno)); 239 229 if (chmod("validate_cap_suidroot", S_ISUID | 0700) != 0) 240 - err(1, "chmod"); 230 + ksft_exit_fail_msg("chmod - %s\n", strerror(errno)); 241 231 242 232 copy_fromat_to(ourpath_fd, "validate_cap", 243 233 "validate_cap_suidnonroot"); 244 234 if (chown("validate_cap_suidnonroot", uid + 1, -1) != 0) 245 - err(1, "chown"); 235 + ksft_exit_fail_msg("chown - %s\n", strerror(errno)); 246 236 if (chmod("validate_cap_suidnonroot", S_ISUID | 0700) != 0) 247 - err(1, "chmod"); 237 + ksft_exit_fail_msg("chmod - %s\n", strerror(errno)); 248 238 249 239 copy_fromat_to(ourpath_fd, "validate_cap", 250 240 "validate_cap_sgidroot"); 251 241 if (chown("validate_cap_sgidroot", -1, 0) != 0) 252 - err(1, "chown"); 242 + ksft_exit_fail_msg("chown - %s\n", strerror(errno)); 253 243 if (chmod("validate_cap_sgidroot", S_ISGID | 0710) != 0) 254 - err(1, "chmod"); 244 + ksft_exit_fail_msg("chmod - %s\n", strerror(errno)); 255 245 256 246 copy_fromat_to(ourpath_fd, "validate_cap", 257 247 "validate_cap_sgidnonroot"); 258 248 if (chown("validate_cap_sgidnonroot", -1, gid + 1) != 0) 259 - err(1, "chown"); 249 + ksft_exit_fail_msg("chown - %s\n", strerror(errno)); 260 250 if (chmod("validate_cap_sgidnonroot", S_ISGID | 0710) != 0) 261 - err(1, "chmod"); 251 + ksft_exit_fail_msg("chmod - %s\n", strerror(errno)); 262 252 } 263 253 264 254 capng_get_caps_process(); ··· 266 256 /* Make sure that i starts out clear */ 267 257 capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE); 268 258 if (capng_apply(CAPNG_SELECT_CAPS) != 0) 269 - err(1, "capng_apply"); 259 + ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno)); 270 260 271 261 if (uid == 0) { 272 262 ksft_print_msg("[RUN]\tRoot => ep\n"); ··· 297 287 capng_update(CAPNG_DROP, CAPNG_PERMITTED, CAP_NET_RAW); 298 288 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_NET_RAW); 299 289 if (capng_apply(CAPNG_SELECT_CAPS) != 0) 300 - err(1, "capng_apply"); 290 + ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno)); 301 291 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_RAW, 0, 0, 0) != -1 || errno != EPERM) { 302 292 ksft_test_result_fail( 303 293 "PR_CAP_AMBIENT_RAISE should have failed on a non-permitted cap\n"); ··· 308 298 309 299 capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE); 310 300 if (capng_apply(CAPNG_SELECT_CAPS) != 0) 311 - err(1, "capng_apply"); 301 + ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno)); 312 302 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) { 313 303 ksft_test_result_fail( 314 304 "PR_CAP_AMBIENT_RAISE should have succeeded\n"); ··· 322 312 } 323 313 324 314 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0, 0) != 0) 325 - err(1, "PR_CAP_AMBIENT_CLEAR_ALL"); 315 + ksft_exit_fail_msg("PR_CAP_AMBIENT_CLEAR_ALL - %s\n", 316 + strerror(errno)); 326 317 327 318 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) { 328 319 ksft_test_result_fail( ··· 332 321 } 333 322 334 323 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) 335 - err(1, "PR_CAP_AMBIENT_RAISE"); 324 + ksft_exit_fail_msg("PR_CAP_AMBIENT_RAISE - %s\n", 325 + strerror(errno)); 336 326 337 327 capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE); 338 328 if (capng_apply(CAPNG_SELECT_CAPS) != 0) 339 - err(1, "capng_apply"); 329 + ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno)); 340 330 341 331 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) { 342 332 ksft_test_result_fail("Dropping I should have dropped A\n"); ··· 348 336 349 337 capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE); 350 338 if (capng_apply(CAPNG_SELECT_CAPS) != 0) 351 - err(1, "capng_apply"); 339 + ksft_exit_fail_msg("capng_apply - %s\n", strerror(errno)); 352 340 if (uid == 0) { 353 341 ksft_print_msg("[RUN]\tRoot +i => eip\n"); 354 342 if (fork_wait()) ··· 360 348 } 361 349 362 350 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) 363 - err(1, "PR_CAP_AMBIENT_RAISE"); 351 + ksft_exit_fail_msg("PR_CAP_AMBIENT_RAISE - %s\n", 352 + strerror(errno)); 364 353 365 354 ksft_print_msg("[RUN]\tUID %d +ia => eipa\n", uid); 366 355 if (fork_wait()) ··· 394 381 ksft_print_msg( 395 382 "[RUN]\tRoot, gid != 0, +ia, sgidroot => eip\n"); 396 383 if (setresgid(1, 1, 1) != 0) 397 - err(1, "setresgid"); 384 + ksft_exit_fail_msg("setresgid - %s\n", 385 + strerror(errno)); 398 386 exec_other_validate_cap("./validate_cap_sgidroot", 399 387 true, true, true, false); 400 388 } ··· 413 399 if (fork_wait()) { 414 400 ksft_print_msg("[RUN]\tNon-root +ia, sgidroot => i\n"); 415 401 if (setresgid(1, 1, 1) != 0) 416 - err(1, "setresgid"); 402 + ksft_exit_fail_msg("setresgid - %s\n", 403 + strerror(errno)); 417 404 exec_other_validate_cap("./validate_cap_sgidroot", 418 405 false, false, true, false); 419 406 } ··· 434 419 /* Find our path */ 435 420 tmp1 = strdup(argv[0]); 436 421 if (!tmp1) 437 - err(1, "strdup"); 422 + ksft_exit_fail_msg("strdup - %s\n", strerror(errno)); 438 423 tmp2 = dirname(tmp1); 439 424 our_path = strdup(tmp2); 440 425 if (!our_path) 441 - err(1, "strdup"); 426 + ksft_exit_fail_msg("strdup - %s\n", strerror(errno)); 442 427 free(tmp1); 443 428 444 429 mpid = getpid();
+5 -4
tools/testing/selftests/capabilities/validate_cap.c
··· 1 1 #include <cap-ng.h> 2 - #include <err.h> 3 2 #include <linux/capability.h> 4 3 #include <stdbool.h> 5 4 #include <string.h> ··· 26 27 return false; 27 28 else if (!strcmp(argv[i], "1")) 28 29 return true; 29 - else 30 - errx(1, "wrong argv[%d]", i); 30 + else { 31 + ksft_exit_fail_msg("wrong argv[%d]\n", i); 32 + return false; 33 + } 31 34 } 32 35 33 36 int main(int argc, char **argv) ··· 42 41 */ 43 42 44 43 if (argc != 5) 45 - errx(1, "wrong argc"); 44 + ksft_exit_fail_msg("wrong argc\n"); 46 45 47 46 #ifdef HAVE_GETAUXVAL 48 47 if (getauxval(AT_SECURE))