]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/open.c
csky: fix syscache.c fallthrough warning
[mirror_ubuntu-hirsute-kernel.git] / fs / open.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
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>
1da177e4 10#include <linux/file.h>
9f3acc31 11#include <linux/fdtable.h>
0eeca283 12#include <linux/fsnotify.h>
1da177e4 13#include <linux/module.h>
1da177e4
LT
14#include <linux/tty.h>
15#include <linux/namei.h>
16#include <linux/backing-dev.h>
16f7e0fe 17#include <linux/capability.h>
086f7316 18#include <linux/securebits.h>
1da177e4
LT
19#include <linux/security.h>
20#include <linux/mount.h>
5590ff0d 21#include <linux/fcntl.h>
5a0e3ad6 22#include <linux/slab.h>
7c0f6ba6 23#include <linux/uaccess.h>
1da177e4 24#include <linux/fs.h>
ef3daeda 25#include <linux/personality.h>
1da177e4
LT
26#include <linux/pagemap.h>
27#include <linux/syscalls.h>
ab2af1f5 28#include <linux/rcupdate.h>
73241ccc 29#include <linux/audit.h>
97ac7350 30#include <linux/falloc.h>
5ad4e53b 31#include <linux/fs_struct.h>
b65a9cfc 32#include <linux/ima.h>
2dfc1cae 33#include <linux/dnotify.h>
3f6d078d 34#include <linux/compat.h>
1da177e4 35
e81e3f4d
EP
36#include "internal.h"
37
4a30131e
N
38int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
39 struct file *filp)
1da177e4 40{
939a9421 41 int ret;
1da177e4
LT
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;
4a30131e 49 newattrs.ia_valid = ATTR_SIZE | time_attrs;
cc4e69de
MS
50 if (filp) {
51 newattrs.ia_file = filp;
52 newattrs.ia_valid |= ATTR_FILE;
53 }
1da177e4 54
45f147a1
JK
55 /* Remove suid, sgid, and file capabilities on truncate too */
56 ret = dentry_needs_remove_privs(dentry);
57 if (ret < 0)
58 return ret;
939a9421
AW
59 if (ret)
60 newattrs.ia_valid |= ret | ATTR_FORCE;
7b82dc0e 61
5955102c 62 inode_lock(dentry->d_inode);
27ac0ffe
BF
63 /* Note any delegations or leases have already been broken: */
64 ret = notify_change(dentry, &newattrs, NULL);
5955102c 65 inode_unlock(dentry->d_inode);
939a9421 66 return ret;
1da177e4 67}
0d331203 68EXPORT_SYMBOL_GPL(do_truncate);
1da177e4 69
7df818b2 70long vfs_truncate(const struct path *path, loff_t length)
1da177e4 71{
2d8f3038 72 struct inode *inode;
a02de960 73 long error;
1da177e4 74
a02de960 75 inode = path->dentry->d_inode;
1da177e4
LT
76
77 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
1da177e4 78 if (S_ISDIR(inode->i_mode))
a02de960 79 return -EISDIR;
1da177e4 80 if (!S_ISREG(inode->i_mode))
a02de960 81 return -EINVAL;
1da177e4 82
a02de960 83 error = mnt_want_write(path->mnt);
1da177e4 84 if (error)
a02de960 85 goto out;
1da177e4 86
256984a8 87 error = inode_permission(inode, MAY_WRITE);
9ac9b847
DH
88 if (error)
89 goto mnt_drop_write_and_out;
1da177e4
LT
90
91 error = -EPERM;
c82e42da 92 if (IS_APPEND(inode))
9ac9b847 93 goto mnt_drop_write_and_out;
1da177e4 94
8cf9ee50 95 error = get_write_access(inode);
1da177e4 96 if (error)
9ac9b847 97 goto mnt_drop_write_and_out;
1da177e4 98
9700382c 99 /*
100 * Make sure that there are no leases. get_write_access() protects
101 * against the truncate racing with a lease-granting setlease().
102 */
8737c930 103 error = break_lease(inode, O_WRONLY);
1da177e4 104 if (error)
9700382c 105 goto put_write_and_out;
1da177e4
LT
106
107 error = locks_verify_truncate(inode, NULL, length);
be6d3e56 108 if (!error)
a02de960 109 error = security_path_truncate(path);
907f4554 110 if (!error)
a02de960 111 error = do_truncate(path->dentry, length, 0, NULL);
1da177e4 112
9700382c 113put_write_and_out:
8cf9ee50 114 put_write_access(inode);
9ac9b847 115mnt_drop_write_and_out:
a02de960 116 mnt_drop_write(path->mnt);
1da177e4
LT
117out:
118 return error;
119}
a02de960
DH
120EXPORT_SYMBOL_GPL(vfs_truncate);
121
df260e21 122long do_sys_truncate(const char __user *pathname, loff_t length)
a02de960 123{
48f7530d 124 unsigned int lookup_flags = LOOKUP_FOLLOW;
a02de960
DH
125 struct path path;
126 int error;
127
128 if (length < 0) /* sorry, but loff_t says... */
129 return -EINVAL;
130
48f7530d
JL
131retry:
132 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
a02de960
DH
133 if (!error) {
134 error = vfs_truncate(&path, length);
135 path_put(&path);
136 }
48f7530d
JL
137 if (retry_estale(error, lookup_flags)) {
138 lookup_flags |= LOOKUP_REVAL;
139 goto retry;
140 }
a02de960
DH
141 return error;
142}
1da177e4 143
4fd8da8d 144SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
1da177e4 145{
4fd8da8d 146 return do_sys_truncate(path, length);
1da177e4
LT
147}
148
3f6d078d
AV
149#ifdef CONFIG_COMPAT
150COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
151{
152 return do_sys_truncate(path, length);
153}
154#endif
155
411d9475 156long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
1da177e4 157{
bf2965d5 158 struct inode *inode;
1da177e4 159 struct dentry *dentry;
2903ff01 160 struct fd f;
1da177e4
LT
161 int error;
162
163 error = -EINVAL;
164 if (length < 0)
165 goto out;
166 error = -EBADF;
2903ff01
AV
167 f = fdget(fd);
168 if (!f.file)
1da177e4
LT
169 goto out;
170
171 /* explicitly opened as large or we are on 64-bit box */
2903ff01 172 if (f.file->f_flags & O_LARGEFILE)
1da177e4
LT
173 small = 0;
174
2903ff01 175 dentry = f.file->f_path.dentry;
1da177e4
LT
176 inode = dentry->d_inode;
177 error = -EINVAL;
2903ff01 178 if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
1da177e4
LT
179 goto out_putf;
180
181 error = -EINVAL;
182 /* Cannot ftruncate over 2^31 bytes without large file support */
183 if (small && length > MAX_NON_LFS)
184 goto out_putf;
185
186 error = -EPERM;
78757af6
AG
187 /* Check IS_APPEND on real upper inode */
188 if (IS_APPEND(file_inode(f.file)))
1da177e4
LT
189 goto out_putf;
190
14da9200 191 sb_start_write(inode->i_sb);
2903ff01 192 error = locks_verify_truncate(inode, f.file, length);
be6d3e56 193 if (!error)
2903ff01 194 error = security_path_truncate(&f.file->f_path);
1da177e4 195 if (!error)
2903ff01 196 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
14da9200 197 sb_end_write(inode->i_sb);
1da177e4 198out_putf:
2903ff01 199 fdput(f);
1da177e4
LT
200out:
201 return error;
202}
203
bdc480e3 204SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
1da177e4 205{
2cf09666 206 return do_sys_ftruncate(fd, length, 1);
1da177e4
LT
207}
208
3f6d078d
AV
209#ifdef CONFIG_COMPAT
210COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
211{
212 return do_sys_ftruncate(fd, length, 1);
213}
214#endif
215
1da177e4
LT
216/* LFS versions of truncate are only needed on 32 bit machines */
217#if BITS_PER_LONG == 32
4a0fd5bf 218SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
1da177e4
LT
219{
220 return do_sys_truncate(path, length);
221}
222
4a0fd5bf 223SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
1da177e4 224{
2cf09666 225 return do_sys_ftruncate(fd, length, 0);
1da177e4 226}
6673e0c3 227#endif /* BITS_PER_LONG == 32 */
1da177e4 228
3e63cbb1 229
72c72bdf 230int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
97ac7350 231{
496ad9aa 232 struct inode *inode = file_inode(file);
3e63cbb1 233 long ret;
97ac7350
AA
234
235 if (offset < 0 || len <= 0)
3e63cbb1 236 return -EINVAL;
97ac7350
AA
237
238 /* Return error if mode is not supported */
dd46c787 239 if (mode & ~FALLOC_FL_SUPPORTED_MASK)
409332b6
LC
240 return -EOPNOTSUPP;
241
242 /* Punch hole and zero range are mutually exclusive */
243 if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
244 (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
79124f18
JB
245 return -EOPNOTSUPP;
246
247 /* Punch hole must have keep size set */
248 if ((mode & FALLOC_FL_PUNCH_HOLE) &&
249 !(mode & FALLOC_FL_KEEP_SIZE))
3e63cbb1 250 return -EOPNOTSUPP;
97ac7350 251
00f5e619
NJ
252 /* Collapse range should only be used exclusively. */
253 if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
254 (mode & ~FALLOC_FL_COLLAPSE_RANGE))
255 return -EINVAL;
256
dd46c787
NJ
257 /* Insert range should only be used exclusively. */
258 if ((mode & FALLOC_FL_INSERT_RANGE) &&
259 (mode & ~FALLOC_FL_INSERT_RANGE))
260 return -EINVAL;
261
71be6b49
DW
262 /* Unshare range should only be used with allocate mode. */
263 if ((mode & FALLOC_FL_UNSHARE_RANGE) &&
264 (mode & ~(FALLOC_FL_UNSHARE_RANGE | FALLOC_FL_KEEP_SIZE)))
265 return -EINVAL;
266
97ac7350 267 if (!(file->f_mode & FMODE_WRITE))
3e63cbb1 268 return -EBADF;
1ca551c6 269
00f5e619 270 /*
8fc61d92 271 * We can only allow pure fallocate on append only files
00f5e619 272 */
8fc61d92 273 if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
1ca551c6
MS
274 return -EPERM;
275
276 if (IS_IMMUTABLE(inode))
277 return -EPERM;
278
0790b31b 279 /*
6d2b6170 280 * We cannot allow any fallocate operation on an active swapfile
0790b31b
LC
281 */
282 if (IS_SWAPFILE(inode))
6d2b6170 283 return -ETXTBSY;
0790b31b 284
97ac7350
AA
285 /*
286 * Revalidate the write permissions, in case security policy has
287 * changed since the files were opened.
288 */
289 ret = security_file_permission(file, MAY_WRITE);
290 if (ret)
3e63cbb1 291 return ret;
97ac7350 292
97ac7350 293 if (S_ISFIFO(inode->i_mode))
3e63cbb1 294 return -ESPIPE;
97ac7350 295
9e79b132
AG
296 if (S_ISDIR(inode->i_mode))
297 return -EISDIR;
298
299 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
3e63cbb1 300 return -ENODEV;
97ac7350 301
97ac7350
AA
302 /* Check for wrap through zero too */
303 if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
3e63cbb1 304 return -EFBIG;
97ac7350 305
2fe17c10 306 if (!file->f_op->fallocate)
3e63cbb1 307 return -EOPNOTSUPP;
97ac7350 308
bfe219d3 309 file_start_write(file);
14da9200 310 ret = file->f_op->fallocate(file, mode, offset, len);
820c12d5
HS
311
312 /*
313 * Create inotify and fanotify events.
314 *
315 * To keep the logic simple always create events if fallocate succeeds.
316 * This implies that events are even created if the file size remains
317 * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
318 */
319 if (ret == 0)
320 fsnotify_modify(file);
321
bfe219d3 322 file_end_write(file);
14da9200 323 return ret;
3e63cbb1 324}
72c72bdf 325EXPORT_SYMBOL_GPL(vfs_fallocate);
3e63cbb1 326
edf292c7 327int ksys_fallocate(int fd, int mode, loff_t offset, loff_t len)
3e63cbb1 328{
2903ff01 329 struct fd f = fdget(fd);
3e63cbb1
AJ
330 int error = -EBADF;
331
2903ff01 332 if (f.file) {
72c72bdf 333 error = vfs_fallocate(f.file, mode, offset, len);
2903ff01 334 fdput(f);
3e63cbb1 335 }
3e63cbb1 336 return error;
97ac7350 337}
3e63cbb1 338
edf292c7
DB
339SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
340{
341 return ksys_fallocate(fd, mode, offset, len);
342}
343
1da177e4
LT
344/*
345 * access() needs to use the real uid/gid, not the effective uid/gid.
346 * We do this by temporarily clearing all FS-related capabilities and
347 * switching the fsuid/fsgid around to the real ones.
348 */
94704515 349static const struct cred *access_override_creds(void)
1da177e4 350{
d84f4f99
DH
351 const struct cred *old_cred;
352 struct cred *override_cred;
1da177e4 353
d84f4f99
DH
354 override_cred = prepare_creds();
355 if (!override_cred)
94704515 356 return NULL;
1da177e4 357
d84f4f99
DH
358 override_cred->fsuid = override_cred->uid;
359 override_cred->fsgid = override_cred->gid;
1da177e4 360
086f7316 361 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
1cdcbec1 362 /* Clear the capabilities if we switch to a non-root user */
18815a18
EB
363 kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
364 if (!uid_eq(override_cred->uid, root_uid))
d84f4f99 365 cap_clear(override_cred->cap_effective);
086f7316 366 else
d84f4f99
DH
367 override_cred->cap_effective =
368 override_cred->cap_permitted;
086f7316 369 }
1da177e4 370
d7852fbd
LT
371 /*
372 * The new set of credentials can *only* be used in
373 * task-synchronous circumstances, and does not need
374 * RCU freeing, unless somebody then takes a separate
375 * reference to it.
376 *
377 * NOTE! This is _only_ true because this credential
378 * is used purely for override_creds() that installs
379 * it as the subjective cred. Other threads will be
380 * accessing ->real_cred, not the subjective cred.
381 *
382 * If somebody _does_ make a copy of this (using the
383 * 'get_current_cred()' function), that will clear the
384 * non_rcu field, because now that other user may be
385 * expecting RCU freeing. But normal thread-synchronous
386 * cred accesses will keep things non-RCY.
387 */
388 override_cred->non_rcu = 1;
389
d84f4f99 390 old_cred = override_creds(override_cred);
94704515
MS
391
392 /* override_cred() gets its own ref */
393 put_cred(override_cred);
394
395 return old_cred;
396}
397
eb9d7d39 398static long do_faccessat(int dfd, const char __user *filename, int mode, int flags)
94704515
MS
399{
400 struct path path;
401 struct inode *inode;
402 int res;
403 unsigned int lookup_flags = LOOKUP_FOLLOW;
c8ffd8bc 404 const struct cred *old_cred = NULL;
94704515
MS
405
406 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
407 return -EINVAL;
408
c8ffd8bc
MS
409 if (flags & ~(AT_EACCESS | AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))
410 return -EINVAL;
411
412 if (flags & AT_SYMLINK_NOFOLLOW)
413 lookup_flags &= ~LOOKUP_FOLLOW;
414 if (flags & AT_EMPTY_PATH)
415 lookup_flags |= LOOKUP_EMPTY;
416
417 if (!(flags & AT_EACCESS)) {
418 old_cred = access_override_creds();
419 if (!old_cred)
420 return -ENOMEM;
421 }
94704515 422
87fa5595
JL
423retry:
424 res = user_path_at(dfd, filename, lookup_flags, &path);
6902d925
DH
425 if (res)
426 goto out;
427
63afdfc7 428 inode = d_backing_inode(path.dentry);
256984a8
AV
429
430 if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
30524472
AV
431 /*
432 * MAY_EXEC on regular files is denied if the fs is mounted
433 * with the "noexec" flag.
434 */
435 res = -EACCES;
90f8572b 436 if (path_noexec(&path))
30524472
AV
437 goto out_path_release;
438 }
439
256984a8 440 res = inode_permission(inode, mode | MAY_ACCESS);
6902d925 441 /* SuS v2 requires we report a read only fs too */
256984a8 442 if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
6902d925 443 goto out_path_release;
2f676cbc
DH
444 /*
445 * This is a rare case where using __mnt_is_readonly()
446 * is OK without a mnt_want/drop_write() pair. Since
447 * no actual write to the fs is performed here, we do
448 * not need to telegraph to that to anyone.
449 *
450 * By doing this, we accept that this access is
451 * inherently racy and know that the fs may change
452 * state before we even see this result.
453 */
2d8f3038 454 if (__mnt_is_readonly(path.mnt))
6902d925 455 res = -EROFS;
1da177e4 456
6902d925 457out_path_release:
2d8f3038 458 path_put(&path);
87fa5595
JL
459 if (retry_estale(res, lookup_flags)) {
460 lookup_flags |= LOOKUP_REVAL;
461 goto retry;
462 }
6902d925 463out:
c8ffd8bc
MS
464 if (old_cred)
465 revert_creds(old_cred);
466
1da177e4
LT
467 return res;
468}
469
cbfe20f5
DB
470SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
471{
c8ffd8bc
MS
472 return do_faccessat(dfd, filename, mode, 0);
473}
474
475SYSCALL_DEFINE4(faccessat2, int, dfd, const char __user *, filename, int, mode,
476 int, flags)
477{
478 return do_faccessat(dfd, filename, mode, flags);
cbfe20f5
DB
479}
480
ca013e94 481SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
5590ff0d 482{
c8ffd8bc 483 return do_faccessat(AT_FDCWD, filename, mode, 0);
5590ff0d
UD
484}
485
db63f1e3 486SYSCALL_DEFINE1(chdir, const char __user *, filename)
1da177e4 487{
2d8f3038 488 struct path path;
1da177e4 489 int error;
0291c0a5
JL
490 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
491retry:
492 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
1da177e4
LT
493 if (error)
494 goto out;
495
9cfcac81 496 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
1da177e4
LT
497 if (error)
498 goto dput_and_out;
499
2d8f3038 500 set_fs_pwd(current->fs, &path);
1da177e4
LT
501
502dput_and_out:
2d8f3038 503 path_put(&path);
0291c0a5
JL
504 if (retry_estale(error, lookup_flags)) {
505 lookup_flags |= LOOKUP_REVAL;
506 goto retry;
507 }
1da177e4
LT
508out:
509 return error;
510}
511
3cdad428 512SYSCALL_DEFINE1(fchdir, unsigned int, fd)
1da177e4 513{
2903ff01 514 struct fd f = fdget_raw(fd);
159b0956 515 int error;
1da177e4
LT
516
517 error = -EBADF;
2903ff01 518 if (!f.file)
1da177e4
LT
519 goto out;
520
1da177e4 521 error = -ENOTDIR;
159b0956 522 if (!d_can_lookup(f.file->f_path.dentry))
1da177e4
LT
523 goto out_putf;
524
159b0956 525 error = inode_permission(file_inode(f.file), MAY_EXEC | MAY_CHDIR);
1da177e4 526 if (!error)
2903ff01 527 set_fs_pwd(current->fs, &f.file->f_path);
1da177e4 528out_putf:
2903ff01 529 fdput(f);
1da177e4
LT
530out:
531 return error;
532}
533
4b7ca501 534SYSCALL_DEFINE1(chroot, const char __user *, filename)
1da177e4 535{
2d8f3038 536 struct path path;
1da177e4 537 int error;
2771261e
JL
538 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
539retry:
540 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
1da177e4
LT
541 if (error)
542 goto out;
543
9cfcac81 544 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
1da177e4
LT
545 if (error)
546 goto dput_and_out;
547
548 error = -EPERM;
c7b96acf 549 if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
1da177e4 550 goto dput_and_out;
8b8efb44
TH
551 error = security_path_chroot(&path);
552 if (error)
553 goto dput_and_out;
1da177e4 554
2d8f3038 555 set_fs_root(current->fs, &path);
1da177e4
LT
556 error = 0;
557dput_and_out:
2d8f3038 558 path_put(&path);
2771261e
JL
559 if (retry_estale(error, lookup_flags)) {
560 lookup_flags |= LOOKUP_REVAL;
561 goto retry;
562 }
1da177e4
LT
563out:
564 return error;
565}
566
1097742e 567int chmod_common(const struct path *path, umode_t mode)
1da177e4 568{
e57712eb 569 struct inode *inode = path->dentry->d_inode;
27ac0ffe 570 struct inode *delegated_inode = NULL;
1da177e4 571 struct iattr newattrs;
e57712eb 572 int error;
1da177e4 573
e57712eb
AV
574 error = mnt_want_write(path->mnt);
575 if (error)
576 return error;
27ac0ffe 577retry_deleg:
5955102c 578 inode_lock(inode);
cdcf116d 579 error = security_path_chmod(path, mode);
e57712eb 580 if (error)
fe542cf5 581 goto out_unlock;
1da177e4
LT
582 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
583 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
27ac0ffe 584 error = notify_change(path->dentry, &newattrs, &delegated_inode);
fe542cf5 585out_unlock:
5955102c 586 inode_unlock(inode);
27ac0ffe
BF
587 if (delegated_inode) {
588 error = break_deleg_wait(&delegated_inode);
589 if (!error)
590 goto retry_deleg;
591 }
e57712eb
AV
592 mnt_drop_write(path->mnt);
593 return error;
594}
595
9e96c8c0
CH
596int vfs_fchmod(struct file *file, umode_t mode)
597{
598 audit_file(file);
599 return chmod_common(&file->f_path, mode);
600}
601
b25ba7c3 602SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
e57712eb 603{
173c8401 604 struct fd f = fdget(fd);
e57712eb
AV
605 int err = -EBADF;
606
173c8401 607 if (f.file) {
9e96c8c0 608 err = vfs_fchmod(f.file, mode);
173c8401 609 fdput(f);
e57712eb 610 }
1da177e4
LT
611 return err;
612}
613
1097742e 614static int do_fchmodat(int dfd, const char __user *filename, umode_t mode)
1da177e4 615{
2d8f3038 616 struct path path;
1da177e4 617 int error;
14ff690c
JL
618 unsigned int lookup_flags = LOOKUP_FOLLOW;
619retry:
620 error = user_path_at(dfd, filename, lookup_flags, &path);
e57712eb
AV
621 if (!error) {
622 error = chmod_common(&path, mode);
623 path_put(&path);
14ff690c
JL
624 if (retry_estale(error, lookup_flags)) {
625 lookup_flags |= LOOKUP_REVAL;
626 goto retry;
627 }
e57712eb 628 }
1da177e4
LT
629 return error;
630}
631
03450e27
DB
632SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename,
633 umode_t, mode)
634{
635 return do_fchmodat(dfd, filename, mode);
636}
637
49f0a076 638SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
5590ff0d 639{
03450e27 640 return do_fchmodat(AT_FDCWD, filename, mode);
5590ff0d
UD
641}
642
b873498f 643int chown_common(const struct path *path, uid_t user, gid_t group)
1da177e4 644{
fe542cf5 645 struct inode *inode = path->dentry->d_inode;
27ac0ffe 646 struct inode *delegated_inode = NULL;
1da177e4
LT
647 int error;
648 struct iattr newattrs;
52137abe
EB
649 kuid_t uid;
650 kgid_t gid;
651
652 uid = make_kuid(current_user_ns(), user);
653 gid = make_kgid(current_user_ns(), group);
1da177e4 654
c1b8940b 655retry_deleg:
1da177e4
LT
656 newattrs.ia_valid = ATTR_CTIME;
657 if (user != (uid_t) -1) {
52137abe
EB
658 if (!uid_valid(uid))
659 return -EINVAL;
1da177e4 660 newattrs.ia_valid |= ATTR_UID;
52137abe 661 newattrs.ia_uid = uid;
1da177e4
LT
662 }
663 if (group != (gid_t) -1) {
52137abe
EB
664 if (!gid_valid(gid))
665 return -EINVAL;
1da177e4 666 newattrs.ia_valid |= ATTR_GID;
52137abe 667 newattrs.ia_gid = gid;
1da177e4
LT
668 }
669 if (!S_ISDIR(inode->i_mode))
b5376771
SH
670 newattrs.ia_valid |=
671 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
5955102c 672 inode_lock(inode);
d2b31ca6 673 error = security_path_chown(path, uid, gid);
fe542cf5 674 if (!error)
27ac0ffe 675 error = notify_change(path->dentry, &newattrs, &delegated_inode);
5955102c 676 inode_unlock(inode);
27ac0ffe
BF
677 if (delegated_inode) {
678 error = break_deleg_wait(&delegated_inode);
679 if (!error)
680 goto retry_deleg;
681 }
1da177e4
LT
682 return error;
683}
684
55731b3c
DB
685int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
686 int flag)
5590ff0d 687{
2d8f3038 688 struct path path;
5590ff0d 689 int error = -EINVAL;
65cfc672 690 int lookup_flags;
5590ff0d 691
65cfc672 692 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
5590ff0d
UD
693 goto out;
694
65cfc672
AV
695 lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
696 if (flag & AT_EMPTY_PATH)
697 lookup_flags |= LOOKUP_EMPTY;
99a5df37 698retry:
65cfc672 699 error = user_path_at(dfd, filename, lookup_flags, &path);
6902d925
DH
700 if (error)
701 goto out;
2d8f3038 702 error = mnt_want_write(path.mnt);
2af482a7
DH
703 if (error)
704 goto out_release;
fe542cf5 705 error = chown_common(&path, user, group);
2d8f3038 706 mnt_drop_write(path.mnt);
2af482a7 707out_release:
2d8f3038 708 path_put(&path);
99a5df37
JL
709 if (retry_estale(error, lookup_flags)) {
710 lookup_flags |= LOOKUP_REVAL;
711 goto retry;
712 }
5590ff0d
UD
713out:
714 return error;
715}
716
55731b3c
DB
717SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
718 gid_t, group, int, flag)
719{
720 return do_fchownat(dfd, filename, user, group, flag);
721}
722
55e4def0 723SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
1da177e4 724{
55731b3c 725 return do_fchownat(AT_FDCWD, filename, user, group, 0);
55e4def0 726}
1da177e4 727
55e4def0
DH
728SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
729{
55731b3c
DB
730 return do_fchownat(AT_FDCWD, filename, user, group,
731 AT_SYMLINK_NOFOLLOW);
1da177e4
LT
732}
733
c04011fe
CH
734int vfs_fchown(struct file *file, uid_t user, gid_t group)
735{
736 int error;
737
738 error = mnt_want_write_file(file);
739 if (error)
740 return error;
741 audit_file(file);
742 error = chown_common(&file->f_path, user, group);
743 mnt_drop_write_file(file);
744 return error;
745}
746
55731b3c 747int ksys_fchown(unsigned int fd, uid_t user, gid_t group)
1da177e4 748{
2903ff01 749 struct fd f = fdget(fd);
1da177e4
LT
750 int error = -EBADF;
751
c04011fe
CH
752 if (f.file) {
753 error = vfs_fchown(f.file, user, group);
754 fdput(f);
755 }
1da177e4
LT
756 return error;
757}
758
55731b3c
DB
759SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
760{
761 return ksys_fchown(fd, user, group);
762}
763
02e5180d 764static int do_dentry_open(struct file *f,
4bacc9c9 765 struct inode *inode,
ae2bb293 766 int (*open)(struct inode *, struct file *))
1da177e4 767{
1abf0c71 768 static const struct file_operations empty_fops = {};
1da177e4
LT
769 int error;
770
b5bcdda3 771 path_get(&f->f_path);
4bacc9c9 772 f->f_inode = inode;
1da177e4 773 f->f_mapping = inode->i_mapping;
5660e13d 774 f->f_wb_err = filemap_sample_wb_err(f->f_mapping);
735e4ae5 775 f->f_sb_err = file_sample_sb_err(f);
5660e13d 776
3f4d5a00 777 if (unlikely(f->f_flags & O_PATH)) {
f5d11409 778 f->f_mode = FMODE_PATH | FMODE_OPENED;
1abf0c71 779 f->f_op = &empty_fops;
af04fadc 780 return 0;
1abf0c71
AV
781 }
782
dd20908a 783 if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
0ccb2863 784 error = get_write_access(inode);
3f4d5a00 785 if (unlikely(error))
1da177e4 786 goto cleanup_file;
0ccb2863 787 error = __mnt_want_write(f->f_path.mnt);
3f4d5a00 788 if (unlikely(error)) {
0ccb2863
AV
789 put_write_access(inode);
790 goto cleanup_file;
791 }
83f936c7 792 f->f_mode |= FMODE_WRITER;
1da177e4
LT
793 }
794
2be7d348
LT
795 /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
796 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
797 f->f_mode |= FMODE_ATOMIC_POS;
798
1abf0c71 799 f->f_op = fops_get(inode->i_fop);
7159d544 800 if (WARN_ON(!f->f_op)) {
72c2d531
AV
801 error = -ENODEV;
802 goto cleanup_all;
803 }
1abf0c71 804
e3f20ae2 805 error = security_file_open(f);
788e7dd4
YN
806 if (error)
807 goto cleanup_all;
808
c568d683 809 error = break_lease(locks_inode(f), f->f_flags);
f3c7691e
BF
810 if (error)
811 goto cleanup_all;
812
ea73ea72
AV
813 /* normally all 3 are set; ->open() can clear them if needed */
814 f->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
72c2d531 815 if (!open)
834f2a4a
TM
816 open = f->f_op->open;
817 if (open) {
818 error = open(inode, f);
1da177e4
LT
819 if (error)
820 goto cleanup_all;
821 }
f5d11409 822 f->f_mode |= FMODE_OPENED;
890275b5
MZ
823 if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
824 i_readcount_inc(inode);
293bc982 825 if ((f->f_mode & FMODE_READ) &&
84363182 826 likely(f->f_op->read || f->f_op->read_iter))
7f7f25e8 827 f->f_mode |= FMODE_CAN_READ;
293bc982 828 if ((f->f_mode & FMODE_WRITE) &&
84363182 829 likely(f->f_op->write || f->f_op->write_iter))
7f7f25e8 830 f->f_mode |= FMODE_CAN_WRITE;
834f2a4a 831
c75b1d94 832 f->f_write_hint = WRITE_LIFE_NOT_SET;
1da177e4
LT
833 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
834
835 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
af04fadc 836
69527c55
AV
837 /* NB: we're sure to have correct a_ops only after f_op->open */
838 if (f->f_flags & O_DIRECT) {
839 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
840 return -EINVAL;
841 }
09d91cda
SL
842
843 /*
844 * XXX: Huge page cache doesn't support writing yet. Drop all page
845 * cache for this file before processing writes.
846 */
847 if ((f->f_mode & FMODE_WRITE) && filemap_nr_thps(inode->i_mapping))
848 truncate_pagecache(inode, 0);
849
96b7e579 850 return 0;
1da177e4
LT
851
852cleanup_all:
6b4e8085
AV
853 if (WARN_ON_ONCE(error > 0))
854 error = -EINVAL;
1da177e4 855 fops_put(f->f_op);
83f936c7 856 if (f->f_mode & FMODE_WRITER) {
1da177e4 857 put_write_access(inode);
83f936c7 858 __mnt_drop_write(f->f_path.mnt);
4a3fd211 859 }
1da177e4 860cleanup_file:
02e5180d
AV
861 path_put(&f->f_path);
862 f->f_path.mnt = NULL;
863 f->f_path.dentry = NULL;
dd37978c 864 f->f_inode = NULL;
96b7e579 865 return error;
1da177e4
LT
866}
867
d18e9008
MS
868/**
869 * finish_open - finish opening a file
0854d450 870 * @file: file pointer
d18e9008
MS
871 * @dentry: pointer to dentry
872 * @open: open callback
0854d450 873 * @opened: state of open
d18e9008
MS
874 *
875 * This can be used to finish opening a file passed to i_op->atomic_open().
876 *
877 * If the open callback is set to NULL, then the standard f_op->open()
878 * filesystem callback is substituted.
0854d450
MS
879 *
880 * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
881 * the return value of d_splice_alias(), then the caller needs to perform dput()
882 * on it after finish_open().
883 *
0854d450 884 * Returns zero on success or -errno if the open failed.
d18e9008 885 */
30d90494 886int finish_open(struct file *file, struct dentry *dentry,
be12af3e 887 int (*open)(struct inode *, struct file *))
d18e9008 888{
aad888f8 889 BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
d18e9008 890
b5bcdda3 891 file->f_path.dentry = dentry;
aad888f8 892 return do_dentry_open(file, d_backing_inode(dentry), open);
d18e9008
MS
893}
894EXPORT_SYMBOL(finish_open);
895
896/**
897 * finish_no_open - finish ->atomic_open() without opening the file
898 *
0854d450 899 * @file: file pointer
d18e9008
MS
900 * @dentry: dentry or NULL (as returned from ->lookup())
901 *
902 * This can be used to set the result of a successful lookup in ->atomic_open().
0854d450
MS
903 *
904 * NB: unlike finish_open() this function does consume the dentry reference and
905 * the caller need not dput() it.
906 *
64e1ac4d 907 * Returns "0" which must be the return value of ->atomic_open() after having
0854d450 908 * called this function.
d18e9008 909 */
e45198a6 910int finish_no_open(struct file *file, struct dentry *dentry)
d18e9008 911{
30d90494 912 file->f_path.dentry = dentry;
64e1ac4d 913 return 0;
d18e9008
MS
914}
915EXPORT_SYMBOL(finish_no_open);
916
9bf39ab2
MS
917char *file_path(struct file *filp, char *buf, int buflen)
918{
919 return d_path(&filp->f_path, buf, buflen);
920}
921EXPORT_SYMBOL(file_path);
922
4bacc9c9
DH
923/**
924 * vfs_open - open the file at the given path
925 * @path: path to open
926 * @file: newly allocated file with f_flag initialized
927 * @cred: credentials to use
928 */
ae2bb293 929int vfs_open(const struct path *path, struct file *file)
4bacc9c9 930{
54d5ca87 931 file->f_path = *path;
a6518f73 932 return do_dentry_open(file, d_backing_inode(path->dentry), NULL);
4bacc9c9
DH
933}
934
765927b2 935struct file *dentry_open(const struct path *path, int flags,
745ca247 936 const struct cred *cred)
a1a5b3d9
PS
937{
938 int error;
939 struct file *f;
940
e0e81739
DH
941 validate_creds(cred);
942
c212f9aa 943 /* We must always pass in a valid mount pointer. */
765927b2 944 BUG_ON(!path->mnt);
322ee5b3 945
ea73ea72 946 f = alloc_empty_file(flags, cred);
af04fadc 947 if (!IS_ERR(f)) {
ae2bb293 948 error = vfs_open(path, f);
4d27f326
AV
949 if (error) {
950 fput(f);
af04fadc
AV
951 f = ERR_PTR(error);
952 }
2a027e7a
AV
953 }
954 return f;
a1a5b3d9 955}
1da177e4
LT
956EXPORT_SYMBOL(dentry_open);
957
2abc77af
AV
958struct file *open_with_fake_path(const struct path *path, int flags,
959 struct inode *inode, const struct cred *cred)
960{
d3b1084d 961 struct file *f = alloc_empty_file_noaccount(flags, cred);
2abc77af
AV
962 if (!IS_ERR(f)) {
963 int error;
964
965 f->f_path = *path;
966 error = do_dentry_open(f, inode, NULL);
967 if (error) {
968 fput(f);
969 f = ERR_PTR(error);
970 }
971 }
972 return f;
973}
974EXPORT_SYMBOL(open_with_fake_path);
975
fddb5d43
AS
976#define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE))
977#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC)
978
35cb6d54 979inline struct open_how build_open_how(int flags, umode_t mode)
fddb5d43
AS
980{
981 struct open_how how = {
982 .flags = flags & VALID_OPEN_FLAGS,
983 .mode = mode & S_IALLUGO,
984 };
985
986 /* O_PATH beats everything else. */
987 if (how.flags & O_PATH)
988 how.flags &= O_PATH_FLAGS;
989 /* Modes should only be set for create-like flags. */
990 if (!WILL_CREATE(how.flags))
991 how.mode = 0;
992 return how;
993}
994
35cb6d54 995inline int build_open_flags(const struct open_how *how, struct open_flags *op)
47c805dc 996{
e7a2cf4a
CB
997 u64 flags = how->flags;
998 u64 strip = FMODE_NONOTIFY | O_CLOEXEC;
47c805dc 999 int lookup_flags = 0;
62fb4a15 1000 int acc_mode = ACC_MODE(flags);
47c805dc 1001
e7a2cf4a
CB
1002 BUILD_BUG_ON_MSG(upper_32_bits(VALID_OPEN_FLAGS),
1003 "struct open_flags doesn't yet handle flags > 32 bits");
1004
1005 /*
1006 * Strip flags that either shouldn't be set by userspace like
1007 * FMODE_NONOTIFY or that aren't relevant in determining struct
1008 * open_flags like O_CLOEXEC.
1009 */
1010 flags &= ~strip;
fddb5d43 1011
629e014b 1012 /*
fddb5d43
AS
1013 * Older syscalls implicitly clear all of the invalid flags or argument
1014 * values before calling build_open_flags(), but openat2(2) checks all
1015 * of its arguments.
629e014b 1016 */
fddb5d43
AS
1017 if (flags & ~VALID_OPEN_FLAGS)
1018 return -EINVAL;
1019 if (how->resolve & ~VALID_RESOLVE_FLAGS)
1020 return -EINVAL;
629e014b 1021
398840f8
AS
1022 /* Scoping flags are mutually exclusive. */
1023 if ((how->resolve & RESOLVE_BENEATH) && (how->resolve & RESOLVE_IN_ROOT))
1024 return -EINVAL;
1025
fddb5d43
AS
1026 /* Deal with the mode. */
1027 if (WILL_CREATE(flags)) {
1028 if (how->mode & ~S_IALLUGO)
1029 return -EINVAL;
1030 op->mode = how->mode | S_IFREG;
1031 } else {
1032 if (how->mode != 0)
1033 return -EINVAL;
e68726ff 1034 op->mode = 0;
fddb5d43 1035 }
47c805dc
AV
1036
1037 /*
fddb5d43
AS
1038 * In order to ensure programs get explicit errors when trying to use
1039 * O_TMPFILE on old kernels, O_TMPFILE is implemented such that it
1040 * looks like (O_DIRECTORY|O_RDWR & ~O_CREAT) to old kernels. But we
1041 * have to require userspace to explicitly set it.
47c805dc 1042 */
bb458c64
AV
1043 if (flags & __O_TMPFILE) {
1044 if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
60545d0d 1045 return -EINVAL;
ba57ea64
AV
1046 if (!(acc_mode & MAY_WRITE))
1047 return -EINVAL;
fddb5d43
AS
1048 }
1049 if (flags & O_PATH) {
1050 /* O_PATH only permits certain other flags to be set. */
1051 if (flags & ~O_PATH_FLAGS)
1052 return -EINVAL;
1abf0c71 1053 acc_mode = 0;
1abf0c71 1054 }
47c805dc 1055
fddb5d43
AS
1056 /*
1057 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
1058 * check for O_DSYNC if the need any syncing at all we enforce it's
1059 * always set instead of having to deal with possibly weird behaviour
1060 * for malicious applications setting only __O_SYNC.
1061 */
1062 if (flags & __O_SYNC)
1063 flags |= O_DSYNC;
1064
1abf0c71 1065 op->open_flag = flags;
47c805dc
AV
1066
1067 /* O_TRUNC implies we need access checks for write permissions */
1068 if (flags & O_TRUNC)
1069 acc_mode |= MAY_WRITE;
1070
1071 /* Allow the LSM permission hook to distinguish append
1072 access from general write access. */
1073 if (flags & O_APPEND)
1074 acc_mode |= MAY_APPEND;
1075
1076 op->acc_mode = acc_mode;
1077
1abf0c71
AV
1078 op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
1079
47c805dc
AV
1080 if (flags & O_CREAT) {
1081 op->intent |= LOOKUP_CREATE;
31d1726d 1082 if (flags & O_EXCL) {
47c805dc 1083 op->intent |= LOOKUP_EXCL;
31d1726d
AV
1084 flags |= O_NOFOLLOW;
1085 }
47c805dc
AV
1086 }
1087
1088 if (flags & O_DIRECTORY)
1089 lookup_flags |= LOOKUP_DIRECTORY;
1090 if (!(flags & O_NOFOLLOW))
1091 lookup_flags |= LOOKUP_FOLLOW;
fddb5d43
AS
1092
1093 if (how->resolve & RESOLVE_NO_XDEV)
1094 lookup_flags |= LOOKUP_NO_XDEV;
1095 if (how->resolve & RESOLVE_NO_MAGICLINKS)
1096 lookup_flags |= LOOKUP_NO_MAGICLINKS;
1097 if (how->resolve & RESOLVE_NO_SYMLINKS)
1098 lookup_flags |= LOOKUP_NO_SYMLINKS;
1099 if (how->resolve & RESOLVE_BENEATH)
1100 lookup_flags |= LOOKUP_BENEATH;
1101 if (how->resolve & RESOLVE_IN_ROOT)
1102 lookup_flags |= LOOKUP_IN_ROOT;
1103
f9652e10
AV
1104 op->lookup_flags = lookup_flags;
1105 return 0;
47c805dc
AV
1106}
1107
669abf4e
JL
1108/**
1109 * file_open_name - open file and return file pointer
1110 *
1111 * @name: struct filename containing path to open
1112 * @flags: open flags as per the open(2) second argument
1113 * @mode: mode for the new file if O_CREAT is set, else ignored
1114 *
1115 * This is the helper to open a file from kernelspace if you really
1116 * have to. But in generally you should not do this, so please move
1117 * along, nothing to see here..
1118 */
1119struct file *file_open_name(struct filename *name, int flags, umode_t mode)
1120{
1121 struct open_flags op;
fddb5d43
AS
1122 struct open_how how = build_open_how(flags, mode);
1123 int err = build_open_flags(&how, &op);
1124 if (err)
1125 return ERR_PTR(err);
1126 return do_filp_open(AT_FDCWD, name, &op);
669abf4e
JL
1127}
1128
47c805dc
AV
1129/**
1130 * filp_open - open file and return file pointer
1131 *
1132 * @filename: path to open
1133 * @flags: open flags as per the open(2) second argument
1134 * @mode: mode for the new file if O_CREAT is set, else ignored
1135 *
1136 * This is the helper to open a file from kernelspace if you really
1137 * have to. But in generally you should not do this, so please move
1138 * along, nothing to see here..
1139 */
a218d0fd 1140struct file *filp_open(const char *filename, int flags, umode_t mode)
47c805dc 1141{
51689104
PM
1142 struct filename *name = getname_kernel(filename);
1143 struct file *file = ERR_CAST(name);
1144
1145 if (!IS_ERR(name)) {
1146 file = file_open_name(name, flags, mode);
1147 putname(name);
1148 }
1149 return file;
47c805dc
AV
1150}
1151EXPORT_SYMBOL(filp_open);
1152
73d049a4 1153struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
378c6520 1154 const char *filename, int flags, umode_t mode)
73d049a4
AV
1155{
1156 struct open_flags op;
fddb5d43
AS
1157 struct open_how how = build_open_how(flags, mode);
1158 int err = build_open_flags(&how, &op);
f9652e10
AV
1159 if (err)
1160 return ERR_PTR(err);
f9652e10 1161 return do_file_open_root(dentry, mnt, filename, &op);
73d049a4
AV
1162}
1163EXPORT_SYMBOL(file_open_root);
1164
fddb5d43
AS
1165static long do_sys_openat2(int dfd, const char __user *filename,
1166 struct open_how *how)
1da177e4 1167{
47c805dc 1168 struct open_flags op;
fddb5d43 1169 int fd = build_open_flags(how, &op);
f9652e10
AV
1170 struct filename *tmp;
1171
1172 if (fd)
1173 return fd;
1174
1175 tmp = getname(filename);
1176 if (IS_ERR(tmp))
1177 return PTR_ERR(tmp);
1178
fddb5d43 1179 fd = get_unused_fd_flags(how->flags);
f9652e10
AV
1180 if (fd >= 0) {
1181 struct file *f = do_filp_open(dfd, tmp, &op);
1182 if (IS_ERR(f)) {
1183 put_unused_fd(fd);
1184 fd = PTR_ERR(f);
1185 } else {
1186 fsnotify_open(f);
1187 fd_install(fd, f);
1da177e4 1188 }
1da177e4 1189 }
f9652e10 1190 putname(tmp);
1da177e4 1191 return fd;
1da177e4 1192}
e922efc3 1193
fddb5d43 1194long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
e922efc3 1195{
fddb5d43
AS
1196 struct open_how how = build_open_how(flags, mode);
1197 return do_sys_openat2(dfd, filename, &how);
1198}
e922efc3 1199
fddb5d43
AS
1200
1201SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1202{
166e07c3
CH
1203 if (force_o_largefile())
1204 flags |= O_LARGEFILE;
1205 return do_sys_open(AT_FDCWD, filename, flags, mode);
e922efc3 1206}
1da177e4 1207
6559eed8 1208SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
a218d0fd 1209 umode_t, mode)
5590ff0d
UD
1210{
1211 if (force_o_largefile())
1212 flags |= O_LARGEFILE;
2cf09666 1213 return do_sys_open(dfd, filename, flags, mode);
5590ff0d 1214}
5590ff0d 1215
fddb5d43
AS
1216SYSCALL_DEFINE4(openat2, int, dfd, const char __user *, filename,
1217 struct open_how __user *, how, size_t, usize)
1218{
1219 int err;
1220 struct open_how tmp;
1221
1222 BUILD_BUG_ON(sizeof(struct open_how) < OPEN_HOW_SIZE_VER0);
1223 BUILD_BUG_ON(sizeof(struct open_how) != OPEN_HOW_SIZE_LATEST);
1224
1225 if (unlikely(usize < OPEN_HOW_SIZE_VER0))
1226 return -EINVAL;
1227
1228 err = copy_struct_from_user(&tmp, sizeof(tmp), how, usize);
1229 if (err)
1230 return err;
1231
1232 /* O_LARGEFILE is only allowed for non-O_PATH. */
1233 if (!(tmp.flags & O_PATH) && force_o_largefile())
1234 tmp.flags |= O_LARGEFILE;
1235
1236 return do_sys_openat2(dfd, filename, &tmp);
1237}
1238
e35d49f6
AV
1239#ifdef CONFIG_COMPAT
1240/*
1241 * Exactly like sys_open(), except that it doesn't set the
1242 * O_LARGEFILE flag.
1243 */
1244COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1245{
1246 return do_sys_open(AT_FDCWD, filename, flags, mode);
1247}
1248
1249/*
1250 * Exactly like sys_openat(), except that it doesn't set the
1251 * O_LARGEFILE flag.
1252 */
1253COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
1254{
1255 return do_sys_open(dfd, filename, flags, mode);
1256}
1257#endif
1258
1da177e4
LT
1259#ifndef __alpha__
1260
1261/*
1262 * For backward compatibility? Maybe this should be moved
1263 * into arch/i386 instead?
1264 */
a218d0fd 1265SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
1da177e4 1266{
166e07c3 1267 int flags = O_CREAT | O_WRONLY | O_TRUNC;
1da177e4 1268
166e07c3
CH
1269 if (force_o_largefile())
1270 flags |= O_LARGEFILE;
1271 return do_sys_open(AT_FDCWD, pathname, flags, mode);
1272}
1da177e4
LT
1273#endif
1274
1275/*
1276 * "id" is the POSIX thread ID. We use the
1277 * files pointer for this..
1278 */
1279int filp_close(struct file *filp, fl_owner_t id)
1280{
45778ca8 1281 int retval = 0;
1da177e4
LT
1282
1283 if (!file_count(filp)) {
1284 printk(KERN_ERR "VFS: Close: file count is 0\n");
45778ca8 1285 return 0;
1da177e4
LT
1286 }
1287
72c2d531 1288 if (filp->f_op->flush)
75e1fcc0 1289 retval = filp->f_op->flush(filp, id);
1da177e4 1290
1abf0c71
AV
1291 if (likely(!(filp->f_mode & FMODE_PATH))) {
1292 dnotify_flush(filp, id);
1293 locks_remove_posix(filp, id);
1294 }
1da177e4
LT
1295 fput(filp);
1296 return retval;
1297}
1298
1299EXPORT_SYMBOL(filp_close);
1300
1301/*
1302 * Careful here! We test whether the file pointer is NULL before
1303 * releasing the fd. This ensures that one clone task can't release
1304 * an fd while another clone is opening it.
1305 */
ca013e94 1306SYSCALL_DEFINE1(close, unsigned int, fd)
1da177e4 1307{
8760c909 1308 int retval = close_fd(fd);
ee731f4f
EP
1309
1310 /* can't restart close syscall because file table entry was cleared */
1311 if (unlikely(retval == -ERESTARTSYS ||
1312 retval == -ERESTARTNOINTR ||
1313 retval == -ERESTARTNOHAND ||
1314 retval == -ERESTART_RESTARTBLOCK))
1315 retval = -EINTR;
1316
1317 return retval;
1da177e4 1318}
1da177e4 1319
278a5fba
CB
1320/**
1321 * close_range() - Close all file descriptors in a given range.
1322 *
1323 * @fd: starting file descriptor to close
1324 * @max_fd: last file descriptor to close
1325 * @flags: reserved for future extensions
1326 *
1327 * This closes a range of file descriptors. All file descriptors
1328 * from @fd up to and including @max_fd are closed.
1329 * Currently, errors to close a given file descriptor are ignored.
1330 */
1331SYSCALL_DEFINE3(close_range, unsigned int, fd, unsigned int, max_fd,
1332 unsigned int, flags)
1333{
60997c3d 1334 return __close_range(fd, max_fd, flags);
278a5fba
CB
1335}
1336
1da177e4
LT
1337/*
1338 * This routine simulates a hangup on the tty, to arrange that users
1339 * are given clean terminals at login time.
1340 */
ca013e94 1341SYSCALL_DEFINE0(vhangup)
1da177e4
LT
1342{
1343 if (capable(CAP_SYS_TTY_CONFIG)) {
2cb5998b 1344 tty_vhangup_self();
1da177e4
LT
1345 return 0;
1346 }
1347 return -EPERM;
1348}
1349
1350/*
1351 * Called when an inode is about to be open.
1352 * We use this to disallow opening large files on 32bit systems if
1353 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1354 * on this flag in sys_open.
1355 */
1356int generic_file_open(struct inode * inode, struct file * filp)
1357{
1358 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
a9c62a18 1359 return -EOVERFLOW;
1da177e4
LT
1360 return 0;
1361}
1362
1363EXPORT_SYMBOL(generic_file_open);
1364
1365/*
1366 * This is used by subsystems that don't want seekable
06b1e104
DT
1367 * file descriptors. The function is not supposed to ever fail, the only
1368 * reason it returns an 'int' and not 'void' is so that it can be plugged
1369 * directly into file_operations structure.
1da177e4
LT
1370 */
1371int nonseekable_open(struct inode *inode, struct file *filp)
1372{
1373 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1374 return 0;
1375}
1376
1377EXPORT_SYMBOL(nonseekable_open);
10dce8af
KS
1378
1379/*
1380 * stream_open is used by subsystems that want stream-like file descriptors.
1381 * Such file descriptors are not seekable and don't have notion of position
438ab720
KS
1382 * (file.f_pos is always 0 and ppos passed to .read()/.write() is always NULL).
1383 * Contrary to file descriptors of other regular files, .read() and .write()
1384 * can run simultaneously.
10dce8af
KS
1385 *
1386 * stream_open never fails and is marked to return int so that it could be
1387 * directly used as file_operations.open .
1388 */
1389int stream_open(struct inode *inode, struct file *filp)
1390{
2be7d348 1391 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE | FMODE_ATOMIC_POS);
10dce8af
KS
1392 filp->f_mode |= FMODE_STREAM;
1393 return 0;
1394}
1395
1396EXPORT_SYMBOL(stream_open);