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