open.c 34 KB

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