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) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
7*49cdfc7eSAndroid Build Coastguard Worker * Verify that, fchmod(2) will succeed to change the mode of a directory
8*49cdfc7eSAndroid Build Coastguard Worker * but fails to set the setgid bit on it if invoked by non-root (uid != 0)
9*49cdfc7eSAndroid Build Coastguard Worker * process with the following constraints,
10*49cdfc7eSAndroid Build Coastguard Worker * - the process is the owner of the directory.
11*49cdfc7eSAndroid Build Coastguard Worker * - the effective group ID or one of the supplementary group ID's of the
12*49cdfc7eSAndroid Build Coastguard Worker * process is not equal to the group ID of the directory.
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * Expected Result:
15*49cdfc7eSAndroid Build Coastguard Worker * fchmod() should return value 0 on success and though succeeds to change
16*49cdfc7eSAndroid Build Coastguard Worker * the mode of a directory but fails to set setgid bit on it.
17*49cdfc7eSAndroid Build Coastguard Worker */
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_uid.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "fchmod.h"
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker #define PERMS_DIR 043777
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker static int fd;
29*49cdfc7eSAndroid Build Coastguard Worker
verify_fchmod(void)30*49cdfc7eSAndroid Build Coastguard Worker static void verify_fchmod(void)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker struct stat stat_buf;
33*49cdfc7eSAndroid Build Coastguard Worker mode_t dir_mode;
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker TEST(fchmod(fd, PERMS_DIR));
36*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1)
37*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "fchmod() failed unexpectly");
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard Worker SAFE_FSTAT(fd, &stat_buf);
40*49cdfc7eSAndroid Build Coastguard Worker dir_mode = stat_buf.st_mode;
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker if ((PERMS_DIR & ~S_ISGID) != dir_mode) {
43*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s: Incorrect modes 0%03o, Expected 0%03o",
44*49cdfc7eSAndroid Build Coastguard Worker TESTDIR, dir_mode & ~S_ISGID, PERMS_DIR);
45*49cdfc7eSAndroid Build Coastguard Worker } else {
46*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Functionality of fchmod(%d, %#o) successful",
47*49cdfc7eSAndroid Build Coastguard Worker fd, PERMS_DIR);
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker
setup(void)51*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
52*49cdfc7eSAndroid Build Coastguard Worker {
53*49cdfc7eSAndroid Build Coastguard Worker struct passwd *ltpuser;
54*49cdfc7eSAndroid Build Coastguard Worker gid_t free_gid;
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker ltpuser = SAFE_GETPWNAM("nobody");
57*49cdfc7eSAndroid Build Coastguard Worker free_gid = tst_get_free_gid(ltpuser->pw_gid);
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TESTDIR, DIR_MODE);
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker if (setgroups(1, <puser->pw_gid) == -1) {
62*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK, "Couldn't change supplementary group Id: %s",
63*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(TST_ERR));
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHOWN(TESTDIR, ltpuser->pw_uid, free_gid);
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEGID(ltpuser->pw_gid);
69*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(ltpuser->pw_uid);
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(TESTDIR, O_RDONLY);
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)74*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
75*49cdfc7eSAndroid Build Coastguard Worker {
76*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
77*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEGID(0);
80*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(0);
81*49cdfc7eSAndroid Build Coastguard Worker }
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
84*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_fchmod,
85*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
86*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
87*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
88*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
89*49cdfc7eSAndroid Build Coastguard Worker };
90