open.c 26 KB

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