1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /* DESCRIPTION
19 * This test will switch to nobody user for correct error code collection.
20 * Verify setuid returns errno EPERM when it switches to root_user.
21 */
22
23 #include <errno.h>
24 #include <pwd.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include "tst_test.h"
28 #include "compat_tst_16.h"
29
30 #define ROOT_USER 0
31
verify_setuid(void)32 static void verify_setuid(void)
33 {
34 TEST(SETUID(ROOT_USER));
35 if (TST_RET != -1) {
36 tst_res(TFAIL | TTERRNO, "setuid() succeeded unexpectedly");
37 return;
38 }
39
40 if (TST_ERR == EPERM)
41 tst_res(TPASS, "setuid() returned errno EPERM");
42 else
43 tst_res(TFAIL | TTERRNO, "setuid() returned unexpected errno");
44 }
45
setup(void)46 static void setup(void)
47 {
48 struct passwd *pw;
49 uid_t uid;
50
51 pw = SAFE_GETPWNAM("nobody");
52 uid = pw->pw_uid;
53
54 if (SETUID(uid) == -1) {
55 tst_brk(TBROK,
56 "setuid() failed to set the effective uid to %d", uid);
57 }
58
59 umask(0);
60 }
61
62 static struct tst_test test = {
63 .setup = setup,
64 .needs_root = 1,
65 .test_all = verify_setuid,
66 };
67