xref: /linux/fs/utimes.c (revision fcb3ad4366b9c810cbb9da34c076a9a52d8aa1e0)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/file.h>
3 #include <linux/mount.h>
4 #include <linux/namei.h>
5 #include <linux/utime.h>
6 #include <linux/syscalls.h>
7 #include <linux/uaccess.h>
8 #include <linux/compat.h>
9 #include <asm/unistd.h>
10 #include <linux/filelock.h>
11 
12 static bool nsec_valid(long nsec)
13 {
14 	if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
15 		return true;
16 
17 	return nsec >= 0 && nsec <= 999999999;
18 }
19 
20 int vfs_utimes(const struct path *path, struct timespec64 *times)
21 {
22 	int error;
23 	struct iattr newattrs;
24 	struct inode *inode = path->dentry->d_inode;
25 	struct inode *delegated_inode = NULL;
26 
27 	if (times) {
28 		if (!nsec_valid(times[0].tv_nsec) ||
29 		    !nsec_valid(times[1].tv_nsec))
30 			return -EINVAL;
31 		if (times[0].tv_nsec == UTIME_NOW &&
32 		    times[1].tv_nsec == UTIME_NOW)
33 			times = NULL;
34 	}
35 
36 	error = mnt_want_write(path->mnt);
37 	if (error)
38 		goto out;
39 
40 	newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
41 	if (times) {
42 		if (times[0].tv_nsec == UTIME_OMIT)
43 			newattrs.ia_valid &= ~ATTR_ATIME;
44 		else if (times[0].tv_nsec != UTIME_NOW) {
45 			newattrs.ia_atime = times[0];
46 			newattrs.ia_valid |= ATTR_ATIME_SET;
47 		}
48 
49 		if (times[1].tv_nsec == UTIME_OMIT)
50 			newattrs.ia_valid &= ~ATTR_MTIME;
51 		else if (times[1].tv_nsec != UTIME_NOW) {
52 			newattrs.ia_mtime = times[1];
53 			newattrs.ia_valid |= ATTR_MTIME_SET;
54 		}
55 		/*
56 		 * Tell setattr_prepare(), that this is an explicit time
57 		 * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET
58 		 * were used.
59 		 */
60 		newattrs.ia_valid |= ATTR_TIMES_SET;
61 	} else {
62 		newattrs.ia_valid |= ATTR_TOUCH;
63 	}
64 retry_deleg:
65 	inode_lock(inode);
66 	error = notify_change(mnt_idmap(path->mnt), path->dentry, &newattrs,
67 			      &delegated_inode);
68 	inode_unlock(inode);
69 	if (delegated_inode) {
70 		error = break_deleg_wait(&delegated_inode);
71 		if (!error)
72 			goto retry_deleg;
73 	}
74 
75 	mnt_drop_write(path->mnt);
76 out:
77 	return error;
78 }
79 
80 static int do_utimes_path(int dfd, const char __user *filename,
81 		struct timespec64 *times, int flags)
82 {
83 	struct path path;
84 	int lookup_flags = 0, error;
85 
86 	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))
87 		return -EINVAL;
88 
89 	if (!(flags & AT_SYMLINK_NOFOLLOW))
90 		lookup_flags |= LOOKUP_FOLLOW;
91 	if (flags & AT_EMPTY_PATH)
92 		lookup_flags |= LOOKUP_EMPTY;
93 
94 retry:
95 	error = user_path_at(dfd, filename, lookup_flags, &path);
96 	if (error)
97 		return error;
98 
99 	error = vfs_utimes(&path, times);
100 	path_put(&path);
101 	if (retry_estale(error, lookup_flags)) {
102 		lookup_flags |= LOOKUP_REVAL;
103 		goto retry;
104 	}
105 
106 	return error;
107 }
108 
109 static int do_utimes_fd(int fd, struct timespec64 *times, int flags)
110 {
111 	if (flags)
112 		return -EINVAL;
113 
114 	CLASS(fd, f)(fd);
115 	if (fd_empty(f))
116 		return -EBADF;
117 	return vfs_utimes(&fd_file(f)->f_path, times);
118 }
119 
120 /*
121  * do_utimes - change times on filename or file descriptor
122  * @dfd: open file descriptor, -1 or AT_FDCWD
123  * @filename: path name or NULL
124  * @times: new times or NULL
125  * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment)
126  *
127  * If filename is NULL and dfd refers to an open file, then operate on
128  * the file.  Otherwise look up filename, possibly using dfd as a
129  * starting point.
130  *
131  * If times==NULL, set access and modification to current time,
132  * must be owner or have write permission.
133  * Else, update from *times, must be owner or super user.
134  */
135 long do_utimes(int dfd, const char __user *filename, struct timespec64 *times,
136 	       int flags)
137 {
138 	if (filename == NULL && dfd != AT_FDCWD)
139 		return do_utimes_fd(dfd, times, flags);
140 	return do_utimes_path(dfd, filename, times, flags);
141 }
142 
143 SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename,
144 		struct __kernel_timespec __user *, utimes, int, flags)
145 {
146 	struct timespec64 tstimes[2];
147 
148 	if (utimes) {
149 		if ((get_timespec64(&tstimes[0], &utimes[0]) ||
150 			get_timespec64(&tstimes[1], &utimes[1])))
151 			return -EFAULT;
152 
153 		/* Nothing to do, we must not even check the path.  */
154 		if (tstimes[0].tv_nsec == UTIME_OMIT &&
155 		    tstimes[1].tv_nsec == UTIME_OMIT)
156 			return 0;
157 	}
158 
159 	return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
160 }
161 
162 #ifdef __ARCH_WANT_SYS_UTIME
163 /*
164  * futimesat(), utimes() and utime() are older versions of utimensat()
165  * that are provided for compatibility with traditional C libraries.
166  * On modern architectures, we always use libc wrappers around
167  * utimensat() instead.
168  */
169 static long do_futimesat(int dfd, const char __user *filename,
170 			 struct __kernel_old_timeval __user *utimes)
171 {
172 	struct __kernel_old_timeval times[2];
173 	struct timespec64 tstimes[2];
174 
175 	if (utimes) {
176 		if (copy_from_user(&times, utimes, sizeof(times)))
177 			return -EFAULT;
178 
179 		/* This test is needed to catch all invalid values.  If we
180 		   would test only in do_utimes we would miss those invalid
181 		   values truncated by the multiplication with 1000.  Note
182 		   that we also catch UTIME_{NOW,OMIT} here which are only
183 		   valid for utimensat.  */
184 		if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
185 		    times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
186 			return -EINVAL;
187 
188 		tstimes[0].tv_sec = times[0].tv_sec;
189 		tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
190 		tstimes[1].tv_sec = times[1].tv_sec;
191 		tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
192 	}
193 
194 	return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
195 }
196 
197 
198 SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename,
199 		struct __kernel_old_timeval __user *, utimes)
200 {
201 	return do_futimesat(dfd, filename, utimes);
202 }
203 
204 SYSCALL_DEFINE2(utimes, char __user *, filename,
205 		struct __kernel_old_timeval __user *, utimes)
206 {
207 	return do_futimesat(AT_FDCWD, filename, utimes);
208 }
209 
210 SYSCALL_DEFINE2(utime, char __user *, filename, struct utimbuf __user *, times)
211 {
212 	struct timespec64 tv[2];
213 
214 	if (times) {
215 		if (get_user(tv[0].tv_sec, &times->actime) ||
216 		    get_user(tv[1].tv_sec, &times->modtime))
217 			return -EFAULT;
218 		tv[0].tv_nsec = 0;
219 		tv[1].tv_nsec = 0;
220 	}
221 	return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
222 }
223 #endif
224 
225 #ifdef CONFIG_COMPAT_32BIT_TIME
226 /*
227  * Not all architectures have sys_utime, so implement this in terms
228  * of sys_utimes.
229  */
230 #ifdef __ARCH_WANT_SYS_UTIME32
231 SYSCALL_DEFINE2(utime32, const char __user *, filename,
232 		struct old_utimbuf32 __user *, t)
233 {
234 	struct timespec64 tv[2];
235 
236 	if (t) {
237 		if (get_user(tv[0].tv_sec, &t->actime) ||
238 		    get_user(tv[1].tv_sec, &t->modtime))
239 			return -EFAULT;
240 		tv[0].tv_nsec = 0;
241 		tv[1].tv_nsec = 0;
242 	}
243 	return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
244 }
245 #endif
246 
247 SYSCALL_DEFINE4(utimensat_time32, unsigned int, dfd, const char __user *, filename, struct old_timespec32 __user *, t, int, flags)
248 {
249 	struct timespec64 tv[2];
250 
251 	if  (t) {
252 		if (get_old_timespec32(&tv[0], &t[0]) ||
253 		    get_old_timespec32(&tv[1], &t[1]))
254 			return -EFAULT;
255 
256 		if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
257 			return 0;
258 	}
259 	return do_utimes(dfd, filename, t ? tv : NULL, flags);
260 }
261 
262 #ifdef __ARCH_WANT_SYS_UTIME32
263 static long do_compat_futimesat(unsigned int dfd, const char __user *filename,
264 				struct old_timeval32 __user *t)
265 {
266 	struct timespec64 tv[2];
267 
268 	if (t) {
269 		if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
270 		    get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
271 		    get_user(tv[1].tv_sec, &t[1].tv_sec) ||
272 		    get_user(tv[1].tv_nsec, &t[1].tv_usec))
273 			return -EFAULT;
274 		if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
275 		    tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
276 			return -EINVAL;
277 		tv[0].tv_nsec *= 1000;
278 		tv[1].tv_nsec *= 1000;
279 	}
280 	return do_utimes(dfd, filename, t ? tv : NULL, 0);
281 }
282 
283 SYSCALL_DEFINE3(futimesat_time32, unsigned int, dfd,
284 		       const char __user *, filename,
285 		       struct old_timeval32 __user *, t)
286 {
287 	return do_compat_futimesat(dfd, filename, t);
288 }
289 
290 SYSCALL_DEFINE2(utimes_time32, const char __user *, filename, struct old_timeval32 __user *, t)
291 {
292 	return do_compat_futimesat(AT_FDCWD, filename, t);
293 }
294 #endif
295 #endif
296