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) 2021 SUSE LLC <mdoucha@suse.cz>
6 */
7
8/*\
9 * [Description]
10 *
11 * Check that renaming/moving a file from directory where the current user does
12 * not have write permissions fails with EACCES.
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <sys/types.h>
18
19#include "tst_test.h"
20#include "tst_safe_file_ops.h"
21#include "tst_uid.h"
22
23#define SRCDIR   "srcdir"
24#define DESTDIR  "destdir"
25#define SRCFILE  SRCDIR "/file"
26#define DESTFILE DESTDIR "/file"
27#define PERMS    0700
28
29static uid_t orig_uid, test_users[2];
30static char *tmpdir;
31
32static void setup(void)
33{
34	umask(0);
35	orig_uid = getuid();
36	tst_get_uids(test_users, 0, 2);
37	tmpdir = tst_get_tmpdir();
38}
39
40static void run(void)
41{
42	gid_t curgid = getgid();
43
44	SAFE_MKDIR(SRCDIR, PERMS);
45	SAFE_TOUCH(SRCFILE, PERMS, NULL);
46	SAFE_CHOWN(SRCDIR, test_users[0], curgid);
47	SAFE_CHOWN(SRCFILE, test_users[0], curgid);
48
49	SAFE_SETEUID(test_users[1]);
50	SAFE_MKDIR(DESTDIR, PERMS);
51	SAFE_TOUCH(DESTFILE, PERMS, NULL);
52
53	TST_EXP_FAIL(rename(SRCFILE, DESTFILE), EACCES, "rename()");
54
55	/* Cleanup between loops */
56	SAFE_SETEUID(orig_uid);
57	tst_purge_dir(tmpdir);
58}
59
60static void cleanup(void)
61{
62	free(tmpdir);
63}
64
65static struct tst_test test = {
66	.test_all = run,
67	.setup = setup,
68	.cleanup = cleanup,
69	.needs_root = 1,
70	.needs_tmpdir = 1,
71};
72