open.c 26 KB

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