open.c 29 KB

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