blob: 7438f7d838d131651c399757a9026e52bac9cbe1 [file] [log] [blame]
vapierd13d74b2006-02-11 07:24:00 +00001/*
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +04002 * Copyright (C) International Business Machines Corp., 2001
3 * Ported by Wayne Boyer
4 * Adapted by Dustin Kirkland (k1rkland@us.ibm.com)
vapierd13d74b2006-02-11 07:24:00 +00005 *
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +04006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
vapierd13d74b2006-02-11 07:24:00 +000010 *
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
vapierd13d74b2006-02-11 07:24:00 +000015 *
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
vapierd13d74b2006-02-11 07:24:00 +000019 */
20
21/*
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040022 * Testcase to test the basic functionality of the setfsuid(2) system
23 * call when called by a user other than root.
vapierd13d74b2006-02-11 07:24:00 +000024 */
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040025
vapierd13d74b2006-02-11 07:24:00 +000026#include <stdio.h>
27#include <unistd.h>
vapierd13d74b2006-02-11 07:24:00 +000028#include <sys/types.h>
29#include <errno.h>
vapierd13d74b2006-02-11 07:24:00 +000030#include <pwd.h>
31
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040032#include "test.h"
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040033#include "compat_16.h"
vapierd13d74b2006-02-11 07:24:00 +000034
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040035static void setup(void);
36static void cleanup(void);
37
38TCID_DEFINE(setfsuid03);
vapierd13d74b2006-02-11 07:24:00 +000039int TST_TOTAL = 1;
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040040
41static char nobody_uid[] = "nobody";
42static struct passwd *ltpuser;
vapierd13d74b2006-02-11 07:24:00 +000043
44int main(int ac, char **av)
45{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020046 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020047 const char *msg;
vapierd13d74b2006-02-11 07:24:00 +000048
49 uid_t uid;
subrata_modakbdbaec52009-02-26 12:14:51 +000050
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040051 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080052 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd13d74b2006-02-11 07:24:00 +000053
54 setup();
55
subrata_modak56207ce2009-03-23 13:35:39 +000056 uid = 1;
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040057 while (!getpwuid(uid))
subrata_modak56207ce2009-03-23 13:35:39 +000058 uid++;
vapierd13d74b2006-02-11 07:24:00 +000059
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040060 UID16_CHECK(uid, setfsuid, cleanup);
61
vapierd13d74b2006-02-11 07:24:00 +000062 for (lc = 0; TEST_LOOPING(lc); lc++) {
63
Caspar Zhangd59a6592013-03-07 14:59:12 +080064 tst_count = 0;
vapierd13d74b2006-02-11 07:24:00 +000065
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040066 TEST(SETFSUID(cleanup, uid));
vapierd13d74b2006-02-11 07:24:00 +000067
68 if (TEST_RETURN == -1) {
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040069 tst_resm(TFAIL | TTERRNO,
70 "setfsuid() failed unexpectedly");
vapierd13d74b2006-02-11 07:24:00 +000071 continue;
72 }
73
vapierd13d74b2006-02-11 07:24:00 +000074 if (TEST_RETURN == uid) {
subrata_modak56207ce2009-03-23 13:35:39 +000075 tst_resm(TFAIL,
subrata_modak923b23f2009-11-02 13:57:16 +000076 "setfsuid() returned %ld, expected anything but %d",
vapierd13d74b2006-02-11 07:24:00 +000077 TEST_RETURN, uid);
78 } else {
79 tst_resm(TPASS, "setfsuid() returned expected value : "
subrata_modak923b23f2009-11-02 13:57:16 +000080 "%ld", TEST_RETURN);
vapierd13d74b2006-02-11 07:24:00 +000081 }
82 }
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040083
vapierd13d74b2006-02-11 07:24:00 +000084 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080085 tst_exit();
Wanlong Gao354ebb42012-12-07 10:10:04 +080086}
vapierd13d74b2006-02-11 07:24:00 +000087
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040088static void setup(void)
vapierd13d74b2006-02-11 07:24:00 +000089{
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040090 tst_require_root(NULL);
91
subrata_modak56207ce2009-03-23 13:35:39 +000092 ltpuser = getpwnam(nobody_uid);
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040093 if (ltpuser == NULL)
94 tst_brkm(TBROK, cleanup, "getpwnam failed for user id %s",
95 nobody_uid);
96
97 if (setuid(ltpuser->pw_uid) == -1)
98 tst_resm(TINFO | TERRNO, "setuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +000099 "to set the effective uid to %d", ltpuser->pw_uid);
vapierd13d74b2006-02-11 07:24:00 +0000100
vapierd13d74b2006-02-11 07:24:00 +0000101 tst_sig(NOFORK, DEF_HANDLER, cleanup);
102
vapierd13d74b2006-02-11 07:24:00 +0000103 TEST_PAUSE;
104}
105
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +0400106static void cleanup(void)
vapierd13d74b2006-02-11 07:24:00 +0000107{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700108}