1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) International Business Machines Corp., 2001 4 */ 5 6 /* DESCRIPTION 7 * This test will switch to nobody user for correct error code collection. 8 * Verify setuid returns errno EPERM when it switches to root_user. 9 */ 10 11 #include <errno.h> 12 #include <pwd.h> 13 #include <unistd.h> 14 #include <sys/types.h> 15 #include "tst_test.h" 16 #include "compat_tst_16.h" 17 18 #define ROOT_USER 0 19 verify_setuid(void)20static void verify_setuid(void) 21 { 22 TEST(SETUID(ROOT_USER)); 23 if (TST_RET != -1) { 24 tst_res(TFAIL | TTERRNO, "setuid() succeeded unexpectedly"); 25 return; 26 } 27 28 if (TST_ERR == EPERM) 29 tst_res(TPASS, "setuid() returned errno EPERM"); 30 else 31 tst_res(TFAIL | TTERRNO, "setuid() returned unexpected errno"); 32 } 33 setup(void)34static void setup(void) 35 { 36 struct passwd *pw; 37 uid_t uid; 38 39 pw = SAFE_GETPWNAM("nobody"); 40 uid = pw->pw_uid; 41 42 if (SETUID(uid) == -1) { 43 tst_brk(TBROK, 44 "setuid() failed to set the effective uid to %d", uid); 45 } 46 47 umask(0); 48 } 49 50 static struct tst_test test = { 51 .setup = setup, 52 .needs_root = 1, 53 .test_all = verify_setuid, 54 }; 55