open.c 27 KB

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