open.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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_truncate2(struct vfsmount *mnt, struct dentry *dentry, loff_t length,
  34. unsigned int time_attrs, 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_change2(mnt, dentry, &newattrs);
  53. mutex_unlock(&dentry->d_inode->i_mutex);
  54. return ret;
  55. }
  56. int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
  57. struct file *filp)
  58. {
  59. return do_truncate2(NULL, dentry, length, time_attrs, filp);
  60. }
  61. static long do_sys_truncate(const char __user *pathname, loff_t length)
  62. {
  63. struct path path;
  64. struct inode *inode;
  65. struct vfsmount *mnt;
  66. int error;
  67. error = -EINVAL;
  68. if (length < 0) /* sorry, but loff_t says... */
  69. goto out;
  70. error = user_path(pathname, &path);
  71. if (error)
  72. goto out;
  73. inode = path.dentry->d_inode;
  74. mnt = path.mnt;
  75. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  76. error = -EISDIR;
  77. if (S_ISDIR(inode->i_mode))
  78. goto dput_and_out;
  79. error = -EINVAL;
  80. if (!S_ISREG(inode->i_mode))
  81. goto dput_and_out;
  82. error = mnt_want_write(path.mnt);
  83. if (error)
  84. goto dput_and_out;
  85. error = inode_permission2(mnt, inode, MAY_WRITE);
  86. if (error)
  87. goto mnt_drop_write_and_out;
  88. error = -EPERM;
  89. if (IS_APPEND(inode))
  90. goto mnt_drop_write_and_out;
  91. error = get_write_access(inode);
  92. if (error)
  93. goto mnt_drop_write_and_out;
  94. /*
  95. * Make sure that there are no leases. get_write_access() protects
  96. * against the truncate racing with a lease-granting setlease().
  97. */
  98. error = break_lease(inode, O_WRONLY);
  99. if (error)
  100. goto put_write_and_out;
  101. error = locks_verify_truncate(inode, NULL, length);
  102. if (!error)
  103. error = security_path_truncate(&path);
  104. if (!error)
  105. error = do_truncate2(mnt, path.dentry, length, 0, NULL);
  106. put_write_and_out:
  107. put_write_access(inode);
  108. mnt_drop_write_and_out:
  109. mnt_drop_write(path.mnt);
  110. dput_and_out:
  111. path_put(&path);
  112. out:
  113. return error;
  114. }
  115. SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
  116. {
  117. return do_sys_truncate(path, length);
  118. }
  119. static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  120. {
  121. struct inode * inode;
  122. struct dentry *dentry;
  123. struct vfsmount *mnt;
  124. struct file * file;
  125. int error;
  126. error = -EINVAL;
  127. if (length < 0)
  128. goto out;
  129. error = -EBADF;
  130. file = fget(fd);
  131. if (!file)
  132. goto out;
  133. /* explicitly opened as large or we are on 64-bit box */
  134. if (file->f_flags & O_LARGEFILE)
  135. small = 0;
  136. dentry = file->f_path.dentry;
  137. mnt = file->f_path.mnt;
  138. inode = dentry->d_inode;
  139. error = -EINVAL;
  140. if (!S_ISREG(inode->i_mode) || !(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. error = locks_verify_truncate(inode, file, length);
  150. if (!error)
  151. error = security_path_truncate(&file->f_path);
  152. if (!error)
  153. error = do_truncate2(mnt, dentry, length, ATTR_MTIME|ATTR_CTIME, file);
  154. out_putf:
  155. fput(file);
  156. out:
  157. return error;
  158. }
  159. SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
  160. {
  161. long ret = do_sys_ftruncate(fd, length, 1);
  162. /* avoid REGPARM breakage on x86: */
  163. asmlinkage_protect(2, ret, fd, length);
  164. return ret;
  165. }
  166. /* LFS versions of truncate are only needed on 32 bit machines */
  167. #if BITS_PER_LONG == 32
  168. SYSCALL_DEFINE(truncate64)(const char __user * path, loff_t length)
  169. {
  170. return do_sys_truncate(path, length);
  171. }
  172. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  173. asmlinkage long SyS_truncate64(long path, loff_t length)
  174. {
  175. return SYSC_truncate64((const char __user *) path, length);
  176. }
  177. SYSCALL_ALIAS(sys_truncate64, SyS_truncate64);
  178. #endif
  179. SYSCALL_DEFINE(ftruncate64)(unsigned int fd, loff_t length)
  180. {
  181. long ret = do_sys_ftruncate(fd, length, 0);
  182. /* avoid REGPARM breakage on x86: */
  183. asmlinkage_protect(2, ret, fd, length);
  184. return ret;
  185. }
  186. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  187. asmlinkage long SyS_ftruncate64(long fd, loff_t length)
  188. {
  189. return SYSC_ftruncate64((unsigned int) fd, length);
  190. }
  191. SYSCALL_ALIAS(sys_ftruncate64, SyS_ftruncate64);
  192. #endif
  193. #endif /* BITS_PER_LONG == 32 */
  194. int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  195. {
  196. struct inode *inode = file->f_path.dentry->d_inode;
  197. long ret;
  198. if (offset < 0 || len <= 0)
  199. return -EINVAL;
  200. /* Return error if mode is not supported */
  201. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  202. return -EOPNOTSUPP;
  203. /* Punch hole must have keep size set */
  204. if ((mode & FALLOC_FL_PUNCH_HOLE) &&
  205. !(mode & FALLOC_FL_KEEP_SIZE))
  206. return -EOPNOTSUPP;
  207. if (!(file->f_mode & FMODE_WRITE))
  208. return -EBADF;
  209. /* It's not possible punch hole on append only file */
  210. if (mode & FALLOC_FL_PUNCH_HOLE && IS_APPEND(inode))
  211. return -EPERM;
  212. if (IS_IMMUTABLE(inode))
  213. return -EPERM;
  214. /*
  215. * Revalidate the write permissions, in case security policy has
  216. * changed since the files were opened.
  217. */
  218. ret = security_file_permission(file, MAY_WRITE);
  219. if (ret)
  220. return ret;
  221. if (S_ISFIFO(inode->i_mode))
  222. return -ESPIPE;
  223. /*
  224. * Let individual file system decide if it supports preallocation
  225. * for directories or not.
  226. */
  227. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  228. return -ENODEV;
  229. /* Check for wrap through zero too */
  230. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  231. return -EFBIG;
  232. if (!file->f_op->fallocate)
  233. return -EOPNOTSUPP;
  234. return file->f_op->fallocate(file, mode, offset, len);
  235. }
  236. SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len)
  237. {
  238. struct file *file;
  239. int error = -EBADF;
  240. file = fget(fd);
  241. if (file) {
  242. error = do_fallocate(file, mode, offset, len);
  243. fput(file);
  244. }
  245. return error;
  246. }
  247. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  248. asmlinkage long SyS_fallocate(long fd, long mode, loff_t offset, loff_t len)
  249. {
  250. return SYSC_fallocate((int)fd, (int)mode, offset, len);
  251. }
  252. SYSCALL_ALIAS(sys_fallocate, SyS_fallocate);
  253. #endif
  254. /*
  255. * access() needs to use the real uid/gid, not the effective uid/gid.
  256. * We do this by temporarily clearing all FS-related capabilities and
  257. * switching the fsuid/fsgid around to the real ones.
  258. */
  259. SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
  260. {
  261. const struct cred *old_cred;
  262. struct cred *override_cred;
  263. struct path path;
  264. struct inode *inode;
  265. struct vfsmount *mnt;
  266. int res;
  267. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  268. return -EINVAL;
  269. override_cred = prepare_creds();
  270. if (!override_cred)
  271. return -ENOMEM;
  272. override_cred->fsuid = override_cred->uid;
  273. override_cred->fsgid = override_cred->gid;
  274. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  275. /* Clear the capabilities if we switch to a non-root user */
  276. if (override_cred->uid)
  277. cap_clear(override_cred->cap_effective);
  278. else
  279. override_cred->cap_effective =
  280. override_cred->cap_permitted;
  281. }
  282. old_cred = override_creds(override_cred);
  283. res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
  284. if (res)
  285. goto out;
  286. inode = path.dentry->d_inode;
  287. mnt = path.mnt;
  288. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  289. /*
  290. * MAY_EXEC on regular files is denied if the fs is mounted
  291. * with the "noexec" flag.
  292. */
  293. res = -EACCES;
  294. if (path.mnt->mnt_flags & MNT_NOEXEC)
  295. goto out_path_release;
  296. }
  297. res = inode_permission2(mnt, inode, mode | MAY_ACCESS);
  298. /* SuS v2 requires we report a read only fs too */
  299. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  300. goto out_path_release;
  301. /*
  302. * This is a rare case where using __mnt_is_readonly()
  303. * is OK without a mnt_want/drop_write() pair. Since
  304. * no actual write to the fs is performed here, we do
  305. * not need to telegraph to that to anyone.
  306. *
  307. * By doing this, we accept that this access is
  308. * inherently racy and know that the fs may change
  309. * state before we even see this result.
  310. */
  311. if (__mnt_is_readonly(path.mnt))
  312. res = -EROFS;
  313. out_path_release:
  314. path_put(&path);
  315. out:
  316. revert_creds(old_cred);
  317. put_cred(override_cred);
  318. return res;
  319. }
  320. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  321. {
  322. return sys_faccessat(AT_FDCWD, filename, mode);
  323. }
  324. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  325. {
  326. struct path path;
  327. int error;
  328. error = user_path_dir(filename, &path);
  329. if (error)
  330. goto out;
  331. error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  332. if (error)
  333. goto dput_and_out;
  334. set_fs_pwd(current->fs, &path);
  335. dput_and_out:
  336. path_put(&path);
  337. out:
  338. return error;
  339. }
  340. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  341. {
  342. struct file *file;
  343. struct inode *inode;
  344. struct vfsmount *mnt;
  345. int error, fput_needed;
  346. error = -EBADF;
  347. file = fget_raw_light(fd, &fput_needed);
  348. if (!file)
  349. goto out;
  350. inode = file->f_path.dentry->d_inode;
  351. mnt = file->f_path.mnt;
  352. error = -ENOTDIR;
  353. if (!S_ISDIR(inode->i_mode))
  354. goto out_putf;
  355. error = inode_permission2(mnt, inode, MAY_EXEC | MAY_CHDIR);
  356. if (!error)
  357. set_fs_pwd(current->fs, &file->f_path);
  358. out_putf:
  359. fput_light(file, fput_needed);
  360. out:
  361. return error;
  362. }
  363. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  364. {
  365. struct path path;
  366. int error;
  367. error = user_path_dir(filename, &path);
  368. if (error)
  369. goto out;
  370. error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  371. if (error)
  372. goto dput_and_out;
  373. error = -EPERM;
  374. if (!capable(CAP_SYS_CHROOT))
  375. goto dput_and_out;
  376. error = security_path_chroot(&path);
  377. if (error)
  378. goto dput_and_out;
  379. set_fs_root(current->fs, &path);
  380. error = 0;
  381. dput_and_out:
  382. path_put(&path);
  383. out:
  384. return error;
  385. }
  386. static int chmod_common(struct path *path, umode_t mode)
  387. {
  388. struct inode *inode = path->dentry->d_inode;
  389. struct iattr newattrs;
  390. int error;
  391. error = mnt_want_write(path->mnt);
  392. if (error)
  393. return error;
  394. mutex_lock(&inode->i_mutex);
  395. error = security_path_chmod(path, mode);
  396. if (error)
  397. goto out_unlock;
  398. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  399. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  400. error = notify_change2(path->mnt, path->dentry, &newattrs);
  401. out_unlock:
  402. mutex_unlock(&inode->i_mutex);
  403. mnt_drop_write(path->mnt);
  404. return error;
  405. }
  406. SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
  407. {
  408. struct file * file;
  409. int err = -EBADF;
  410. file = fget(fd);
  411. if (file) {
  412. audit_inode(NULL, file->f_path.dentry);
  413. err = chmod_common(&file->f_path, mode);
  414. fput(file);
  415. }
  416. return err;
  417. }
  418. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
  419. {
  420. struct path path;
  421. int error;
  422. error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
  423. if (!error) {
  424. error = chmod_common(&path, mode);
  425. path_put(&path);
  426. }
  427. return error;
  428. }
  429. SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
  430. {
  431. return sys_fchmodat(AT_FDCWD, filename, mode);
  432. }
  433. static int chown_common(struct path *path, uid_t user, gid_t group)
  434. {
  435. struct inode *inode = path->dentry->d_inode;
  436. int error;
  437. struct iattr newattrs;
  438. newattrs.ia_valid = ATTR_CTIME;
  439. if (user != (uid_t) -1) {
  440. newattrs.ia_valid |= ATTR_UID;
  441. newattrs.ia_uid = user;
  442. }
  443. if (group != (gid_t) -1) {
  444. newattrs.ia_valid |= ATTR_GID;
  445. newattrs.ia_gid = group;
  446. }
  447. if (!S_ISDIR(inode->i_mode))
  448. newattrs.ia_valid |=
  449. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  450. mutex_lock(&inode->i_mutex);
  451. error = security_path_chown(path, user, group);
  452. if (!error)
  453. error = notify_change2(path->mnt, path->dentry, &newattrs);
  454. mutex_unlock(&inode->i_mutex);
  455. return error;
  456. }
  457. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  458. {
  459. struct path path;
  460. int error;
  461. error = user_path(filename, &path);
  462. if (error)
  463. goto out;
  464. error = mnt_want_write(path.mnt);
  465. if (error)
  466. goto out_release;
  467. error = chown_common(&path, user, group);
  468. mnt_drop_write(path.mnt);
  469. out_release:
  470. path_put(&path);
  471. out:
  472. return error;
  473. }
  474. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  475. gid_t, group, int, flag)
  476. {
  477. struct path path;
  478. int error = -EINVAL;
  479. int lookup_flags;
  480. if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  481. goto out;
  482. lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  483. if (flag & AT_EMPTY_PATH)
  484. lookup_flags |= LOOKUP_EMPTY;
  485. error = user_path_at(dfd, filename, lookup_flags, &path);
  486. if (error)
  487. goto out;
  488. error = mnt_want_write(path.mnt);
  489. if (error)
  490. goto out_release;
  491. error = chown_common(&path, user, group);
  492. mnt_drop_write(path.mnt);
  493. out_release:
  494. path_put(&path);
  495. out:
  496. return error;
  497. }
  498. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  499. {
  500. struct path path;
  501. int error;
  502. error = user_lpath(filename, &path);
  503. if (error)
  504. goto out;
  505. error = mnt_want_write(path.mnt);
  506. if (error)
  507. goto out_release;
  508. error = chown_common(&path, user, group);
  509. mnt_drop_write(path.mnt);
  510. out_release:
  511. path_put(&path);
  512. out:
  513. return error;
  514. }
  515. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  516. {
  517. struct file * file;
  518. int error = -EBADF;
  519. struct dentry * dentry;
  520. file = fget(fd);
  521. if (!file)
  522. goto out;
  523. error = mnt_want_write_file(file);
  524. if (error)
  525. goto out_fput;
  526. dentry = file->f_path.dentry;
  527. audit_inode(NULL, dentry);
  528. error = chown_common(&file->f_path, user, group);
  529. mnt_drop_write_file(file);
  530. out_fput:
  531. fput(file);
  532. out:
  533. return error;
  534. }
  535. /*
  536. * You have to be very careful that these write
  537. * counts get cleaned up in error cases and
  538. * upon __fput(). This should probably never
  539. * be called outside of __dentry_open().
  540. */
  541. static inline int __get_file_write_access(struct inode *inode,
  542. struct vfsmount *mnt)
  543. {
  544. int error;
  545. error = get_write_access(inode);
  546. if (error)
  547. return error;
  548. /*
  549. * Do not take mount writer counts on
  550. * special files since no writes to
  551. * the mount itself will occur.
  552. */
  553. if (!special_file(inode->i_mode)) {
  554. /*
  555. * Balanced in __fput()
  556. */
  557. error = mnt_want_write(mnt);
  558. if (error)
  559. put_write_access(inode);
  560. }
  561. return error;
  562. }
  563. static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
  564. struct file *f,
  565. int (*open)(struct inode *, struct file *),
  566. const struct cred *cred)
  567. {
  568. static const struct file_operations empty_fops = {};
  569. struct inode *inode;
  570. int error;
  571. f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
  572. FMODE_PREAD | FMODE_PWRITE;
  573. if (unlikely(f->f_flags & O_PATH))
  574. f->f_mode = FMODE_PATH;
  575. inode = dentry->d_inode;
  576. if (f->f_mode & FMODE_WRITE) {
  577. error = __get_file_write_access(inode, mnt);
  578. if (error)
  579. goto cleanup_file;
  580. if (!special_file(inode->i_mode))
  581. file_take_write(f);
  582. }
  583. f->f_mapping = inode->i_mapping;
  584. f->f_path.dentry = dentry;
  585. f->f_path.mnt = mnt;
  586. f->f_pos = 0;
  587. if (unlikely(f->f_mode & FMODE_PATH)) {
  588. f->f_op = &empty_fops;
  589. return f;
  590. }
  591. if (S_ISREG(inode->i_mode))
  592. f->f_mode |= FMODE_SPLICE_WRITE | FMODE_SPLICE_READ;
  593. f->f_op = fops_get(inode->i_fop);
  594. error = security_dentry_open(f, cred);
  595. if (error)
  596. goto cleanup_all;
  597. error = break_lease(inode, f->f_flags);
  598. if (error)
  599. goto cleanup_all;
  600. if (!open && f->f_op)
  601. open = f->f_op->open;
  602. if (open) {
  603. error = open(inode, f);
  604. if (error)
  605. goto cleanup_all;
  606. }
  607. if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  608. i_readcount_inc(inode);
  609. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  610. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  611. /* NB: we're sure to have correct a_ops only after f_op->open */
  612. if (f->f_flags & O_DIRECT) {
  613. if (!f->f_mapping->a_ops ||
  614. ((!f->f_mapping->a_ops->direct_IO) &&
  615. (!f->f_mapping->a_ops->get_xip_mem))) {
  616. fput(f);
  617. f = ERR_PTR(-EINVAL);
  618. }
  619. }
  620. return f;
  621. cleanup_all:
  622. fops_put(f->f_op);
  623. if (f->f_mode & FMODE_WRITE) {
  624. put_write_access(inode);
  625. if (!special_file(inode->i_mode)) {
  626. /*
  627. * We don't consider this a real
  628. * mnt_want/drop_write() pair
  629. * because it all happenend right
  630. * here, so just reset the state.
  631. */
  632. file_reset_write(f);
  633. mnt_drop_write(mnt);
  634. }
  635. }
  636. f->f_path.dentry = NULL;
  637. f->f_path.mnt = NULL;
  638. cleanup_file:
  639. put_filp(f);
  640. dput(dentry);
  641. mntput(mnt);
  642. return ERR_PTR(error);
  643. }
  644. /**
  645. * lookup_instantiate_filp - instantiates the open intent filp
  646. * @nd: pointer to nameidata
  647. * @dentry: pointer to dentry
  648. * @open: open callback
  649. *
  650. * Helper for filesystems that want to use lookup open intents and pass back
  651. * a fully instantiated struct file to the caller.
  652. * This function is meant to be called from within a filesystem's
  653. * lookup method.
  654. * Beware of calling it for non-regular files! Those ->open methods might block
  655. * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
  656. * leading to a deadlock, as nobody can open that fifo anymore, because
  657. * another process to open fifo will block on locked parent when doing lookup).
  658. * Note that in case of error, nd->intent.open.file is destroyed, but the
  659. * path information remains valid.
  660. * If the open callback is set to NULL, then the standard f_op->open()
  661. * filesystem callback is substituted.
  662. */
  663. struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
  664. int (*open)(struct inode *, struct file *))
  665. {
  666. const struct cred *cred = current_cred();
  667. if (IS_ERR(nd->intent.open.file))
  668. goto out;
  669. if (IS_ERR(dentry))
  670. goto out_err;
  671. nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
  672. nd->intent.open.file,
  673. open, cred);
  674. out:
  675. return nd->intent.open.file;
  676. out_err:
  677. release_open_intent(nd);
  678. nd->intent.open.file = ERR_CAST(dentry);
  679. goto out;
  680. }
  681. EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
  682. /**
  683. * nameidata_to_filp - convert a nameidata to an open filp.
  684. * @nd: pointer to nameidata
  685. * @flags: open flags
  686. *
  687. * Note that this function destroys the original nameidata
  688. */
  689. struct file *nameidata_to_filp(struct nameidata *nd)
  690. {
  691. const struct cred *cred = current_cred();
  692. struct file *filp;
  693. /* Pick up the filp from the open intent */
  694. filp = nd->intent.open.file;
  695. nd->intent.open.file = NULL;
  696. /* Has the filesystem initialised the file for us? */
  697. if (filp->f_path.dentry == NULL) {
  698. path_get(&nd->path);
  699. filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
  700. NULL, cred);
  701. }
  702. return filp;
  703. }
  704. /*
  705. * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
  706. * error.
  707. */
  708. struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, 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(!mnt);
  716. error = -ENFILE;
  717. f = get_empty_filp();
  718. if (f == NULL) {
  719. dput(dentry);
  720. mntput(mnt);
  721. return ERR_PTR(error);
  722. }
  723. f->f_flags = flags;
  724. return __dentry_open(dentry, mnt, f, NULL, cred);
  725. }
  726. EXPORT_SYMBOL(dentry_open);
  727. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  728. {
  729. struct fdtable *fdt = files_fdtable(files);
  730. __clear_open_fd(fd, fdt);
  731. if (fd < files->next_fd)
  732. files->next_fd = fd;
  733. }
  734. void put_unused_fd(unsigned int fd)
  735. {
  736. struct files_struct *files = current->files;
  737. spin_lock(&files->file_lock);
  738. __put_unused_fd(files, fd);
  739. spin_unlock(&files->file_lock);
  740. }
  741. EXPORT_SYMBOL(put_unused_fd);
  742. /*
  743. * Install a file pointer in the fd array.
  744. *
  745. * The VFS is full of places where we drop the files lock between
  746. * setting the open_fds bitmap and installing the file in the file
  747. * array. At any such point, we are vulnerable to a dup2() race
  748. * installing a file in the array before us. We need to detect this and
  749. * fput() the struct file we are about to overwrite in this case.
  750. *
  751. * It should never happen - if we allow dup2() do it, _really_ bad things
  752. * will follow.
  753. */
  754. void fd_install(unsigned int fd, struct file *file)
  755. {
  756. struct files_struct *files = current->files;
  757. struct fdtable *fdt;
  758. spin_lock(&files->file_lock);
  759. fdt = files_fdtable(files);
  760. BUG_ON(fdt->fd[fd] != NULL);
  761. rcu_assign_pointer(fdt->fd[fd], file);
  762. spin_unlock(&files->file_lock);
  763. }
  764. EXPORT_SYMBOL(fd_install);
  765. static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
  766. {
  767. int lookup_flags = 0;
  768. int acc_mode;
  769. if (flags & O_CREAT)
  770. op->mode = (mode & S_IALLUGO) | S_IFREG;
  771. else
  772. op->mode = 0;
  773. /* Must never be set by userspace */
  774. flags &= ~FMODE_NONOTIFY;
  775. /*
  776. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  777. * check for O_DSYNC if the need any syncing at all we enforce it's
  778. * always set instead of having to deal with possibly weird behaviour
  779. * for malicious applications setting only __O_SYNC.
  780. */
  781. if (flags & __O_SYNC)
  782. flags |= O_DSYNC;
  783. /*
  784. * If we have O_PATH in the open flag. Then we
  785. * cannot have anything other than the below set of flags
  786. */
  787. if (flags & O_PATH) {
  788. flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
  789. acc_mode = 0;
  790. } else {
  791. acc_mode = MAY_OPEN | ACC_MODE(flags);
  792. }
  793. op->open_flag = flags;
  794. /* O_TRUNC implies we need access checks for write permissions */
  795. if (flags & O_TRUNC)
  796. acc_mode |= MAY_WRITE;
  797. /* Allow the LSM permission hook to distinguish append
  798. access from general write access. */
  799. if (flags & O_APPEND)
  800. acc_mode |= MAY_APPEND;
  801. op->acc_mode = acc_mode;
  802. op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
  803. if (flags & O_CREAT) {
  804. op->intent |= LOOKUP_CREATE;
  805. if (flags & O_EXCL)
  806. op->intent |= LOOKUP_EXCL;
  807. }
  808. if (flags & O_DIRECTORY)
  809. lookup_flags |= LOOKUP_DIRECTORY;
  810. if (!(flags & O_NOFOLLOW))
  811. lookup_flags |= LOOKUP_FOLLOW;
  812. return lookup_flags;
  813. }
  814. /**
  815. * filp_open - open file and return file pointer
  816. *
  817. * @filename: path to open
  818. * @flags: open flags as per the open(2) second argument
  819. * @mode: mode for the new file if O_CREAT is set, else ignored
  820. *
  821. * This is the helper to open a file from kernelspace if you really
  822. * have to. But in generally you should not do this, so please move
  823. * along, nothing to see here..
  824. */
  825. struct file *filp_open(const char *filename, int flags, umode_t mode)
  826. {
  827. struct open_flags op;
  828. int lookup = build_open_flags(flags, mode, &op);
  829. return do_filp_open(AT_FDCWD, filename, &op, lookup);
  830. }
  831. EXPORT_SYMBOL(filp_open);
  832. struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  833. const char *filename, int flags)
  834. {
  835. struct open_flags op;
  836. int lookup = build_open_flags(flags, 0, &op);
  837. if (flags & O_CREAT)
  838. return ERR_PTR(-EINVAL);
  839. if (!filename && (flags & O_DIRECTORY))
  840. if (!dentry->d_inode->i_op->lookup)
  841. return ERR_PTR(-ENOTDIR);
  842. return do_file_open_root(dentry, mnt, filename, &op, lookup);
  843. }
  844. EXPORT_SYMBOL(file_open_root);
  845. long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
  846. {
  847. struct open_flags op;
  848. int lookup = build_open_flags(flags, mode, &op);
  849. char *tmp = getname(filename);
  850. int fd = PTR_ERR(tmp);
  851. if (!IS_ERR(tmp)) {
  852. fd = get_unused_fd_flags(flags);
  853. if (fd >= 0) {
  854. struct file *f = do_filp_open(dfd, tmp, &op, lookup);
  855. if (IS_ERR(f)) {
  856. put_unused_fd(fd);
  857. fd = PTR_ERR(f);
  858. } else {
  859. fsnotify_open(f);
  860. fd_install(fd, f);
  861. }
  862. }
  863. putname(tmp);
  864. }
  865. return fd;
  866. }
  867. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  868. {
  869. long ret;
  870. if (force_o_largefile())
  871. flags |= O_LARGEFILE;
  872. ret = do_sys_open(AT_FDCWD, filename, flags, mode);
  873. /* avoid REGPARM breakage on x86: */
  874. asmlinkage_protect(3, ret, filename, flags, mode);
  875. return ret;
  876. }
  877. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  878. umode_t, mode)
  879. {
  880. long ret;
  881. if (force_o_largefile())
  882. flags |= O_LARGEFILE;
  883. ret = do_sys_open(dfd, filename, flags, mode);
  884. /* avoid REGPARM breakage on x86: */
  885. asmlinkage_protect(4, ret, dfd, filename, flags, mode);
  886. return ret;
  887. }
  888. #ifndef __alpha__
  889. /*
  890. * For backward compatibility? Maybe this should be moved
  891. * into arch/i386 instead?
  892. */
  893. SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
  894. {
  895. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  896. }
  897. #endif
  898. /*
  899. * "id" is the POSIX thread ID. We use the
  900. * files pointer for this..
  901. */
  902. int filp_close(struct file *filp, fl_owner_t id)
  903. {
  904. int retval = 0;
  905. if (!file_count(filp)) {
  906. printk(KERN_ERR "VFS: Close: file count is 0\n");
  907. return 0;
  908. }
  909. if (filp->f_op && filp->f_op->flush)
  910. retval = filp->f_op->flush(filp, id);
  911. if (likely(!(filp->f_mode & FMODE_PATH))) {
  912. dnotify_flush(filp, id);
  913. locks_remove_posix(filp, id);
  914. }
  915. security_file_close(filp);
  916. fput(filp);
  917. return retval;
  918. }
  919. EXPORT_SYMBOL(filp_close);
  920. /*
  921. * Careful here! We test whether the file pointer is NULL before
  922. * releasing the fd. This ensures that one clone task can't release
  923. * an fd while another clone is opening it.
  924. */
  925. SYSCALL_DEFINE1(close, unsigned int, fd)
  926. {
  927. struct file * filp;
  928. struct files_struct *files = current->files;
  929. struct fdtable *fdt;
  930. int retval;
  931. #ifdef CONFIG_SEC_DEBUG_ZERO_FD_CLOSE
  932. if (fd == 0 && strcmp(current->group_leader->comm,"mediaserver") == 0)
  933. panic("trying to close fd=0");
  934. #endif
  935. spin_lock(&files->file_lock);
  936. fdt = files_fdtable(files);
  937. if (fd >= fdt->max_fds)
  938. goto out_unlock;
  939. filp = fdt->fd[fd];
  940. if (!filp)
  941. goto out_unlock;
  942. rcu_assign_pointer(fdt->fd[fd], NULL);
  943. __clear_close_on_exec(fd, fdt);
  944. __put_unused_fd(files, fd);
  945. spin_unlock(&files->file_lock);
  946. retval = filp_close(filp, files);
  947. /* can't restart close syscall because file table entry was cleared */
  948. if (unlikely(retval == -ERESTARTSYS ||
  949. retval == -ERESTARTNOINTR ||
  950. retval == -ERESTARTNOHAND ||
  951. retval == -ERESTART_RESTARTBLOCK))
  952. retval = -EINTR;
  953. return retval;
  954. out_unlock:
  955. spin_unlock(&files->file_lock);
  956. return -EBADF;
  957. }
  958. EXPORT_SYMBOL(sys_close);
  959. /*
  960. * This routine simulates a hangup on the tty, to arrange that users
  961. * are given clean terminals at login time.
  962. */
  963. SYSCALL_DEFINE0(vhangup)
  964. {
  965. if (capable(CAP_SYS_TTY_CONFIG)) {
  966. tty_vhangup_self();
  967. return 0;
  968. }
  969. return -EPERM;
  970. }
  971. /*
  972. * Called when an inode is about to be open.
  973. * We use this to disallow opening large files on 32bit systems if
  974. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  975. * on this flag in sys_open.
  976. */
  977. int generic_file_open(struct inode * inode, struct file * filp)
  978. {
  979. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  980. return -EOVERFLOW;
  981. return 0;
  982. }
  983. EXPORT_SYMBOL(generic_file_open);
  984. /*
  985. * This is used by subsystems that don't want seekable
  986. * file descriptors. The function is not supposed to ever fail, the only
  987. * reason it returns an 'int' and not 'void' is so that it can be plugged
  988. * directly into file_operations structure.
  989. */
  990. int nonseekable_open(struct inode *inode, struct file *filp)
  991. {
  992. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  993. return 0;
  994. }
  995. EXPORT_SYMBOL(nonseekable_open);