blob: ae1f500f1ce34cbd96c118c075c3883e7fd7e1f8 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +04002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 *
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +04004 * 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.
plars865695b2001-08-27 22:15:12 +00008 *
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +04009 * 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.
plars865695b2001-08-27 22:15:12 +000013 *
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000017 *
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040018 * Ported by John George
plars865695b2001-08-27 22:15:12 +000019 */
20
21/*
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040022 * Test that EPERM is set when setreuid is given an invalid user id.
plars865695b2001-08-27 22:15:12 +000023 */
24
robbiewfa451a12003-03-27 20:52:36 +000025#include <wait.h>
plars865695b2001-08-27 22:15:12 +000026#include <limits.h>
27#include <signal.h>
28#include <errno.h>
plars865695b2001-08-27 22:15:12 +000029#include <unistd.h>
robbiewab28aa92001-09-05 18:45:56 +000030#include <pwd.h>
robbiewfa451a12003-03-27 20:52:36 +000031#include <sys/param.h>
32#include <sys/types.h>
33#include <sys/stat.h>
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040034
robbiewfa451a12003-03-27 20:52:36 +000035#include "test.h"
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040036#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000037
robbiew40363822004-01-05 20:04:39 +000038#define INVAL_USER (USHRT_MAX-2)
plars865695b2001-08-27 22:15:12 +000039
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040040TCID_DEFINE(setreuid06);
plars865695b2001-08-27 22:15:12 +000041int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000042
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040043static struct passwd *ltpuser;
robbiewab28aa92001-09-05 18:45:56 +000044
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040045static void setup(void);
46static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000047
robbiewfa451a12003-03-27 20:52:36 +000048int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000049{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020050 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020051 const char *msg;
plars865695b2001-08-27 22:15:12 +000052
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040053 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080054 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000055
plars865695b2001-08-27 22:15:12 +000056 setup();
57
plars865695b2001-08-27 22:15:12 +000058 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080059 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000060
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040061 TEST(SETREUID(cleanup, -1, INVAL_USER));
plars865695b2001-08-27 22:15:12 +000062 if (TEST_RETURN != -1) {
63 tst_resm(TFAIL, "%s did not fail as expected", TCID);
64 } else if (TEST_ERRNO == EPERM) {
plars865695b2001-08-27 22:15:12 +000065 tst_resm(TPASS, "setreuid set errno to EPERM as "
subrata_modak56207ce2009-03-23 13:35:39 +000066 "expected");
plars865695b2001-08-27 22:15:12 +000067 } else {
plars865695b2001-08-27 22:15:12 +000068 tst_resm(TFAIL, "setreuid FAILED, expected 1 but "
subrata_modak56207ce2009-03-23 13:35:39 +000069 "returned %d", TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +000070 }
71
72 }
73 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080074 tst_exit();
plars865695b2001-08-27 22:15:12 +000075}
76
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040077static void setup(void)
plars865695b2001-08-27 22:15:12 +000078{
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040079 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -080080
plars865695b2001-08-27 22:15:12 +000081 tst_sig(FORK, DEF_HANDLER, cleanup);
82
plars865695b2001-08-27 22:15:12 +000083 umask(0);
84
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040085 ltpuser = getpwnam("nobody");
86 if (ltpuser == NULL)
87 tst_brkm(TBROK, NULL, "nobody must be a valid user.");
88
89 if (setuid(ltpuser->pw_uid) == -1)
90 tst_brkm(TBROK | TERRNO, NULL, "setuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +000091 "to set the effective uid to %d", ltpuser->pw_uid);
plars865695b2001-08-27 22:15:12 +000092
plars865695b2001-08-27 22:15:12 +000093 TEST_PAUSE;
94}
95
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040096static void cleanup(void)
plars865695b2001-08-27 22:15:12 +000097{
Chris Dearmanec6edca2012-10-17 19:54:01 -070098}