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 */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * Verify that, fchmod(2) will succeed to change the mode of a file
10*49cdfc7eSAndroid Build Coastguard Worker * and set the sticky bit on it if invoked by non-root (uid != 0)
11*49cdfc7eSAndroid Build Coastguard Worker * process with the following constraints:
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * - the process is the owner of the file
14*49cdfc7eSAndroid Build Coastguard Worker * - the effective group ID or one of the supplementary group ID's of the
15*49cdfc7eSAndroid Build Coastguard Worker * process is equal to the group ID of the file
16*49cdfc7eSAndroid Build Coastguard Worker */
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "fchmod.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker static int fd;
23*49cdfc7eSAndroid Build Coastguard Worker static const char nobody_uid[] = "nobody";
24*49cdfc7eSAndroid Build Coastguard Worker static struct passwd *ltpuser;
25*49cdfc7eSAndroid Build Coastguard Worker
verify_fchmod(void)26*49cdfc7eSAndroid Build Coastguard Worker static void verify_fchmod(void)
27*49cdfc7eSAndroid Build Coastguard Worker {
28*49cdfc7eSAndroid Build Coastguard Worker struct stat stat_buf;
29*49cdfc7eSAndroid Build Coastguard Worker mode_t file_mode;
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS_SILENT(fchmod(fd, PERMS));
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker if (fstat(fd, &stat_buf) == -1)
34*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TFAIL | TERRNO, "fstat failed");
35*49cdfc7eSAndroid Build Coastguard Worker file_mode = stat_buf.st_mode;
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker if ((file_mode & PERMS) != PERMS)
38*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s: Incorrect modes 0%3o, "
39*49cdfc7eSAndroid Build Coastguard Worker "Expected 0777", TESTFILE, file_mode);
40*49cdfc7eSAndroid Build Coastguard Worker else
41*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Functionality of fchmod(%d, "
42*49cdfc7eSAndroid Build Coastguard Worker "%#o) successful", fd, PERMS);
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker
setup(void)45*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
46*49cdfc7eSAndroid Build Coastguard Worker {
47*49cdfc7eSAndroid Build Coastguard Worker ltpuser = SAFE_GETPWNAM(nobody_uid);
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(ltpuser->pw_uid);
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)54*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
55*49cdfc7eSAndroid Build Coastguard Worker {
56*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
57*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
61*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_fchmod,
62*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
63*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
64*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
65*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
66*49cdfc7eSAndroid Build Coastguard Worker };
67