1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/fs/open.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8 #include <linux/string.h>
9 #include <linux/mm.h>
10 #include <linux/file.h>
11 #include <linux/fdtable.h>
12 #include <linux/fsnotify.h>
13 #include <linux/module.h>
14 #include <linux/tty.h>
15 #include <linux/namei.h>
16 #include <linux/backing-dev.h>
17 #include <linux/capability.h>
18 #include <linux/securebits.h>
19 #include <linux/security.h>
20 #include <linux/mount.h>
21 #include <linux/fcntl.h>
22 #include <linux/slab.h>
23 #include <linux/uaccess.h>
24 #include <linux/fs.h>
25 #include <linux/personality.h>
26 #include <linux/pagemap.h>
27 #include <linux/syscalls.h>
28 #include <linux/rcupdate.h>
29 #include <linux/audit.h>
30 #include <linux/falloc.h>
31 #include <linux/fs_struct.h>
32 #include <linux/ima.h>
33 #include <linux/dnotify.h>
34 #include <linux/compat.h>
35
36 #include "internal.h"
37
do_truncate2(struct vfsmount * mnt,struct dentry * dentry,loff_t length,unsigned int time_attrs,struct file * filp)38 int do_truncate2(struct vfsmount *mnt, struct dentry *dentry, loff_t length,
39 unsigned int time_attrs, struct file *filp)
40 {
41 int ret;
42 struct iattr newattrs;
43
44 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
45 if (length < 0)
46 return -EINVAL;
47
48 newattrs.ia_size = length;
49 newattrs.ia_valid = ATTR_SIZE | time_attrs;
50 if (filp) {
51 newattrs.ia_file = filp;
52 newattrs.ia_valid |= ATTR_FILE;
53 }
54
55 /* Remove suid, sgid, and file capabilities on truncate too */
56 ret = dentry_needs_remove_privs(dentry);
57 if (ret < 0)
58 return ret;
59 if (ret)
60 newattrs.ia_valid |= ret | ATTR_FORCE;
61
62 inode_lock(dentry->d_inode);
63 /* Note any delegations or leases have already been broken: */
64 ret = notify_change2(mnt, dentry, &newattrs, NULL);
65 inode_unlock(dentry->d_inode);
66 return ret;
67 }
do_truncate(struct dentry * dentry,loff_t length,unsigned int time_attrs,struct file * filp)68 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
69 struct file *filp)
70 {
71 return do_truncate2(NULL, dentry, length, time_attrs, filp);
72 }
73
vfs_truncate(const struct path * path,loff_t length)74 long vfs_truncate(const struct path *path, loff_t length)
75 {
76 struct inode *inode;
77 struct vfsmount *mnt;
78 long error;
79
80 inode = path->dentry->d_inode;
81 mnt = path->mnt;
82
83 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
84 if (S_ISDIR(inode->i_mode))
85 return -EISDIR;
86 if (!S_ISREG(inode->i_mode))
87 return -EINVAL;
88
89 error = mnt_want_write(path->mnt);
90 if (error)
91 goto out;
92
93 error = inode_permission2(mnt, inode, MAY_WRITE);
94 if (error)
95 goto mnt_drop_write_and_out;
96
97 error = -EPERM;
98 if (IS_APPEND(inode))
99 goto mnt_drop_write_and_out;
100
101 error = get_write_access(inode);
102 if (error)
103 goto mnt_drop_write_and_out;
104
105 /*
106 * Make sure that there are no leases. get_write_access() protects
107 * against the truncate racing with a lease-granting setlease().
108 */
109 error = break_lease(inode, O_WRONLY);
110 if (error)
111 goto put_write_and_out;
112
113 error = locks_verify_truncate(inode, NULL, length);
114 if (!error)
115 error = security_path_truncate(path);
116 if (!error)
117 error = do_truncate2(mnt, path->dentry, length, 0, NULL);
118
119 put_write_and_out:
120 put_write_access(inode);
121 mnt_drop_write_and_out:
122 mnt_drop_write(path->mnt);
123 out:
124 return error;
125 }
126 EXPORT_SYMBOL_GPL(vfs_truncate);
127
do_sys_truncate(const char __user * pathname,loff_t length)128 long do_sys_truncate(const char __user *pathname, loff_t length)
129 {
130 unsigned int lookup_flags = LOOKUP_FOLLOW;
131 struct path path;
132 int error;
133
134 if (length < 0) /* sorry, but loff_t says... */
135 return -EINVAL;
136
137 retry:
138 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
139 if (!error) {
140 error = vfs_truncate(&path, length);
141 path_put(&path);
142 }
143 if (retry_estale(error, lookup_flags)) {
144 lookup_flags |= LOOKUP_REVAL;
145 goto retry;
146 }
147 return error;
148 }
149
SYSCALL_DEFINE2(truncate,const char __user *,path,long,length)150 SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
151 {
152 return do_sys_truncate(path, length);
153 }
154
155 #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(truncate,const char __user *,path,compat_off_t,length)156 COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
157 {
158 return do_sys_truncate(path, length);
159 }
160 #endif
161
do_sys_ftruncate(unsigned int fd,loff_t length,int small)162 long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
163 {
164 struct inode *inode;
165 struct dentry *dentry;
166 struct vfsmount *mnt;
167 struct fd f;
168 int error;
169
170 error = -EINVAL;
171 if (length < 0)
172 goto out;
173 error = -EBADF;
174 f = fdget(fd);
175 if (!f.file)
176 goto out;
177
178 /* explicitly opened as large or we are on 64-bit box */
179 if (f.file->f_flags & O_LARGEFILE)
180 small = 0;
181
182 dentry = f.file->f_path.dentry;
183 mnt = f.file->f_path.mnt;
184 inode = dentry->d_inode;
185 error = -EINVAL;
186 if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
187 goto out_putf;
188
189 error = -EINVAL;
190 /* Cannot ftruncate over 2^31 bytes without large file support */
191 if (small && length > MAX_NON_LFS)
192 goto out_putf;
193
194 error = -EPERM;
195 /* Check IS_APPEND on real upper inode */
196 if (IS_APPEND(file_inode(f.file)))
197 goto out_putf;
198
199 sb_start_write(inode->i_sb);
200 error = locks_verify_truncate(inode, f.file, length);
201 if (!error)
202 error = security_path_truncate(&f.file->f_path);
203 if (!error)
204 error = do_truncate2(mnt, dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
205 sb_end_write(inode->i_sb);
206 out_putf:
207 fdput(f);
208 out:
209 return error;
210 }
211
SYSCALL_DEFINE2(ftruncate,unsigned int,fd,unsigned long,length)212 SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
213 {
214 return do_sys_ftruncate(fd, length, 1);
215 }
216
217 #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(ftruncate,unsigned int,fd,compat_ulong_t,length)218 COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
219 {
220 return do_sys_ftruncate(fd, length, 1);
221 }
222 #endif
223
224 /* LFS versions of truncate are only needed on 32 bit machines */
225 #if BITS_PER_LONG == 32
SYSCALL_DEFINE2(truncate64,const char __user *,path,loff_t,length)226 SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
227 {
228 return do_sys_truncate(path, length);
229 }
230
SYSCALL_DEFINE2(ftruncate64,unsigned int,fd,loff_t,length)231 SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
232 {
233 return do_sys_ftruncate(fd, length, 0);
234 }
235 #endif /* BITS_PER_LONG == 32 */
236
237
vfs_fallocate(struct file * file,int mode,loff_t offset,loff_t len)238 int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
239 {
240 struct inode *inode = file_inode(file);
241 long ret;
242
243 if (offset < 0 || len <= 0)
244 return -EINVAL;
245
246 /* Return error if mode is not supported */
247 if (mode & ~FALLOC_FL_SUPPORTED_MASK)
248 return -EOPNOTSUPP;
249
250 /* Punch hole and zero range are mutually exclusive */
251 if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
252 (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
253 return -EOPNOTSUPP;
254
255 /* Punch hole must have keep size set */
256 if ((mode & FALLOC_FL_PUNCH_HOLE) &&
257 !(mode & FALLOC_FL_KEEP_SIZE))
258 return -EOPNOTSUPP;
259
260 /* Collapse range should only be used exclusively. */
261 if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
262 (mode & ~FALLOC_FL_COLLAPSE_RANGE))
263 return -EINVAL;
264
265 /* Insert range should only be used exclusively. */
266 if ((mode & FALLOC_FL_INSERT_RANGE) &&
267 (mode & ~FALLOC_FL_INSERT_RANGE))
268 return -EINVAL;
269
270 /* Unshare range should only be used with allocate mode. */
271 if ((mode & FALLOC_FL_UNSHARE_RANGE) &&
272 (mode & ~(FALLOC_FL_UNSHARE_RANGE | FALLOC_FL_KEEP_SIZE)))
273 return -EINVAL;
274
275 if (!(file->f_mode & FMODE_WRITE))
276 return -EBADF;
277
278 /*
279 * We can only allow pure fallocate on append only files
280 */
281 if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
282 return -EPERM;
283
284 if (IS_IMMUTABLE(inode))
285 return -EPERM;
286
287 /*
288 * We cannot allow any fallocate operation on an active swapfile
289 */
290 if (IS_SWAPFILE(inode))
291 return -ETXTBSY;
292
293 /*
294 * Revalidate the write permissions, in case security policy has
295 * changed since the files were opened.
296 */
297 ret = security_file_permission(file, MAY_WRITE);
298 if (ret)
299 return ret;
300
301 if (S_ISFIFO(inode->i_mode))
302 return -ESPIPE;
303
304 if (S_ISDIR(inode->i_mode))
305 return -EISDIR;
306
307 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
308 return -ENODEV;
309
310 /* Check for wrap through zero too */
311 if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
312 return -EFBIG;
313
314 if (!file->f_op->fallocate)
315 return -EOPNOTSUPP;
316
317 file_start_write(file);
318 ret = file->f_op->fallocate(file, mode, offset, len);
319
320 /*
321 * Create inotify and fanotify events.
322 *
323 * To keep the logic simple always create events if fallocate succeeds.
324 * This implies that events are even created if the file size remains
325 * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
326 */
327 if (ret == 0)
328 fsnotify_modify(file);
329
330 file_end_write(file);
331 return ret;
332 }
333 EXPORT_SYMBOL_GPL(vfs_fallocate);
334
ksys_fallocate(int fd,int mode,loff_t offset,loff_t len)335 int ksys_fallocate(int fd, int mode, loff_t offset, loff_t len)
336 {
337 struct fd f = fdget(fd);
338 int error = -EBADF;
339
340 if (f.file) {
341 error = vfs_fallocate(f.file, mode, offset, len);
342 fdput(f);
343 }
344 return error;
345 }
346
SYSCALL_DEFINE4(fallocate,int,fd,int,mode,loff_t,offset,loff_t,len)347 SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
348 {
349 return ksys_fallocate(fd, mode, offset, len);
350 }
351
352 /*
353 * access() needs to use the real uid/gid, not the effective uid/gid.
354 * We do this by temporarily clearing all FS-related capabilities and
355 * switching the fsuid/fsgid around to the real ones.
356 */
do_faccessat(int dfd,const char __user * filename,int mode)357 long do_faccessat(int dfd, const char __user *filename, int mode)
358 {
359 const struct cred *old_cred;
360 struct cred *override_cred;
361 struct path path;
362 struct inode *inode;
363 struct vfsmount *mnt;
364 int res;
365 unsigned int lookup_flags = LOOKUP_FOLLOW;
366
367 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
368 return -EINVAL;
369
370 override_cred = prepare_creds();
371 if (!override_cred)
372 return -ENOMEM;
373
374 override_cred->fsuid = override_cred->uid;
375 override_cred->fsgid = override_cred->gid;
376
377 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
378 /* Clear the capabilities if we switch to a non-root user */
379 kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
380 if (!uid_eq(override_cred->uid, root_uid))
381 cap_clear(override_cred->cap_effective);
382 else
383 override_cred->cap_effective =
384 override_cred->cap_permitted;
385 }
386
387 /*
388 * The new set of credentials can *only* be used in
389 * task-synchronous circumstances, and does not need
390 * RCU freeing, unless somebody then takes a separate
391 * reference to it.
392 *
393 * NOTE! This is _only_ true because this credential
394 * is used purely for override_creds() that installs
395 * it as the subjective cred. Other threads will be
396 * accessing ->real_cred, not the subjective cred.
397 *
398 * If somebody _does_ make a copy of this (using the
399 * 'get_current_cred()' function), that will clear the
400 * non_rcu field, because now that other user may be
401 * expecting RCU freeing. But normal thread-synchronous
402 * cred accesses will keep things non-RCY.
403 */
404 override_cred->non_rcu = 1;
405
406 old_cred = override_creds(override_cred);
407 retry:
408 res = user_path_at(dfd, filename, lookup_flags, &path);
409 if (res)
410 goto out;
411
412 inode = d_backing_inode(path.dentry);
413 mnt = path.mnt;
414
415 if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
416 /*
417 * MAY_EXEC on regular files is denied if the fs is mounted
418 * with the "noexec" flag.
419 */
420 res = -EACCES;
421 if (path_noexec(&path))
422 goto out_path_release;
423 }
424
425 res = inode_permission2(mnt, inode, mode | MAY_ACCESS);
426 /* SuS v2 requires we report a read only fs too */
427 if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
428 goto out_path_release;
429 /*
430 * This is a rare case where using __mnt_is_readonly()
431 * is OK without a mnt_want/drop_write() pair. Since
432 * no actual write to the fs is performed here, we do
433 * not need to telegraph to that to anyone.
434 *
435 * By doing this, we accept that this access is
436 * inherently racy and know that the fs may change
437 * state before we even see this result.
438 */
439 if (__mnt_is_readonly(path.mnt))
440 res = -EROFS;
441
442 out_path_release:
443 path_put(&path);
444 if (retry_estale(res, lookup_flags)) {
445 lookup_flags |= LOOKUP_REVAL;
446 goto retry;
447 }
448 out:
449 revert_creds(old_cred);
450 put_cred(override_cred);
451 return res;
452 }
453
SYSCALL_DEFINE3(faccessat,int,dfd,const char __user *,filename,int,mode)454 SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
455 {
456 return do_faccessat(dfd, filename, mode);
457 }
458
SYSCALL_DEFINE2(access,const char __user *,filename,int,mode)459 SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
460 {
461 return do_faccessat(AT_FDCWD, filename, mode);
462 }
463
ksys_chdir(const char __user * filename)464 int ksys_chdir(const char __user *filename)
465 {
466 struct path path;
467 int error;
468 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
469 retry:
470 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
471 if (error)
472 goto out;
473
474 error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
475 if (error)
476 goto dput_and_out;
477
478 set_fs_pwd(current->fs, &path);
479
480 dput_and_out:
481 path_put(&path);
482 if (retry_estale(error, lookup_flags)) {
483 lookup_flags |= LOOKUP_REVAL;
484 goto retry;
485 }
486 out:
487 return error;
488 }
489
SYSCALL_DEFINE1(chdir,const char __user *,filename)490 SYSCALL_DEFINE1(chdir, const char __user *, filename)
491 {
492 return ksys_chdir(filename);
493 }
494
SYSCALL_DEFINE1(fchdir,unsigned int,fd)495 SYSCALL_DEFINE1(fchdir, unsigned int, fd)
496 {
497 struct fd f = fdget_raw(fd);
498 int error;
499
500 error = -EBADF;
501 if (!f.file)
502 goto out;
503
504 error = -ENOTDIR;
505 if (!d_can_lookup(f.file->f_path.dentry))
506 goto out_putf;
507
508 error = inode_permission2(f.file->f_path.mnt, file_inode(f.file),
509 MAY_EXEC | MAY_CHDIR);
510 if (!error)
511 set_fs_pwd(current->fs, &f.file->f_path);
512 out_putf:
513 fdput(f);
514 out:
515 return error;
516 }
517
ksys_chroot(const char __user * filename)518 int ksys_chroot(const char __user *filename)
519 {
520 struct path path;
521 int error;
522 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
523 retry:
524 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
525 if (error)
526 goto out;
527
528 error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
529 if (error)
530 goto dput_and_out;
531
532 error = -EPERM;
533 if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
534 goto dput_and_out;
535 error = security_path_chroot(&path);
536 if (error)
537 goto dput_and_out;
538
539 set_fs_root(current->fs, &path);
540 error = 0;
541 dput_and_out:
542 path_put(&path);
543 if (retry_estale(error, lookup_flags)) {
544 lookup_flags |= LOOKUP_REVAL;
545 goto retry;
546 }
547 out:
548 return error;
549 }
550
SYSCALL_DEFINE1(chroot,const char __user *,filename)551 SYSCALL_DEFINE1(chroot, const char __user *, filename)
552 {
553 return ksys_chroot(filename);
554 }
555
chmod_common(const struct path * path,umode_t mode)556 static int chmod_common(const struct path *path, umode_t mode)
557 {
558 struct inode *inode = path->dentry->d_inode;
559 struct inode *delegated_inode = NULL;
560 struct iattr newattrs;
561 int error;
562
563 error = mnt_want_write(path->mnt);
564 if (error)
565 return error;
566 retry_deleg:
567 inode_lock(inode);
568 error = security_path_chmod(path, mode);
569 if (error)
570 goto out_unlock;
571 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
572 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
573 error = notify_change2(path->mnt, path->dentry, &newattrs, &delegated_inode);
574 out_unlock:
575 inode_unlock(inode);
576 if (delegated_inode) {
577 error = break_deleg_wait(&delegated_inode);
578 if (!error)
579 goto retry_deleg;
580 }
581 mnt_drop_write(path->mnt);
582 return error;
583 }
584
ksys_fchmod(unsigned int fd,umode_t mode)585 int ksys_fchmod(unsigned int fd, umode_t mode)
586 {
587 struct fd f = fdget(fd);
588 int err = -EBADF;
589
590 if (f.file) {
591 audit_file(f.file);
592 err = chmod_common(&f.file->f_path, mode);
593 fdput(f);
594 }
595 return err;
596 }
597
SYSCALL_DEFINE2(fchmod,unsigned int,fd,umode_t,mode)598 SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
599 {
600 return ksys_fchmod(fd, mode);
601 }
602
do_fchmodat(int dfd,const char __user * filename,umode_t mode)603 int do_fchmodat(int dfd, const char __user *filename, umode_t mode)
604 {
605 struct path path;
606 int error;
607 unsigned int lookup_flags = LOOKUP_FOLLOW;
608 retry:
609 error = user_path_at(dfd, filename, lookup_flags, &path);
610 if (!error) {
611 error = chmod_common(&path, mode);
612 path_put(&path);
613 if (retry_estale(error, lookup_flags)) {
614 lookup_flags |= LOOKUP_REVAL;
615 goto retry;
616 }
617 }
618 return error;
619 }
620
SYSCALL_DEFINE3(fchmodat,int,dfd,const char __user *,filename,umode_t,mode)621 SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename,
622 umode_t, mode)
623 {
624 return do_fchmodat(dfd, filename, mode);
625 }
626
SYSCALL_DEFINE2(chmod,const char __user *,filename,umode_t,mode)627 SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
628 {
629 return do_fchmodat(AT_FDCWD, filename, mode);
630 }
631
chown_common(const struct path * path,uid_t user,gid_t group)632 static int chown_common(const struct path *path, uid_t user, gid_t group)
633 {
634 struct inode *inode = path->dentry->d_inode;
635 struct inode *delegated_inode = NULL;
636 int error;
637 struct iattr newattrs;
638 kuid_t uid;
639 kgid_t gid;
640
641 uid = make_kuid(current_user_ns(), user);
642 gid = make_kgid(current_user_ns(), group);
643
644 retry_deleg:
645 newattrs.ia_valid = ATTR_CTIME;
646 if (user != (uid_t) -1) {
647 if (!uid_valid(uid))
648 return -EINVAL;
649 newattrs.ia_valid |= ATTR_UID;
650 newattrs.ia_uid = uid;
651 }
652 if (group != (gid_t) -1) {
653 if (!gid_valid(gid))
654 return -EINVAL;
655 newattrs.ia_valid |= ATTR_GID;
656 newattrs.ia_gid = gid;
657 }
658 if (!S_ISDIR(inode->i_mode))
659 newattrs.ia_valid |=
660 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
661 inode_lock(inode);
662 error = security_path_chown(path, uid, gid);
663 if (!error)
664 error = notify_change2(path->mnt, path->dentry, &newattrs, &delegated_inode);
665 inode_unlock(inode);
666 if (delegated_inode) {
667 error = break_deleg_wait(&delegated_inode);
668 if (!error)
669 goto retry_deleg;
670 }
671 return error;
672 }
673
do_fchownat(int dfd,const char __user * filename,uid_t user,gid_t group,int flag)674 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
675 int flag)
676 {
677 struct path path;
678 int error = -EINVAL;
679 int lookup_flags;
680
681 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
682 goto out;
683
684 lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
685 if (flag & AT_EMPTY_PATH)
686 lookup_flags |= LOOKUP_EMPTY;
687 retry:
688 error = user_path_at(dfd, filename, lookup_flags, &path);
689 if (error)
690 goto out;
691 error = mnt_want_write(path.mnt);
692 if (error)
693 goto out_release;
694 error = chown_common(&path, user, group);
695 mnt_drop_write(path.mnt);
696 out_release:
697 path_put(&path);
698 if (retry_estale(error, lookup_flags)) {
699 lookup_flags |= LOOKUP_REVAL;
700 goto retry;
701 }
702 out:
703 return error;
704 }
705
SYSCALL_DEFINE5(fchownat,int,dfd,const char __user *,filename,uid_t,user,gid_t,group,int,flag)706 SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
707 gid_t, group, int, flag)
708 {
709 return do_fchownat(dfd, filename, user, group, flag);
710 }
711
SYSCALL_DEFINE3(chown,const char __user *,filename,uid_t,user,gid_t,group)712 SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
713 {
714 return do_fchownat(AT_FDCWD, filename, user, group, 0);
715 }
716
SYSCALL_DEFINE3(lchown,const char __user *,filename,uid_t,user,gid_t,group)717 SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
718 {
719 return do_fchownat(AT_FDCWD, filename, user, group,
720 AT_SYMLINK_NOFOLLOW);
721 }
722
ksys_fchown(unsigned int fd,uid_t user,gid_t group)723 int ksys_fchown(unsigned int fd, uid_t user, gid_t group)
724 {
725 struct fd f = fdget(fd);
726 int error = -EBADF;
727
728 if (!f.file)
729 goto out;
730
731 error = mnt_want_write_file(f.file);
732 if (error)
733 goto out_fput;
734 audit_file(f.file);
735 error = chown_common(&f.file->f_path, user, group);
736 mnt_drop_write_file(f.file);
737 out_fput:
738 fdput(f);
739 out:
740 return error;
741 }
742
SYSCALL_DEFINE3(fchown,unsigned int,fd,uid_t,user,gid_t,group)743 SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
744 {
745 return ksys_fchown(fd, user, group);
746 }
747
do_dentry_open(struct file * f,struct inode * inode,int (* open)(struct inode *,struct file *))748 static int do_dentry_open(struct file *f,
749 struct inode *inode,
750 int (*open)(struct inode *, struct file *))
751 {
752 static const struct file_operations empty_fops = {};
753 int error;
754
755 path_get(&f->f_path);
756 f->f_inode = inode;
757 f->f_mapping = inode->i_mapping;
758
759 /* Ensure that we skip any errors that predate opening of the file */
760 f->f_wb_err = filemap_sample_wb_err(f->f_mapping);
761
762 if (unlikely(f->f_flags & O_PATH)) {
763 f->f_mode = FMODE_PATH | FMODE_OPENED;
764 f->f_op = &empty_fops;
765 return 0;
766 }
767
768 /* Any file opened for execve()/uselib() has to be a regular file. */
769 if (unlikely(f->f_flags & FMODE_EXEC && !S_ISREG(inode->i_mode))) {
770 error = -EACCES;
771 goto cleanup_file;
772 }
773
774 if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
775 error = get_write_access(inode);
776 if (unlikely(error))
777 goto cleanup_file;
778 error = __mnt_want_write(f->f_path.mnt);
779 if (unlikely(error)) {
780 put_write_access(inode);
781 goto cleanup_file;
782 }
783 f->f_mode |= FMODE_WRITER;
784 }
785
786 /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
787 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
788 f->f_mode |= FMODE_ATOMIC_POS;
789
790 f->f_op = fops_get(inode->i_fop);
791 if (WARN_ON(!f->f_op)) {
792 error = -ENODEV;
793 goto cleanup_all;
794 }
795
796 error = security_file_open(f);
797 if (error)
798 goto cleanup_all;
799
800 error = break_lease(locks_inode(f), f->f_flags);
801 if (error)
802 goto cleanup_all;
803
804 /* normally all 3 are set; ->open() can clear them if needed */
805 f->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
806 if (!open)
807 open = f->f_op->open;
808 if (open) {
809 error = open(inode, f);
810 if (error)
811 goto cleanup_all;
812 }
813 f->f_mode |= FMODE_OPENED;
814 if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
815 i_readcount_inc(inode);
816 if ((f->f_mode & FMODE_READ) &&
817 likely(f->f_op->read || f->f_op->read_iter))
818 f->f_mode |= FMODE_CAN_READ;
819 if ((f->f_mode & FMODE_WRITE) &&
820 likely(f->f_op->write || f->f_op->write_iter))
821 f->f_mode |= FMODE_CAN_WRITE;
822
823 f->f_write_hint = WRITE_LIFE_NOT_SET;
824 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
825
826 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
827
828 /* NB: we're sure to have correct a_ops only after f_op->open */
829 if (f->f_flags & O_DIRECT) {
830 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
831 return -EINVAL;
832 }
833
834 /*
835 * XXX: Huge page cache doesn't support writing yet. Drop all page
836 * cache for this file before processing writes.
837 */
838 if ((f->f_mode & FMODE_WRITE) && filemap_nr_thps(inode->i_mapping))
839 truncate_pagecache(inode, 0);
840
841 return 0;
842
843 cleanup_all:
844 if (WARN_ON_ONCE(error > 0))
845 error = -EINVAL;
846 fops_put(f->f_op);
847 if (f->f_mode & FMODE_WRITER) {
848 put_write_access(inode);
849 __mnt_drop_write(f->f_path.mnt);
850 }
851 cleanup_file:
852 path_put(&f->f_path);
853 f->f_path.mnt = NULL;
854 f->f_path.dentry = NULL;
855 f->f_inode = NULL;
856 return error;
857 }
858
859 /**
860 * finish_open - finish opening a file
861 * @file: file pointer
862 * @dentry: pointer to dentry
863 * @open: open callback
864 * @opened: state of open
865 *
866 * This can be used to finish opening a file passed to i_op->atomic_open().
867 *
868 * If the open callback is set to NULL, then the standard f_op->open()
869 * filesystem callback is substituted.
870 *
871 * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
872 * the return value of d_splice_alias(), then the caller needs to perform dput()
873 * on it after finish_open().
874 *
875 * On successful return @file is a fully instantiated open file. After this, if
876 * an error occurs in ->atomic_open(), it needs to clean up with fput().
877 *
878 * Returns zero on success or -errno if the open failed.
879 */
finish_open(struct file * file,struct dentry * dentry,int (* open)(struct inode *,struct file *))880 int finish_open(struct file *file, struct dentry *dentry,
881 int (*open)(struct inode *, struct file *))
882 {
883 BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
884
885 file->f_path.dentry = dentry;
886 return do_dentry_open(file, d_backing_inode(dentry), open);
887 }
888 EXPORT_SYMBOL(finish_open);
889
890 /**
891 * finish_no_open - finish ->atomic_open() without opening the file
892 *
893 * @file: file pointer
894 * @dentry: dentry or NULL (as returned from ->lookup())
895 *
896 * This can be used to set the result of a successful lookup in ->atomic_open().
897 *
898 * NB: unlike finish_open() this function does consume the dentry reference and
899 * the caller need not dput() it.
900 *
901 * Returns "0" which must be the return value of ->atomic_open() after having
902 * called this function.
903 */
finish_no_open(struct file * file,struct dentry * dentry)904 int finish_no_open(struct file *file, struct dentry *dentry)
905 {
906 file->f_path.dentry = dentry;
907 return 0;
908 }
909 EXPORT_SYMBOL(finish_no_open);
910
file_path(struct file * filp,char * buf,int buflen)911 char *file_path(struct file *filp, char *buf, int buflen)
912 {
913 return d_path(&filp->f_path, buf, buflen);
914 }
915 EXPORT_SYMBOL(file_path);
916
917 /**
918 * vfs_open - open the file at the given path
919 * @path: path to open
920 * @file: newly allocated file with f_flag initialized
921 * @cred: credentials to use
922 */
vfs_open(const struct path * path,struct file * file)923 int vfs_open(const struct path *path, struct file *file)
924 {
925 file->f_path = *path;
926 return do_dentry_open(file, d_backing_inode(path->dentry), NULL);
927 }
928
dentry_open(const struct path * path,int flags,const struct cred * cred)929 struct file *dentry_open(const struct path *path, int flags,
930 const struct cred *cred)
931 {
932 int error;
933 struct file *f;
934
935 validate_creds(cred);
936
937 /* We must always pass in a valid mount pointer. */
938 BUG_ON(!path->mnt);
939
940 f = alloc_empty_file(flags, cred);
941 if (!IS_ERR(f)) {
942 error = vfs_open(path, f);
943 if (error) {
944 fput(f);
945 f = ERR_PTR(error);
946 }
947 }
948 return f;
949 }
950 EXPORT_SYMBOL(dentry_open);
951
open_with_fake_path(const struct path * path,int flags,struct inode * inode,const struct cred * cred)952 struct file *open_with_fake_path(const struct path *path, int flags,
953 struct inode *inode, const struct cred *cred)
954 {
955 struct file *f = alloc_empty_file_noaccount(flags, cred);
956 if (!IS_ERR(f)) {
957 int error;
958
959 f->f_path = *path;
960 error = do_dentry_open(f, inode, NULL);
961 if (error) {
962 fput(f);
963 f = ERR_PTR(error);
964 }
965 }
966 return f;
967 }
968 EXPORT_SYMBOL(open_with_fake_path);
969
build_open_flags(int flags,umode_t mode,struct open_flags * op)970 static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
971 {
972 int lookup_flags = 0;
973 int acc_mode = ACC_MODE(flags);
974
975 /*
976 * Clear out all open flags we don't know about so that we don't report
977 * them in fcntl(F_GETFD) or similar interfaces.
978 */
979 flags &= VALID_OPEN_FLAGS;
980
981 if (flags & (O_CREAT | __O_TMPFILE))
982 op->mode = (mode & S_IALLUGO) | S_IFREG;
983 else
984 op->mode = 0;
985
986 /* Must never be set by userspace */
987 flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
988
989 /*
990 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
991 * check for O_DSYNC if the need any syncing at all we enforce it's
992 * always set instead of having to deal with possibly weird behaviour
993 * for malicious applications setting only __O_SYNC.
994 */
995 if (flags & __O_SYNC)
996 flags |= O_DSYNC;
997
998 if (flags & __O_TMPFILE) {
999 if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
1000 return -EINVAL;
1001 if (!(acc_mode & MAY_WRITE))
1002 return -EINVAL;
1003 } else if (flags & O_PATH) {
1004 /*
1005 * If we have O_PATH in the open flag. Then we
1006 * cannot have anything other than the below set of flags
1007 */
1008 flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
1009 acc_mode = 0;
1010 }
1011
1012 op->open_flag = flags;
1013
1014 /* O_TRUNC implies we need access checks for write permissions */
1015 if (flags & O_TRUNC)
1016 acc_mode |= MAY_WRITE;
1017
1018 /* Allow the LSM permission hook to distinguish append
1019 access from general write access. */
1020 if (flags & O_APPEND)
1021 acc_mode |= MAY_APPEND;
1022
1023 op->acc_mode = acc_mode;
1024
1025 op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
1026
1027 if (flags & O_CREAT) {
1028 op->intent |= LOOKUP_CREATE;
1029 if (flags & O_EXCL)
1030 op->intent |= LOOKUP_EXCL;
1031 }
1032
1033 if (flags & O_DIRECTORY)
1034 lookup_flags |= LOOKUP_DIRECTORY;
1035 if (!(flags & O_NOFOLLOW))
1036 lookup_flags |= LOOKUP_FOLLOW;
1037 op->lookup_flags = lookup_flags;
1038 return 0;
1039 }
1040
1041 /**
1042 * file_open_name - open file and return file pointer
1043 *
1044 * @name: struct filename containing path to open
1045 * @flags: open flags as per the open(2) second argument
1046 * @mode: mode for the new file if O_CREAT is set, else ignored
1047 *
1048 * This is the helper to open a file from kernelspace if you really
1049 * have to. But in generally you should not do this, so please move
1050 * along, nothing to see here..
1051 */
file_open_name(struct filename * name,int flags,umode_t mode)1052 struct file *file_open_name(struct filename *name, int flags, umode_t mode)
1053 {
1054 struct open_flags op;
1055 int err = build_open_flags(flags, mode, &op);
1056 return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
1057 }
1058
1059 /**
1060 * filp_open - open file and return file pointer
1061 *
1062 * @filename: path to open
1063 * @flags: open flags as per the open(2) second argument
1064 * @mode: mode for the new file if O_CREAT is set, else ignored
1065 *
1066 * This is the helper to open a file from kernelspace if you really
1067 * have to. But in generally you should not do this, so please move
1068 * along, nothing to see here..
1069 */
filp_open(const char * filename,int flags,umode_t mode)1070 struct file *filp_open(const char *filename, int flags, umode_t mode)
1071 {
1072 struct filename *name = getname_kernel(filename);
1073 struct file *file = ERR_CAST(name);
1074
1075 if (!IS_ERR(name)) {
1076 file = file_open_name(name, flags, mode);
1077 putname(name);
1078 }
1079 return file;
1080 }
1081 EXPORT_SYMBOL(filp_open);
1082
file_open_root(struct dentry * dentry,struct vfsmount * mnt,const char * filename,int flags,umode_t mode)1083 struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
1084 const char *filename, int flags, umode_t mode)
1085 {
1086 struct open_flags op;
1087 int err = build_open_flags(flags, mode, &op);
1088 if (err)
1089 return ERR_PTR(err);
1090 return do_file_open_root(dentry, mnt, filename, &op);
1091 }
1092 EXPORT_SYMBOL(file_open_root);
1093
do_sys_open(int dfd,const char __user * filename,int flags,umode_t mode)1094 long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
1095 {
1096 struct open_flags op;
1097 int fd = build_open_flags(flags, mode, &op);
1098 struct filename *tmp;
1099
1100 if (fd)
1101 return fd;
1102
1103 tmp = getname(filename);
1104 if (IS_ERR(tmp))
1105 return PTR_ERR(tmp);
1106
1107 fd = get_unused_fd_flags(flags);
1108 if (fd >= 0) {
1109 struct file *f = do_filp_open(dfd, tmp, &op);
1110 if (IS_ERR(f)) {
1111 put_unused_fd(fd);
1112 fd = PTR_ERR(f);
1113 } else {
1114 fsnotify_open(f);
1115 fd_install(fd, f);
1116 }
1117 }
1118 putname(tmp);
1119 return fd;
1120 }
1121
SYSCALL_DEFINE3(open,const char __user *,filename,int,flags,umode_t,mode)1122 SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1123 {
1124 if (force_o_largefile())
1125 flags |= O_LARGEFILE;
1126
1127 return do_sys_open(AT_FDCWD, filename, flags, mode);
1128 }
1129
SYSCALL_DEFINE4(openat,int,dfd,const char __user *,filename,int,flags,umode_t,mode)1130 SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
1131 umode_t, mode)
1132 {
1133 if (force_o_largefile())
1134 flags |= O_LARGEFILE;
1135
1136 return do_sys_open(dfd, filename, flags, mode);
1137 }
1138
1139 #ifdef CONFIG_COMPAT
1140 /*
1141 * Exactly like sys_open(), except that it doesn't set the
1142 * O_LARGEFILE flag.
1143 */
COMPAT_SYSCALL_DEFINE3(open,const char __user *,filename,int,flags,umode_t,mode)1144 COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1145 {
1146 return do_sys_open(AT_FDCWD, filename, flags, mode);
1147 }
1148
1149 /*
1150 * Exactly like sys_openat(), except that it doesn't set the
1151 * O_LARGEFILE flag.
1152 */
COMPAT_SYSCALL_DEFINE4(openat,int,dfd,const char __user *,filename,int,flags,umode_t,mode)1153 COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
1154 {
1155 return do_sys_open(dfd, filename, flags, mode);
1156 }
1157 #endif
1158
1159 #ifndef __alpha__
1160
1161 /*
1162 * For backward compatibility? Maybe this should be moved
1163 * into arch/i386 instead?
1164 */
SYSCALL_DEFINE2(creat,const char __user *,pathname,umode_t,mode)1165 SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
1166 {
1167 return ksys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1168 }
1169
1170 #endif
1171
1172 /*
1173 * "id" is the POSIX thread ID. We use the
1174 * files pointer for this..
1175 */
filp_close(struct file * filp,fl_owner_t id)1176 int filp_close(struct file *filp, fl_owner_t id)
1177 {
1178 int retval = 0;
1179
1180 if (!file_count(filp)) {
1181 printk(KERN_ERR "VFS: Close: file count is 0\n");
1182 return 0;
1183 }
1184
1185 if (filp->f_op->flush)
1186 retval = filp->f_op->flush(filp, id);
1187
1188 if (likely(!(filp->f_mode & FMODE_PATH))) {
1189 dnotify_flush(filp, id);
1190 locks_remove_posix(filp, id);
1191 }
1192 fput(filp);
1193 return retval;
1194 }
1195
1196 EXPORT_SYMBOL(filp_close);
1197
1198 /*
1199 * Careful here! We test whether the file pointer is NULL before
1200 * releasing the fd. This ensures that one clone task can't release
1201 * an fd while another clone is opening it.
1202 */
SYSCALL_DEFINE1(close,unsigned int,fd)1203 SYSCALL_DEFINE1(close, unsigned int, fd)
1204 {
1205 int retval = __close_fd(current->files, fd);
1206
1207 /* can't restart close syscall because file table entry was cleared */
1208 if (unlikely(retval == -ERESTARTSYS ||
1209 retval == -ERESTARTNOINTR ||
1210 retval == -ERESTARTNOHAND ||
1211 retval == -ERESTART_RESTARTBLOCK))
1212 retval = -EINTR;
1213
1214 return retval;
1215 }
1216
1217 /*
1218 * This routine simulates a hangup on the tty, to arrange that users
1219 * are given clean terminals at login time.
1220 */
SYSCALL_DEFINE0(vhangup)1221 SYSCALL_DEFINE0(vhangup)
1222 {
1223 if (capable(CAP_SYS_TTY_CONFIG)) {
1224 tty_vhangup_self();
1225 return 0;
1226 }
1227 return -EPERM;
1228 }
1229
1230 /*
1231 * Called when an inode is about to be open.
1232 * We use this to disallow opening large files on 32bit systems if
1233 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1234 * on this flag in sys_open.
1235 */
generic_file_open(struct inode * inode,struct file * filp)1236 int generic_file_open(struct inode * inode, struct file * filp)
1237 {
1238 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1239 return -EOVERFLOW;
1240 return 0;
1241 }
1242
1243 EXPORT_SYMBOL(generic_file_open);
1244
1245 /*
1246 * This is used by subsystems that don't want seekable
1247 * file descriptors. The function is not supposed to ever fail, the only
1248 * reason it returns an 'int' and not 'void' is so that it can be plugged
1249 * directly into file_operations structure.
1250 */
nonseekable_open(struct inode * inode,struct file * filp)1251 int nonseekable_open(struct inode *inode, struct file *filp)
1252 {
1253 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1254 return 0;
1255 }
1256
1257 EXPORT_SYMBOL(nonseekable_open);
1258
1259 /*
1260 * stream_open is used by subsystems that want stream-like file descriptors.
1261 * Such file descriptors are not seekable and don't have notion of position
1262 * (file.f_pos is always 0 and ppos passed to .read()/.write() is always NULL).
1263 * Contrary to file descriptors of other regular files, .read() and .write()
1264 * can run simultaneously.
1265 *
1266 * stream_open never fails and is marked to return int so that it could be
1267 * directly used as file_operations.open .
1268 */
stream_open(struct inode * inode,struct file * filp)1269 int stream_open(struct inode *inode, struct file *filp)
1270 {
1271 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE | FMODE_ATOMIC_POS);
1272 filp->f_mode |= FMODE_STREAM;
1273 return 0;
1274 }
1275
1276 EXPORT_SYMBOL(stream_open);
1277