open.c 33 KB

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