open.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /*
  2. * linux/fs/open.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/string.h>
  7. #include <linux/mm.h>
  8. #include <linux/file.h>
  9. #include <linux/fdtable.h>
  10. #include <linux/fsnotify.h>
  11. #include <linux/module.h>
  12. #include <linux/tty.h>
  13. #include <linux/namei.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/capability.h>
  16. #include <linux/securebits.h>
  17. #include <linux/security.h>
  18. #include <linux/mount.h>
  19. #include <linux/fcntl.h>
  20. #include <linux/slab.h>
  21. #include <asm/uaccess.h>
  22. #include <linux/fs.h>
  23. #include <linux/personality.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/audit.h>
  28. #include <linux/falloc.h>
  29. #include <linux/fs_struct.h>
  30. #include <linux/ima.h>
  31. #include <linux/dnotify.h>
  32. #include <linux/compat.h>
  33. #include "internal.h"
  34. int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
  35. struct file *filp)
  36. {
  37. int ret;
  38. struct iattr newattrs;
  39. /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
  40. if (length < 0)
  41. return -EINVAL;
  42. newattrs.ia_size = length;
  43. newattrs.ia_valid = ATTR_SIZE | time_attrs;
  44. if (filp) {
  45. newattrs.ia_file = filp;
  46. newattrs.ia_valid |= ATTR_FILE;
  47. }
  48. /* Remove suid/sgid on truncate too */
  49. ret = should_remove_suid(dentry);
  50. if (ret)
  51. newattrs.ia_valid |= ret | ATTR_FORCE;
  52. mutex_lock(&dentry->d_inode->i_mutex);
  53. /* Note any delegations or leases have already been broken: */
  54. ret = notify_change(dentry, &newattrs, NULL);
  55. mutex_unlock(&dentry->d_inode->i_mutex);
  56. return ret;
  57. }
  58. long vfs_truncate(struct path *path, loff_t length)
  59. {
  60. struct inode *inode;
  61. long error;
  62. inode = path->dentry->d_inode;
  63. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  64. if (S_ISDIR(inode->i_mode))
  65. return -EISDIR;
  66. if (!S_ISREG(inode->i_mode))
  67. return -EINVAL;
  68. error = mnt_want_write(path->mnt);
  69. if (error)
  70. goto out;
  71. error = inode_permission(inode, MAY_WRITE);
  72. if (error)
  73. goto mnt_drop_write_and_out;
  74. error = -EPERM;
  75. if (IS_APPEND(inode))
  76. goto mnt_drop_write_and_out;
  77. error = get_write_access(inode);
  78. if (error)
  79. goto mnt_drop_write_and_out;
  80. /*
  81. * Make sure that there are no leases. get_write_access() protects
  82. * against the truncate racing with a lease-granting setlease().
  83. */
  84. error = break_lease(inode, O_WRONLY);
  85. if (error)
  86. goto put_write_and_out;
  87. error = locks_verify_truncate(inode, NULL, length);
  88. if (!error)
  89. error = security_path_truncate(path);
  90. if (!error)
  91. error = do_truncate(path->dentry, length, 0, NULL);
  92. put_write_and_out:
  93. put_write_access(inode);
  94. mnt_drop_write_and_out:
  95. mnt_drop_write(path->mnt);
  96. out:
  97. return error;
  98. }
  99. EXPORT_SYMBOL_GPL(vfs_truncate);
  100. static long do_sys_truncate(const char __user *pathname, loff_t length)
  101. {
  102. unsigned int lookup_flags = LOOKUP_FOLLOW;
  103. struct path path;
  104. int error;
  105. if (length < 0) /* sorry, but loff_t says... */
  106. return -EINVAL;
  107. retry:
  108. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  109. if (!error) {
  110. error = vfs_truncate(&path, length);
  111. path_put(&path);
  112. }
  113. if (retry_estale(error, lookup_flags)) {
  114. lookup_flags |= LOOKUP_REVAL;
  115. goto retry;
  116. }
  117. return error;
  118. }
  119. SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
  120. {
  121. return do_sys_truncate(path, length);
  122. }
  123. #ifdef CONFIG_COMPAT
  124. COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
  125. {
  126. return do_sys_truncate(path, length);
  127. }
  128. #endif
  129. static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  130. {
  131. struct inode *inode;
  132. struct dentry *dentry;
  133. struct fd f;
  134. int error;
  135. error = -EINVAL;
  136. if (length < 0)
  137. goto out;
  138. error = -EBADF;
  139. f = fdget(fd);
  140. if (!f.file)
  141. goto out;
  142. /* explicitly opened as large or we are on 64-bit box */
  143. if (f.file->f_flags & O_LARGEFILE)
  144. small = 0;
  145. dentry = f.file->f_path.dentry;
  146. inode = dentry->d_inode;
  147. error = -EINVAL;
  148. if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
  149. goto out_putf;
  150. error = -EINVAL;
  151. /* Cannot ftruncate over 2^31 bytes without large file support */
  152. if (small && length > MAX_NON_LFS)
  153. goto out_putf;
  154. error = -EPERM;
  155. if (IS_APPEND(inode))
  156. goto out_putf;
  157. sb_start_write(inode->i_sb);
  158. error = locks_verify_truncate(inode, f.file, length);
  159. if (!error)
  160. error = security_path_truncate(&f.file->f_path);
  161. if (!error)
  162. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
  163. sb_end_write(inode->i_sb);
  164. out_putf:
  165. fdput(f);
  166. out:
  167. return error;
  168. }
  169. SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
  170. {
  171. return do_sys_ftruncate(fd, length, 1);
  172. }
  173. #ifdef CONFIG_COMPAT
  174. COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
  175. {
  176. return do_sys_ftruncate(fd, length, 1);
  177. }
  178. #endif
  179. /* LFS versions of truncate are only needed on 32 bit machines */
  180. #if BITS_PER_LONG == 32
  181. SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
  182. {
  183. return do_sys_truncate(path, length);
  184. }
  185. SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
  186. {
  187. return do_sys_ftruncate(fd, length, 0);
  188. }
  189. #endif /* BITS_PER_LONG == 32 */
  190. int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  191. {
  192. struct inode *inode = file_inode(file);
  193. long ret;
  194. if (offset < 0 || len <= 0)
  195. return -EINVAL;
  196. /* Return error if mode is not supported */
  197. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
  198. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE))
  199. return -EOPNOTSUPP;
  200. /* Punch hole and zero range are mutually exclusive */
  201. if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
  202. (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
  203. return -EOPNOTSUPP;
  204. /* Punch hole must have keep size set */
  205. if ((mode & FALLOC_FL_PUNCH_HOLE) &&
  206. !(mode & FALLOC_FL_KEEP_SIZE))
  207. return -EOPNOTSUPP;
  208. /* Collapse range should only be used exclusively. */
  209. if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
  210. (mode & ~FALLOC_FL_COLLAPSE_RANGE))
  211. return -EINVAL;
  212. if (!(file->f_mode & FMODE_WRITE))
  213. return -EBADF;
  214. /*
  215. * We can only allow pure fallocate on append only files
  216. */
  217. if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
  218. return -EPERM;
  219. if (IS_IMMUTABLE(inode))
  220. return -EPERM;
  221. /*
  222. * We cannot allow any fallocate operation on an active swapfile
  223. */
  224. if (IS_SWAPFILE(inode))
  225. return -ETXTBSY;
  226. /*
  227. * Revalidate the write permissions, in case security policy has
  228. * changed since the files were opened.
  229. */
  230. ret = security_file_permission(file, MAY_WRITE);
  231. if (ret)
  232. return ret;
  233. if (S_ISFIFO(inode->i_mode))
  234. return -ESPIPE;
  235. /*
  236. * Let individual file system decide if it supports preallocation
  237. * for directories or not.
  238. */
  239. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  240. return -ENODEV;
  241. /* Check for wrap through zero too */
  242. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  243. return -EFBIG;
  244. if (!file->f_op->fallocate)
  245. return -EOPNOTSUPP;
  246. sb_start_write(inode->i_sb);
  247. ret = file->f_op->fallocate(file, mode, offset, len);
  248. sb_end_write(inode->i_sb);
  249. return ret;
  250. }
  251. SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
  252. {
  253. struct fd f = fdget(fd);
  254. int error = -EBADF;
  255. if (f.file) {
  256. error = do_fallocate(f.file, mode, offset, len);
  257. fdput(f);
  258. }
  259. return error;
  260. }
  261. /*
  262. * access() needs to use the real uid/gid, not the effective uid/gid.
  263. * We do this by temporarily clearing all FS-related capabilities and
  264. * switching the fsuid/fsgid around to the real ones.
  265. */
  266. SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
  267. {
  268. const struct cred *old_cred;
  269. struct cred *override_cred;
  270. struct path path;
  271. struct inode *inode;
  272. int res;
  273. unsigned int lookup_flags = LOOKUP_FOLLOW;
  274. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  275. return -EINVAL;
  276. override_cred = prepare_creds();
  277. if (!override_cred)
  278. return -ENOMEM;
  279. override_cred->fsuid = override_cred->uid;
  280. override_cred->fsgid = override_cred->gid;
  281. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  282. /* Clear the capabilities if we switch to a non-root user */
  283. kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
  284. if (!uid_eq(override_cred->uid, root_uid))
  285. cap_clear(override_cred->cap_effective);
  286. else
  287. override_cred->cap_effective =
  288. override_cred->cap_permitted;
  289. }
  290. old_cred = override_creds(override_cred);
  291. retry:
  292. res = user_path_at(dfd, filename, lookup_flags, &path);
  293. if (res)
  294. goto out;
  295. inode = path.dentry->d_inode;
  296. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  297. /*
  298. * MAY_EXEC on regular files is denied if the fs is mounted
  299. * with the "noexec" flag.
  300. */
  301. res = -EACCES;
  302. if (path.mnt->mnt_flags & MNT_NOEXEC)
  303. goto out_path_release;
  304. }
  305. res = inode_permission(inode, mode | MAY_ACCESS);
  306. /* SuS v2 requires we report a read only fs too */
  307. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  308. goto out_path_release;
  309. /*
  310. * This is a rare case where using __mnt_is_readonly()
  311. * is OK without a mnt_want/drop_write() pair. Since
  312. * no actual write to the fs is performed here, we do
  313. * not need to telegraph to that to anyone.
  314. *
  315. * By doing this, we accept that this access is
  316. * inherently racy and know that the fs may change
  317. * state before we even see this result.
  318. */
  319. if (__mnt_is_readonly(path.mnt))
  320. res = -EROFS;
  321. out_path_release:
  322. path_put(&path);
  323. if (retry_estale(res, lookup_flags)) {
  324. lookup_flags |= LOOKUP_REVAL;
  325. goto retry;
  326. }
  327. out:
  328. revert_creds(old_cred);
  329. put_cred(override_cred);
  330. return res;
  331. }
  332. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  333. {
  334. return sys_faccessat(AT_FDCWD, filename, mode);
  335. }
  336. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  337. {
  338. struct path path;
  339. int error;
  340. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  341. retry:
  342. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  343. if (error)
  344. goto out;
  345. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  346. if (error)
  347. goto dput_and_out;
  348. set_fs_pwd(current->fs, &path);
  349. dput_and_out:
  350. path_put(&path);
  351. if (retry_estale(error, lookup_flags)) {
  352. lookup_flags |= LOOKUP_REVAL;
  353. goto retry;
  354. }
  355. out:
  356. return error;
  357. }
  358. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  359. {
  360. struct fd f = fdget_raw(fd);
  361. struct inode *inode;
  362. int error = -EBADF;
  363. error = -EBADF;
  364. if (!f.file)
  365. goto out;
  366. inode = file_inode(f.file);
  367. error = -ENOTDIR;
  368. if (!S_ISDIR(inode->i_mode))
  369. goto out_putf;
  370. error = inode_permission(inode, MAY_EXEC | MAY_CHDIR);
  371. if (!error)
  372. set_fs_pwd(current->fs, &f.file->f_path);
  373. out_putf:
  374. fdput(f);
  375. out:
  376. return error;
  377. }
  378. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  379. {
  380. struct path path;
  381. int error;
  382. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  383. retry:
  384. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  385. if (error)
  386. goto out;
  387. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  388. if (error)
  389. goto dput_and_out;
  390. error = -EPERM;
  391. if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
  392. goto dput_and_out;
  393. error = security_path_chroot(&path);
  394. if (error)
  395. goto dput_and_out;
  396. set_fs_root(current->fs, &path);
  397. error = 0;
  398. dput_and_out:
  399. path_put(&path);
  400. if (retry_estale(error, lookup_flags)) {
  401. lookup_flags |= LOOKUP_REVAL;
  402. goto retry;
  403. }
  404. out:
  405. return error;
  406. }
  407. static int chmod_common(struct path *path, umode_t mode)
  408. {
  409. struct inode *inode = path->dentry->d_inode;
  410. struct inode *delegated_inode = NULL;
  411. struct iattr newattrs;
  412. int error;
  413. error = mnt_want_write(path->mnt);
  414. if (error)
  415. return error;
  416. retry_deleg:
  417. mutex_lock(&inode->i_mutex);
  418. error = security_path_chmod(path, mode);
  419. if (error)
  420. goto out_unlock;
  421. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  422. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  423. error = notify_change(path->dentry, &newattrs, &delegated_inode);
  424. out_unlock:
  425. mutex_unlock(&inode->i_mutex);
  426. if (delegated_inode) {
  427. error = break_deleg_wait(&delegated_inode);
  428. if (!error)
  429. goto retry_deleg;
  430. }
  431. mnt_drop_write(path->mnt);
  432. return error;
  433. }
  434. SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
  435. {
  436. struct fd f = fdget(fd);
  437. int err = -EBADF;
  438. if (f.file) {
  439. audit_inode(NULL, f.file->f_path.dentry, 0);
  440. err = chmod_common(&f.file->f_path, mode);
  441. fdput(f);
  442. }
  443. return err;
  444. }
  445. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
  446. {
  447. struct path path;
  448. int error;
  449. unsigned int lookup_flags = LOOKUP_FOLLOW;
  450. retry:
  451. error = user_path_at(dfd, filename, lookup_flags, &path);
  452. if (!error) {
  453. error = chmod_common(&path, mode);
  454. path_put(&path);
  455. if (retry_estale(error, lookup_flags)) {
  456. lookup_flags |= LOOKUP_REVAL;
  457. goto retry;
  458. }
  459. }
  460. return error;
  461. }
  462. SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
  463. {
  464. return sys_fchmodat(AT_FDCWD, filename, mode);
  465. }
  466. static int chown_common(struct path *path, uid_t user, gid_t group)
  467. {
  468. struct inode *inode = path->dentry->d_inode;
  469. struct inode *delegated_inode = NULL;
  470. int error;
  471. struct iattr newattrs;
  472. kuid_t uid;
  473. kgid_t gid;
  474. uid = make_kuid(current_user_ns(), user);
  475. gid = make_kgid(current_user_ns(), group);
  476. newattrs.ia_valid = ATTR_CTIME;
  477. if (user != (uid_t) -1) {
  478. if (!uid_valid(uid))
  479. return -EINVAL;
  480. newattrs.ia_valid |= ATTR_UID;
  481. newattrs.ia_uid = uid;
  482. }
  483. if (group != (gid_t) -1) {
  484. if (!gid_valid(gid))
  485. return -EINVAL;
  486. newattrs.ia_valid |= ATTR_GID;
  487. newattrs.ia_gid = gid;
  488. }
  489. if (!S_ISDIR(inode->i_mode))
  490. newattrs.ia_valid |=
  491. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  492. retry_deleg:
  493. mutex_lock(&inode->i_mutex);
  494. error = security_path_chown(path, uid, gid);
  495. if (!error)
  496. error = notify_change(path->dentry, &newattrs, &delegated_inode);
  497. mutex_unlock(&inode->i_mutex);
  498. if (delegated_inode) {
  499. error = break_deleg_wait(&delegated_inode);
  500. if (!error)
  501. goto retry_deleg;
  502. }
  503. return error;
  504. }
  505. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  506. gid_t, group, int, flag)
  507. {
  508. struct path path;
  509. int error = -EINVAL;
  510. int lookup_flags;
  511. if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  512. goto out;
  513. lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  514. if (flag & AT_EMPTY_PATH)
  515. lookup_flags |= LOOKUP_EMPTY;
  516. retry:
  517. error = user_path_at(dfd, filename, lookup_flags, &path);
  518. if (error)
  519. goto out;
  520. error = mnt_want_write(path.mnt);
  521. if (error)
  522. goto out_release;
  523. error = chown_common(&path, user, group);
  524. mnt_drop_write(path.mnt);
  525. out_release:
  526. path_put(&path);
  527. if (retry_estale(error, lookup_flags)) {
  528. lookup_flags |= LOOKUP_REVAL;
  529. goto retry;
  530. }
  531. out:
  532. return error;
  533. }
  534. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  535. {
  536. return sys_fchownat(AT_FDCWD, filename, user, group, 0);
  537. }
  538. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  539. {
  540. return sys_fchownat(AT_FDCWD, filename, user, group,
  541. AT_SYMLINK_NOFOLLOW);
  542. }
  543. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  544. {
  545. struct fd f = fdget(fd);
  546. int error = -EBADF;
  547. if (!f.file)
  548. goto out;
  549. error = mnt_want_write_file(f.file);
  550. if (error)
  551. goto out_fput;
  552. audit_inode(NULL, f.file->f_path.dentry, 0);
  553. error = chown_common(&f.file->f_path, user, group);
  554. mnt_drop_write_file(f.file);
  555. out_fput:
  556. fdput(f);
  557. out:
  558. return error;
  559. }
  560. int open_check_o_direct(struct file *f)
  561. {
  562. /* NB: we're sure to have correct a_ops only after f_op->open */
  563. if (f->f_flags & O_DIRECT) {
  564. if (!f->f_mapping->a_ops ||
  565. ((!f->f_mapping->a_ops->direct_IO) &&
  566. (!f->f_mapping->a_ops->get_xip_mem))) {
  567. return -EINVAL;
  568. }
  569. }
  570. return 0;
  571. }
  572. static int do_dentry_open(struct file *f,
  573. int (*open)(struct inode *, struct file *),
  574. const struct cred *cred)
  575. {
  576. static const struct file_operations empty_fops = {};
  577. struct inode *inode;
  578. int error;
  579. f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
  580. FMODE_PREAD | FMODE_PWRITE;
  581. path_get(&f->f_path);
  582. inode = f->f_inode = f->f_path.dentry->d_inode;
  583. f->f_mapping = inode->i_mapping;
  584. if (unlikely(f->f_flags & O_PATH)) {
  585. f->f_mode = FMODE_PATH;
  586. f->f_op = &empty_fops;
  587. return 0;
  588. }
  589. if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
  590. error = get_write_access(inode);
  591. if (unlikely(error))
  592. goto cleanup_file;
  593. error = __mnt_want_write(f->f_path.mnt);
  594. if (unlikely(error)) {
  595. put_write_access(inode);
  596. goto cleanup_file;
  597. }
  598. f->f_mode |= FMODE_WRITER;
  599. }
  600. /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
  601. if (S_ISREG(inode->i_mode))
  602. f->f_mode |= FMODE_ATOMIC_POS;
  603. f->f_op = fops_get(inode->i_fop);
  604. if (unlikely(WARN_ON(!f->f_op))) {
  605. error = -ENODEV;
  606. goto cleanup_all;
  607. }
  608. error = security_file_open(f, cred);
  609. if (error)
  610. goto cleanup_all;
  611. error = break_lease(inode, f->f_flags);
  612. if (error)
  613. goto cleanup_all;
  614. if (!open)
  615. open = f->f_op->open;
  616. if (open) {
  617. error = open(inode, f);
  618. if (error)
  619. goto cleanup_all;
  620. }
  621. if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  622. i_readcount_inc(inode);
  623. if ((f->f_mode & FMODE_READ) &&
  624. likely(f->f_op->read || f->f_op->aio_read || f->f_op->read_iter))
  625. f->f_mode |= FMODE_CAN_READ;
  626. if ((f->f_mode & FMODE_WRITE) &&
  627. likely(f->f_op->write || f->f_op->aio_write || f->f_op->write_iter))
  628. f->f_mode |= FMODE_CAN_WRITE;
  629. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  630. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  631. return 0;
  632. cleanup_all:
  633. fops_put(f->f_op);
  634. if (f->f_mode & FMODE_WRITER) {
  635. put_write_access(inode);
  636. __mnt_drop_write(f->f_path.mnt);
  637. }
  638. cleanup_file:
  639. path_put(&f->f_path);
  640. f->f_path.mnt = NULL;
  641. f->f_path.dentry = NULL;
  642. f->f_inode = NULL;
  643. return error;
  644. }
  645. /**
  646. * finish_open - finish opening a file
  647. * @file: file pointer
  648. * @dentry: pointer to dentry
  649. * @open: open callback
  650. * @opened: state of open
  651. *
  652. * This can be used to finish opening a file passed to i_op->atomic_open().
  653. *
  654. * If the open callback is set to NULL, then the standard f_op->open()
  655. * filesystem callback is substituted.
  656. *
  657. * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
  658. * the return value of d_splice_alias(), then the caller needs to perform dput()
  659. * on it after finish_open().
  660. *
  661. * On successful return @file is a fully instantiated open file. After this, if
  662. * an error occurs in ->atomic_open(), it needs to clean up with fput().
  663. *
  664. * Returns zero on success or -errno if the open failed.
  665. */
  666. int finish_open(struct file *file, struct dentry *dentry,
  667. int (*open)(struct inode *, struct file *),
  668. int *opened)
  669. {
  670. int error;
  671. BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  672. file->f_path.dentry = dentry;
  673. error = do_dentry_open(file, open, current_cred());
  674. if (!error)
  675. *opened |= FILE_OPENED;
  676. return error;
  677. }
  678. EXPORT_SYMBOL(finish_open);
  679. /**
  680. * finish_no_open - finish ->atomic_open() without opening the file
  681. *
  682. * @file: file pointer
  683. * @dentry: dentry or NULL (as returned from ->lookup())
  684. *
  685. * This can be used to set the result of a successful lookup in ->atomic_open().
  686. *
  687. * NB: unlike finish_open() this function does consume the dentry reference and
  688. * the caller need not dput() it.
  689. *
  690. * Returns "1" which must be the return value of ->atomic_open() after having
  691. * called this function.
  692. */
  693. int finish_no_open(struct file *file, struct dentry *dentry)
  694. {
  695. file->f_path.dentry = dentry;
  696. return 1;
  697. }
  698. EXPORT_SYMBOL(finish_no_open);
  699. struct file *dentry_open(const struct path *path, int flags,
  700. const struct cred *cred)
  701. {
  702. int error;
  703. struct file *f;
  704. validate_creds(cred);
  705. /* We must always pass in a valid mount pointer. */
  706. BUG_ON(!path->mnt);
  707. f = get_empty_filp();
  708. if (!IS_ERR(f)) {
  709. f->f_flags = flags;
  710. f->f_path = *path;
  711. error = do_dentry_open(f, NULL, cred);
  712. if (!error) {
  713. /* from now on we need fput() to dispose of f */
  714. error = open_check_o_direct(f);
  715. if (error) {
  716. fput(f);
  717. f = ERR_PTR(error);
  718. }
  719. } else {
  720. put_filp(f);
  721. f = ERR_PTR(error);
  722. }
  723. }
  724. return f;
  725. }
  726. EXPORT_SYMBOL(dentry_open);
  727. static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
  728. {
  729. int lookup_flags = 0;
  730. int acc_mode;
  731. if (flags & (O_CREAT | __O_TMPFILE))
  732. op->mode = (mode & S_IALLUGO) | S_IFREG;
  733. else
  734. op->mode = 0;
  735. /* Must never be set by userspace */
  736. flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
  737. /*
  738. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  739. * check for O_DSYNC if the need any syncing at all we enforce it's
  740. * always set instead of having to deal with possibly weird behaviour
  741. * for malicious applications setting only __O_SYNC.
  742. */
  743. if (flags & __O_SYNC)
  744. flags |= O_DSYNC;
  745. if (flags & __O_TMPFILE) {
  746. if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
  747. return -EINVAL;
  748. acc_mode = MAY_OPEN | ACC_MODE(flags);
  749. if (!(acc_mode & MAY_WRITE))
  750. return -EINVAL;
  751. } else if (flags & O_PATH) {
  752. /*
  753. * If we have O_PATH in the open flag. Then we
  754. * cannot have anything other than the below set of flags
  755. */
  756. flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
  757. acc_mode = 0;
  758. } else {
  759. acc_mode = MAY_OPEN | ACC_MODE(flags);
  760. }
  761. op->open_flag = flags;
  762. /* O_TRUNC implies we need access checks for write permissions */
  763. if (flags & O_TRUNC)
  764. acc_mode |= MAY_WRITE;
  765. /* Allow the LSM permission hook to distinguish append
  766. access from general write access. */
  767. if (flags & O_APPEND)
  768. acc_mode |= MAY_APPEND;
  769. op->acc_mode = acc_mode;
  770. op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
  771. if (flags & O_CREAT) {
  772. op->intent |= LOOKUP_CREATE;
  773. if (flags & O_EXCL)
  774. op->intent |= LOOKUP_EXCL;
  775. }
  776. if (flags & O_DIRECTORY)
  777. lookup_flags |= LOOKUP_DIRECTORY;
  778. if (!(flags & O_NOFOLLOW))
  779. lookup_flags |= LOOKUP_FOLLOW;
  780. op->lookup_flags = lookup_flags;
  781. return 0;
  782. }
  783. /**
  784. * file_open_name - open file and return file pointer
  785. *
  786. * @name: struct filename containing path to open
  787. * @flags: open flags as per the open(2) second argument
  788. * @mode: mode for the new file if O_CREAT is set, else ignored
  789. *
  790. * This is the helper to open a file from kernelspace if you really
  791. * have to. But in generally you should not do this, so please move
  792. * along, nothing to see here..
  793. */
  794. struct file *file_open_name(struct filename *name, int flags, umode_t mode)
  795. {
  796. struct open_flags op;
  797. int err = build_open_flags(flags, mode, &op);
  798. return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
  799. }
  800. /**
  801. * filp_open - open file and return file pointer
  802. *
  803. * @filename: path to open
  804. * @flags: open flags as per the open(2) second argument
  805. * @mode: mode for the new file if O_CREAT is set, else ignored
  806. *
  807. * This is the helper to open a file from kernelspace if you really
  808. * have to. But in generally you should not do this, so please move
  809. * along, nothing to see here..
  810. */
  811. struct file *filp_open(const char *filename, int flags, umode_t mode)
  812. {
  813. struct filename name = {.name = filename};
  814. return file_open_name(&name, flags, mode);
  815. }
  816. EXPORT_SYMBOL(filp_open);
  817. struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  818. const char *filename, int flags)
  819. {
  820. struct open_flags op;
  821. int err = build_open_flags(flags, 0, &op);
  822. if (err)
  823. return ERR_PTR(err);
  824. if (flags & O_CREAT)
  825. return ERR_PTR(-EINVAL);
  826. if (!filename && (flags & O_DIRECTORY))
  827. if (!dentry->d_inode->i_op->lookup)
  828. return ERR_PTR(-ENOTDIR);
  829. return do_file_open_root(dentry, mnt, filename, &op);
  830. }
  831. EXPORT_SYMBOL(file_open_root);
  832. long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
  833. {
  834. struct open_flags op;
  835. int fd = build_open_flags(flags, mode, &op);
  836. struct filename *tmp;
  837. if (fd)
  838. return fd;
  839. tmp = getname(filename);
  840. if (IS_ERR(tmp))
  841. return PTR_ERR(tmp);
  842. fd = get_unused_fd_flags(flags);
  843. if (fd >= 0) {
  844. struct file *f = do_filp_open(dfd, tmp, &op);
  845. if (IS_ERR(f)) {
  846. put_unused_fd(fd);
  847. fd = PTR_ERR(f);
  848. } else {
  849. fsnotify_open(f);
  850. fd_install(fd, f);
  851. }
  852. }
  853. putname(tmp);
  854. return fd;
  855. }
  856. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  857. {
  858. if (force_o_largefile())
  859. flags |= O_LARGEFILE;
  860. return do_sys_open(AT_FDCWD, filename, flags, mode);
  861. }
  862. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  863. umode_t, mode)
  864. {
  865. if (force_o_largefile())
  866. flags |= O_LARGEFILE;
  867. return do_sys_open(dfd, filename, flags, mode);
  868. }
  869. #ifndef __alpha__
  870. /*
  871. * For backward compatibility? Maybe this should be moved
  872. * into arch/i386 instead?
  873. */
  874. SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
  875. {
  876. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  877. }
  878. #endif
  879. /*
  880. * "id" is the POSIX thread ID. We use the
  881. * files pointer for this..
  882. */
  883. int filp_close(struct file *filp, fl_owner_t id)
  884. {
  885. int retval = 0;
  886. if (!file_count(filp)) {
  887. printk(KERN_ERR "VFS: Close: file count is 0\n");
  888. return 0;
  889. }
  890. if (filp->f_op->flush)
  891. retval = filp->f_op->flush(filp, id);
  892. if (likely(!(filp->f_mode & FMODE_PATH))) {
  893. dnotify_flush(filp, id);
  894. locks_remove_posix(filp, id);
  895. }
  896. fput(filp);
  897. return retval;
  898. }
  899. EXPORT_SYMBOL(filp_close);
  900. /*
  901. * Careful here! We test whether the file pointer is NULL before
  902. * releasing the fd. This ensures that one clone task can't release
  903. * an fd while another clone is opening it.
  904. */
  905. SYSCALL_DEFINE1(close, unsigned int, fd)
  906. {
  907. int retval = __close_fd(current->files, fd);
  908. /* can't restart close syscall because file table entry was cleared */
  909. if (unlikely(retval == -ERESTARTSYS ||
  910. retval == -ERESTARTNOINTR ||
  911. retval == -ERESTARTNOHAND ||
  912. retval == -ERESTART_RESTARTBLOCK))
  913. retval = -EINTR;
  914. return retval;
  915. }
  916. EXPORT_SYMBOL(sys_close);
  917. /*
  918. * This routine simulates a hangup on the tty, to arrange that users
  919. * are given clean terminals at login time.
  920. */
  921. SYSCALL_DEFINE0(vhangup)
  922. {
  923. if (capable(CAP_SYS_TTY_CONFIG)) {
  924. tty_vhangup_self();
  925. return 0;
  926. }
  927. return -EPERM;
  928. }
  929. /*
  930. * Called when an inode is about to be open.
  931. * We use this to disallow opening large files on 32bit systems if
  932. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  933. * on this flag in sys_open.
  934. */
  935. int generic_file_open(struct inode * inode, struct file * filp)
  936. {
  937. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  938. return -EOVERFLOW;
  939. return 0;
  940. }
  941. EXPORT_SYMBOL(generic_file_open);
  942. /*
  943. * This is used by subsystems that don't want seekable
  944. * file descriptors. The function is not supposed to ever fail, the only
  945. * reason it returns an 'int' and not 'void' is so that it can be plugged
  946. * directly into file_operations structure.
  947. */
  948. int nonseekable_open(struct inode *inode, struct file *filp)
  949. {
  950. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  951. return 0;
  952. }
  953. EXPORT_SYMBOL(nonseekable_open);