open.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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 * path, struct statfs __user * buf)
  114. {
  115. struct nameidata nd;
  116. int error;
  117. error = user_path_walk(path, &nd);
  118. if (!error) {
  119. struct statfs tmp;
  120. error = vfs_statfs_native(nd.path.dentry, &tmp);
  121. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  122. error = -EFAULT;
  123. path_put(&nd.path);
  124. }
  125. return error;
  126. }
  127. asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
  128. {
  129. struct nameidata nd;
  130. long error;
  131. if (sz != sizeof(*buf))
  132. return -EINVAL;
  133. error = user_path_walk(path, &nd);
  134. if (!error) {
  135. struct statfs64 tmp;
  136. error = vfs_statfs64(nd.path.dentry, &tmp);
  137. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  138. error = -EFAULT;
  139. path_put(&nd.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 * path, loff_t length)
  199. {
  200. struct nameidata nd;
  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_walk(path, &nd);
  207. if (error)
  208. goto out;
  209. inode = nd.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(nd.path.mnt);
  218. if (error)
  219. goto dput_and_out;
  220. error = vfs_permission(&nd, 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(nd.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(nd.path.mnt);
  245. dput_and_out:
  246. path_put(&nd.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. struct nameidata nd;
  368. int old_fsuid, old_fsgid;
  369. kernel_cap_t uninitialized_var(old_cap); /* !SECURE_NO_SETUID_FIXUP */
  370. int res;
  371. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  372. return -EINVAL;
  373. old_fsuid = current->fsuid;
  374. old_fsgid = current->fsgid;
  375. current->fsuid = current->uid;
  376. current->fsgid = current->gid;
  377. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  378. /*
  379. * Clear the capabilities if we switch to a non-root user
  380. */
  381. #ifndef CONFIG_SECURITY_FILE_CAPABILITIES
  382. /*
  383. * FIXME: There is a race here against sys_capset. The
  384. * capabilities can change yet we will restore the old
  385. * value below. We should hold task_capabilities_lock,
  386. * but we cannot because user_path_walk can sleep.
  387. */
  388. #endif /* ndef CONFIG_SECURITY_FILE_CAPABILITIES */
  389. if (current->uid)
  390. old_cap = cap_set_effective(__cap_empty_set);
  391. else
  392. old_cap = cap_set_effective(current->cap_permitted);
  393. }
  394. res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
  395. if (res)
  396. goto out;
  397. if ((mode & MAY_EXEC) && S_ISREG(nd.path.dentry->d_inode->i_mode)) {
  398. /*
  399. * MAY_EXEC on regular files is denied if the fs is mounted
  400. * with the "noexec" flag.
  401. */
  402. res = -EACCES;
  403. if (nd.path.mnt->mnt_flags & MNT_NOEXEC)
  404. goto out_path_release;
  405. }
  406. res = vfs_permission(&nd, mode | MAY_ACCESS);
  407. /* SuS v2 requires we report a read only fs too */
  408. if(res || !(mode & S_IWOTH) ||
  409. special_file(nd.path.dentry->d_inode->i_mode))
  410. goto out_path_release;
  411. /*
  412. * This is a rare case where using __mnt_is_readonly()
  413. * is OK without a mnt_want/drop_write() pair. Since
  414. * no actual write to the fs is performed here, we do
  415. * not need to telegraph to that to anyone.
  416. *
  417. * By doing this, we accept that this access is
  418. * inherently racy and know that the fs may change
  419. * state before we even see this result.
  420. */
  421. if (__mnt_is_readonly(nd.path.mnt))
  422. res = -EROFS;
  423. out_path_release:
  424. path_put(&nd.path);
  425. out:
  426. current->fsuid = old_fsuid;
  427. current->fsgid = old_fsgid;
  428. if (!issecure(SECURE_NO_SETUID_FIXUP))
  429. cap_set_effective(old_cap);
  430. return res;
  431. }
  432. asmlinkage long sys_access(const char __user *filename, int mode)
  433. {
  434. return sys_faccessat(AT_FDCWD, filename, mode);
  435. }
  436. asmlinkage long sys_chdir(const char __user * filename)
  437. {
  438. struct nameidata nd;
  439. int error;
  440. error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
  441. if (error)
  442. goto out;
  443. error = vfs_permission(&nd, MAY_EXEC | MAY_ACCESS);
  444. if (error)
  445. goto dput_and_out;
  446. set_fs_pwd(current->fs, &nd.path);
  447. dput_and_out:
  448. path_put(&nd.path);
  449. out:
  450. return error;
  451. }
  452. asmlinkage long sys_fchdir(unsigned int fd)
  453. {
  454. struct file *file;
  455. struct inode *inode;
  456. int error;
  457. error = -EBADF;
  458. file = fget(fd);
  459. if (!file)
  460. goto out;
  461. inode = file->f_path.dentry->d_inode;
  462. error = -ENOTDIR;
  463. if (!S_ISDIR(inode->i_mode))
  464. goto out_putf;
  465. error = file_permission(file, MAY_EXEC | MAY_ACCESS);
  466. if (!error)
  467. set_fs_pwd(current->fs, &file->f_path);
  468. out_putf:
  469. fput(file);
  470. out:
  471. return error;
  472. }
  473. asmlinkage long sys_chroot(const char __user * filename)
  474. {
  475. struct nameidata nd;
  476. int error;
  477. error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
  478. if (error)
  479. goto out;
  480. error = vfs_permission(&nd, MAY_EXEC | MAY_ACCESS);
  481. if (error)
  482. goto dput_and_out;
  483. error = -EPERM;
  484. if (!capable(CAP_SYS_CHROOT))
  485. goto dput_and_out;
  486. set_fs_root(current->fs, &nd.path);
  487. error = 0;
  488. dput_and_out:
  489. path_put(&nd.path);
  490. out:
  491. return error;
  492. }
  493. asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
  494. {
  495. struct inode * inode;
  496. struct dentry * dentry;
  497. struct file * file;
  498. int err = -EBADF;
  499. struct iattr newattrs;
  500. file = fget(fd);
  501. if (!file)
  502. goto out;
  503. dentry = file->f_path.dentry;
  504. inode = dentry->d_inode;
  505. audit_inode(NULL, dentry);
  506. err = mnt_want_write(file->f_path.mnt);
  507. if (err)
  508. goto out_putf;
  509. mutex_lock(&inode->i_mutex);
  510. if (mode == (mode_t) -1)
  511. mode = inode->i_mode;
  512. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  513. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  514. err = notify_change(dentry, &newattrs);
  515. mutex_unlock(&inode->i_mutex);
  516. mnt_drop_write(file->f_path.mnt);
  517. out_putf:
  518. fput(file);
  519. out:
  520. return err;
  521. }
  522. asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
  523. mode_t mode)
  524. {
  525. struct nameidata nd;
  526. struct inode * inode;
  527. int error;
  528. struct iattr newattrs;
  529. error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
  530. if (error)
  531. goto out;
  532. inode = nd.path.dentry->d_inode;
  533. error = mnt_want_write(nd.path.mnt);
  534. if (error)
  535. goto dput_and_out;
  536. mutex_lock(&inode->i_mutex);
  537. if (mode == (mode_t) -1)
  538. mode = inode->i_mode;
  539. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  540. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  541. error = notify_change(nd.path.dentry, &newattrs);
  542. mutex_unlock(&inode->i_mutex);
  543. mnt_drop_write(nd.path.mnt);
  544. dput_and_out:
  545. path_put(&nd.path);
  546. out:
  547. return error;
  548. }
  549. asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
  550. {
  551. return sys_fchmodat(AT_FDCWD, filename, mode);
  552. }
  553. static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
  554. {
  555. struct inode *inode = dentry->d_inode;
  556. int error;
  557. struct iattr newattrs;
  558. newattrs.ia_valid = ATTR_CTIME;
  559. if (user != (uid_t) -1) {
  560. newattrs.ia_valid |= ATTR_UID;
  561. newattrs.ia_uid = user;
  562. }
  563. if (group != (gid_t) -1) {
  564. newattrs.ia_valid |= ATTR_GID;
  565. newattrs.ia_gid = group;
  566. }
  567. if (!S_ISDIR(inode->i_mode))
  568. newattrs.ia_valid |=
  569. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  570. mutex_lock(&inode->i_mutex);
  571. error = notify_change(dentry, &newattrs);
  572. mutex_unlock(&inode->i_mutex);
  573. return error;
  574. }
  575. asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
  576. {
  577. struct nameidata nd;
  578. int error;
  579. error = user_path_walk(filename, &nd);
  580. if (error)
  581. goto out;
  582. error = mnt_want_write(nd.path.mnt);
  583. if (error)
  584. goto out_release;
  585. error = chown_common(nd.path.dentry, user, group);
  586. mnt_drop_write(nd.path.mnt);
  587. out_release:
  588. path_put(&nd.path);
  589. out:
  590. return error;
  591. }
  592. asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
  593. gid_t group, int flag)
  594. {
  595. struct nameidata nd;
  596. int error = -EINVAL;
  597. int follow;
  598. if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
  599. goto out;
  600. follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  601. error = __user_walk_fd(dfd, filename, follow, &nd);
  602. if (error)
  603. goto out;
  604. error = mnt_want_write(nd.path.mnt);
  605. if (error)
  606. goto out_release;
  607. error = chown_common(nd.path.dentry, user, group);
  608. mnt_drop_write(nd.path.mnt);
  609. out_release:
  610. path_put(&nd.path);
  611. out:
  612. return error;
  613. }
  614. asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
  615. {
  616. struct nameidata nd;
  617. int error;
  618. error = user_path_walk_link(filename, &nd);
  619. if (error)
  620. goto out;
  621. error = mnt_want_write(nd.path.mnt);
  622. if (error)
  623. goto out_release;
  624. error = chown_common(nd.path.dentry, user, group);
  625. mnt_drop_write(nd.path.mnt);
  626. out_release:
  627. path_put(&nd.path);
  628. out:
  629. return error;
  630. }
  631. asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
  632. {
  633. struct file * file;
  634. int error = -EBADF;
  635. struct dentry * dentry;
  636. file = fget(fd);
  637. if (!file)
  638. goto out;
  639. error = mnt_want_write(file->f_path.mnt);
  640. if (error)
  641. goto out_fput;
  642. dentry = file->f_path.dentry;
  643. audit_inode(NULL, dentry);
  644. error = chown_common(dentry, user, group);
  645. mnt_drop_write(file->f_path.mnt);
  646. out_fput:
  647. fput(file);
  648. out:
  649. return error;
  650. }
  651. /*
  652. * You have to be very careful that these write
  653. * counts get cleaned up in error cases and
  654. * upon __fput(). This should probably never
  655. * be called outside of __dentry_open().
  656. */
  657. static inline int __get_file_write_access(struct inode *inode,
  658. struct vfsmount *mnt)
  659. {
  660. int error;
  661. error = get_write_access(inode);
  662. if (error)
  663. return error;
  664. /*
  665. * Do not take mount writer counts on
  666. * special files since no writes to
  667. * the mount itself will occur.
  668. */
  669. if (!special_file(inode->i_mode)) {
  670. /*
  671. * Balanced in __fput()
  672. */
  673. error = mnt_want_write(mnt);
  674. if (error)
  675. put_write_access(inode);
  676. }
  677. return error;
  678. }
  679. static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
  680. int flags, struct file *f,
  681. int (*open)(struct inode *, struct file *))
  682. {
  683. struct inode *inode;
  684. int error;
  685. f->f_flags = flags;
  686. f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
  687. FMODE_PREAD | FMODE_PWRITE;
  688. inode = dentry->d_inode;
  689. if (f->f_mode & FMODE_WRITE) {
  690. error = __get_file_write_access(inode, mnt);
  691. if (error)
  692. goto cleanup_file;
  693. if (!special_file(inode->i_mode))
  694. file_take_write(f);
  695. }
  696. f->f_mapping = inode->i_mapping;
  697. f->f_path.dentry = dentry;
  698. f->f_path.mnt = mnt;
  699. f->f_pos = 0;
  700. f->f_op = fops_get(inode->i_fop);
  701. file_move(f, &inode->i_sb->s_files);
  702. error = security_dentry_open(f);
  703. if (error)
  704. goto cleanup_all;
  705. if (!open && f->f_op)
  706. open = f->f_op->open;
  707. if (open) {
  708. error = open(inode, f);
  709. if (error)
  710. goto cleanup_all;
  711. }
  712. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  713. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  714. /* NB: we're sure to have correct a_ops only after f_op->open */
  715. if (f->f_flags & O_DIRECT) {
  716. if (!f->f_mapping->a_ops ||
  717. ((!f->f_mapping->a_ops->direct_IO) &&
  718. (!f->f_mapping->a_ops->get_xip_mem))) {
  719. fput(f);
  720. f = ERR_PTR(-EINVAL);
  721. }
  722. }
  723. return f;
  724. cleanup_all:
  725. fops_put(f->f_op);
  726. if (f->f_mode & FMODE_WRITE) {
  727. put_write_access(inode);
  728. if (!special_file(inode->i_mode)) {
  729. /*
  730. * We don't consider this a real
  731. * mnt_want/drop_write() pair
  732. * because it all happenend right
  733. * here, so just reset the state.
  734. */
  735. file_reset_write(f);
  736. mnt_drop_write(mnt);
  737. }
  738. }
  739. file_kill(f);
  740. f->f_path.dentry = NULL;
  741. f->f_path.mnt = NULL;
  742. cleanup_file:
  743. put_filp(f);
  744. dput(dentry);
  745. mntput(mnt);
  746. return ERR_PTR(error);
  747. }
  748. /**
  749. * lookup_instantiate_filp - instantiates the open intent filp
  750. * @nd: pointer to nameidata
  751. * @dentry: pointer to dentry
  752. * @open: open callback
  753. *
  754. * Helper for filesystems that want to use lookup open intents and pass back
  755. * a fully instantiated struct file to the caller.
  756. * This function is meant to be called from within a filesystem's
  757. * lookup method.
  758. * Beware of calling it for non-regular files! Those ->open methods might block
  759. * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
  760. * leading to a deadlock, as nobody can open that fifo anymore, because
  761. * another process to open fifo will block on locked parent when doing lookup).
  762. * Note that in case of error, nd->intent.open.file is destroyed, but the
  763. * path information remains valid.
  764. * If the open callback is set to NULL, then the standard f_op->open()
  765. * filesystem callback is substituted.
  766. */
  767. struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
  768. int (*open)(struct inode *, struct file *))
  769. {
  770. if (IS_ERR(nd->intent.open.file))
  771. goto out;
  772. if (IS_ERR(dentry))
  773. goto out_err;
  774. nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
  775. nd->intent.open.flags - 1,
  776. nd->intent.open.file,
  777. open);
  778. out:
  779. return nd->intent.open.file;
  780. out_err:
  781. release_open_intent(nd);
  782. nd->intent.open.file = (struct file *)dentry;
  783. goto out;
  784. }
  785. EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
  786. /**
  787. * nameidata_to_filp - convert a nameidata to an open filp.
  788. * @nd: pointer to nameidata
  789. * @flags: open flags
  790. *
  791. * Note that this function destroys the original nameidata
  792. */
  793. struct file *nameidata_to_filp(struct nameidata *nd, int flags)
  794. {
  795. struct file *filp;
  796. /* Pick up the filp from the open intent */
  797. filp = nd->intent.open.file;
  798. /* Has the filesystem initialised the file for us? */
  799. if (filp->f_path.dentry == NULL)
  800. filp = __dentry_open(nd->path.dentry, nd->path.mnt, flags, filp,
  801. NULL);
  802. else
  803. path_put(&nd->path);
  804. return filp;
  805. }
  806. /*
  807. * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
  808. * error.
  809. */
  810. struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
  811. {
  812. int error;
  813. struct file *f;
  814. /*
  815. * We must always pass in a valid mount pointer. Historically
  816. * callers got away with not passing it, but we must enforce this at
  817. * the earliest possible point now to avoid strange problems deep in the
  818. * filesystem stack.
  819. */
  820. if (!mnt) {
  821. printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
  822. dump_stack();
  823. return ERR_PTR(-EINVAL);
  824. }
  825. error = -ENFILE;
  826. f = get_empty_filp();
  827. if (f == NULL) {
  828. dput(dentry);
  829. mntput(mnt);
  830. return ERR_PTR(error);
  831. }
  832. return __dentry_open(dentry, mnt, flags, f, NULL);
  833. }
  834. EXPORT_SYMBOL(dentry_open);
  835. /*
  836. * Find an empty file descriptor entry, and mark it busy.
  837. */
  838. int get_unused_fd_flags(int flags)
  839. {
  840. struct files_struct * files = current->files;
  841. int fd, error;
  842. struct fdtable *fdt;
  843. error = -EMFILE;
  844. spin_lock(&files->file_lock);
  845. repeat:
  846. fdt = files_fdtable(files);
  847. fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
  848. files->next_fd);
  849. /*
  850. * N.B. For clone tasks sharing a files structure, this test
  851. * will limit the total number of files that can be opened.
  852. */
  853. if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  854. goto out;
  855. /* Do we need to expand the fd array or fd set? */
  856. error = expand_files(files, fd);
  857. if (error < 0)
  858. goto out;
  859. if (error) {
  860. /*
  861. * If we needed to expand the fs array we
  862. * might have blocked - try again.
  863. */
  864. error = -EMFILE;
  865. goto repeat;
  866. }
  867. FD_SET(fd, fdt->open_fds);
  868. if (flags & O_CLOEXEC)
  869. FD_SET(fd, fdt->close_on_exec);
  870. else
  871. FD_CLR(fd, fdt->close_on_exec);
  872. files->next_fd = fd + 1;
  873. #if 1
  874. /* Sanity check */
  875. if (fdt->fd[fd] != NULL) {
  876. printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
  877. fdt->fd[fd] = NULL;
  878. }
  879. #endif
  880. error = fd;
  881. out:
  882. spin_unlock(&files->file_lock);
  883. return error;
  884. }
  885. int get_unused_fd(void)
  886. {
  887. return get_unused_fd_flags(0);
  888. }
  889. EXPORT_SYMBOL(get_unused_fd);
  890. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  891. {
  892. struct fdtable *fdt = files_fdtable(files);
  893. __FD_CLR(fd, fdt->open_fds);
  894. if (fd < files->next_fd)
  895. files->next_fd = fd;
  896. }
  897. void put_unused_fd(unsigned int fd)
  898. {
  899. struct files_struct *files = current->files;
  900. spin_lock(&files->file_lock);
  901. __put_unused_fd(files, fd);
  902. spin_unlock(&files->file_lock);
  903. }
  904. EXPORT_SYMBOL(put_unused_fd);
  905. /*
  906. * Install a file pointer in the fd array.
  907. *
  908. * The VFS is full of places where we drop the files lock between
  909. * setting the open_fds bitmap and installing the file in the file
  910. * array. At any such point, we are vulnerable to a dup2() race
  911. * installing a file in the array before us. We need to detect this and
  912. * fput() the struct file we are about to overwrite in this case.
  913. *
  914. * It should never happen - if we allow dup2() do it, _really_ bad things
  915. * will follow.
  916. */
  917. void fd_install(unsigned int fd, struct file *file)
  918. {
  919. struct files_struct *files = current->files;
  920. struct fdtable *fdt;
  921. spin_lock(&files->file_lock);
  922. fdt = files_fdtable(files);
  923. BUG_ON(fdt->fd[fd] != NULL);
  924. rcu_assign_pointer(fdt->fd[fd], file);
  925. spin_unlock(&files->file_lock);
  926. }
  927. EXPORT_SYMBOL(fd_install);
  928. long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
  929. {
  930. char *tmp = getname(filename);
  931. int fd = PTR_ERR(tmp);
  932. if (!IS_ERR(tmp)) {
  933. fd = get_unused_fd_flags(flags);
  934. if (fd >= 0) {
  935. struct file *f = do_filp_open(dfd, tmp, flags, mode);
  936. if (IS_ERR(f)) {
  937. put_unused_fd(fd);
  938. fd = PTR_ERR(f);
  939. } else {
  940. fsnotify_open(f->f_path.dentry);
  941. fd_install(fd, f);
  942. }
  943. }
  944. putname(tmp);
  945. }
  946. return fd;
  947. }
  948. asmlinkage long sys_open(const char __user *filename, int flags, int mode)
  949. {
  950. long ret;
  951. if (force_o_largefile())
  952. flags |= O_LARGEFILE;
  953. ret = do_sys_open(AT_FDCWD, filename, flags, mode);
  954. /* avoid REGPARM breakage on x86: */
  955. asmlinkage_protect(3, ret, filename, flags, mode);
  956. return ret;
  957. }
  958. asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
  959. int mode)
  960. {
  961. long ret;
  962. if (force_o_largefile())
  963. flags |= O_LARGEFILE;
  964. ret = do_sys_open(dfd, filename, flags, mode);
  965. /* avoid REGPARM breakage on x86: */
  966. asmlinkage_protect(4, ret, dfd, filename, flags, mode);
  967. return ret;
  968. }
  969. #ifndef __alpha__
  970. /*
  971. * For backward compatibility? Maybe this should be moved
  972. * into arch/i386 instead?
  973. */
  974. asmlinkage long sys_creat(const char __user * pathname, int mode)
  975. {
  976. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  977. }
  978. #endif
  979. /*
  980. * "id" is the POSIX thread ID. We use the
  981. * files pointer for this..
  982. */
  983. int filp_close(struct file *filp, fl_owner_t id)
  984. {
  985. int retval = 0;
  986. if (!file_count(filp)) {
  987. printk(KERN_ERR "VFS: Close: file count is 0\n");
  988. return 0;
  989. }
  990. if (filp->f_op && filp->f_op->flush)
  991. retval = filp->f_op->flush(filp, id);
  992. dnotify_flush(filp, id);
  993. locks_remove_posix(filp, id);
  994. fput(filp);
  995. return retval;
  996. }
  997. EXPORT_SYMBOL(filp_close);
  998. /*
  999. * Careful here! We test whether the file pointer is NULL before
  1000. * releasing the fd. This ensures that one clone task can't release
  1001. * an fd while another clone is opening it.
  1002. */
  1003. asmlinkage long sys_close(unsigned int fd)
  1004. {
  1005. struct file * filp;
  1006. struct files_struct *files = current->files;
  1007. struct fdtable *fdt;
  1008. int retval;
  1009. spin_lock(&files->file_lock);
  1010. fdt = files_fdtable(files);
  1011. if (fd >= fdt->max_fds)
  1012. goto out_unlock;
  1013. filp = fdt->fd[fd];
  1014. if (!filp)
  1015. goto out_unlock;
  1016. rcu_assign_pointer(fdt->fd[fd], NULL);
  1017. FD_CLR(fd, fdt->close_on_exec);
  1018. __put_unused_fd(files, fd);
  1019. spin_unlock(&files->file_lock);
  1020. retval = filp_close(filp, files);
  1021. /* can't restart close syscall because file table entry was cleared */
  1022. if (unlikely(retval == -ERESTARTSYS ||
  1023. retval == -ERESTARTNOINTR ||
  1024. retval == -ERESTARTNOHAND ||
  1025. retval == -ERESTART_RESTARTBLOCK))
  1026. retval = -EINTR;
  1027. return retval;
  1028. out_unlock:
  1029. spin_unlock(&files->file_lock);
  1030. return -EBADF;
  1031. }
  1032. EXPORT_SYMBOL(sys_close);
  1033. /*
  1034. * This routine simulates a hangup on the tty, to arrange that users
  1035. * are given clean terminals at login time.
  1036. */
  1037. asmlinkage long sys_vhangup(void)
  1038. {
  1039. if (capable(CAP_SYS_TTY_CONFIG)) {
  1040. /* XXX: this needs locking */
  1041. tty_vhangup(current->signal->tty);
  1042. return 0;
  1043. }
  1044. return -EPERM;
  1045. }
  1046. /*
  1047. * Called when an inode is about to be open.
  1048. * We use this to disallow opening large files on 32bit systems if
  1049. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  1050. * on this flag in sys_open.
  1051. */
  1052. int generic_file_open(struct inode * inode, struct file * filp)
  1053. {
  1054. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  1055. return -EOVERFLOW;
  1056. return 0;
  1057. }
  1058. EXPORT_SYMBOL(generic_file_open);
  1059. /*
  1060. * This is used by subsystems that don't want seekable
  1061. * file descriptors
  1062. */
  1063. int nonseekable_open(struct inode *inode, struct file *filp)
  1064. {
  1065. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1066. return 0;
  1067. }
  1068. EXPORT_SYMBOL(nonseekable_open);