1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) Bull S.A. 2001
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2003-2023
6*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
7*49cdfc7eSAndroid Build Coastguard Worker * 05/2002 Ported by Andre Merlier
8*49cdfc7eSAndroid Build Coastguard Worker */
9*49cdfc7eSAndroid Build Coastguard Worker
10*49cdfc7eSAndroid Build Coastguard Worker /*\
11*49cdfc7eSAndroid Build Coastguard Worker * [Description]
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * Test for EINVAL, EPERM, EFAULT errors.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * - setgroups() fails with EINVAL if the size argument value is > NGROUPS.
16*49cdfc7eSAndroid Build Coastguard Worker *
17*49cdfc7eSAndroid Build Coastguard Worker * - setgroups() fails with EPERM if the calling process is not super-user.
18*49cdfc7eSAndroid Build Coastguard Worker *
19*49cdfc7eSAndroid Build Coastguard Worker * - setgroups() fails with EFAULT if the list has an invalid address.
20*49cdfc7eSAndroid Build Coastguard Worker */
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "compat_tst_16.h"
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker #define TESTUSER "nobody"
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker static GID_T *glist1, *glist2, *glist3;
31*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *user_info;
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
34*49cdfc7eSAndroid Build Coastguard Worker int gsize;
35*49cdfc7eSAndroid Build Coastguard Worker GID_T **glist;
36*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
37*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
38*49cdfc7eSAndroid Build Coastguard Worker {NGROUPS + 1, &glist1, EINVAL},
39*49cdfc7eSAndroid Build Coastguard Worker {1, &glist2, EPERM},
40*49cdfc7eSAndroid Build Coastguard Worker {NGROUPS, &glist3, EFAULT},
41*49cdfc7eSAndroid Build Coastguard Worker };
42*49cdfc7eSAndroid Build Coastguard Worker
verify_setgroups(unsigned int i)43*49cdfc7eSAndroid Build Coastguard Worker static void verify_setgroups(unsigned int i)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[i];
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_errno == EPERM)
48*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(user_info->pw_uid);
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(SETGROUPS(tc->gsize, *tc->glist), tc->exp_errno,
51*49cdfc7eSAndroid Build Coastguard Worker "setgroups(%d, groups_list)", tc->gsize);
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker if (tc->exp_errno == EPERM)
54*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(0);
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
setup(void)57*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker user_info = SAFE_GETPWNAM(TESTUSER);
60*49cdfc7eSAndroid Build Coastguard Worker glist2[0] = 42;
61*49cdfc7eSAndroid Build Coastguard Worker glist3 = tst_get_bad_addr(NULL);
62*49cdfc7eSAndroid Build Coastguard Worker }
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
65*49cdfc7eSAndroid Build Coastguard Worker .test = verify_setgroups,
66*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
67*49cdfc7eSAndroid Build Coastguard Worker .bufs = (struct tst_buffers []) {
68*49cdfc7eSAndroid Build Coastguard Worker {&glist1, .size = sizeof(GID_T) * (NGROUPS + 1)},
69*49cdfc7eSAndroid Build Coastguard Worker {&glist2, .size = sizeof(GID_T)},
70*49cdfc7eSAndroid Build Coastguard Worker {&user_info, .size = sizeof(struct passwd)},
71*49cdfc7eSAndroid Build Coastguard Worker {},
72*49cdfc7eSAndroid Build Coastguard Worker },
73*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
74*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
75*49cdfc7eSAndroid Build Coastguard Worker };
76