blob: a120924448545b9f01d10f97542b7ff0d4790a87 [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 check the basic functionality of setfsgid(2) system
23 * call fails when called by a non-root user.
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>
28#include <sys/types.h>
29#include <errno.h>
30#include <pwd.h>
31#include <grp.h>
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040032
vapierd13d74b2006-02-11 07:24:00 +000033#include "test.h"
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040034#include "compat_16.h"
vapierd13d74b2006-02-11 07:24:00 +000035
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040036TCID_DEFINE(setfsgid03);
vapierd13d74b2006-02-11 07:24:00 +000037int TST_TOTAL = 1;
vapierd13d74b2006-02-11 07:24:00 +000038
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040039static char nobody_uid[] = "nobody";
40static struct passwd *ltpuser;
41
42static void setup(void);
43static void cleanup(void);
vapierd13d74b2006-02-11 07:24:00 +000044
45int main(int ac, char **av)
46{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020047 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020048 const char *msg;
vapierd13d74b2006-02-11 07:24:00 +000049
50 gid_t gid;
subrata_modakbdbaec52009-02-26 12:14:51 +000051
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040052 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080053 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd13d74b2006-02-11 07:24:00 +000054
55 setup();
56
vapierd13d74b2006-02-11 07:24:00 +000057 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080058 tst_count = 0;
vapierd13d74b2006-02-11 07:24:00 +000059
subrata_modak56207ce2009-03-23 13:35:39 +000060 gid = 1;
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040061 while (!getgrgid(gid))
subrata_modak56207ce2009-03-23 13:35:39 +000062 gid++;
vapierd13d74b2006-02-11 07:24:00 +000063
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040064 GID16_CHECK(gid, setfsgid, cleanup);
65
66 TEST(SETFSGID(cleanup, gid));
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 "setfsgid() failed unexpectedly");
vapierd13d74b2006-02-11 07:24:00 +000071 continue;
72 }
73
vapierd13d74b2006-02-11 07:24:00 +000074 if (TEST_RETURN == gid) {
subrata_modak56207ce2009-03-23 13:35:39 +000075 tst_resm(TFAIL,
subrata_modak923b23f2009-11-02 13:57:16 +000076 "setfsgid() returned %ld, expected anything but %d",
vapierd13d74b2006-02-11 07:24:00 +000077 TEST_RETURN, gid);
78 } else {
79 tst_resm(TPASS, "setfsgid() returned expected value : "
subrata_modak923b23f2009-11-02 13:57:16 +000080 "%ld", TEST_RETURN);
vapierd13d74b2006-02-11 07:24:00 +000081 }
82 }
vapierd13d74b2006-02-11 07:24:00 +000083
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +040084 cleanup();
85 tst_exit();
vapierd13d74b2006-02-11 07:24:00 +000086}
87
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,
99 "setuid failed to set the effective uid to %d",
100 ltpuser->pw_uid);
vapierd13d74b2006-02-11 07:24:00 +0000101
vapierd13d74b2006-02-11 07:24:00 +0000102 tst_sig(NOFORK, DEF_HANDLER, cleanup);
103
vapierd13d74b2006-02-11 07:24:00 +0000104 TEST_PAUSE;
105}
106
Stanislav Kholmanskikh0e7a99b2013-09-13 13:59:10 +0400107static void cleanup(void)
vapierd13d74b2006-02-11 07:24:00 +0000108{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700109}