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