• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  *    07/2001 ported by John George
5  * Copyright (C) 2021 SUSE LLC <mdoucha@suse.cz>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Verify that the system call utime() successfully sets the modification
12  * and access times of a file to the current time, under the following
13  * constraints:
14  *
15  * - The times argument is NULL.
16  * - The user ID of the process is not "root".
17  * - The file is not owned by the user ID of the process.
18  * - The user ID of the process has write access to the file.
19  */
20 
21 #include <sys/types.h>
22 #include <pwd.h>
23 #include <utime.h>
24 #include <sys/stat.h>
25 #include <time.h>
26 
27 #include "tst_test.h"
28 #include "tst_uid.h"
29 #include "tst_clocks.h"
30 
31 #define MNTPOINT	"mntpoint"
32 #define TEMP_FILE	MNTPOINT"/tmp_file"
33 #define FILE_MODE	0766
34 
35 static uid_t root_uid, user_uid;
36 
setup(void)37 static void setup(void)
38 {
39 	struct passwd *pw;
40 	uid_t test_users[2];
41 	int fd;
42 
43 	root_uid = getuid();
44 	pw = SAFE_GETPWNAM("nobody");
45 	test_users[0] = pw->pw_uid;
46 	tst_get_uids(test_users, 1, 2);
47 	user_uid = test_users[1];
48 
49 	fd = SAFE_CREAT(TEMP_FILE, FILE_MODE);
50 	SAFE_CLOSE(fd);
51 
52 	/* Override umask */
53 	SAFE_CHMOD(TEMP_FILE, FILE_MODE);
54 	SAFE_CHOWN(TEMP_FILE, pw->pw_uid, pw->pw_gid);
55 }
56 
run(void)57 static void run(void)
58 {
59 	struct utimbuf utbuf;
60 	struct stat statbuf;
61 	time_t mintime, maxtime;
62 
63 	utbuf.modtime = time(0) - 5;
64 	utbuf.actime = utbuf.modtime + 1;
65 	TST_EXP_PASS_SILENT(utime(TEMP_FILE, &utbuf));
66 	SAFE_STAT(TEMP_FILE, &statbuf);
67 
68 	if (statbuf.st_atime != utbuf.actime ||
69 		statbuf.st_mtime != utbuf.modtime) {
70 		tst_res(TFAIL, "Could not set initial file times");
71 		return;
72 	}
73 
74 	SAFE_SETEUID(user_uid);
75 	mintime = tst_get_fs_timestamp();
76 	TST_EXP_PASS(utime(TEMP_FILE, NULL));
77 	maxtime = tst_get_fs_timestamp();
78 	SAFE_SETEUID(root_uid);
79 	SAFE_STAT(TEMP_FILE, &statbuf);
80 
81 	if (statbuf.st_atime < mintime || statbuf.st_atime > maxtime)
82 		tst_res(TFAIL, "utime() did not set expected atime");
83 
84 	if (statbuf.st_mtime < mintime || statbuf.st_mtime > maxtime)
85 		tst_res(TFAIL, "utime() did not set expected mtime");
86 }
87 
88 static struct tst_test test = {
89 	.test_all = run,
90 	.setup = setup,
91 	.needs_root = 1,
92 	.mount_device = 1,
93 	.mntpoint = MNTPOINT,
94 	.all_filesystems = 1,
95 	.skip_filesystems = (const char *const[]) {
96 		"vfat",
97 		"exfat",
98 		NULL
99 	}
100 };
101