blob: 8491c5678e532822ae00c4f2ad768c79a8880038 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis7dc42c92013-09-12 14:55:55 +02002 * Copyright (c) International Business Machines Corp., 2001
3 * Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubis7dc42c92013-09-12 14:55:55 +02005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
plars865695b2001-08-27 22:15:12 +00009 *
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020010 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
plars865695b2001-08-27 22:15:12 +000014 *
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020021 * Test to check the error and trivial conditions in setuid
plars865695b2001-08-27 22:15:12 +000022 */
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020023
plars865695b2001-08-27 22:15:12 +000024#include <errno.h>
Stanislav Kholmanskikh1c442442013-09-11 13:25:43 +040025#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000026#include <sys/types.h>
robbiewfa451a12003-03-27 20:52:36 +000027#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000028#include <unistd.h>
Stanislav Kholmanskikh1c442442013-09-11 13:25:43 +040029
plars865695b2001-08-27 22:15:12 +000030#include "test.h"
Stanislav Kholmanskikh1c442442013-09-11 13:25:43 +040031#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000032
33#define ROOT_USER 0
34
nstrazfa31d552002-05-14 16:50:06 +000035char *TCID = "setuid03";
plars865695b2001-08-27 22:15:12 +000036int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000037
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020038static char nobody_uid[] = "nobody";
39static struct passwd *ltpuser;
plars865695b2001-08-27 22:15:12 +000040
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020041static void setup(void);
42static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000043
robbiewfa451a12003-03-27 20:52:36 +000044int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000045{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020046 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020047 const char *msg;
plars865695b2001-08-27 22:15:12 +000048
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020049 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080050 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000051
plars865695b2001-08-27 22:15:12 +000052 setup();
53
plars865695b2001-08-27 22:15:12 +000054 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080055 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000056
Stanislav Kholmanskikh1c442442013-09-11 13:25:43 +040057 TEST(SETUID(cleanup, ROOT_USER));
plars865695b2001-08-27 22:15:12 +000058
59 if (TEST_RETURN != -1) {
60 tst_resm(TFAIL, "call succeeded unexpectedly");
61 continue;
62 }
63
plars865695b2001-08-27 22:15:12 +000064 if (TEST_ERRNO == EPERM) {
65 tst_resm(TPASS, "setuid returned errno EPERM");
66 } else {
67 tst_resm(TFAIL, "setuid returned unexpected errno - %d",
68 TEST_ERRNO);
69 }
70 }
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020071
plars865695b2001-08-27 22:15:12 +000072 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080073 tst_exit();
plars865695b2001-08-27 22:15:12 +000074}
75
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020076static void setup(void)
plars865695b2001-08-27 22:15:12 +000077{
Stanislav Kholmanskikh1c442442013-09-11 13:25:43 +040078 tst_require_root(NULL);
79
robbiew5901ff42001-08-28 15:11:41 +000080 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +000081 ltpuser = getpwnam(nobody_uid);
Stanislav Kholmanskikh1c442442013-09-11 13:25:43 +040082 if (ltpuser == NULL)
83 tst_brkm(TBROK, cleanup, "getpwnam failed for user id %s",
84 nobody_uid);
85
subrata_modak56207ce2009-03-23 13:35:39 +000086 if (setuid(ltpuser->pw_uid) == -1) {
87 tst_resm(TINFO, "setuid failed to "
88 "to set the effective uid to %d", ltpuser->pw_uid);
89 perror("setuid");
90 }
robbiew5901ff42001-08-28 15:11:41 +000091
plars865695b2001-08-27 22:15:12 +000092 tst_sig(FORK, DEF_HANDLER, cleanup);
93
94 umask(0);
95
plars865695b2001-08-27 22:15:12 +000096 TEST_PAUSE;
97}
98
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020099static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000100{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700101}