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