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. DQUOT_INIT(inode);
  239. error = do_truncate(path.dentry, length, 0, NULL);
  240. }
  241. put_write_and_out:
  242. put_write_access(inode);
  243. mnt_drop_write_and_out:
  244. mnt_drop_write(path.mnt);
  245. dput_and_out:
  246. path_put(&path);
  247. out:
  248. return error;
  249. }
  250. asmlinkage long sys_truncate(const char __user * path, unsigned long length)
  251. {
  252. /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
  253. return do_sys_truncate(path, (long)length);
  254. }
  255. static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  256. {
  257. struct inode * inode;
  258. struct dentry *dentry;
  259. struct file * file;
  260. int error;
  261. error = -EINVAL;
  262. if (length < 0)
  263. goto out;
  264. error = -EBADF;
  265. file = fget(fd);
  266. if (!file)
  267. goto out;
  268. /* explicitly opened as large or we are on 64-bit box */
  269. if (file->f_flags & O_LARGEFILE)
  270. small = 0;
  271. dentry = file->f_path.dentry;
  272. inode = dentry->d_inode;
  273. error = -EINVAL;
  274. if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
  275. goto out_putf;
  276. error = -EINVAL;
  277. /* Cannot ftruncate over 2^31 bytes without large file support */
  278. if (small && length > MAX_NON_LFS)
  279. goto out_putf;
  280. error = -EPERM;
  281. if (IS_APPEND(inode))
  282. goto out_putf;
  283. error = locks_verify_truncate(inode, file, length);
  284. if (!error)
  285. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
  286. out_putf:
  287. fput(file);
  288. out:
  289. return error;
  290. }
  291. asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
  292. {
  293. long ret = do_sys_ftruncate(fd, length, 1);
  294. /* avoid REGPARM breakage on x86: */
  295. asmlinkage_protect(2, ret, fd, length);
  296. return ret;
  297. }
  298. /* LFS versions of truncate are only needed on 32 bit machines */
  299. #if BITS_PER_LONG == 32
  300. asmlinkage long sys_truncate64(const char __user * path, loff_t length)
  301. {
  302. return do_sys_truncate(path, length);
  303. }
  304. asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
  305. {
  306. long ret = do_sys_ftruncate(fd, length, 0);
  307. /* avoid REGPARM breakage on x86: */
  308. asmlinkage_protect(2, ret, fd, length);
  309. return ret;
  310. }
  311. #endif
  312. asmlinkage long sys_fallocate(int fd, int mode, loff_t offset, loff_t len)
  313. {
  314. struct file *file;
  315. struct inode *inode;
  316. long ret = -EINVAL;
  317. if (offset < 0 || len <= 0)
  318. goto out;
  319. /* Return error if mode is not supported */
  320. ret = -EOPNOTSUPP;
  321. if (mode && !(mode & FALLOC_FL_KEEP_SIZE))
  322. goto out;
  323. ret = -EBADF;
  324. file = fget(fd);
  325. if (!file)
  326. goto out;
  327. if (!(file->f_mode & FMODE_WRITE))
  328. goto out_fput;
  329. /*
  330. * Revalidate the write permissions, in case security policy has
  331. * changed since the files were opened.
  332. */
  333. ret = security_file_permission(file, MAY_WRITE);
  334. if (ret)
  335. goto out_fput;
  336. inode = file->f_path.dentry->d_inode;
  337. ret = -ESPIPE;
  338. if (S_ISFIFO(inode->i_mode))
  339. goto out_fput;
  340. ret = -ENODEV;
  341. /*
  342. * Let individual file system decide if it supports preallocation
  343. * for directories or not.
  344. */
  345. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  346. goto out_fput;
  347. ret = -EFBIG;
  348. /* Check for wrap through zero too */
  349. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  350. goto out_fput;
  351. if (inode->i_op && inode->i_op->fallocate)
  352. ret = inode->i_op->fallocate(inode, mode, offset, len);
  353. else
  354. ret = -EOPNOTSUPP;
  355. out_fput:
  356. fput(file);
  357. out:
  358. return ret;
  359. }
  360. /*
  361. * access() needs to use the real uid/gid, not the effective uid/gid.
  362. * We do this by temporarily clearing all FS-related capabilities and
  363. * switching the fsuid/fsgid around to the real ones.
  364. */
  365. asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
  366. {
  367. struct path path;
  368. struct inode *inode;
  369. int old_fsuid, old_fsgid;
  370. kernel_cap_t uninitialized_var(old_cap); /* !SECURE_NO_SETUID_FIXUP */
  371. int res;
  372. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  373. return -EINVAL;
  374. old_fsuid = current->fsuid;
  375. old_fsgid = current->fsgid;
  376. current->fsuid = current->uid;
  377. current->fsgid = current->gid;
  378. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  379. /*
  380. * Clear the capabilities if we switch to a non-root user
  381. */
  382. #ifndef CONFIG_SECURITY_FILE_CAPABILITIES
  383. /*
  384. * FIXME: There is a race here against sys_capset. The
  385. * capabilities can change yet we will restore the old
  386. * value below. We should hold task_capabilities_lock,
  387. * but we cannot because user_path_at can sleep.
  388. */
  389. #endif /* ndef CONFIG_SECURITY_FILE_CAPABILITIES */
  390. if (current->uid)
  391. old_cap = cap_set_effective(__cap_empty_set);
  392. else
  393. old_cap = cap_set_effective(current->cap_permitted);
  394. }
  395. res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
  396. if (res)
  397. goto out;
  398. inode = path.dentry->d_inode;
  399. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  400. /*
  401. * MAY_EXEC on regular files is denied if the fs is mounted
  402. * with the "noexec" flag.
  403. */
  404. res = -EACCES;
  405. if (path.mnt->mnt_flags & MNT_NOEXEC)
  406. goto out_path_release;
  407. }
  408. res = inode_permission(inode, mode | MAY_ACCESS);
  409. /* SuS v2 requires we report a read only fs too */
  410. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  411. goto out_path_release;
  412. /*
  413. * This is a rare case where using __mnt_is_readonly()
  414. * is OK without a mnt_want/drop_write() pair. Since
  415. * no actual write to the fs is performed here, we do
  416. * not need to telegraph to that to anyone.
  417. *
  418. * By doing this, we accept that this access is
  419. * inherently racy and know that the fs may change
  420. * state before we even see this result.
  421. */
  422. if (__mnt_is_readonly(path.mnt))
  423. res = -EROFS;
  424. out_path_release:
  425. path_put(&path);
  426. out:
  427. current->fsuid = old_fsuid;
  428. current->fsgid = old_fsgid;
  429. if (!issecure(SECURE_NO_SETUID_FIXUP))
  430. cap_set_effective(old_cap);
  431. return res;
  432. }
  433. asmlinkage long sys_access(const char __user *filename, int mode)
  434. {
  435. return sys_faccessat(AT_FDCWD, filename, mode);
  436. }
  437. asmlinkage long sys_chdir(const char __user * filename)
  438. {
  439. struct path path;
  440. int error;
  441. error = user_path_dir(filename, &path);
  442. if (error)
  443. goto out;
  444. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
  445. if (error)
  446. goto dput_and_out;
  447. set_fs_pwd(current->fs, &path);
  448. dput_and_out:
  449. path_put(&path);
  450. out:
  451. return error;
  452. }
  453. asmlinkage long sys_fchdir(unsigned int fd)
  454. {
  455. struct file *file;
  456. struct inode *inode;
  457. int error;
  458. error = -EBADF;
  459. file = fget(fd);
  460. if (!file)
  461. goto out;
  462. inode = file->f_path.dentry->d_inode;
  463. error = -ENOTDIR;
  464. if (!S_ISDIR(inode->i_mode))
  465. goto out_putf;
  466. error = inode_permission(inode, MAY_EXEC | MAY_ACCESS);
  467. if (!error)
  468. set_fs_pwd(current->fs, &file->f_path);
  469. out_putf:
  470. fput(file);
  471. out:
  472. return error;
  473. }
  474. asmlinkage long sys_chroot(const char __user * filename)
  475. {
  476. struct path path;
  477. int error;
  478. error = user_path_dir(filename, &path);
  479. if (error)
  480. goto out;
  481. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
  482. if (error)
  483. goto dput_and_out;
  484. error = -EPERM;
  485. if (!capable(CAP_SYS_CHROOT))
  486. goto dput_and_out;
  487. set_fs_root(current->fs, &path);
  488. error = 0;
  489. dput_and_out:
  490. path_put(&path);
  491. out:
  492. return error;
  493. }
  494. asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
  495. {
  496. struct inode * inode;
  497. struct dentry * dentry;
  498. struct file * file;
  499. int err = -EBADF;
  500. struct iattr newattrs;
  501. file = fget(fd);
  502. if (!file)
  503. goto out;
  504. dentry = file->f_path.dentry;
  505. inode = dentry->d_inode;
  506. audit_inode(NULL, dentry);
  507. err = mnt_want_write(file->f_path.mnt);
  508. if (err)
  509. goto out_putf;
  510. mutex_lock(&inode->i_mutex);
  511. if (mode == (mode_t) -1)
  512. mode = inode->i_mode;
  513. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  514. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  515. err = notify_change(dentry, &newattrs);
  516. mutex_unlock(&inode->i_mutex);
  517. mnt_drop_write(file->f_path.mnt);
  518. out_putf:
  519. fput(file);
  520. out:
  521. return err;
  522. }
  523. asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
  524. mode_t mode)
  525. {
  526. struct path path;
  527. struct inode *inode;
  528. int error;
  529. struct iattr newattrs;
  530. error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
  531. if (error)
  532. goto out;
  533. inode = path.dentry->d_inode;
  534. error = mnt_want_write(path.mnt);
  535. if (error)
  536. goto dput_and_out;
  537. mutex_lock(&inode->i_mutex);
  538. if (mode == (mode_t) -1)
  539. mode = inode->i_mode;
  540. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  541. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  542. error = notify_change(path.dentry, &newattrs);
  543. mutex_unlock(&inode->i_mutex);
  544. mnt_drop_write(path.mnt);
  545. dput_and_out:
  546. path_put(&path);
  547. out:
  548. return error;
  549. }
  550. asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
  551. {
  552. return sys_fchmodat(AT_FDCWD, filename, mode);
  553. }
  554. static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
  555. {
  556. struct inode *inode = dentry->d_inode;
  557. int error;
  558. struct iattr newattrs;
  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. return error;
  575. }
  576. asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
  577. {
  578. struct path path;
  579. int error;
  580. error = user_path(filename, &path);
  581. if (error)
  582. goto out;
  583. error = mnt_want_write(path.mnt);
  584. if (error)
  585. goto out_release;
  586. error = chown_common(path.dentry, user, group);
  587. mnt_drop_write(path.mnt);
  588. out_release:
  589. path_put(&path);
  590. out:
  591. return error;
  592. }
  593. asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
  594. gid_t group, int flag)
  595. {
  596. struct path path;
  597. int error = -EINVAL;
  598. int follow;
  599. if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
  600. goto out;
  601. follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  602. error = user_path_at(dfd, filename, follow, &path);
  603. if (error)
  604. goto out;
  605. error = mnt_want_write(path.mnt);
  606. if (error)
  607. goto out_release;
  608. error = chown_common(path.dentry, user, group);
  609. mnt_drop_write(path.mnt);
  610. out_release:
  611. path_put(&path);
  612. out:
  613. return error;
  614. }
  615. asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
  616. {
  617. struct path path;
  618. int error;
  619. error = user_lpath(filename, &path);
  620. if (error)
  621. goto out;
  622. error = mnt_want_write(path.mnt);
  623. if (error)
  624. goto out_release;
  625. error = chown_common(path.dentry, user, group);
  626. mnt_drop_write(path.mnt);
  627. out_release:
  628. path_put(&path);
  629. out:
  630. return error;
  631. }
  632. asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
  633. {
  634. struct file * file;
  635. int error = -EBADF;
  636. struct dentry * dentry;
  637. file = fget(fd);
  638. if (!file)
  639. goto out;
  640. error = mnt_want_write(file->f_path.mnt);
  641. if (error)
  642. goto out_fput;
  643. dentry = file->f_path.dentry;
  644. audit_inode(NULL, dentry);
  645. error = chown_common(dentry, user, group);
  646. mnt_drop_write(file->f_path.mnt);
  647. out_fput:
  648. fput(file);
  649. out:
  650. return error;
  651. }
  652. /*
  653. * You have to be very careful that these write
  654. * counts get cleaned up in error cases and
  655. * upon __fput(). This should probably never
  656. * be called outside of __dentry_open().
  657. */
  658. static inline int __get_file_write_access(struct inode *inode,
  659. struct vfsmount *mnt)
  660. {
  661. int error;
  662. error = get_write_access(inode);
  663. if (error)
  664. return error;
  665. /*
  666. * Do not take mount writer counts on
  667. * special files since no writes to
  668. * the mount itself will occur.
  669. */
  670. if (!special_file(inode->i_mode)) {
  671. /*
  672. * Balanced in __fput()
  673. */
  674. error = mnt_want_write(mnt);
  675. if (error)
  676. put_write_access(inode);
  677. }
  678. return error;
  679. }
  680. static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
  681. int flags, struct file *f,
  682. int (*open)(struct inode *, struct file *))
  683. {
  684. struct inode *inode;
  685. int error;
  686. f->f_flags = flags;
  687. f->f_mode = (__force fmode_t)((flags+1) & O_ACCMODE) | FMODE_LSEEK |
  688. FMODE_PREAD | FMODE_PWRITE;
  689. inode = dentry->d_inode;
  690. if (f->f_mode & FMODE_WRITE) {
  691. error = __get_file_write_access(inode, mnt);
  692. if (error)
  693. goto cleanup_file;
  694. if (!special_file(inode->i_mode))
  695. file_take_write(f);
  696. }
  697. f->f_mapping = inode->i_mapping;
  698. f->f_path.dentry = dentry;
  699. f->f_path.mnt = mnt;
  700. f->f_pos = 0;
  701. f->f_op = fops_get(inode->i_fop);
  702. file_move(f, &inode->i_sb->s_files);
  703. error = security_dentry_open(f);
  704. if (error)
  705. goto cleanup_all;
  706. if (!open && f->f_op)
  707. open = f->f_op->open;
  708. if (open) {
  709. error = open(inode, f);
  710. if (error)
  711. goto cleanup_all;
  712. }
  713. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  714. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  715. /* NB: we're sure to have correct a_ops only after f_op->open */
  716. if (f->f_flags & O_DIRECT) {
  717. if (!f->f_mapping->a_ops ||
  718. ((!f->f_mapping->a_ops->direct_IO) &&
  719. (!f->f_mapping->a_ops->get_xip_mem))) {
  720. fput(f);
  721. f = ERR_PTR(-EINVAL);
  722. }
  723. }
  724. return f;
  725. cleanup_all:
  726. fops_put(f->f_op);
  727. if (f->f_mode & FMODE_WRITE) {
  728. put_write_access(inode);
  729. if (!special_file(inode->i_mode)) {
  730. /*
  731. * We don't consider this a real
  732. * mnt_want/drop_write() pair
  733. * because it all happenend right
  734. * here, so just reset the state.
  735. */
  736. file_reset_write(f);
  737. mnt_drop_write(mnt);
  738. }
  739. }
  740. file_kill(f);
  741. f->f_path.dentry = NULL;
  742. f->f_path.mnt = NULL;
  743. cleanup_file:
  744. put_filp(f);
  745. dput(dentry);
  746. mntput(mnt);
  747. return ERR_PTR(error);
  748. }
  749. /**
  750. * lookup_instantiate_filp - instantiates the open intent filp
  751. * @nd: pointer to nameidata
  752. * @dentry: pointer to dentry
  753. * @open: open callback
  754. *
  755. * Helper for filesystems that want to use lookup open intents and pass back
  756. * a fully instantiated struct file to the caller.
  757. * This function is meant to be called from within a filesystem's
  758. * lookup method.
  759. * Beware of calling it for non-regular files! Those ->open methods might block
  760. * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
  761. * leading to a deadlock, as nobody can open that fifo anymore, because
  762. * another process to open fifo will block on locked parent when doing lookup).
  763. * Note that in case of error, nd->intent.open.file is destroyed, but the
  764. * path information remains valid.
  765. * If the open callback is set to NULL, then the standard f_op->open()
  766. * filesystem callback is substituted.
  767. */
  768. struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
  769. int (*open)(struct inode *, struct file *))
  770. {
  771. if (IS_ERR(nd->intent.open.file))
  772. goto out;
  773. if (IS_ERR(dentry))
  774. goto out_err;
  775. nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
  776. nd->intent.open.flags - 1,
  777. nd->intent.open.file,
  778. open);
  779. out:
  780. return nd->intent.open.file;
  781. out_err:
  782. release_open_intent(nd);
  783. nd->intent.open.file = (struct file *)dentry;
  784. goto out;
  785. }
  786. EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
  787. /**
  788. * nameidata_to_filp - convert a nameidata to an open filp.
  789. * @nd: pointer to nameidata
  790. * @flags: open flags
  791. *
  792. * Note that this function destroys the original nameidata
  793. */
  794. struct file *nameidata_to_filp(struct nameidata *nd, int flags)
  795. {
  796. struct file *filp;
  797. /* Pick up the filp from the open intent */
  798. filp = nd->intent.open.file;
  799. /* Has the filesystem initialised the file for us? */
  800. if (filp->f_path.dentry == NULL)
  801. filp = __dentry_open(nd->path.dentry, nd->path.mnt, flags, filp,
  802. NULL);
  803. else
  804. path_put(&nd->path);
  805. return filp;
  806. }
  807. /*
  808. * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
  809. * error.
  810. */
  811. struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
  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);
  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);