open.c 27 KB

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