xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/rename/rename12.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) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *		07/2001 Ported by Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Verify that rename() fails with EPERM or EACCES when the directory
12*49cdfc7eSAndroid Build Coastguard Worker  * containing oldpath has the sticky bit (S_ISVTX) set and the caller
13*49cdfc7eSAndroid Build Coastguard Worker  * is not privileged.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT "mntpoint"
21*49cdfc7eSAndroid Build Coastguard Worker #define TEMP_DIR "tempdir"
22*49cdfc7eSAndroid Build Coastguard Worker #define TEMP_FILE1 TEMP_DIR"/tmpfile1"
23*49cdfc7eSAndroid Build Coastguard Worker #define TEMP_FILE2 TEMP_DIR"/tmpfile2"
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static uid_t nobody_uid;
26*49cdfc7eSAndroid Build Coastguard Worker static struct stat buf1;
27*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)28*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *pw;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	pw = SAFE_GETPWNAM("nobody");
33*49cdfc7eSAndroid Build Coastguard Worker 	nobody_uid = pw->pw_uid;
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHDIR(MNT_POINT);
36*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TEMP_DIR, 0777);
37*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(TEMP_DIR, &buf1);
38*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(TEMP_DIR, buf1.st_mode | S_ISVTX);
39*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TEMP_FILE1, 0700, NULL);
40*49cdfc7eSAndroid Build Coastguard Worker }
41*49cdfc7eSAndroid Build Coastguard Worker 
run(void)42*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(nobody_uid);
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	TEST(rename(TEMP_FILE1, TEMP_FILE2));
47*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1 && (TST_ERR == EPERM || TST_ERR == EACCES))
48*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "rename() failed as expected");
49*49cdfc7eSAndroid Build Coastguard Worker 	else if (TST_RET == 0)
50*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "rename() succeeded unexpectedly");
51*49cdfc7eSAndroid Build Coastguard Worker 	else
52*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "rename() failed, but not with expected errno");
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
56*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
57*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
58*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
59*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNT_POINT,
60*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
61*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
62*49cdfc7eSAndroid Build Coastguard Worker 	.skip_filesystems = (const char *const[]){
63*49cdfc7eSAndroid Build Coastguard Worker 		"exfat",
64*49cdfc7eSAndroid Build Coastguard Worker 		"vfat",
65*49cdfc7eSAndroid Build Coastguard Worker 		"fuse",
66*49cdfc7eSAndroid Build Coastguard Worker 		"ntfs",
67*49cdfc7eSAndroid Build Coastguard Worker 		NULL
68*49cdfc7eSAndroid Build Coastguard Worker 	},
69*49cdfc7eSAndroid Build Coastguard Worker };
70