open.c 28 KB

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