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