open.c 28 KB

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