xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fchown/fchown02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2003-2021
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2014 Cyril Hrubis <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
6*49cdfc7eSAndroid Build Coastguard Worker  * 07/2001 Ported by Wayne Boyer
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Verify that fchown(2) invoked by super-user:
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  *  - clears setuid and setgid bits set on an executable file
15*49cdfc7eSAndroid Build Coastguard Worker  *  - preserves setgid bit set on a non-group-executable file
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "compat_tst_16.h"
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE	(S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
28*49cdfc7eSAndroid Build Coastguard Worker #define NEW_PERMS1	(S_IFREG|S_IRWXU|S_IRWXG|S_ISUID|S_ISGID)
29*49cdfc7eSAndroid Build Coastguard Worker #define NEW_PERMS2	(S_IFREG|S_IRWXU|S_ISGID)
30*49cdfc7eSAndroid Build Coastguard Worker #define EXP_PERMS	(S_IFREG|S_IRWXU|S_IRWXG)
31*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE1	"testfile1"
32*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE2	"testfile2"
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker static int fd1, fd2;
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {
37*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
38*49cdfc7eSAndroid Build Coastguard Worker 	const char *filename;
39*49cdfc7eSAndroid Build Coastguard Worker 	mode_t set_mode;
40*49cdfc7eSAndroid Build Coastguard Worker 	mode_t exp_mode;
41*49cdfc7eSAndroid Build Coastguard Worker } tc[] = {
42*49cdfc7eSAndroid Build Coastguard Worker 	{&fd1, TESTFILE1, NEW_PERMS1, EXP_PERMS},
43*49cdfc7eSAndroid Build Coastguard Worker 	{&fd2, TESTFILE2, NEW_PERMS2, NEW_PERMS2}
44*49cdfc7eSAndroid Build Coastguard Worker };
45*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int i)46*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	struct stat stat_buf;
49*49cdfc7eSAndroid Build Coastguard Worker 	uid_t uid;
50*49cdfc7eSAndroid Build Coastguard Worker 	gid_t gid;
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	UID16_CHECK((uid = geteuid()), "fchown");
53*49cdfc7eSAndroid Build Coastguard Worker 	GID16_CHECK((gid = getegid()), "fchown");
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(tc[i].filename, tc[i].set_mode);
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(FCHOWN(*tc[i].fd, uid, gid),
58*49cdfc7eSAndroid Build Coastguard Worker 		"fchown(%i, %i, %i)", *tc[i].fd, uid, gid);
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(tc[i].filename, &stat_buf);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	if (stat_buf.st_uid != uid || stat_buf.st_gid != gid)
63*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s: owner set to (uid=%d, gid=%d), expected (uid=%d, gid=%d)",
64*49cdfc7eSAndroid Build Coastguard Worker 			tc[i].filename, stat_buf.st_uid, stat_buf.st_gid, uid, gid);
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	if (stat_buf.st_mode != tc[i].exp_mode)
67*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s: wrong mode permissions %#o, expected %#o",
68*49cdfc7eSAndroid Build Coastguard Worker 			tc[i].filename, stat_buf.st_mode, tc[i].exp_mode);
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)71*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ARRAY_SIZE(tc); i++)
76*49cdfc7eSAndroid Build Coastguard Worker 		*tc[i].fd = SAFE_OPEN(tc[i].filename, O_RDWR | O_CREAT, FILE_MODE);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)79*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
80*49cdfc7eSAndroid Build Coastguard Worker {
81*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ARRAY_SIZE(tc); i++) {
84*49cdfc7eSAndroid Build Coastguard Worker 		if (*tc[i].fd > 0)
85*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_CLOSE(*tc[i].fd);
86*49cdfc7eSAndroid Build Coastguard Worker 	}
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
90*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tc),
91*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
92*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
93*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
94*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
95*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
96*49cdfc7eSAndroid Build Coastguard Worker };
97