open.c 25 KB

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