• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *	- the process is the owner of the file.
13  *	- the effective group ID or one of the supplementary group ID's of the
14  *	  process is equal to the group ID of the file.
15  */
16 
17 #include <pwd.h>
18 #include "fchmod.h"
19 #include "tst_test.h"
20 
21 static int fd;
22 static const char nobody_uid[] = "nobody";
23 static struct passwd *ltpuser;
24 
verify_fchmod(void)25 static void verify_fchmod(void)
26 {
27 	struct stat stat_buf;
28 	mode_t file_mode;
29 
30 	TST_EXP_PASS_SILENT(fchmod(fd, PERMS));
31 
32 	if (fstat(fd, &stat_buf) == -1)
33 		tst_brk(TFAIL | TERRNO, "fstat failed");
34 	file_mode = stat_buf.st_mode;
35 
36 	if ((file_mode & PERMS) != PERMS)
37 		tst_res(TFAIL, "%s: Incorrect modes 0%3o, "
38 			"Expected 0777", TESTFILE, file_mode);
39 	else
40 		tst_res(TPASS, "Functionality of fchmod(%d, "
41 			"%#o) successful", fd, PERMS);
42 }
43 
setup(void)44 static void setup(void)
45 {
46 	ltpuser = SAFE_GETPWNAM(nobody_uid);
47 
48 	SAFE_SETEUID(ltpuser->pw_uid);
49 
50 	fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
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