open.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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. if (override_cred->uid)
  267. cap_clear(override_cred->cap_effective);
  268. else
  269. override_cred->cap_effective =
  270. override_cred->cap_permitted;
  271. }
  272. old_cred = override_creds(override_cred);
  273. res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
  274. if (res)
  275. goto out;
  276. inode = path.dentry->d_inode;
  277. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  278. /*
  279. * MAY_EXEC on regular files is denied if the fs is mounted
  280. * with the "noexec" flag.
  281. */
  282. res = -EACCES;
  283. if (path.mnt->mnt_flags & MNT_NOEXEC)
  284. goto out_path_release;
  285. }
  286. res = inode_permission(inode, mode | MAY_ACCESS);
  287. /* SuS v2 requires we report a read only fs too */
  288. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  289. goto out_path_release;
  290. /*
  291. * This is a rare case where using __mnt_is_readonly()
  292. * is OK without a mnt_want/drop_write() pair. Since
  293. * no actual write to the fs is performed here, we do
  294. * not need to telegraph to that to anyone.
  295. *
  296. * By doing this, we accept that this access is
  297. * inherently racy and know that the fs may change
  298. * state before we even see this result.
  299. */
  300. if (__mnt_is_readonly(path.mnt))
  301. res = -EROFS;
  302. out_path_release:
  303. path_put(&path);
  304. out:
  305. revert_creds(old_cred);
  306. put_cred(override_cred);
  307. return res;
  308. }
  309. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  310. {
  311. return sys_faccessat(AT_FDCWD, filename, mode);
  312. }
  313. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  314. {
  315. struct path path;
  316. int error;
  317. error = user_path_dir(filename, &path);
  318. if (error)
  319. goto out;
  320. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  321. if (error)
  322. goto dput_and_out;
  323. set_fs_pwd(current->fs, &path);
  324. dput_and_out:
  325. path_put(&path);
  326. out:
  327. return error;
  328. }
  329. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  330. {
  331. struct file *file;
  332. struct inode *inode;
  333. int error;
  334. error = -EBADF;
  335. file = fget(fd);
  336. if (!file)
  337. goto out;
  338. inode = file->f_path.dentry->d_inode;
  339. error = -ENOTDIR;
  340. if (!S_ISDIR(inode->i_mode))
  341. goto out_putf;
  342. error = inode_permission(inode, MAY_EXEC | MAY_CHDIR);
  343. if (!error)
  344. set_fs_pwd(current->fs, &file->f_path);
  345. out_putf:
  346. fput(file);
  347. out:
  348. return error;
  349. }
  350. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  351. {
  352. struct path path;
  353. int error;
  354. error = user_path_dir(filename, &path);
  355. if (error)
  356. goto out;
  357. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  358. if (error)
  359. goto dput_and_out;
  360. error = -EPERM;
  361. if (!capable(CAP_SYS_CHROOT))
  362. goto dput_and_out;
  363. error = security_path_chroot(&path);
  364. if (error)
  365. goto dput_and_out;
  366. set_fs_root(current->fs, &path);
  367. error = 0;
  368. dput_and_out:
  369. path_put(&path);
  370. out:
  371. return error;
  372. }
  373. SYSCALL_DEFINE2(fchmod, unsigned int, fd, mode_t, mode)
  374. {
  375. struct inode * inode;
  376. struct dentry * dentry;
  377. struct file * file;
  378. int err = -EBADF;
  379. struct iattr newattrs;
  380. file = fget(fd);
  381. if (!file)
  382. goto out;
  383. dentry = file->f_path.dentry;
  384. inode = dentry->d_inode;
  385. audit_inode(NULL, dentry);
  386. err = mnt_want_write_file(file);
  387. if (err)
  388. goto out_putf;
  389. mutex_lock(&inode->i_mutex);
  390. err = security_path_chmod(dentry, file->f_vfsmnt, mode);
  391. if (err)
  392. goto out_unlock;
  393. if (mode == (mode_t) -1)
  394. mode = inode->i_mode;
  395. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  396. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  397. err = notify_change(dentry, &newattrs);
  398. out_unlock:
  399. mutex_unlock(&inode->i_mutex);
  400. mnt_drop_write(file->f_path.mnt);
  401. out_putf:
  402. fput(file);
  403. out:
  404. return err;
  405. }
  406. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, mode_t, mode)
  407. {
  408. struct path path;
  409. struct inode *inode;
  410. int error;
  411. struct iattr newattrs;
  412. error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
  413. if (error)
  414. goto out;
  415. inode = path.dentry->d_inode;
  416. error = mnt_want_write(path.mnt);
  417. if (error)
  418. goto dput_and_out;
  419. mutex_lock(&inode->i_mutex);
  420. error = security_path_chmod(path.dentry, path.mnt, mode);
  421. if (error)
  422. goto out_unlock;
  423. if (mode == (mode_t) -1)
  424. mode = inode->i_mode;
  425. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  426. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  427. error = notify_change(path.dentry, &newattrs);
  428. out_unlock:
  429. mutex_unlock(&inode->i_mutex);
  430. mnt_drop_write(path.mnt);
  431. dput_and_out:
  432. path_put(&path);
  433. out:
  434. return error;
  435. }
  436. SYSCALL_DEFINE2(chmod, const char __user *, filename, mode_t, mode)
  437. {
  438. return sys_fchmodat(AT_FDCWD, filename, mode);
  439. }
  440. static int chown_common(struct path *path, uid_t user, gid_t group)
  441. {
  442. struct inode *inode = path->dentry->d_inode;
  443. int error;
  444. struct iattr newattrs;
  445. newattrs.ia_valid = ATTR_CTIME;
  446. if (user != (uid_t) -1) {
  447. newattrs.ia_valid |= ATTR_UID;
  448. newattrs.ia_uid = user;
  449. }
  450. if (group != (gid_t) -1) {
  451. newattrs.ia_valid |= ATTR_GID;
  452. newattrs.ia_gid = group;
  453. }
  454. if (!S_ISDIR(inode->i_mode))
  455. newattrs.ia_valid |=
  456. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  457. mutex_lock(&inode->i_mutex);
  458. error = security_path_chown(path, user, group);
  459. if (!error)
  460. error = notify_change(path->dentry, &newattrs);
  461. mutex_unlock(&inode->i_mutex);
  462. return error;
  463. }
  464. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  465. {
  466. struct path path;
  467. int error;
  468. error = user_path(filename, &path);
  469. if (error)
  470. goto out;
  471. error = mnt_want_write(path.mnt);
  472. if (error)
  473. goto out_release;
  474. error = chown_common(&path, user, group);
  475. mnt_drop_write(path.mnt);
  476. out_release:
  477. path_put(&path);
  478. out:
  479. return error;
  480. }
  481. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  482. gid_t, group, int, flag)
  483. {
  484. struct path path;
  485. int error = -EINVAL;
  486. int lookup_flags;
  487. if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  488. goto out;
  489. lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  490. if (flag & AT_EMPTY_PATH)
  491. lookup_flags |= LOOKUP_EMPTY;
  492. error = user_path_at(dfd, filename, lookup_flags, &path);
  493. if (error)
  494. goto out;
  495. error = mnt_want_write(path.mnt);
  496. if (error)
  497. goto out_release;
  498. error = chown_common(&path, user, group);
  499. mnt_drop_write(path.mnt);
  500. out_release:
  501. path_put(&path);
  502. out:
  503. return error;
  504. }
  505. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  506. {
  507. struct path path;
  508. int error;
  509. error = user_lpath(filename, &path);
  510. if (error)
  511. goto out;
  512. error = mnt_want_write(path.mnt);
  513. if (error)
  514. goto out_release;
  515. error = chown_common(&path, user, group);
  516. mnt_drop_write(path.mnt);
  517. out_release:
  518. path_put(&path);
  519. out:
  520. return error;
  521. }
  522. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  523. {
  524. struct file * file;
  525. int error = -EBADF;
  526. struct dentry * dentry;
  527. file = fget(fd);
  528. if (!file)
  529. goto out;
  530. error = mnt_want_write_file(file);
  531. if (error)
  532. goto out_fput;
  533. dentry = file->f_path.dentry;
  534. audit_inode(NULL, dentry);
  535. error = chown_common(&file->f_path, user, group);
  536. mnt_drop_write(file->f_path.mnt);
  537. out_fput:
  538. fput(file);
  539. out:
  540. return error;
  541. }
  542. /*
  543. * You have to be very careful that these write
  544. * counts get cleaned up in error cases and
  545. * upon __fput(). This should probably never
  546. * be called outside of __dentry_open().
  547. */
  548. static inline int __get_file_write_access(struct inode *inode,
  549. struct vfsmount *mnt)
  550. {
  551. int error;
  552. error = get_write_access(inode);
  553. if (error)
  554. return error;
  555. /*
  556. * Do not take mount writer counts on
  557. * special files since no writes to
  558. * the mount itself will occur.
  559. */
  560. if (!special_file(inode->i_mode)) {
  561. /*
  562. * Balanced in __fput()
  563. */
  564. error = mnt_want_write(mnt);
  565. if (error)
  566. put_write_access(inode);
  567. }
  568. return error;
  569. }
  570. static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
  571. struct file *f,
  572. int (*open)(struct inode *, struct file *),
  573. const struct cred *cred)
  574. {
  575. static const struct file_operations empty_fops = {};
  576. struct inode *inode;
  577. int error;
  578. f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
  579. FMODE_PREAD | FMODE_PWRITE;
  580. if (unlikely(f->f_flags & O_PATH))
  581. f->f_mode = FMODE_PATH;
  582. inode = dentry->d_inode;
  583. if (f->f_mode & FMODE_WRITE) {
  584. error = __get_file_write_access(inode, mnt);
  585. if (error)
  586. goto cleanup_file;
  587. if (!special_file(inode->i_mode))
  588. file_take_write(f);
  589. }
  590. f->f_mapping = inode->i_mapping;
  591. f->f_path.dentry = dentry;
  592. f->f_path.mnt = mnt;
  593. f->f_pos = 0;
  594. file_sb_list_add(f, inode->i_sb);
  595. if (unlikely(f->f_mode & FMODE_PATH)) {
  596. f->f_op = &empty_fops;
  597. return f;
  598. }
  599. f->f_op = fops_get(inode->i_fop);
  600. error = security_dentry_open(f, cred);
  601. if (error)
  602. goto cleanup_all;
  603. if (!open && f->f_op)
  604. open = f->f_op->open;
  605. if (open) {
  606. error = open(inode, f);
  607. if (error)
  608. goto cleanup_all;
  609. }
  610. if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  611. i_readcount_inc(inode);
  612. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  613. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  614. /* NB: we're sure to have correct a_ops only after f_op->open */
  615. if (f->f_flags & O_DIRECT) {
  616. if (!f->f_mapping->a_ops ||
  617. ((!f->f_mapping->a_ops->direct_IO) &&
  618. (!f->f_mapping->a_ops->get_xip_mem))) {
  619. fput(f);
  620. f = ERR_PTR(-EINVAL);
  621. }
  622. }
  623. return f;
  624. cleanup_all:
  625. fops_put(f->f_op);
  626. if (f->f_mode & FMODE_WRITE) {
  627. put_write_access(inode);
  628. if (!special_file(inode->i_mode)) {
  629. /*
  630. * We don't consider this a real
  631. * mnt_want/drop_write() pair
  632. * because it all happenend right
  633. * here, so just reset the state.
  634. */
  635. file_reset_write(f);
  636. mnt_drop_write(mnt);
  637. }
  638. }
  639. file_sb_list_del(f);
  640. f->f_path.dentry = NULL;
  641. f->f_path.mnt = NULL;
  642. cleanup_file:
  643. put_filp(f);
  644. dput(dentry);
  645. mntput(mnt);
  646. return ERR_PTR(error);
  647. }
  648. /**
  649. * lookup_instantiate_filp - instantiates the open intent filp
  650. * @nd: pointer to nameidata
  651. * @dentry: pointer to dentry
  652. * @open: open callback
  653. *
  654. * Helper for filesystems that want to use lookup open intents and pass back
  655. * a fully instantiated struct file to the caller.
  656. * This function is meant to be called from within a filesystem's
  657. * lookup method.
  658. * Beware of calling it for non-regular files! Those ->open methods might block
  659. * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
  660. * leading to a deadlock, as nobody can open that fifo anymore, because
  661. * another process to open fifo will block on locked parent when doing lookup).
  662. * Note that in case of error, nd->intent.open.file is destroyed, but the
  663. * path information remains valid.
  664. * If the open callback is set to NULL, then the standard f_op->open()
  665. * filesystem callback is substituted.
  666. */
  667. struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
  668. int (*open)(struct inode *, struct file *))
  669. {
  670. const struct cred *cred = current_cred();
  671. if (IS_ERR(nd->intent.open.file))
  672. goto out;
  673. if (IS_ERR(dentry))
  674. goto out_err;
  675. nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
  676. nd->intent.open.file,
  677. open, cred);
  678. out:
  679. return nd->intent.open.file;
  680. out_err:
  681. release_open_intent(nd);
  682. nd->intent.open.file = (struct file *)dentry;
  683. goto out;
  684. }
  685. EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
  686. /**
  687. * nameidata_to_filp - convert a nameidata to an open filp.
  688. * @nd: pointer to nameidata
  689. * @flags: open flags
  690. *
  691. * Note that this function destroys the original nameidata
  692. */
  693. struct file *nameidata_to_filp(struct nameidata *nd)
  694. {
  695. const struct cred *cred = current_cred();
  696. struct file *filp;
  697. /* Pick up the filp from the open intent */
  698. filp = nd->intent.open.file;
  699. nd->intent.open.file = NULL;
  700. /* Has the filesystem initialised the file for us? */
  701. if (filp->f_path.dentry == NULL) {
  702. path_get(&nd->path);
  703. filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
  704. NULL, cred);
  705. }
  706. return filp;
  707. }
  708. /*
  709. * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
  710. * error.
  711. */
  712. struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
  713. const struct cred *cred)
  714. {
  715. int error;
  716. struct file *f;
  717. validate_creds(cred);
  718. /*
  719. * We must always pass in a valid mount pointer. Historically
  720. * callers got away with not passing it, but we must enforce this at
  721. * the earliest possible point now to avoid strange problems deep in the
  722. * filesystem stack.
  723. */
  724. if (!mnt) {
  725. printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
  726. dump_stack();
  727. return ERR_PTR(-EINVAL);
  728. }
  729. error = -ENFILE;
  730. f = get_empty_filp();
  731. if (f == NULL) {
  732. dput(dentry);
  733. mntput(mnt);
  734. return ERR_PTR(error);
  735. }
  736. f->f_flags = flags;
  737. return __dentry_open(dentry, mnt, f, NULL, cred);
  738. }
  739. EXPORT_SYMBOL(dentry_open);
  740. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  741. {
  742. struct fdtable *fdt = files_fdtable(files);
  743. __FD_CLR(fd, fdt->open_fds);
  744. if (fd < files->next_fd)
  745. files->next_fd = fd;
  746. }
  747. void put_unused_fd(unsigned int fd)
  748. {
  749. struct files_struct *files = current->files;
  750. spin_lock(&files->file_lock);
  751. __put_unused_fd(files, fd);
  752. spin_unlock(&files->file_lock);
  753. }
  754. EXPORT_SYMBOL(put_unused_fd);
  755. /*
  756. * Install a file pointer in the fd array.
  757. *
  758. * The VFS is full of places where we drop the files lock between
  759. * setting the open_fds bitmap and installing the file in the file
  760. * array. At any such point, we are vulnerable to a dup2() race
  761. * installing a file in the array before us. We need to detect this and
  762. * fput() the struct file we are about to overwrite in this case.
  763. *
  764. * It should never happen - if we allow dup2() do it, _really_ bad things
  765. * will follow.
  766. */
  767. void fd_install(unsigned int fd, struct file *file)
  768. {
  769. struct files_struct *files = current->files;
  770. struct fdtable *fdt;
  771. spin_lock(&files->file_lock);
  772. fdt = files_fdtable(files);
  773. BUG_ON(fdt->fd[fd] != NULL);
  774. rcu_assign_pointer(fdt->fd[fd], file);
  775. spin_unlock(&files->file_lock);
  776. }
  777. EXPORT_SYMBOL(fd_install);
  778. static inline int build_open_flags(int flags, int mode, struct open_flags *op)
  779. {
  780. int lookup_flags = 0;
  781. int acc_mode;
  782. if (!(flags & O_CREAT))
  783. mode = 0;
  784. op->mode = mode;
  785. /* Must never be set by userspace */
  786. flags &= ~FMODE_NONOTIFY;
  787. /*
  788. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  789. * check for O_DSYNC if the need any syncing at all we enforce it's
  790. * always set instead of having to deal with possibly weird behaviour
  791. * for malicious applications setting only __O_SYNC.
  792. */
  793. if (flags & __O_SYNC)
  794. flags |= O_DSYNC;
  795. /*
  796. * If we have O_PATH in the open flag. Then we
  797. * cannot have anything other than the below set of flags
  798. */
  799. if (flags & O_PATH) {
  800. flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
  801. acc_mode = 0;
  802. } else {
  803. acc_mode = MAY_OPEN | ACC_MODE(flags);
  804. }
  805. op->open_flag = flags;
  806. /* O_TRUNC implies we need access checks for write permissions */
  807. if (flags & O_TRUNC)
  808. acc_mode |= MAY_WRITE;
  809. /* Allow the LSM permission hook to distinguish append
  810. access from general write access. */
  811. if (flags & O_APPEND)
  812. acc_mode |= MAY_APPEND;
  813. op->acc_mode = acc_mode;
  814. op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
  815. if (flags & O_CREAT) {
  816. op->intent |= LOOKUP_CREATE;
  817. if (flags & O_EXCL)
  818. op->intent |= LOOKUP_EXCL;
  819. }
  820. if (flags & O_DIRECTORY)
  821. lookup_flags |= LOOKUP_DIRECTORY;
  822. if (!(flags & O_NOFOLLOW))
  823. lookup_flags |= LOOKUP_FOLLOW;
  824. return lookup_flags;
  825. }
  826. /**
  827. * filp_open - open file and return file pointer
  828. *
  829. * @filename: path to open
  830. * @flags: open flags as per the open(2) second argument
  831. * @mode: mode for the new file if O_CREAT is set, else ignored
  832. *
  833. * This is the helper to open a file from kernelspace if you really
  834. * have to. But in generally you should not do this, so please move
  835. * along, nothing to see here..
  836. */
  837. struct file *filp_open(const char *filename, int flags, int mode)
  838. {
  839. struct open_flags op;
  840. int lookup = build_open_flags(flags, mode, &op);
  841. return do_filp_open(AT_FDCWD, filename, &op, lookup);
  842. }
  843. EXPORT_SYMBOL(filp_open);
  844. struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  845. const char *filename, int flags)
  846. {
  847. struct open_flags op;
  848. int lookup = build_open_flags(flags, 0, &op);
  849. if (flags & O_CREAT)
  850. return ERR_PTR(-EINVAL);
  851. if (!filename && (flags & O_DIRECTORY))
  852. if (!dentry->d_inode->i_op->lookup)
  853. return ERR_PTR(-ENOTDIR);
  854. return do_file_open_root(dentry, mnt, filename, &op, lookup);
  855. }
  856. EXPORT_SYMBOL(file_open_root);
  857. long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
  858. {
  859. struct open_flags op;
  860. int lookup = build_open_flags(flags, mode, &op);
  861. char *tmp = getname(filename);
  862. int fd = PTR_ERR(tmp);
  863. if (!IS_ERR(tmp)) {
  864. fd = get_unused_fd_flags(flags);
  865. if (fd >= 0) {
  866. struct file *f = do_filp_open(dfd, tmp, &op, lookup);
  867. if (IS_ERR(f)) {
  868. put_unused_fd(fd);
  869. fd = PTR_ERR(f);
  870. } else {
  871. fsnotify_open(f);
  872. fd_install(fd, f);
  873. }
  874. }
  875. putname(tmp);
  876. }
  877. return fd;
  878. }
  879. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
  880. {
  881. long ret;
  882. if (force_o_largefile())
  883. flags |= O_LARGEFILE;
  884. ret = do_sys_open(AT_FDCWD, filename, flags, mode);
  885. /* avoid REGPARM breakage on x86: */
  886. asmlinkage_protect(3, ret, filename, flags, mode);
  887. return ret;
  888. }
  889. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  890. int, mode)
  891. {
  892. long ret;
  893. if (force_o_largefile())
  894. flags |= O_LARGEFILE;
  895. ret = do_sys_open(dfd, filename, flags, mode);
  896. /* avoid REGPARM breakage on x86: */
  897. asmlinkage_protect(4, ret, dfd, filename, flags, mode);
  898. return ret;
  899. }
  900. #ifndef __alpha__
  901. /*
  902. * For backward compatibility? Maybe this should be moved
  903. * into arch/i386 instead?
  904. */
  905. SYSCALL_DEFINE2(creat, const char __user *, pathname, int, mode)
  906. {
  907. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  908. }
  909. #endif
  910. /*
  911. * "id" is the POSIX thread ID. We use the
  912. * files pointer for this..
  913. */
  914. int filp_close(struct file *filp, fl_owner_t id)
  915. {
  916. int retval = 0;
  917. if (!file_count(filp)) {
  918. printk(KERN_ERR "VFS: Close: file count is 0\n");
  919. return 0;
  920. }
  921. if (filp->f_op && filp->f_op->flush)
  922. retval = filp->f_op->flush(filp, id);
  923. if (likely(!(filp->f_mode & FMODE_PATH))) {
  924. dnotify_flush(filp, id);
  925. locks_remove_posix(filp, id);
  926. }
  927. fput(filp);
  928. return retval;
  929. }
  930. EXPORT_SYMBOL(filp_close);
  931. /*
  932. * Careful here! We test whether the file pointer is NULL before
  933. * releasing the fd. This ensures that one clone task can't release
  934. * an fd while another clone is opening it.
  935. */
  936. SYSCALL_DEFINE1(close, unsigned int, fd)
  937. {
  938. struct file * filp;
  939. struct files_struct *files = current->files;
  940. struct fdtable *fdt;
  941. int retval;
  942. spin_lock(&files->file_lock);
  943. fdt = files_fdtable(files);
  944. if (fd >= fdt->max_fds)
  945. goto out_unlock;
  946. filp = fdt->fd[fd];
  947. if (!filp)
  948. goto out_unlock;
  949. rcu_assign_pointer(fdt->fd[fd], NULL);
  950. FD_CLR(fd, fdt->close_on_exec);
  951. __put_unused_fd(files, fd);
  952. spin_unlock(&files->file_lock);
  953. retval = filp_close(filp, files);
  954. /* can't restart close syscall because file table entry was cleared */
  955. if (unlikely(retval == -ERESTARTSYS ||
  956. retval == -ERESTARTNOINTR ||
  957. retval == -ERESTARTNOHAND ||
  958. retval == -ERESTART_RESTARTBLOCK))
  959. retval = -EINTR;
  960. return retval;
  961. out_unlock:
  962. spin_unlock(&files->file_lock);
  963. return -EBADF;
  964. }
  965. EXPORT_SYMBOL(sys_close);
  966. /*
  967. * This routine simulates a hangup on the tty, to arrange that users
  968. * are given clean terminals at login time.
  969. */
  970. SYSCALL_DEFINE0(vhangup)
  971. {
  972. if (capable(CAP_SYS_TTY_CONFIG)) {
  973. tty_vhangup_self();
  974. return 0;
  975. }
  976. return -EPERM;
  977. }
  978. /*
  979. * Called when an inode is about to be open.
  980. * We use this to disallow opening large files on 32bit systems if
  981. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  982. * on this flag in sys_open.
  983. */
  984. int generic_file_open(struct inode * inode, struct file * filp)
  985. {
  986. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  987. return -EOVERFLOW;
  988. return 0;
  989. }
  990. EXPORT_SYMBOL(generic_file_open);
  991. /*
  992. * This is used by subsystems that don't want seekable
  993. * file descriptors. The function is not supposed to ever fail, the only
  994. * reason it returns an 'int' and not 'void' is so that it can be plugged
  995. * directly into file_operations structure.
  996. */
  997. int nonseekable_open(struct inode *inode, struct file *filp)
  998. {
  999. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1000. return 0;
  1001. }
  1002. EXPORT_SYMBOL(nonseekable_open);