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