open.c 28 KB

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