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 file
10 * and set the sticky bit on it if invoked by non-root (uid != 0)
11 * process with the following constraints:
12 *
13 * - the process is the owner of the file
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 file
16 */
17
18 #include <pwd.h>
19 #include "fchmod.h"
20 #include "tst_test.h"
21
22 static int fd;
23 static const char nobody_uid[] = "nobody";
24 static struct passwd *ltpuser;
25
verify_fchmod(void)26 static void verify_fchmod(void)
27 {
28 struct stat stat_buf;
29 mode_t file_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 file_mode = stat_buf.st_mode;
36
37 if ((file_mode & PERMS) != PERMS)
38 tst_res(TFAIL, "%s: Incorrect modes 0%3o, "
39 "Expected 0777", TESTFILE, file_mode);
40 else
41 tst_res(TPASS, "Functionality of fchmod(%d, "
42 "%#o) successful", fd, PERMS);
43 }
44
setup(void)45 static void setup(void)
46 {
47 ltpuser = SAFE_GETPWNAM(nobody_uid);
48
49 SAFE_SETEUID(ltpuser->pw_uid);
50
51 fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
52 }
53
cleanup(void)54 static void cleanup(void)
55 {
56 if (fd > 0)
57 SAFE_CLOSE(fd);
58 }
59
60 static struct tst_test test = {
61 .test_all = verify_fchmod,
62 .setup = setup,
63 .cleanup = cleanup,
64 .needs_tmpdir = 1,
65 .needs_root = 1,
66 };
67