blob: 86b1e6e8a43111813441862f0f41de8d0e15347a [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * 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.
9 *
10 * 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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
subrata_modak4bb656a2009-02-26 12:02:09 +000020/*
robbiew007dbc92002-02-27 18:02:38 +000021 * Test Name: setgroups03
plars865695b2001-08-27 22:15:12 +000022 *
23 * Test Description:
24 * Verify that,
25 * 1. setgroups() fails with -1 and sets errno to EINVAL if the size
26 * argument value is > NGROUPS
27 * 2. setgroups() fails with -1 and sets errno to EPERM if the
28 * calling process is not super-user.
29 *
30 * Expected Result:
31 * setgroups() should fail with return value -1 and set expected errno.
32 *
33 * Algorithm:
34 * Setup:
35 * Setup signal handling.
36 * Pause for SIGUSR1 if option specified.
37 *
38 * Test:
39 * Loop if the proper options are given.
40 * Execute system call
41 * Check return code, if system call failed (return=-1)
42 * if errno set == expected errno
43 * Issue sys call fails with expected return value and errno.
44 * Otherwise,
45 * Issue sys call fails with unexpected errno.
46 * Otherwise,
47 * Issue sys call returns unexpected value.
48 *
49 * Cleanup:
50 * Print errno log and/or timing stats if options given
51 *
52 * Usage: <for command-line>
robbiew007dbc92002-02-27 18:02:38 +000053 * setgroups03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000054 * where, -c n : Run n copies concurrently.
55 * -f : Turn off functionality Testing.
56 * -i n : Execute test n times.
57 * -I x : Execute test for x seconds.
58 * -P x : Pause for x seconds between iterations.
59 * -t : Turn on syscall timing.
60 *
61 * HISTORY
62 * 07/2001 Ported by Wayne Boyer
63 *
64 * RESTRICTIONS:
65 * This test should be executed by 'non-super-user' only.
66 *
67 */
mridgeb3db28f2004-04-06 15:46:16 +000068#include <limits.h>
plars865695b2001-08-27 22:15:12 +000069#include <sys/types.h>
70#include <unistd.h>
71#include <errno.h>
72#include <pwd.h>
robbiew8541eeb2003-03-27 19:59:32 +000073#include <grp.h>
plars865695b2001-08-27 22:15:12 +000074
75#include "test.h"
plars865695b2001-08-27 22:15:12 +000076
subrata_modak34a572a2008-08-27 12:02:42 +000077#include "compat_16.h"
78
plars865695b2001-08-27 22:15:12 +000079#define TESTUSER "nobody"
80
robbiew5901ff42001-08-28 15:11:41 +000081char nobody_uid[] = "nobody";
82struct passwd *ltpuser;
83
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020084TCID_DEFINE(setgroups03);
Cyril Hrubis605fa332015-02-04 13:11:20 +010085int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000086
subrata_modak56207ce2009-03-23 13:35:39 +000087GID_T *groups_list; /* Array to hold gids for getgroups() */
subrata_modakbdbaec52009-02-26 12:14:51 +000088
plars865695b2001-08-27 22:15:12 +000089int setup1(); /* setup function to test error EPERM */
90void setup(); /* setup function for the test */
91void cleanup(); /* cleanup function for the test */
92
subrata_modak56207ce2009-03-23 13:35:39 +000093struct test_case_t { /* test case struct. to hold ref. test cond's */
robbiewa1ac5ae2005-05-26 19:22:54 +000094 size_t gsize_add;
plars865695b2001-08-27 22:15:12 +000095 int list;
96 char *desc;
97 int exp_errno;
subrata_modak56207ce2009-03-23 13:35:39 +000098 int (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +000099} Test_cases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000100 {
101 1, 1, "Size is > sysconf(_SC_NGROUPS_MAX)", EINVAL, NULL}, {
102 0, 2, "Permission denied, not super-user", EPERM, setup1}
plars865695b2001-08-27 22:15:12 +0000103};
104
subrata_modak56207ce2009-03-23 13:35:39 +0000105int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000106{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200107 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200108 const char *msg;
plars865695b2001-08-27 22:15:12 +0000109 int gidsetsize; /* total no. of groups */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200110 int i;
subrata_modak56207ce2009-03-23 13:35:39 +0000111 char *test_desc; /* test specific error message */
robbiewa1ac5ae2005-05-26 19:22:54 +0000112 int ngroups_max = sysconf(_SC_NGROUPS_MAX); /* max no. of groups in the current system */
113
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800114 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800115 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakbdbaec52009-02-26 12:14:51 +0000116
subrata_modak34a572a2008-08-27 12:02:42 +0000117 groups_list = malloc(ngroups_max * sizeof(GID_T));
robbiewa1ac5ae2005-05-26 19:22:54 +0000118 if (groups_list == NULL) {
Markos Chandrasf4539c62012-01-03 09:41:10 +0000119 tst_brkm(TBROK, NULL, "malloc failed to alloc %zu errno "
subrata_modak34a572a2008-08-27 12:02:42 +0000120 " %d ", ngroups_max * sizeof(GID_T), errno);
robbiewa1ac5ae2005-05-26 19:22:54 +0000121 }
plars865695b2001-08-27 22:15:12 +0000122
plars865695b2001-08-27 22:15:12 +0000123 setup();
124
subrata_modak4bb656a2009-02-26 12:02:09 +0000125 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000126
Caspar Zhangd59a6592013-03-07 14:59:12 +0800127 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000128
129 for (i = 0; i < TST_TOTAL; i++) {
subrata_modak56207ce2009-03-23 13:35:39 +0000130 if (Test_cases[i].setupfunc != NULL) {
plars1ad84512002-07-23 13:11:18 +0000131 Test_cases[i].setupfunc();
132 }
133
robbiewa1ac5ae2005-05-26 19:22:54 +0000134 gidsetsize = ngroups_max + Test_cases[i].gsize_add;
plars865695b2001-08-27 22:15:12 +0000135 test_desc = Test_cases[i].desc;
subrata_modak56207ce2009-03-23 13:35:39 +0000136
plars865695b2001-08-27 22:15:12 +0000137 /*
138 * Call setgroups() to test different test conditions
139 * verify that it fails with -1 return value and
140 * sets appropriate errno.
subrata_modak4bb656a2009-02-26 12:02:09 +0000141 */
Stanislav Kholmanskikhce0dffe2013-09-05 11:01:48 +0400142 TEST(SETGROUPS(cleanup, gidsetsize, groups_list));
subrata_modakbdbaec52009-02-26 12:14:51 +0000143
plars865695b2001-08-27 22:15:12 +0000144 if (TEST_RETURN != -1) {
subrata_modak923b23f2009-11-02 13:57:16 +0000145 tst_resm(TFAIL, "setgroups(%d) returned %ld, "
subrata_modak56207ce2009-03-23 13:35:39 +0000146 "expected -1, errno=%d", gidsetsize,
147 TEST_RETURN, Test_cases[i].exp_errno);
plars865695b2001-08-27 22:15:12 +0000148 continue;
149 }
150
plars865695b2001-08-27 22:15:12 +0000151 if (TEST_ERRNO == Test_cases[i].exp_errno) {
152 tst_resm(TPASS,
subrata_modak56207ce2009-03-23 13:35:39 +0000153 "setgroups(%d) fails, %s, errno=%d",
154 gidsetsize, test_desc, TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +0000155 } else {
vapiercff4af02006-02-11 04:46:30 +0000156 tst_resm(TFAIL, "setgroups(%d) fails, %s, "
subrata_modak56207ce2009-03-23 13:35:39 +0000157 "errno=%d, expected errno=%d",
158 gidsetsize, test_desc, TEST_ERRNO,
plars865695b2001-08-27 22:15:12 +0000159 Test_cases[i].exp_errno);
160 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800161 }
plars865695b2001-08-27 22:15:12 +0000162
Garrett Cooper2c282152010-12-16 00:55:50 -0800163 }
plars865695b2001-08-27 22:15:12 +0000164
plars865695b2001-08-27 22:15:12 +0000165 cleanup();
166
Garrett Cooper53740502010-12-16 00:04:01 -0800167 tst_exit();
plars865695b2001-08-27 22:15:12 +0000168}
169
170/*
171 * setup() - performs all ONE TIME setup for this test.
172 *
173 * Call individual test specific setup functions.
174 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400175void setup(void)
plars865695b2001-08-27 22:15:12 +0000176{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200177 tst_require_root(NULL);
robbiew5901ff42001-08-28 15:11:41 +0000178
plars865695b2001-08-27 22:15:12 +0000179 tst_sig(NOFORK, DEF_HANDLER, cleanup);
180
plars865695b2001-08-27 22:15:12 +0000181 TEST_PAUSE;
182
plars865695b2001-08-27 22:15:12 +0000183}
184
185/*
186 * setup1 - Setup function to test setgroups() which returns -1
187 * and sets errno to EPERM.
188 *
189 * Get the user info. from /etc/passwd file.
190 * This function returns 0 on success.
191 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400192int setup1(void)
plars865695b2001-08-27 22:15:12 +0000193{
194 struct passwd *user_info; /* struct. to hold test user info */
195
plars1ad84512002-07-23 13:11:18 +0000196/* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000197 ltpuser = getpwnam(nobody_uid);
198 if (seteuid(ltpuser->pw_uid) == -1) {
199 tst_resm(TINFO, "setreuid failed to "
200 "to set the effective uid to %d", ltpuser->pw_uid);
201 perror("setreuid");
202 }
plars1ad84512002-07-23 13:11:18 +0000203
plars865695b2001-08-27 22:15:12 +0000204 if ((user_info = getpwnam(TESTUSER)) == NULL) {
205 tst_brkm(TFAIL, cleanup, "getpwnam(2) of %s Failed", TESTUSER);
206 }
subrata_modak34a572a2008-08-27 12:02:42 +0000207
subrata_modak07af28f2008-09-10 08:59:40 +0000208 if (!GID_SIZE_CHECK(user_info->pw_gid)) {
subrata_modak34a572a2008-08-27 12:02:42 +0000209 tst_brkm(TBROK,
210 cleanup,
211 "gid returned from getpwnam is too large for testing setgroups16");
212 }
plars865695b2001-08-27 22:15:12 +0000213 groups_list[0] = user_info->pw_gid;
214 return 0;
215}
216
217/*
218 * cleanup() - performs all ONE TIME cleanup for this test at
219 * completion or premature exit.
220 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400221void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000222{
plars865695b2001-08-27 22:15:12 +0000223
Markos Chandrasf4539c62012-01-03 09:41:10 +0000224}