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