aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/fchmod/fchmod02.c
blob: d6abeffce8ae029f7ccaed49226818dd201d7943 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) International Business Machines  Corp., 2001
 */

/*\
 * [Description]
 *
 * Verify that, fchmod(2) will succeed to change the mode of a file/directory
 * set the sticky bit on it if invoked by root (uid = 0) process with
 * the following constraints:
 *
 * - the process is not the owner of the file/directory
 * - the effective group ID or one of the supplementary group ID's of the
 *   process is equal to the group ID of the file/directory
 */

#include <pwd.h>
#include <grp.h>
#include <errno.h>

#include "tst_test.h"
#include "fchmod.h"

static int fd;

static void verify_fchmod(void)
{
	struct stat stat_buf;
	mode_t file_mode;

	TEST(fchmod(fd, PERMS));
	if (TST_RET == -1)
		tst_res(TFAIL | TTERRNO, "fchmod() failed unexpectly");

	SAFE_FSTAT(fd, &stat_buf);
	file_mode = stat_buf.st_mode;

	if ((file_mode & ~S_IFREG) != PERMS) {
		tst_res(TFAIL, "%s: Incorrect modes 0%03o, Expected 0%03o",
			TESTFILE, file_mode, PERMS);
	} else {
		tst_res(TPASS, "Functionality of fchmod(%d, %#o) Successful",
			fd, PERMS);
	}
}

static void setup(void)
{
	struct passwd *ltpuser;
	struct group *ltpgroup;

	ltpuser = SAFE_GETPWNAM("nobody");
	ltpgroup = SAFE_GETGRNAM_FALLBACK("users", "daemon");

	fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
	SAFE_CHOWN(TESTFILE, ltpuser->pw_uid, ltpgroup->gr_gid);
	SAFE_SETGID(ltpgroup->gr_gid);
}

static void cleanup(void)
{
	if (fd > 0)
		SAFE_CLOSE(fd);
}

static struct tst_test test = {
	.test_all = verify_fchmod,
	.needs_root = 1,
	.setup = setup,
	.cleanup = cleanup,
	.needs_tmpdir = 1,
};