open.c 27 KB

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