• 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  *  07/2001 Ported by Wayne Boyer
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Verify that, chmod(2) will succeed to change the mode of a file or directory
11  * and set the sticky bit on it if invoked by non-root (uid != 0)
12  * process with the following constraints:
13  *
14  * - the process is the owner of the file or directory.
15  * - the effective group ID or one of the supplementary group ID's of the
16  *   process is equal to the group ID of the file or directory.
17  */
18 
19 #include <pwd.h>
20 #include "tst_test.h"
21 
22 #define FILE_MODE   S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
23 #define DIR_MODE	S_IRWXU | S_IRWXG | S_IRWXO
24 #define PERMS		01777
25 
26 #define TESTFILE	"testfile"
27 #define TESTDIR		"testdir_3"
28 
29 static struct tcase {
30 	char *name;
31 	char *desc;
32 } tcases[] = {
33 	{TESTFILE, "verify permissions of file"},
34 	{TESTDIR, "verify permissions of directory"},
35 };
36 
verify_chmod(unsigned int n)37 static void verify_chmod(unsigned int n)
38 {
39 	struct stat stat_buf;
40 	struct tcase *tc = &tcases[n];
41 
42 	TST_EXP_PASS(chmod(tc->name, PERMS), "chmod(%s, %04o)",
43 		tc->name, PERMS);
44 
45 	if (!TST_PASS)
46 		return;
47 
48 	SAFE_STAT(tc->name, &stat_buf);
49 
50 	if ((stat_buf.st_mode & PERMS) != PERMS) {
51 		tst_res(TFAIL, "stat(%s) mode=%04o",
52 			tc->name, stat_buf.st_mode);
53 	} else {
54 		tst_res(TPASS, "stat(%s) mode=%04o",
55 			tc->name, stat_buf.st_mode);
56 	}
57 }
58 
setup(void)59 static void setup(void)
60 {
61 	char nobody_uid[] = "nobody";
62 	struct passwd *ltpuser;
63 
64 	ltpuser = SAFE_GETPWNAM(nobody_uid);
65 	SAFE_SETEUID(ltpuser->pw_uid);
66 
67 	SAFE_TOUCH(TESTFILE, FILE_MODE, NULL);
68 	SAFE_MKDIR(TESTDIR, DIR_MODE);
69 }
70 
71 static struct tst_test test = {
72 	.setup = setup,
73 	.tcnt = ARRAY_SIZE(tcases),
74 	.test = verify_chmod,
75 	.needs_root = 1,
76 	.needs_tmpdir = 1,
77 };
78