open.c 26 KB

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