open.c 26 KB

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