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